How to setup MRTG on Centos (RHEL) 4.x and 5.x?

By Scott Seong

MRTG stands for "Multi Router Traffic Grapher", and it is a tool to monitor the traffic load on the network interfaces. It generates HTML pages containing PNG images which depicts visual representation of inbound and outbound traffic. MRTG is free software designed by Tobi Oetiker and is licensed under the Gnu GPL. Most Linux distributions include MRTG as an RPM package, which you can install; or download the latest copy from the official MRTG website. You may also install MRTG on Windows platform.

MRTG uses a highly portable SNMP implementation written in Perl programming language, which interacts with SNMP agent to receive traffic characteristics. To use MRTG to draw pretty traffic graphs, the server running MRTG must communicate with SNMP agent. Most routers these days support SNMP agent functionality. If you do not have control over a router, you may install and run any snmp agent (i.e. net-snmp on linux distributions) on the server where you want to monitor traffic. To learn more about snmp, please consult MRTG and SNMP Overview.

1. Setup SNMP Agent

In order for SNMP Manager such as the MRTG to poll information from a Router (or a server), we need to setup SNMP community on the SNMP agent. If you have the control over a router, you may simply setup a read-only (public) community on the router. Most routers today support SNMP agent functionality.

You may have to follow your SNMP agent documentation to configure the public community that will allow MRTG to poll information from it. On a Cisco router, you may use the following command to configure public community:

cisco# snmp-server community public RO

If you have other brand of router, check with manufacturer's manual to determine how to configure SNMP community that will allow SNMP managers to query information from a MIB.

If you do not have an access to a router or prefer installing SNMP agent on your server, you may install SNMP agent on the server. There are a couple of SNMP packages that are available on Linux platform: cmu-snmp and net-snmp. You may install any SNMP packages (available as a RPM or tarball), and they will work virtually the same.

bash# yum install net-snmp

If you have installed net-snmp package, you may configure read-only community by replacing snmp configuration file, /etc/snmp/snmpd.conf, with the following:

# Content of /etc/snmp/snmpd.conf
rocommunity public

# Start the snmpd daemon on Linux command-line.
bash# service snmpd start
# Allow snmpd damon to restart on reboot
bash# chkconfig snmpd on

2. Install and configure MRTG

Most Linux distributions include MRTG as an RPM package. You may install MRTG with 'yum' command. You may also use cfgmaker script to build configuration file for MRTG.

bash# yum install mrtg
bash# cfgmaker {community}@{SNMP agent host IP} > /etc/mrtg/mrtg.cfg

#OR, for an example:
bash# cfgmaker public@localhost > /etc/mrtg/mrtg.cfg

The cfgmaker script will discover every interface in your router and write a section in the configuration file with its interface number, speed and description. You may edit mrtg.cfg file manually to further customize the configuration for your environment.

3. Run MRTG

Running mrtg is very straight-forward. Simply run mrtg with the configuration file built in the previous step.

bash# mrtg /etc/mrtg/mrtg.conf

### You may see following error messages when you run 'mrtg' for the first time.
### You may ignore them, as necessary files will created (and removed) as you run
### 'mrtg' multiple times to monitor network traffic on a regular basis.
bash# mrtg
ERROR: Mrtg will most likely not work properly when the environment
variable LANG is set to UTF-8. Please run mrtg in an environment
where this is not the case. Try the following command to start:

env LANG=C /usr/bin/mrtg
bash# LANG=C
bash# mrtg mrtg.cfg

21-08-2007 01:08:55, Rateup WARNING: /usr/bin/rateup could not read the primary log file for localhost_2
21-08-2007 01:08:55, Rateup WARNING: /usr/bin/rateup The backup log file for localhost_2 was invalid as well
21-08-2007 01:08:55, Rateup WARNING: /usr/bin/rateup Can't remove localhost_2.old updating log file
21-08-2007 01:08:55, Rateup WARNING: /usr/bin/rateup Can't rename localhost_2.log to localhost_2.old updating log file

bash# mrtg mrtg.cfg

21-08-2007 01:09:55, Rateup WARNING: /usr/bin/rateup Can't remove localhost_2.old updating log file

bash# mrtg mrtg.cfg

If you wish to monitor your network continuously, you may want to add a cron entry to run mrtg on a regular interval. For example, if you wish to run mrtg every 10 minutes, create a cron entry similar to the following:

bash# crontab -e
*/10 * * * * /usr/bin/mrtg /etc/mrtg/mrtg.cfg> /dev/null 2>&1

# Create a master index.html file. This only needs to be done once.
bash# /usr/bin/indexmaker /etc/mrtg/mrtg.cfg >/home/mrtg/index.html

The indexmaker script above creates a master index.html file that builds a HTML page for all the interfaces defined.

All the necessary files have been created by the MORT and the indexmaker in the directory specified by the mrtg.cfg configuration file, so setup a virtual host in the same directory and you'll be able to view traffic characteristics of the network interfaces. The end result may look something similar to below.

The solid green represents the inbound traffic into your router (or a server), and the blue line represents the outbound traffic.