How-To: Measure your bandwidth with SNMP

For today's How-To, you'll need:
- Router, firewall or switch that supports SNMP
- Linux (or any *nix box, we're using Ubuntu for today)
Today we'll be setting up a free utility called MRTG (Multi Router Traffic Grapher) to help keep an eye on how much bandwidth we're using. It can run on Windows or Linux (or other *nix operating systems). We'll set it up under Ubuntu Linux to collect data at five minute intervals and draw some pretty graphs.

A SNMP string acts as a simple login for SNMP queries. If you have the right string, you can connect. The read only string is usually set by default to public. Most home network routers actually support SNMP, but you may have to do some hunting to find out how to turn it on. (To get SNMP on the WRT54G, you might want to check out the WiFi-Box firmware.)
For today's How-To you only need to set up a read only string. We suggest changing the read/write string to something no-one will ever guess. (Read / Write access can be used to alter the configuration of a device)
If you followed How-To: Build your own Network Firewall and have a Devil-Linux firewall, then edit /etc/snmpd.conf and add the following:
syslocation "home"
syscontact you@yourdomain
rocommunity public
Don't forget to run save-config when you're done! Once the config it saved, snmpd will start up automatically at boot. For now, start up the snmp deamon by hand. Run the following as root.
/etc/init.d/snmpd start
Once you've managed to activate SNMP on your network device, head over to your *nix box. We'll walk you through setting everything up under Ubuntu Linux today.

When it's done, pull up a shell and run the following command to set up MRTG.
cfgmaker --global 'WorkDir:/var/www/mrtg' \
--global 'Options[_]: bits,growright' \
--output /etc/mrtg.cfg \
public@192.168.1.1
The workdir is where all the data will get stored, the options make things pretty, the output sets the location of the config file, and public is the read only snmp community string you set up. 192.168.1.1 is the ip address on the lan side of the router/firewall/switch you're monitoring. If want to run cfgmaker as a regular user, store the config in your home directory.
If your crazy long command succeeded, you should have a spiffy new config file for MRTG to play with. To get it collecting data every five minutes (plenty for the graphs that MRTG makes) run the following command (as whatever user you want to run MRTG as):
crontab -e
This will edit your crontab. Here you can schedule commands to run at regular intervals. Put the following on one line of the crontab:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * env LANG=C /usr/bin/mrtg /etc/mrtg.cfg --logging /var/log/mrtg.log
Save the file and MRTG will be set to collect data every five minutes. Nice, eh? The env C line is a fix for running MRTG under Ubuntu. It might not be neccesary for your machine, but we needed it.

Troubleshooting suggestions:
Permissions are a big part of the game. As long as the user that's running MRTG can read the config file and has write access to the /var/www/mrtg directory, you should be golden. You may wish to create a user just for MRTG and keep everything in its home directory. Then you can point Apache at that directory. Check out the documentation at the MRTG homepage for more.
Good luck, and remember, this will only tell you how much traffic you've got pumping in an out -- it won't prioritize your network traffic over the others' using the pipe. We find the easiest way to take care of that is using MRTG to find the bandwidth hogs, and then walking over to their machines and unplugging them. Enjoy!
















Reader Comments (Page 1 of 1)
Todd @ Aug 1st 2006 9:59PM
Instead of MRTG, try PRTG. It is all GUI, much easier to setup, and much more powerful! We just started using it at work and it rocks.
Sean @ Aug 1st 2006 10:56PM
Just curious if there's anything like this for Windows.
Luis Camino @ Aug 1st 2006 11:09PM
sean, there must be, but in this mac-ux webmpire it can't be bothered :)
daniel @ Aug 1st 2006 11:22PM
my router (2wire homeportal 1700) has a little java applet on its config site that displays incoming and outgoing traffic like that... i wonder which is more
precise?
dr.nomolos @ Aug 1st 2006 11:55PM
I gotta say, that's pretty impressive. Teaching generic geeks how to do hardcore sysadmin manuevers. What's next, a write-up on using Ethereal to analyze your Ethernet packets?
Brian @ Aug 2nd 2006 12:35AM
I use Cacti, and have used Cacti, since I switched from MRTG. Cacti not only is PHP-based, the whole config process is done through a GUI.
I can also monitor Latency and other SNMP variables. There are a whold bunch of templates. Best of all, I can see the total bandwidth (read GB/month) off of any window. Totally beats MRTG.
Good tutorial though!
Mike @ Aug 2nd 2006 1:28AM
I second the comment on Cacti, I used to use MRTG but now that I'm monitoring multiple machines Cacti is a much better choice. More viewing options and looks nice and pretty.
http://gentoo-wiki.com/Cacti
Dirk @ Aug 2nd 2006 1:29AM
@Sean: If you use Windows try PRTG www.paessler.com/prtg (mentioned by Todd above)
Mark @ Aug 2nd 2006 4:14AM
If u just want to look at your computers bandwith, its as easy as entering as opening task manager,
nice how to btw
blankoboy @ Aug 2nd 2006 6:51AM
Another vote for PRTG (www.paessler.com). Great SNMP graphing tool! Been using it for the last 2+ years and love it.
Just wish D-Link had the sense to put SNMP on their freaking routers...ugh!
Todd @ Aug 2nd 2006 8:34AM
The PRTG program is free for up to 3 nodes. It's cost is very low for more nodes. Only $625 for the unlimited enterprise version. It put MRTG to shame!
Also, if you just want to monitor bandwidth on your local computer, 'NetStat Live' from www.analogx.com works quite well. It's totally free too.
PMX @ Aug 2nd 2006 8:59AM
For monitoring bandwidth usage on a single Mac, there is also the free (and open source) MenuMeters. It shows instant +historic graph (up to an hour) in the menu bar:
http://www.ragingmenace.com/software/menumeters/
Garrow @ Aug 2nd 2006 9:37AM
A simple app graph for windows, that you can place in the windows taskbar or desktop toolbar is called 3rtg.
It uses SNMP and is dead simple to use.
not the prettiest or most configurable, but great for everyday all-the-time use.
I found it a while ago on one of those meta-meta sites. or something.
http://codefromthe70s.org/3rtg.asp
anonc @ Aug 2nd 2006 9:47AM
That long crontab entry is unnecessary. Use "*/5" for "every five minutes" instead.
Aaron @ Aug 2nd 2006 9:55AM
NetStat rules!
http://www.analogx.com/contents/download/network/nsl.htm
Bill Durr @ Aug 2nd 2006 10:07AM
For the Linksys WRT54G/GS/GL routers I would recommend using the DD-WRT firmware. You can find it at http://www.dd-wrt.com/ . I've also heard good things about OpenWRT, found at http://openwrt.org/ , but do not know much about it. Either choice is good, but again, I would reccomend DD-WRT.
John Laur @ Aug 2nd 2006 10:34AM
I was just dropping in to mention 3rtg also. It's a great little toolbar app.
otakucode @ Aug 2nd 2006 11:41AM
After tiring of putting up with my Linksys router and wanting more flexibility and features than it could provide, I decided to setup a little PC to do router and other duties. This was a fantastic idea. I got a mini-PC that used to be used in an airline terminal kiosk for $25 off of eBay that has a tiny case, uses a laptop hard drive and CD-ROM (the case really is tiny, not one of those gigantic Shuttle cases), etc.
For software, I got a customized free Linux distribution called IPCop. It generates graphs just like the ones pictured here. For traffic and for system resource usage. And it includes a web cache via squid, a DNS cache, intrusion detection and rule-based firewall protection via snort, traffic shaping, DHCP server, local DNS, network segmentation so you can treat your wireless network differently from your wired network... all kinds of stuff. I HIGHLY recommend it. Oh, and it is end-to-end web-based. Quite nice... almost feels like cheating.
Tricky @ Aug 2nd 2006 3:27PM
There is an shorter command for the crontab.
*/5 ***** for every 5 minutes. But for those who like to list it out that kewl with me.
Mike @ Dec 6th 2008 7:34AM
Another Cacti convert.
I was just too dumb/ignorant/lazy to get mrtg/nagios going.
Cacti was more straight-forward, even a drooling moron can install it!
colin @ May 5th 2009 12:07AM
I must be a drooling moron, because Cacti refused to install on my Mythbuntu machine. On top of that, it borked my apt manager, so now I can't uninstall Cacti, because I get an error code that the following scripts returned exit code 1 - ufw, cups, bluez-cups, and cacti. Not sure I can install anything until this is resolved, but strangely it does appear things can still upgrade. Beware of Cacti all those without great Linux foo.
colin @ May 5th 2009 12:08AM
You forgot to tell people to sudo mkdir ./var/www/mrtg before running cfgmaker
When I go to http://localhost/mrtg, all I get is an apache directory listing of 3 pngs of icons, but no sexy graphs. Any ideas on what I did wrong or how to fix it?
Also, even though my crontab is exactly as specified with the log location as /var/log/mrtg.log, this file doesn't exist, my log goes into /var/log/mrtg/mrtg.log . Any ideas why this is?
Thanks.