
MRTG on Ubuntu Server
Lets start by installing the goods. Then we will get into the meat of things.
apt-get install snmpd mrtg apache2
When installing MRTG it will ask if you want to only make /etc/mrtg.cfg readable by the MRTG user. In my case, this will be an internal server only, and I do not need to worry about security, so I am going to select NO. If you choose YES, the steps in this tutorial may not work. I have not tested this. However, keep in mind if security is a concern, you may want to go this route and keep MRTG contained in a non-root user environment.
Assuming that you want to monitor your networking equipment with MRTG, you need to set up the snmp community on the piece of equipment. Usually its just a matter of enabling snmp, and the default community will be public. If you change the default community to another name, make sure you remember the name for later.
Now lets set up MRTG to read the data.
With Ubuntu server, MRTG will automatically set up /var/www/mrtg. In other distrobutions, it will set it up in a different area, so you will need to change the 'WorkDir:' variable in the following scenario to the directory where MRTG is installed.
I'm going to create an /etc/mrtg directory rather then move the mrtg.cfg file within /etc/ This will make it less confusing in the future.
mkdir /etc/mrtg
mv /etc/mrtg.cfg /etc/mrtg/
Now we run the cfgmaker command to get things up and running:
cfgmaker --global 'WorkDir:/var/www/mrtg' --global 'Options[_]: bits,growright' --output /etc/mrtg/mrtg.cfg public@<yourrouterip>
We need to make sure the following lines are correct in /etc/mrtg/mrtg.cfg If they are not there, add them.
EnableIPv6: no
RunAsDaemon: Yes
Interval: 5
Logdir: /var/log/mrtg
Now run:
indexmaker --output=/var/www/mrtg/index.html /etc/mrtg/mrtg.cfg
Which will build the index so you can view the output.
Then I run this command to set an environmental variable
env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg
and open http://localhost/mrtg/index.html
You should now see an output graph.
crontab -e
0,5,10,15,20,25,30,35,40,45,50,55 * * * * env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg --logging /var/log/mrtg.log
Let cron pick up stats for an hour or so. After a while, you will see that your graphs are picking up data about your bandwidth.
Here is mine with several cisco 2811 routers configured to show bandwidth.
SNMP and MRTG can monitor much more then bandwidth. For instance you can monitor disk usage, processing use, and more on your servers. There is plenty of documenation on the subject, which leans towards going in and adding snmpd to the servers you want to monitor, setting it up, and then going back and executing the cfgmaker and indexmaker command again so that the new hardware will show up on the page. Because the output of these files is an html page, you can also skin the page and make it look nicer, or embed it into your favorite CMS.
I encourage you to play around with MRTG, because it is the foundation program for a lot of software suites that monitor traffic. One of those, we will talk about next time. Until then, keep hackin.
I used the following articles and papers to create this tutorial:
http://ubuntuforums.org/

