MRTG, or Multi Router Traffic Grapher, is a popular monitoring and statistics tool for use on Linux platforms. It was designed to monitor the traffic load on network links, but it’s highly extensible and configurable. Not only can you watch interfaces on routers and switches, but you can also analyze Linux system variables. Although this is usually done with the Simple Network Management Protocol (SNMP), MRTG also supports the use of local scripts to feed it data. The data is then converted into HTML with graphs in PNG format for a visual representation of your network.

Overview of SNMP
Before we delve too deeply into the installation and configuration of MRTG, it is important to understand how MRTG and SNMP work together. By default, MRTG will take an SNMP community string and a device hostname or IP address and discover what it needs to know. This is done by polling Management Information Base (MIB) objects predefined for all SNMP-ready devices. A MIB is basically a database of variables that corresponds to a system value. Basic examples include information such as system name, interface type, and uptime.

Most vendors will have MIBs for their line of products, and these will conform to SNMP standards. On the device, SNMP then needs to be enabled and a community string set. Such configurations will vary from vendor to vendor and sometimes from device to device. On Cisco products, for instance, the configuration command snmp-server is used as follows:
snmp-server community c1sc0 RO

This informs the Cisco IOS to provide read-only access to a querying SNMP tool that provides the community string c1sc0. It is best that you treat SNMP strings like passwords and configure them appropriately. SNMP is probably already configured and running if you currently use other network and system monitoring tools, such as HP OpenView, Cisco Works, or OpenNMS.

Linux will also need to be configured to provide real-time data to MRTG. This can be done with SNMP or through the use of local scripts. To listen for incoming SNMP requests, you will need to install and configure snmpd. Configuration is done through the snmpd.conf file and requires you to set your community string. For a standard UCD SNMP implementation, the line in snmpd.conf will look something like this:
com2sec readonly  default       liNuX42

Notice that once again, we are giving only read-only access to SNMP agents. Read-write access is also configurable, but if you don’t need it, don’t configure it. With read-only, you will be able to view only statistics and configuration options. Although this is something that should be monitored closely for security reasons, it is not nearly as dangerous as allowing remote commands to be executed via SNMP.

Installing and configuring MRTG
MRTG, like more and more packages these days, should be available through your favorite automated installer. So whether it is APT or up2date, that is usually the quickest way to get it onto your system. Alternatively, source and RPM distributions are available from the maintainer’s site at MRTG.org. MRTG has a number of dependencies as well, the most important being Perl, gd, libpng, and zlib. Keep this in mind during installation if you encounter problems. Source code installation follows the standard scheme with:
tar xpfz mrtg-2.9.18.tar.gz
./configure
make
make install

Installing with RPM is also easy. The package name may vary, but you simply use the command:
rpm –ivh mrtg-2.9.18.rpm

Once MRTG is installed on your system, you may need to configure a Web directory where it will place its HTML and PNGs for viewing. Use mkdir to create a directory called mrtg under your main HTTP path and modify your permissions. For example:
mkdir /var/www/mrtg
chmod 755 /var/www/mrtg

The next step is to create a configuration for MRTG to use. The quickest way to do this is by running cfgmaker and passing it the SNMP community string and device and outputting it to a text file, like this:
cfgmaker  c1sc0@192.168.66.72 > /etc/mrtg.cfg

Then, edit the new mrtg.cfg file and add a line listing the path to your working directory (where your MRTG-related files will be stored), like this:
WorkDir: /var/www/mrtg

Note that the device can be represented as either an IP address or a hostname. Also, cfgmaker can support a variety of options and settings at the command line, depending on the version. The best way to get started, though, is to use the default options. You can always make changes directly to the configuration file later.

The basic configuration file will look something like this:
Target[192.168.66.72.1]: 1:c1sc0@192.168.66.72
MaxBytes[192.168.66.72.1]: 1250000
Title[192.168.66.72.1]: RTR2: Ethernet0/0
PageTop[192.168.66.72.1]: <h1>Traffic Analysis for Ethernet0/0</h1>

These are just a few of the many options that can be placed into the MRTG configuration file. The first value here is Target. Inside the brackets is a unique name that will allow separate options for multiple devices and interfaces. Then, we have what will actually be providing the data, in this case, SNMP.

Next, we have the MaxBytes option. This is how the upper end is configured, and it’s what percentage values are based on. It will vary between mediums and targets, depending on what you are polling. The next two options are for the HTML and act as descriptors.

Local scripts provide another way to get information to MRTG. These can range from simple to complex shell and Perl scripts. We’ll look at how to do a simple shell script that will provide MRTG with data on packets inbound to an iptables firewall.

First, we will create the script, /usr/local/mrtg/iptables-in:
#!/bin/bash
IPTABLES=”/sbin/iptables”
UPTIME=”/usr/bin/uptime”
$IPTABLES -nvxL | grep INPUT | awk ‘{ print $5 }’
$UPTIME | awk ‘{ print $3, $4, $5 }’

This script will pull the number of packets it has accepted from the INPUT rule. It uses grep to search for the line containing INPUT and prints only the fifth value, which in this case is the number of packets processed. The uptime command is used so that MRTG will know how much time has passed between each value and can chart it over a period of time.

The mrtg.cfg will then look something like this:
WorkDir: /var/www/mrtg
Target[in_packets]: `/usr/local/mrtg/iptables-in`
MaxBytes[in_packets]: 1500
Title[in_packets]: Inbound packets
PageTop[in_packets]: <h1>Inbound Packet Stats</h1>

This is similar to the configuration used for SNMP above; however, we are pointing the target to an external script called iptables-in. We are also setting a fairly arbitrary MaxBytes setting since we are dealing with packets as opposed to bits or bytes. This will vary from system to system and a few different values may need to be tried. Although this is a rather simple example, it should give you an idea of what is possible with MRTG. Any data you can gather via the command line can be imported into MRTG and graphed in real time.

Running MRTG
Once MRTG has been installed successfully, you will need to configure it to run on your system. MRTG can be run manually as follows:
/usr/bin/mrtg  /etc/mrtg.cfg

MRTG updates its log files every time it is run. The first two times, all the files will not have been created and you may see errors. These can be ignored, but anything you see on the third time around will probably warrant closer investigation.

It is a good idea to configure cron to run the program, as MRTG will need to execute often to poll devices and gather data. The standard interval is five minutes, but you can change it to fit your particular needs. Place an entry in /etc/crontab giving the interval, user to run as, path to mrtg, and the path to mrtg.cfg like this:
*/5 * * * *  root  /usr/bin/mrtg  /etc/mrtg.cfg

Once MRTG is configured and running, all you will need to do is access the pages through a Web browser. You may want to tweak the HTML manually to do such things as index multiple devices and enable auto-refresh. Figure A shows a sample graph to give you an idea of what the finished product will output.

Figure A

Summary
MRTG is a powerful and extensible tool that will allow you to graph network links and system information over a configurable period of time. Utilizing SNMP, local scripts, or a combination of the two, MRTG allows you to easily monitor your devices. Such things as trends, traffic spikes, and slow periods are easy to detect with a visual representation. MRTG does this in excellent form, outputting data in HTML and PNG formats viewable through a Web browser. MRTG doesn’t have all the bells and whistles of commercial monitoring software, but it does the job well and is definitely worth considering as part of your network monitoring activities.

 

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays