Linux — MRTG in Minutes!

MRTG is one of those tools a Network Engineer has on his/her toolbelt. MRTG allows the graphing of trending network activity for a wide range of devices. It uses SNMP to query a host and poll it’s network information and statistics. It can be used with Routers, Switches, Linux Hosts, etc.

I’m going to run MRTG against a Linux Router(running IPtables), a HP PowerConnect Switch, a Cisco Router, and an ESXi host. Anyway….Let’s get started.

Prepare SNMP Devices

  • Linux Router –Install SNMPd on the Linux Host being used as a router.

    apt-get install snmpd
    vi /etc/snmp/snmpd.conf
    
    #Paste in the follow:
    
    com2sec local localhost public
    com2sec mynetwork 10.0.0.0/24 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 Linux (RH3_UP2), Home Linux Router.
    syscontact Jim <jim@techjockey.net>
    

    What we are doing here is setting up the default settings for SNMPd. local is the Read/Write group profile, which will only be localhost. mynetwork is the Read Only group profile, which will be accessable by any host address on the 10.0.0.0/24.

    service snmpd restart
  • PowerConnect Switch

    SW1# conf t
    SW1(conf)# snmp-server 10.0.0.9
    SW1(conf)# snmp-server community public ro view Default
    SW1(conf)# exit
    SW1# copy running-config startup-config 

    Note:We are setting the snmp server IP and the community string of public to Read Only.This way when MRTG trys to query SNMP statistics it will have permissions to read the Switch’s metrics.

  • Cisco Switch/Router

    Router# conf t
    Router(config)# snmp-server community public RO 
    Router(config)# snmp-server host 10.0.0.9
    Router(config)# end
    Router# wr me

    Very similar to PowerConnect Switch

  • ESXi 5.1 Host

    1. Get the current Community
      esxcli system snmp get
         Authentication: 
         Communities: public
         Enable: false
         Engineid: 00000063000000a1c0a8000a
         Hwsrc: indications
         Loglevel: info
         Notraps: 
         Port: 161
         Privacy: 
         Remoteusers: 
         Syscontact: 
         Syslocation: 
         Targets:
         Users: 
         V3targets: 
      

      Notice, SNMP is currently not enabled. So we need to enable it :-p

    2. Enable SNMP
      esxcli system snmp set --enable 1
      esxcli system snmp set --communities public --targets 10.0.0.9
      esxcli system snmp test

      This will configure the ESXi host to use community public and trust 10.0.0.9

Setting up the MRTG Collector

    1. First off, install MRTG:

      apt-get install mrtg apache2
    2. Create a MRTG config for each Device

      Run the cfgmaker command to auto populate the cfg for this type of host. This will create a config file for this device type….

      cfgmaker --global "WorkDir: /var/www/mrtg/router" --output /etc/mrtg.d/router.cfg --ifref=name public@10.0.0.1

      We assume 10.0.0.1 is our Router

    3. Create an Index.html page for each Device

      indexmaker --output /var/www/mrtg/router/index.html /etc/mrtg.d/router.cfg 
      env LANG=C mrtg /etc/mrtg.d/router.cfg 

      Notice, The indexmaker reads the MRTG config file that you made above and creates HTML to present the graphs for each interface.

    4. Configure CRON job to periodically poll each Device

vi /etc/cron.d/mrtg
#Paste the following for each device:
/5 * * * * root env LANG=C mrtg /etc/mrtg.d/router.cfg

Notice, the above line sets a cron job to run every 5 minutes and run MRTG using configuration file router.cfg. So if you have multiple devices, just add a new line for each one.

Sources: