USB DSL Traffic Meter / Alarm Thingy
Recently, something went rogue on our network and happily consumed over 3GB every hour, for 16 hours — over 85GB in less than a day. :-/ I was not impressed.
So, this afternoon I thought, “Wouldn’t it be nice to have some simple, visual / audible display or alarm that would alert me to problems like this, without having to remember to check on my traffic accounting system.” So I made one.
I soldered up this little circuit board today, based on a previous project for which I already had a number of blank PCBs handy.
The board contains an ATmega88p MCU and runs V-USB based firmware, which I cobbled together to control the simple LED display and alarm buzzer.
Finally, I wrote the host C program (employing libusb
, based on a V-USB example project) on the Linux gateway server, along with a cron job to run an awk script to collect the traffic data and … well yes, it is, “nice to have” 😛
There are four LEDs [Green Yellow Yellow* Red ] (*yellow because I couldn’t find an orange one in the junk drawer – grr!) and one rather noisy buzzer thingy.
The LED’s (had they been the colours I wanted) represent previous-hour bandwidth usage of …
- Green — up to 500MB
- Yellow — up to 1GB
- Orange — up to 2GB
- Red — more than 2GB
These are updated every five minutes by our Ubuntu Linux gateway server, into which the interface board is plugged.
I chose the rather high value for the red/alarm condition because we quite often rent Apple TV videos (movies), which are around a Gig’ a shot and often not much over an hour in viewing time. Plus, we have three other home boarders using the ‘net, through the same gateway. Excess traffic over and above our 150GB/mth plan is actually only $0.50 a Gig’. So it should be fine. (With two boarders and myself home during the week most of the time, we struggle to stay under 150GB a month, actually! Crazy.)
The alarm sounds whenever the red LED is on (pulsating on/off, so as not to kill my ears!) and can also be independently shut off from the host interface program.
The green LED will go off if no data update has been received over the USB wire for more than ten minutes. This lets me know that data updates aren’t happening and that I need to check the system. (The other lights remain as they were in this case — just because.)
Every five minutes, 10 seconds after traffic data is logged into rrdtool
, a cron job on the gateway server updates the LED/alarm unit. Following is the awk script for that …
#!/usr/bin/awk -f
BEGIN {
cmd = "rrdtool fetch traffic.rrd AVERAGE -e now -s end-1h";
while ((cmd | getline) > 0) {
if ($2 ~ /^[0-9.e+]+$/) {
hourlyTraffic += ($2 * 300);
}
}
close(cmd);
oneGig = 1073741824;
if (hourlyTraffic > 2 * oneGig) level = 3; # 2.0GB
else if (hourlyTraffic > oneGig) level = 2; # 1.0GB
else if (hourlyTraffic > oneGig / 2) level = 1; # 0.5GB
else level = 0;
printf "Traffic: %4.2f MB over last hour\n", hourlyTraffic / 1048576;
printf "Level: %d\n", level;
system("tMeterControl " level);
exit;
}
I was already logging traffic into an RRD-Tool database at 5-minute intervals. (That also gives me all the traffic graphing ability I need.) So, at the top of the awk script, I use rrdtool fetch
to extract the last hour’s worth of samples (12 of them), filter that into just the valid inbound traffic values, multiply by 300 seconds (5 minutes — since rrdtool
stores everything as per-second rates) and sum those up to arrive at the total bytes of ingress bandwidth for the hour.
The rest of the code is just about telling the LED/alarm unit what to do. The actual USB communication for that is handled by the C program tMeterControl
…
/* Name: powerSwitch.c
* Project: tMeterControl based on PowerSwitch based on AVR USB driver
* Author: Gruvin
* Original Author: Christian Starkjohann
* Creation Date: 2005-01-16
* Tabsize: 4
* Copyright: (c) 2013 by Bryan J. Rentoul (aka Gruvin)
* Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH
* License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt)
* This Revision: $Id$
*/
/* ... truncated for brevity ... */
All I need now is a nice little box and some labels stuck on it. Then again, it looks kinda cool, just Blu-Tackâ„¢’d to the wall. 😀 (Except with the cable hanging down and I should really swap the red and green LEDs.)
Leave a Reply