Monitor network health with somebar

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.

1
$ echo -n "green" | nc -4u -w0 localhost 1738

So I came up with this little script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/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:

1
2
3
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.