Share this page : facebooktwitterlinkedinmailfacebooktwitterlinkedinmail

In order to compile and use mrtg you need a C compiler and a copy of perl installed on your machine. In most cases this will already be available. In case it is not, here are some starting points. Below I’ll give you a detailed run through the whole compilation process.


The GNU C compiler comes preinstalled on most of the free Unicies out there. For commercial derivatives you may have to download and compile it first. If you have no compiler at all there is a chicken and egg problem, but there are also precompiled versions of gcc available for most operating systems.


Large parts of the MRTG system are written in the Perl scripting language. Make sure there is a recent copy of perl on your machine (try perl -v). At least version 5.005 is required for mrtg to work well. If you use SNMPV3 and other new features you should use at least 5.8.

You can get the latest perl from

MRTG generates traffic graphs in the PNG format. To be able to do this it needs several 3rd party libraries. When compiling these libraries I urge you to make sure you compile them as static libraries. There is just much less trouble ahead if you are doing it like this. See the Instructions in the next section for inspiration. Note that many free unices have all the required libraries already in place so there is no need to install another copy. To check it is best to skip all the library instructions below and go straight into the mrtg compile.

If the first attempt fails and you do not get a working version of mrtg, try compiling new copies of all libraries as explained below. Do this BEFORE you send email to me about problems compiling mrtg.


This is a basic graph drawing library created by Thomas Boutell. Note that all releases after Version 1.3 only create PNG images. This is because a) Thomas got into trouble because the GIF format which it used to produce uses a compression technology patented by Unisys. b) PNG is more efficient and patent free. MRTG can work with old and new version of the GD library. You can get a recent copy of GD from:


Is required by gd in order to produce PNG graphics files. Get it from:

Is needed by libpng to compress the graphics files you create. Get a copy from

And last but not least you also need mrtg itself. In case you have not yet downloaded it, you can find a copy on my website:


Note, that the version here may not be downloadable, try to find the available link from the official website.

In this section I will give you step by step instructions on how to compile the various libraries required for the compilation of mrtg. Note that these libraries may already be installed if you have a *BSD or Linux system so you can skip recompiling them. The wget program used below is a simple web downloader. You can also enter the address into your netscape if you don’t have wget available.

First let’s create a directory for the compilation. Note that this may already exist on your system. No problem, just use it.

 mkdir -p /usr/local/src
 cd /usr/local/src

If you do not have zlib installed:

 gunzip -c zlib-*.tar.gz | tar xf -
 rm zlib-*.tar.gz
 mv zlib-* zlib
 cd zlib
 cd ..

If you don’t have libpng installed

 gunzip -c libpng-1.2.34.tar.gz | tar xf -
 mv libpng-* libpng
 cd libpng
 env CFLAGS="-O3 -fPIC" ./configure --prefix=$INSTALL_DIR 
 rm *.so.* *.so
 cd ..

And now you can compile gd

Note that you do rename the folder name of the library above, or you need to change the /zlib /libpng into your own name, or else you will get error in the following gd installation.

