Today I had cause to want to track the per-user (per-MAC address, really) traffic counts on an OpenWRT-based router. There’s nothing I could see that was built in, so I dug around and found wrtbwmon, which is a pretty good script for this sort of thing. However, it doesn’t work with the OpenWRT version I’m running (Backfire (10.03, r20728).)
So here I document what I had to do to make it work.
To start with, grab my version of the script itself from here. I put this into /usr/local/bin on the router, and made it executable (chmod +x /usr/local/bin/wrtbwmon.)
Using crontab -e, add the following (modified as you like) to the crontab:
* * * * * /usr/local/bin/wrtbwmon setup br-lan
*/5 * * * * /usr/local/bin/wrtbwmon update /tmp/usage.db
1,6,11,16,21,26,31,36,41,46,51,56 * * * * /usr/local/bin/wrtbwmon publish /tmp/usage.db /tmp/www/usage.htm /usr/local/lib/macusers.txt
3 0 12 * * rm -f /tmp/usage.db
7 */6 * * * cp /tmp/usage.db /usr/local/lib/usage.db.bak
(the one with all the numbers is actually one long line, it may wrap on this page)
Line 1 sets up iptables to track everything it sees on the network, 2 and 3 save and write out the data every 5 minutes, and 4 resets the database when the monthly transfer quota resets. The last line backs up the database to flash every 6 hours, so that in case of power failure at most 6 hours of data are lost. The script expects the backup to be found in /usr/local/lib/usage.db.bak, so if you alter this, you’ll have to alter the script.
Running /etc/init.d/cron restart may be needed here to make the cron daemon reload the new commands.
Set up /usr/local/lib/macusers.txt to map MAC addresses to users to give a nicer output. For example:
00:aa:bb:cc:dd:ee,Person 1
11:22:33:44:55:66,Person 2
The MAC addresses should be lower case.
In /www/cgi-bin/usage I put:
#!/bin/sh
echo 'Content-Type: text/html'
echo 'X-Dummy: dummy'
echo
cat /tmp/www/usage.htm
The dummy header line is needed to work around a bug in the uhttp version on the router. chmod +x this also.
Now, you can go to http://IP_OF_ROUTER/cgi-bin/usage and it’ll give you an HTML table showing who’s been uploading and downloading what. Good for finding out who hasn’t capped their torrent client’s upload speed.
Update: added the backup cron job, updated the script to restore the backup if needed, also now adds totals to the display.
{ 11 } Comments
Thanks for updating this to work with recent versions of OpenWRT. Much appreciated.
It worked, thank you. but we still want to add this page in LUCI main page, could you do that?
I haven’t meddled with this in a while, but I can’t imagine it’d be too hard, ideally you’d just be able to edit the templates that LUCI uses.
Yay! I got it working once I understood that /www/cgi-bin/usage was a file I had to create, doh!!!
Now I must try to figure out how to get totals per row as well as keeping daily summaries for two months…
P.S. MAC addresses in macusers.txt must be lower-case, not upper-case as per the example above (or the usage information in the script itself)…
Good to know, I’ll update with a note to that effect. Curiously, I don’t remember having that issue, but I was probably cut-and-pasting from elsewhere which was correctly formatted.
Thank you so much, this is exactly what I have been looking for for quite some time.
Why back it up? Why not just store it directly to flash? Does the script do IO too frequently?
Thank you for this great script!
This morning I was wondering why the counters didn’t have been reset as it should be because of line four of the cronjobs. The reason for this is the backup/restore functionality you added to the script later:
# Restore this from a backup if needed
[ ! -f "${2}" -a -f /usr/local/lib/usage.db.bak ] && cp /usr/local/lib/usage.db.bak ${2}
So with active backup/restore functionality the script restores the backup if it is missing. To force an intended counter reset we have to create a new blank file, so replace line four of the cronjobs with the following one:
3 0 12 * * rm -f /tmp/usage.db && touch /tmp/usage.db
Thank you very much for this!
And for giving me a reason to investigate how crontab works
A little modification that I made to the crontab is:
3 0 1 * * mv -f /tmp/usage.db /usr/local/lib/usage_$(date +%b_%Y).db
7 */2 * * * cp /tmp/usage.db /usr/local/lib/usage.db.bak
Make a backup of the database instead of deleting it every month, save by month-year.
And make a backup every other hour instead of every 6 hours
Hi! I noticed if I add more that 1 line MAC,User in macusers.txt then it stops working, if I leave the txt file blank then I see only MACs in statistics table! Can anyone fix it? Thx!
works like a charm. great!
Thank you very much. Was looking for something exactly like this and with a few changes worked perfectly for me. I appreciate it.
{ 1 } Trackback
[...] our network’s bandwidth courtesy of Robin by way of their blog, which can be found at this link. This handy tool allows us to track the incoming and outgoing volume of data that leaves and enters [...]
Post a Comment