MRTG is an open source network traffic monitoring tool. It creates png images in the form of graphs that show the traffic load of network nodes. MRTG works on SNMP protocol to request from the nodes. So the devices monitored by MRTG must be SNMP enabled. Many managed switches and routers are SNMP enabled.
MRTG creates reports in png images. These images are embedded into the web pages. So it needs a web server running where it can put the reports generated.
MRTG is writen in perl. This perl script reads the traffic counters of the routers or switches. MRTG integrates a C program which logs the data and creates the png images.
MRTG creates 4 types of graphs:
1) Daily Graph (5 minute average)
2) Weekly Graph (30 Minute Average)
3) Monthly Graph (2 Hour Average)
4) Yearly Graph (1 Day Average)
For creating these graphs, MRTG needs data. This data does not grow over time because the logs are automatically consolidated. Still, it contains all the relevant data for last 2 years traffic. MRTG is not limited to traffic monitoring. It can query any SNMP data, like weather monitoring, CPU load monitoring, disk usage and many more.
MRTG requires some packages prior to its installation.
1) GCC - The GNU C Compiler
It is required for compiling other packages. Make sure it is installed on the system. Pre-compiled versions of GCC are available. For installing GCC on Debian/Ubuntu, run
$ apt-get install gcc
Large part of MRTG is written in perl. Like GCC, it must already be installed on most of the systems. But in case perl installation is needed, install perl with
$ apt-get install perl
The graphics drawing library for dynamically manipulating images.
required by gd in order to produce PNG graphics files.
Needed by libpng to compress the graphics files created.
Here, two methods for library installation are provided: from source and via apt-get. If apt-get does not install the required version, you might want to opt for manual compilation via source.
1) Install zlib
Compile and install from source with
$ wget http://www.zlib.net/zlib-1.2.3.tar.gz
$ gunzip -c zlib-*.tar.gz | tar xf -
$ rm zlib-*.tar.gz
$ mv zlib-* zlib
$ cd zlib
$ cd ..
or you can
$ apt-get install zlib1g zlib1g-devel
2) Install libpng
Compile and install from source with
$ wget ftp://ftp.simplesystems.org/pub/libpng/png/src/libpng-1.2.40.tar.gz
$ 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 ..
$ apt-get install libpng12-0 libpng12-dev
3) Install gd
Similarly, gd can be installed with
$ wget http://www.boutell.com/gd/http/gd-1.8.4.tar.gz
$ gunzip -c gd-*.tar.gz |tar xf -
$ rm gd-*.tar.gz
$ mv gd-* gd
$ cd gd
$ perl -i~ -p -e s/gd_jpeg.o//g Makefile
$ make INCLUDEDIRS="-I. -I../zlib -I../libpng" \
LIBDIRS="-L../zlib -L. -L../libpng" \
LIBS="-lgd -lpng -lz -lm" \
$ cd ..
For versions starting around 2.0.11,
$ wget http://www.boutell.com/gd/http/gd-2.0.33.tar.gz
$ gunzip -c gd-2.0.33.tar.gz |tar xf -
$ mv gd-2.0.33 gd
$ cd gd
$ env CPPFLAGS="-I../zlib -I../libpng" LDFLAGS="-L../zlib -L../libpng" \
./configure --disable-shared --without-freetype --without-jpeg
$ cp .libs/* .
or via apt-get,
$ apt-get install libgd2-xpm
MRTG requires gd.h header file. This can be found in the gd development package. Perform the following in case you get error while executing configure script for MRTG installation. For me, installation of libgd2-noxpm worked:
$ apt-get install libgd2-noxpm
This will remove prior libgd2-xpm installation)
4) Finally, install mrtg
$ cd /usr/local/src
$ gunzip -c mrtg-2.17.4.tar.gz | tar xvf -
$ cd mrtg-2.17.4
If the libraries have been pre-installed, just configure providing the installation path:
$ ./configure --prefix=/usr/local/mrtg-2
otherwise, you might have to provide with appropriate library paths:
$ ./configure --prefix=/usr/local/mrtg-2 \
Once configured, compile and install with:
$ make install
Web server installation
As you know by now that MRTG creates its output in the form of web pages. So it needs a running web server where it can put its output.
1) Install Apache2
$ apt-get install apache2
2) Start your web server
$ /etc/init.d/apache2 start
$ service apache2 start
The default DocumentRoot for apache is /var/www. You may want to create some directory where you want to put your mrtg files.
$ mkdir /var/www/mrtg
The SNMP protocol provides access control with communities. Different SNMP communities have different privileges. Most SNMP devices have default community named 'public'. You might want to check out the SNMP community for your switch/router.
The mrtg configuration tool cfgmaker creates configuration file for you by probing the switch/router for different ports. This checks out what ports are up and thus creates the corresponding entries in the configuration file. You can provide as parameter as many snmp devices as you want. Run cfgmaker to create mrtg configuration file:
$ cfgmaker --global 'WorkDir: /var/www/mrtg' \
--output /etc/mrtg/mrtg.cfg \
The WorkDir directory must be readable by your web server.
The indexmaker command creates index file (index.html) for mrtg web site. It reads the mrtg configuration and sends the output to STDOUT by default. You can provide the output file with --output option. For viewing mrtg output, you might need index file, otherwise the web server will just lit out the files in the directory.
$ indexmaker /etc/mrtg/mrtg.cfg --output=/var/www/mrtg/index.html
Now everything is set. all we need is run mrtg.
$ /usr/local/mrtg-2/bin/mrtg /etc/mrtg/mrtg.cfg
This will query the SNMP device and create the png images for the traffic on the device. But for long run, you would want to query the router in regular intervals. So we create a cron job for mrtg:
$ crontab -e
This will open a file in the editor specified by your EDITOR or VISUAL variables. Enter the following in the file
*/5 * * * * /usr/local/mrtg-2/bin/mrtg /etc/mrtg/mrtg.cfg --logging /var/log/mrtg.log
We run the command every 5 minutes because the minimum unit shown in mrtg graphs is 5 minute interval.