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

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

  1. Luke

    Thanks a lot for the instructions, worked perfectly. I also managed to have multiple devices in one page with mrtg, basically you have to set up a separate workdir and .cfg file for every device, add the crontab entries and then link all the .html pages in the main .index.html
    (or just copy&paste relevant sections from the other index.html pages to the main index.html).

  2. Kevin

    Hi guys…

    I got everything done as instructed above… but I got permission problems πŸ™

    Forbidden
    You don’t have permission to access /mrtg/ on this server.

    ——————————————————————————–

    Apache/2.2.3 (CentOS) Server at 192.168.159.11 Port 80

    Can someone point me in the right direction? I did make the /home/mrtg folder permissions 755, then 777… to no avail

  3. Sunny Walia Post author

    Kevin,

    Make sure below line in httpd.conf includes your own ip:
    Allow from 127.0.0.1 192.168.30.0/24

    Add it in and restart apache and see if it works.

  4. Kevin

    Hi Sunny,

    Thanks! Turns out mrtg is already defined as an alias prior to my alias… which points to a place where apache user cannot access…

    I was also able to run cfgmaker couple times with different *.cfg output files, then run indexmaker with those multiple *.cfg files by separating them with space…. turns out MRTG now runs monitoring on all those devices… I feel good πŸ™‚

  5. Kevin

    Hey guys… got another question… quite important for me… I have two ASA5505 being monitored by MRTG right now and they output seem great…

    The million dollars question is… how do I monitor the VPN tunnel between them? I just want to measure the traffic on the tunnel.

    Appreciate any kind of help… point me to a link would be good enough… been asking Dr. G and he has too many vague answers πŸ™‚ Thanks!

  6. shamshad

    Dear Sir,

    I have to configure MRTG in linux server please send me the configuration details so that i will configure easily.

    Waiting for you reply.
    Thanking You

  7. Sunny Walia Post author

    You are very welcome Alex. If you have any questions, you can post ’em in the forums section and I will try to help as I get a chance πŸ™‚

  8. immortal

    Dear all,
    like the above, i get

    Forbidden

    You don’t have permission to access /mrtg/ on this server.
    Apache/2.2.14 (EL) Server at 127.0.0.1 Port 80

    when i put the http://127.0.0.1/mrtg in the browzer.

    anyways, my intention is to play around with the configuration of the MRTG in localhost( i mean in my computer)..but no other devices.

    Please any one ..help..me

  9. Towhid

    Dear Sunny this is my httpd.conf entry for alias

    Order deny,allow
    Deny from all
    Allow from 127.0.0.1 192.168.7.0/24

    Still i am getting following error

    Forbidden

    You don’t have permission to access /mrtg/ on this server.
    Apache/2.2.3 (CentOS) Server at 192.168.7.96 Port 80

    Please help me out.

  10. borja

    You only have to coment one line on /etc/httpd/conf.d/mrtg. You must replace allow 127.0.0.1, with allow any.

  11. Nahush Mundra

    Dear sir please send the configuration step by step of MRTG in linux RHCE…Yours response is highy appreciated.

    Thank you!!!

  12. Nahush Mundra

    Dear sir ,

    Please send the configuration step by step of MRTG server in linux
    Yours response will be highly appreciated

    Thank you..!!!

  13. Agyan

    Is it possible to configure MRTG to show only half the rate of actual rate? if yes, how?

  14. abbas

    when i enter snmpwalk -v 1 -c public localhost IP-MIB::ipAdEntIfIndex
    this command do i have to replace IP with my own ip or not ? one more thing when i enter this command it says

    -bash command not found

  15. Www.Yelp.com

    Literally millions of spores are produced by each mold colony
    and discolored streaks result along with clumps of blue,
    green, or white furry looking growths on the surface of the cheese.
    Even if thereÒ€ℒs just a little bit of moisture, the possibility
    of mold infestation is high. If you hire a company that handles both processes from the start, they are likely to
    be able to discover that mold is going to be a problem and take care of it before it becomes
    a health issue.

  16. Desiree

    Howdy! I simply would like to give you a big thumbs
    up for your great information you have got right
    here on this post. I will be returning to your web site for more soon.

  17. youtube.com

    Its like you read my mind! You appear to know so much approximately this, like you wrote the guide in it or something.
    I think that you could do with a few percent to power the message
    home a bit, but other than that, this is wonderful blog. A great read.
    I’ll certainly be back.

    Feel free to visit my homepage :: vindictus hack – youtube.com

  18. BEST MOUSE BAIT

    I like the helpful info you provide in your articles.
    I will bookmark your weblog and check again here regularly.
    I am quite certain I’ll learn many new stuff right here!
    Best of luck for the next!

    Feel free to surf to my page – BEST MOUSE BAIT

  19. Stacie

    Pretty nice post. I just stumbled upon your weblog and wanted to say that I’ve really enjoyed surfing around
    your blog posts. In any case I will be subscribing
    to your feed and I hope you write again very soon!

    Also visit my web-site: throne rush hack (Stacie)

  20. Kingsjunkcars.com

    In many cases, local companies offer fast and convenient vehicle pickups every
    dayy of thee week. The most adventurous thing you can do with a junk vehicle is take off the alternator and use it to
    make your own wind mill. Used books can go well wiith a younger crowd, and near cafes and
    newsstands.

  21. Boats India

    With probably around five or six films to choose from,
    the one he showed on that particular evening was
    the quintessential World War Two drama, in glorious black and white, “Sink The Bismark”.

    To sail off fast on the new close-hauled course, sheet in the jib and pull the tiller towards you.

    You often fail to complete the tack or misjudge the wind when first learning how to
    handle a catamaran.

  22. sleep and

    Wow, wonderful blog structure! How long have you been running a blog for?
    you made blogging look easy. The full glance of your website is excellent, as smartly as the content material!

  23. ????????

    Hey there! I know this is somewhat off topic but I was wondering if
    you knew where I could find a captcha plugin for my
    comment form? I’m using the same blog platform as yours and I’m having trouble finding one?
    Thanks a lot!

  24. penile extenders results curve

    Hello, I think your blog might be having browser compatibility issues.

    When I look at your blog site in Ie, it looks fine but when opening in Internet Explorer,
    it has some overlapping. I just wanted to give you
    a quick heads up! Other then that, excellent blog!

  25. http://ricardoalfonsinblog.com/profile-2992/info/

    Hello I am so excited I found your blog, I really found you
    by error, while I was looking on Askjeeve for something else, Anyhow I am here now
    and would just like to say kudos for a remarkable post and a all round exciting blog (I also love the theme/design), I don’t have time to browse it all at the
    minute but I have saved it and also included your RSS feeds,
    so when I have time I will be back to read much more, Please do keep up the superb job.

  26. kd 10

    I in addition to my friends were reviewing the nice ideas from your web site and then immediately I got a terrible feeling I had not expressed respect to the web site owner for those tips. My ladies had been for this reason warmed to study all of them and now have definitely been taking pleasure in those things. Thank you for really being quite considerate and then for picking out variety of essential subject areas millions of individuals are really wanting to know about. My sincere regret for not saying thanks to sooner.

  27. balenciaga sneakers

    Thank you a lot for providing individuals with an extraordinarily nice opportunity to discover important secrets from this blog. It really is so beneficial and as well , jam-packed with a lot of fun for me and my office peers to visit your blog a minimum of three times every week to read the newest secrets you have. And lastly, I’m also certainly pleased with your powerful things you give. Selected two tips in this article are essentially the finest we have had.

Leave a Reply

Your email address will not be published. Required fields are marked *