Installation and Configuration of MRTG

MRTG (Multi Router Traffic Grapher) is a tool to monitor network traffic and can be configured to monitor other server resources i.e CPU, Memory usage, etc. MRTG basically takes regular statical information from a host and generates HTML and PNG code to display the data. The MRTG home page can be found at http://people.ee.ethz.ch/~oetiker/webtools/mrtg. The main documentation pages can be found there and also detailed installation and configuration.

This document provides a quick installation and configuration of MRTG.

What is required
MRTG requires the following: - Library Compilation
This section details how to get and compile the above libraries. These may already be compiled on the system so skip this section.

First create a directory for the compilation.

  # mkdir $HOME/src
# cd $HOME/src

Next check that gcc, perl, make, ar and ld are in your $PATH. Use which to verify.
To compile zlib

  # wget ftp://sunsite.cnlab-switch.ch/mirror/infozip/zlib/zlib.tar.gz
# gunzip –c zlib.tar.gz | tar xf –
# rm zlib.tar.gz                    # remove to keep directory clean
# cd zlib-?.?.?                     # ?.?.? = version number
# ./configure
# make
# cd ..

To compile jpeg

  Obtain the latest version
# wget ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz
# gunzip –c jpegsrc.v6b.tar.gz | tar xf –
# rm jpegsrc.v6b.tar.gz
# cd jpeg-6b
# ./configure
# make
# cd ..

To compile libpng.

 

Obtain the latest version
# wget http://www.libpng.org/pub/png/src/libpng-1.0.11.tar.gz
# gunzip –c libpng-1.0.11.tar.gz | tar xf –
# rm libpng-1.0.11.tar.gz
# cd libpng-1.0.11
# make –f scripts/makefile.std CC=gcc ZLIBLIB=../zlib-?.?.? ZLIBINC=../zlib-?.?.?
# rm *.so.* *.so
# cd ..


To compile gd.

  # wget http://www.boutell.com/gd/http/gd-1.8.3.tar.gz                  # Get latest version
# gunzip –c gd-1.8.3.tar.gz | tar xf –
# rm gd-1.8.3.tar.gz
# cd gd-1.8.3
# make INCLUDEDIRS=”-I. –I../zlib-?.?.? –I../libpng-?.?.? –I../jpeg-6b” \
LIBDIRS=”-L../zlib-?.?.? –L. –L../libpng-?.?.? –L../jpeg-6b” \
LIBS=”-lgd –lpng –lz –lm”
# cd ..

Now to compile and install MRTG

  Obtain the latest version
# gunzip –c mrtg-2.9.14.tar.gz | tar xf -                                                               
# rm mrtg-2.9.14.tar.gz
# cd mrtg-2.9.14
# ./configure –prefix=/usr/local/mrtg-2.9.14 \
--with-gd=$HOME/src/gd-1.8.3 \
--with-z=$HOME/src/zlib-1.1.3 \
--with-png=$HOME/src/png-1.0.11

# make
Now su to root
# make install

Configuring MRTG
The next step is to now configure MRTG for monitoring a network device. The below script can be run against each server to create a cfg file for MRTG to use.

Create the user mrtg and the group mrtg then create the cfg directory to hold the configuration scripts and web pages.

  #mkdir /home/mrtg/cfg
#mkdir /home/mrtg/html

#!/bin/sh
PATH=$PATH:/usr/local/mrtg-2.9.14/bin; export PATH

cfgmaker --global “WorkDir: /home/mrtg/html/” \
--global “Options[ _ ]: growright, bits” \
--output “/home/mrtg/cfg/<hostname>-mrtg.cfg public@<hostname>.tornadogroup.com”

MRTG will create a cfg file for each server and place it into the /home/mrtg/cfg.
If the cfg file creatation fails, there can be two possible causes. Testing cfg scripts
To test that statical information can be collected from a server, run mrtg with the cfg script.
#/usr/local/mrtg-2.9.14/bin/mrtg /home/mrtg/cfg/<hostname>.cfg

Please note that the first couple of times errors regarding creating/moving are displayed this is normal and will disappear. If they don’t then you have a configuration problem.

If the mrtg run successfully then an html file will be created displaying probably a blank chart until some stats have been collected. Point a web browser at http://<your website>/mrtg/<hostname>.html, if no html page exists make sure that the cfg file points to the right location for the output.

Automating MRTG

Once all the cfg files have been created it’s best to automate the whole process via cron. Create a script like below to call from cron and collect the stats.

/usr/local/mrtg-2.9.14/bin/mrtg /home/mrtg/cfg/<hostname>.cfg
/usr/local/mrtg-2.9.14/bin/mrtg /home/mrtg/cfg/<hostname>.cfg
/usr/local/mrtg-2.9.14/bin/mrtg /home/mrtg/cfg/<hostname>.cfg
etc…………..


Now add to cron something like below:
5,10,15,20,25,30,35,40,45,50,55 /home/mrtg/cfg/<script above>

Check the web page and see if the network stats are being collected (should see blue and green lines in chart).

Creating an Index web page

To create a complete picture of all your charts on one web page we use indexmaker. Create a script and run any time you add a new host.

  PATH=$PATH:/usr/local/mrtg-2.9.14/bin;export PATH
indexmaker /home/mrtg/cfg/*.cfg > /home/mrtg/html/daily.html
indexmaker –show=week /home/mrtg/cfg/*.cfg > /home/mrtg/html/weekly.html
indexmaker –show=month /home/mrtg/cfg/*.cfg > /home/mrtg/html/monthly.html
indexmaker –show=year /home/mrtg/cfg/*.cfg > /home/mrtg/html/yearly.html

The html files should have been created in /home/mrtg/html.

Lastly create a link to the mrtg html files: -
# ln –s /home/mrtg/html /usr/local/apache-?.?.?/htdocs/mrtg      # if this is were htdocs resides