Category Archives: SNMP

MRTG (Multi Router Traffic Grapher): How to setup MRTG in Linux to monitor Bandwidth usage?

If you run Linux server, there are times you probably wonder how much traffic is flowing through your server. It could be you got a fat bill from your ISP because you used too much bandwidth (good problem to have most of the time). At that point, you probably want to monitor how much bandwidth you are using. One of the best tools you can use is MRTG (Multi Router Traffic Grapher). It is not very hard to setup MRTG and requires only few packages to be installed.

Assumptions:

  1. Linux server with Yum installed (of course you can download individual packages in other flavors of Linux which I am not going into details here).
  2. Your server ip is on 192.x.x.x network. If your server is not, please replace all the places where I mention using 192.x.x.x)
  3. We are going to store files in /home/vhosts/mrtg If you want a different location, change it in all appropriate places.
  4. Apache is installed at: /usr/local/apache2/ with httpd.conf in conf directory in Apache installed directory.
  5. SNMP/MRTG packages are not installed.
  6. Server IP is 192.168.30.115
  7. Last but not least, YOU are logged in as root!

Ok now let us go into details on how to install MRTG.

Lets install all the packages we need. Note that this will also install few other packages. Eg. gd, lm_sensors, net-snmp-libs, etc
yum -y install mrtg net-snmp-utils net-snmp

Now lets start SNMP server
/etc/init.d/snmpd start

Is SNMP running? You should see something after you run following command
netstat -na | grep '0.0.0.0:199'

If it is not working, check logs and see why not. If it is (which is most probably the case if you didn’t miss the second step), try this command and see if you get output:
snmpwalk -v 1 -c public localhost IP-MIB::ipAdEntIfIndex
OUTPUT (something similar, your 192.x.x.x ip should be your server ip):
IP-MIB::ipAdEntIfIndex.127.0.0.1 = INTEGER: 1
IP-MIB::ipAdEntIfIndex.192.168.30.115 = INTEGER: 2

If you don’t get the above or something similar output, do following.
NOTE: I had to do following on all the servers I had setup:
vi /etc/snmp/snmpd.conf
Replace the content in there with following:
com2sec local localhost public
com2sec mynetwork 192.0.0.0/8 public
group MyRWGroup v1 local
group MyRWGroup v2c local
group MyRWGroup usm local
group MyROGroup v1 mynetwork
group MyROGroup v2c mynetwork
group MyROGroup usm mynetwork
view all included .1 80
access MyROGroup "" any noauth exact all none none
access MyRWGroup "" any noauth exact all all none
syslocation "Some place out there"
syscontact Admin <[email protected]>

You should change last two lines to something appropriate to your usage.

At this point you should be good to go, so lets restart SNMP.
/etc/init.d/snmpd restart
Run this command look for appropriate output as mentioned above.
snmpwalk -v 1 -c public localhost IP-MIB::ipAdEntIfIndex

Ok. That is all we will do with SNMP. Let us continue our MRTG installation.

Let us set up data directory to store html files:
mkdir /home/vhosts/mrtg

Now let us set up mrtg.conf to use for monitoring bandwidth usage:
cfgmaker --global "workdir: /home/vhosts/mrtg" -ifref=ip --output /etc/mrtg/mrtg.cfg --global 'options[_]: growright,bits' [email protected]

Let us create index file for MRTG page:
indexmaker --output=/home/vhosts/mrtg/index.html /etc/mrtg/mrtg.cfg

It is time for us to tell Apache to serve these pages
vi /usr/local/apache2/conf/httpd.conf
Alias /mrtg /home/vhosts/mrtg
<location /mrtg>
Order deny,allow
Deny from all
Allow from 127.0.0.1 192.168.30.0/24
</location>

NOTE: Make sure your subnet is in “Allow from ” line. Otherwise you won’t be able to access MRTG files from browser.
Restart Apache to read the new conf file.
/usr/local/apache2/bin/apachectl graceful

It is time for us fire this thing up and start creating some graphs!
env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg --logging /var/log/mrtg.log

You should be able to view your MRTG graphs at: http://192.168.30.115/mrtg

If everything is working fine, let us add this to cron so these stats are updated every 5 mins.
crontab -e
*/5 * * * * env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg --logging /var/log/mrtg.log