For versions starting around 2.2.4, try:

 gunzip -c libgd-2.2.4.tar.gz |tar xf -
 mv libgd-2.2.4 gd
 cd gd
 env CPPFLAGS="-I../zlib -I../libpng" LDFLAGS="-L../zlib -L../libpng" \
     ./configure --disable-shared --without-freetype --without-jpeg
 cp .libs/* .


Ok, now everything is ready for the mrtg compilation.

 cd /usr/local/src
 tar -zxvf mrtg.tar.gz
 cd mrtg

If all the libraries have been preinstalled on your system you can configure mrtg by doing a simple:

 ./configure --prefix=/usr/local/mrtg-2

Otherwise you may have to give some hints on where to find the various libraries required to compile mrtg:

 ./configure --prefix=/usr/local/mrtg-2       \
             --with-gd=/usr/local/src/gd      \
             --with-z=/usr/local/src/zlib     \

If you have RRDtool available you might want to tell mrtg about it so that you can opt to use rrdtool with mrtg. Check mrtg-rrd.

Configure will make sure your environment is fit for building mrtg. If it finds a problem, it will tell you so and it will also tell you what to do about it. If everything is OK, you will end up with a custom Makefile for your system. Now type:


This builds the rateup binary and edits all the perl pathnames in the scripts. You can now install mrtg by typing

 make install   (requires gnu install)

All the software required by MRTG is now installed under the /usr/local/mrtg-2 subdirectory.

You can now safely delete the libraries we compiled above. Then again, you might want to keep them around so that you have them available when compiling the next version of mrtg.


Configure SNMP on Cisco Router or switch:

snmp-server community string ro | rw SNMP_ACL ip access-list standard SNMP_ACL permit xx.xx.xx.xx

Login on the Linux machine and go to the www_root folder (it’s something like “/var/www/html/”), then make a new folder and go inside:

[[email protected] ~]#
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# mkdir Cisco-MRTG
[[email protected] html]# cd Cisco-MRTG/


Create the MRTG configuration files based on information pulled from the Cisco element, using the “cfgmaker” command ( is the Cisco IP address):

It’s always good practice to put the configuration files in the /etc directory:

sudo midir /etc/mrtg
sudo /usr/local/mrtg-2/bin/cfgmaker --global "WorkDir: /var/www/html/Cisco-MRTG" --output /etc/mrtg/Cisco-MRTG.cfg [email protected]

You will get a configuration file named Cisco-MRTG.cfg in the Folder: /etc/mrtg/Cisco-MRTG.cfg

We will talk about how to customize it later.

Create the Main web page which display the status of an array of mrtg interface status pages, using the “indexmaker” command:

sudo /usr/local/mrtg-2/bin/indexmaker --output index.html /etc/mrtg/Cisco-MRTG.cfg

Then create all the sub pages and pictures with the  configuration file:

env LANG=C /usr/local/mrtg-2/bin/mrtg /etc/mrtg/Cisco-MRTG.cfg

now you can enter the http://URL/Cisco-MRTG/index.html, in a web browser. however, this is still a static webpage. By default it will automatically refresh every 5 minutes, you can check it in advanced configuration part.

To define it type “crontab -e” command and paste this setting:

*/5 * * * * env LANG=C /usr/local/mrtg-2/bin/mrtg /etc/mrtg/Cisco-MRTG.cfg
Advanced Configure

You may have notice some interface you may not want to monitor, or you don’t want the web page to display the IP address. You can custmize the Cisco-MRTG.cfg file in /var/www/html/Cisco-MRTG.according to your need by command vi /etc/mrtg/Cisco-MRTG.cfg


### Interface 2 >> Descr: 'GigabitEthernet0/0' | Name: 'Gi0/0' | Ip: '' | Eth: '00-2c-c8-76-ff-c0' ###

Target[]: [email protected]:
SetEnv[]: MRTG_INT_IP="" MRTG_INT_DESCR="GigabitEthernet0/0"
MaxBytes[]: 1000000000
Title[]: Traffic Analysis for 2 --
PageTop[]: <h1>Traffic Analysis for 2 --</h1>
                <div id="sysdetails">
                                        <td> in </td>
                                        <td>GigabitEthernet0/0 PrimaryWANDesc_ </td>
                                        <td>ethernetCsmacd (6)</td>
                                        <td>Max Speed:</td>
                                        <td>1.0 GBytes/s</td>                    // You can check if the speed here shows the correct speed.If not, change the string between td tag.
                                        <td>xx.xx.xx.20</td>                     // You may don't want to show the IP address if its a public one.

If you don’t want to show a interface, you can add # before all the lines regarding to that interface.

Now let’s poke around the work folder and see more configuration.

ls -l /var/www/html/Cisco-MRTG

There are a couple of files like ,,,,,,,,,, The log file stores the information, which can be used for nagios or other monitoring software, and the gif files are the graph generated from the log file. To understand which files are which interface. you can check the index.html file:

vi /var/www/html/Cisco-MRTG/index.html

    <META HTTP-EQUIV="Refresh" CONTENT="300" >                              // so the interval in crontab should mirror this interval here.
    <META HTTP-EQUIV="Cache-Control" content="no-cache" >
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache" >
    <META HTTP-EQUIV="Expires" CONTENT="Mon, 08 May 2017 06:11:33 GMT" >
    <LINK HREF="favicon.ico" rel="shortcut icon" >

Open a new browser. type http://URL/Cisco-MRTG/index.html

Open a subpage.


From the description and index number 2. we can know this report mapping to If you want to create a report for this interface in Nagios, you can use the, we will talk about it in next part.


Integrate the MRTG with NAGIOS