Have you tried Cacti?
Hi Chad,
I just heard you talk about the Virginia open school project on TLLTS. Keep up the great work! After checking out your article here, I just wondered if you have heard of or tried Cacti? I think you would really like it if you like MRTG. It uses RRDtool which is very similar to MRTG. Everything is open source of course. Cacti is a PHP web-based interface to RRDtool. The web interface makes it very easy to manage and view graphs. I manage about 20 Red Hat Enterprise Linux servers for a couple of large hotel chains. I don't know what I would do without Cacti. It has a lot of plugins as well like "thold" which is a threshhold plugin to send you alerts when an item you are monitoring breaks a threshhold. It also has a reporting plugin which I haven't gotten working yet but looks interesting. At any rate, they have a large and friendly user community. The Cacti web site is at www.cacti.net. Check it out if you get a chance. One of the primary things I monitor with these hotels is Tomcat. I use Perl scripts to query the Tomcat manager app to return data such as sessions, memory usage, thread count, busy threads, etc. I then enabled custom snmp OIDs to wrap around the Perl scripts. These are queried by Cacti and graphed. I included a sample graph of "Tomcat Free Memory" below. I took out the hostname for privacy but this is a real hotel's graph on one of the servers. :-) (The fall and spike of free memory is due to the JVM garbage collector running.) One of the things I like about Cacti is that the graphs look nice. You can customize pretty much every aspect of it - colors, font, legends, etc. Also, the query tool makes it easy to target certain ranges - last 4 hours, last 6 hours, last 8 hours, last two weeks, etc. Perhaps if I get some time I will try and post a Cacti tutorial here. At least I'll provide some links as I think there are already some good ones out there. Just to warn you though, I'm a Fedora fan boy, ha ha so my tutorials would probably be geared toward Red Hat/Fedora.
Rick
Hey, thanks for the info and
Hey, thanks for the info and thanks for listening. Yes, I've used cacti and I hope to cover it this season along with zenoss and opennms. Right now I'm just trying to build the knowledge needed to know what is happening behind the scenes. That is awesome that you monitor Tomboy with it. I am not very good with Perl, but if I remember correctly a lot of the "plugins" for cacti need very little configuration. Good stuff.
Thanks,
Chad
Podcast 31
Hi again Chad,
I just heard podcast 31 and heard you mention Cacti. I had not listened to the podcast when I made my original comment. If you get to an episode where you want to talk more about Cacti I'd be happy to help if I can. Getting a basic setup up and running is pretty easy but anything custom can be tricky. Also, one gotcha is CPU monitoring. The ucd/net CPU Usage templates that come with Cacti assume one logical CPU. If you have a dual-core or quad-core system, etc. you must add a custom CDEF function to divide by the number of logical CPUs. If you don't do this, the graphs are off. For example, on a dual core system, the graph might show 10% user CPU when actually it was 5%, etc. Took me a long time to figure that one out! :-)
For Tomcat monitoring, there is a manager app that comes with Tomcat that gives a decent amount of statistics about a deployed application. My Perl script just calls wget to pull the stats (every 5 minutes on cron) and then parses out the desired values and writes them to a file. Then, using SNMP's "exec" command tied to an OID, you can read the value of your stat file. This same setup could be used to query pretty much anything. I like using a custom OID so that any device capable of reading SNMP can use the same query. For the hotel, they use Solar Winds so we just tell them the OID and they are good to go. Here is a sample line out of my snmpd.conf:
exec .1.3.6.1.4.1.2021.54 tomcatSessions /usr/local/bin/statgrab TOMCAT_SESSIONS
That basically says that when somebody queries .1.3.6.1.4.1.2021.54, snmpd will run "statgrab" and pass in "TOMCAT_SESSIONS". statgrab is a very simple Perl script to return the value of a text file. The wget script that pulls the values every five minutes writes them out to "/usr/local/stats/<STAT_NAME>". So TOMCAT_SESSIONS, for example, gets written to /usr/local/stats/TOMCAT_SESSIONS. statgrab opens the file and returns the value to snmpd. The script could be python or bash, etc. I happen to use Perl because I've been using it for awhile and like it (lately switching to Python though).
Here is sample output of snmpwalk against the above OID.
$ snmpwalk -v 1 -c public localhost .1.3.6.1.4.1.2021.54
UCD-SNMP-MIB::ucdavis.54.1.1 = INTEGER: 1
UCD-SNMP-MIB::ucdavis.54.2.1 = STRING: "tomcatSessions"
UCD-SNMP-MIB::ucdavis.54.3.1 = STRING: "/usr/local/bin/statgrab_string TOMCAT_SESSIONS"
UCD-SNMP-MIB::ucdavis.54.100.1 = INTEGER: 0
UCD-SNMP-MIB::ucdavis.54.101.1 = STRING: "717"
UCD-SNMP-MIB::ucdavis.54.102.1 = INTEGER: 0
UCD-SNMP-MIB::ucdavis.54.103.1 = ""
The value (717 just now) gets placed in the 54.101.1 ID so in Cacti, for sessions, I use the OID .1.3.6.1.4.1.2021.54.101.1.
Anyway, didn't mean to get carried away in my reply. I hope this info might be helpful to some people.
Have a good one,
Rick
Awesome, thanks for the info
Awesome, thanks for the info and thanks for listening Rick!
Chad