Monitor network health with somebar
by Emile `iMil' Heitor - 2019-12-22
I knew about a MacOS task bar plugin called Anybar, which basically draws an icon on the task bar to which you can send behaviors with a simple nc
command. Naturally, someone cloned it for our beloved Free Unices environments, and it’s called somebar.
I am sometimes in places with weak network, and I like to see at a glance how is my connection doing, somebar seemed the perfect tool for the task.
Somebar waits for a message on an UDP port, 1738
by default, i.e.
$ echo -n "green" | nc -4u -w0 localhost 1738
So I came up with this little script:
#!/bin/sh
nccmd="nc -4u -w0 localhost 1738"
pingcmd="ping -c 1 -w 1 -q"
okcolor="green"
while :
do
for l in $(grep -v ^# $1)
do
loss=${l%%@*}
latc=${l##*@}
h=${loss%%:*}
c=${loss##*:}
# packet loss
pl=$($pingcmd $h|egrep -o '[0-9\.]+%')
# average ping latency
lt=$($pingcmd $h|sed -rn 's,.*/([0-9\.]+)\.[0-9]+/.*,\1,p')
if [ $# -gt 1 ]; then # for debugging
echo "$h packet loss $pl"
echo "$h latency: $lt / $latc"
fi
[ -z "$pl" ] || [ -z "$lt" ] && continue
if [ "$pl" != "0%" ] || [ $lt -gt $latc ]; then
[ "$c" = "red" ] && break
[ "$c" != "$okcolor" ] && break
fi
c=$okcolor
done
echo -n $c|$nccmd
sleep 5
done
It will read the file given as argv[1]
, which has the following format:
senate:red@70
discobus:red@10
ddwrt:orange@10
The first field is obviously the host, then the severity of this host being hard to reach and finally the average ICMP delay we tolerate. The script will check both packet loss and latency.
Start somebar
and then this script, with an optional random parameter to see some output on the terminal.