If you’re monitoring bandwidth usage on your switches or routers using MRTG, you can have Nagios alert you when traffic rates exceed thresholds you specify. The check_mrtgtraf plugin (which is included in the Nagios plugins distribution) allows you to do this.

You’ll need to let the check_mrtgtraf plugin know what log file the MRTG data is being stored in, along with thresholds, etc. In my example, I’m monitoring one of the ports on a Cisco router. The MRTG log file is stored in /var/www/html/Cisco-MRTG/ Here’s the service definition I use to monitor the bandwidth data that’s stored in the log file.

vi /usr/local/nagios/etc/objects/switch.cfg

define service {
    use                 generic-service ; Inherit values from a template
    host_name           Cisco_router
    service_description WAN Port Bandwidth Usage
    check_command       check_local_mrtgtraf!/var/www/html/Cisco-MRTG/!AVG!1000000,2000000!5000000,5000000!10

To understand this command, in /usr/local/nagios/etc/objects/commands.cfg

define command{
        command_name    check_local_mrtgtraf
        command_line    $USER1$/check_mrtgtraf -F $ARG1$ -a $ARG2$ -w $ARG3$ -c $ARG4$ -e $ARG5$
 -F, --filename=STRING
    File to read log from
 -a, --aggregation=(AVG|MAX)
    Test average or maximum
 -w, --warning
    Warning threshold pair <incoming>,<outgoing>  (in Bytes/sec)
 -c, --critical
    Critical threshold pair <incoming>,<outgoing> (in Bytes/sec)
 -e, --expires=INTEGER 
    Minutes after which log expires

Check the Link status is UP or Down:

define service{
 use generic-service ; Inherit values from a template
 host_name TL-SG3424
 service_description Port 2 Link Status
 check_command check_snmp!-C g1c05m05 -o ifOperStatus.2 -r 1 -m RFC1213-MIB
Switch port number:


The switchport labeled port 1 on your Cisco box is probably not index 1. If  you wanted the status of GigabitEthernet0/1 you would want the status of the interface at index 10101, so your service would read like this actually:

define service{
use generic-service
host_name switchcisco
service_description Port 1 Status
check_command check_snmp!ifOperStatus.10101
UPtime showing time ticks

First of all double-checked if we are using the right OID number, go to the SNMP Object Navigator, search sysUpTime

Then realised the unit is in time ticks instead of days, hours.

Regarding to this post:

Plugin 2.0.2. works good, showing timeticks and human readable uptime. 2.2.0. shows only timeticks.

Then redownload the 2.02 plugin from, compile it, issue resolved.

MRTG does not work once the terminal is turned off

In Ubuntu, if you configured your personal profile encryption, after logging out, the service may not be able to read your configuration files in your home folder,it’s better to put the configuration file in another directory.

you need sudo permission to create the files in /var/www/Cisco-MRTG, and if you add following line in the sudo crontab -e , it won’t run because, by default it will ask for password.

*/5 * * * * env LANG=C /usr/local/mrtg-2/bin/mrtg /var/www/html/Cisco-MRTG/Cisco-MRTG.cfg

The work around is create a script file in /etc/mrtg, with following content:

env LANG=C /usr/local/mrtg-2/bin/mrtg /var/www/html/Cisco-MRTG/Cisco-MRTG.cfg

Then use sudo crontab -e command, and add following line:

*/5 * * * * sudo sh /etc/mrtg/

Then we bypass the password for this script.

sudo visudo

Added the following line to the end of the file:

administrator ALL=NOPASSWD: /home/administrator/


Monitoring multiple devices

/usr/local/mrtg-2/bin/cfgmaker –global ‘WorkDir: /var/www/mrtg’ –output /tmp/server1.cfg  [email protected]

/usr/local/mrtg-2/bin/cfgmaker –global ‘WorkDir: /var/www/mrtg’ –output /tmp/server2.cfg  [email protected]

/usr/local/mrtg-2/bin/cfgmaker –global ‘WorkDir: /var/www/mrtg’ –output /tmp/router1.cfg  [email protected]

You may wanna manually edit or review file before updating the main mrtg.cfg file. Use vi text editor to view all files:
vi /tmp/server{1,2}.cfg
Next, ipdate main config file by updating all /tmp/*.cfg config file:

cat /tmp/server{1,2}.cfg /tmp/router1.cfg >> /etc/mrtg/mrtg.cfg

Just create graphs for all devices:

LANG=C LC_ALL=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg


MRTG configuration reference: