Created
December 11, 2012 07:01
-
-
Save jheitzeb/4256472 to your computer and use it in GitHub Desktop.
blink(1) script to light up osx network usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Quick hack to get blink(1) to light up while monitoring osx network usage | |
# Based on "Simple script to monitor network usage on OSX - Joe McManus 2011/03/30 [email protected]" | |
#Interval between tests in seconds, change this to whatever you want | |
interval=1 | |
if [ -z $1 ] | |
then | |
echo "ERROR: No interface specified" | |
echo "USAGE: $0 interface_name" | |
exit | |
else | |
interface=$1 | |
fi | |
intcount=`ifconfig $interface | wc -l` | |
if [ $intcount == 0 ] | |
then | |
echo "ERROR: Interface $interface not found" | |
exit | |
fi | |
#echo "Monitoring: $interface " | |
#echo "Hit CTRL-C to stop" | |
i=1 | |
while [ 1 -ne 0 ] | |
do | |
now=`date +'%Y-%m-%d %H:%M:%S'` | |
intinfo=`netstat -b -I $interface | grep ^$interface | sort -u` | |
inbound=`echo $intinfo | awk ' {print $7 } ' ` | |
outbound=`echo $intinfo | awk ' {print $10 } ' ` | |
if [ $i -eq 1 ] | |
then | |
#We dont have any data yet, so lets just set the starting point | |
lastin=$inbound | |
lastout=$outbound | |
totalin=0 | |
totalout=0 | |
outmaxxxx=0 | |
inmaxxxx=0 | |
else | |
diffin=`expr $inbound - $lastin` | |
diffpersecin=`expr $diffin / $interval` | |
totalin=`expr $totalin + $diffin` | |
diffout=`expr $outbound - $lastout` | |
diffpersecout=`expr $diffout / $interval` | |
totalout=`expr $totalout + $diffout` | |
#echo "Time: $now, In: $diffin, Out: $diffout, In/Sec: $diffpersecin, Out/Sec: $diffpersecout, Total In: $totalin, Total Out, $totalout" | |
outtotal=$diffout | |
intotal=$diffin | |
# These seem to be the max in and out...at least on my computer + connection. | |
inmax=235574 | |
outmax=73122 | |
colormax=255 | |
# slope of a line | |
# normalize 0-255 for coloring [y-b/m] where b=0 | |
in_m=`echo $inmax / $colormax | bc -l` | |
out_m=`echo $outmax / $colormax | bc -l` | |
# And now normalize output | |
outcolor=`echo $outtotal / $out_m | bc -l` | |
if [ ${outcolor/.*} -gt ${colormax/.*} ]; then | |
outcolor=$colormax | |
fi | |
# And now normalize input | |
incolor=`echo $intotal / $in_m | bc -l` | |
if [ ${incolor/.*} -gt ${colormax/.*} ]; then | |
incolor=$colormax | |
fi | |
# show the input and output colors, red and green on blink(1) device | |
blink_cmd="./blink1-tool -m 300 --rgb $incolor,$outcolor,0" | |
eval $blink_cmd | |
lastin=$inbound | |
lastout=$outbound | |
fi | |
i=`expr $i + 1` | |
sleep 0.03 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment