How to Install and Configure MRTG on Ubuntu 18.04

MRTG (Multi Router Traffic Grapher) is an open source tool written in Perl used for monitoring and measuring the traffic load on network links and presenting data in graphical form. This tool helps you trace and troubleshoot network anomalies like Network traffic spikes at odd hours.

MRTG works with all network devices that support SNMP protocol.  It monitors SNMP network devices and draws pretty pictures showing how much traffic has passed through each interface. MRTG runs on Unix/Linux, Windows and Netware systems.

In this post, we're going to look at how to Install and Configure MRTG on Ubuntu 18.04.

MRTG Prerequisites

You need to have Apache installed before you can run MRTG on your machine. On Ubuntu 18.04, you can easily install Apache by pulling the package from the official repository.

$ sudo apt-get install apache2

Install snmp packages for Ubuntu:

$ sudo apt-get install snmpd snmp

Configure SNMP (Simple Network Management Protocol).

I like enabling full access from localhost.

$ sudo vim /etc/snmp/snmpd.conf

rocommunity public localhost

You need to restart snmpd service for the change to be effected.

$ sudo systemctl restart snmpd

Install MRTG on Ubuntu 18.04

All prereqs are okay, so we can install MRTG by running the command:

$  sudo apt-get install mrtg

Accept any popup prompt and click yes to start the installation.

Configuring MRTG

Once the installation process is complete, you need to configure it before you can start monitoring target devices. We'll set MRTG working directory to be /var/www/mrtg

# mkdir /var/www/mrtg

Make sure to change owner of this directory to www-data, which is the default user account for Apache web server.

# chown -R www-data:www-data /var/www/mrtg

Then edit /etc/mrtg.conf to set Working directory:

# cat /etc/mrtg.cfg | grep -v "^#"

WorkDir: /var/www/mrtg
WriteExpires: Yes
Title[^]: Traffic Analysis for

Rebuild MRTG configuration from modified file:

# cfgmaker public@localhost > /etc/mrtg.cfg
--base: Get Device Info on public@localhost:
--base: Vendor Id: Unknown Vendor -
--base: Populating confcache
--base: Get Interface Info
--base: Walking ifIndex
--snpd: public@localhost: -> 1 -> ifIndex = 1
--snpd: public@localhost: -> 2 -> ifIndex = 2
--snpd: public@localhost: -> 3 -> ifIndex = 3
--base: Walking ifType
--snpd: public@localhost: -> 1 -> ifType = 24
--snpd: public@localhost: -> 2 -> ifType = 6
--snpd: public@localhost: -> 3 -> ifType = 6
--base: Walking ifAdminStatus
--snpd: public@localhost: -> 1 -> ifAdminStatus = 1
--snpd: public@localhost: -> 2 -> ifAdminStatus = 1
--snpd: public@localhost: -> 3 -> ifAdminStatus = 1
--base: Walking ifOperStatus
--snpd: public@localhost: -> 1 -> ifOperStatus = 1
--snpd: public@localhost: -> 2 -> ifOperStatus = 1
--snpd: public@localhost: -> 3 -> ifOperStatus = 1
--base: Walking ifMtu
--snpd: public@localhost: -> 1 -> ifMtu = 65536
--snpd: public@localhost: -> 2 -> ifMtu = 1500
--snpd: public@localhost: -> 3 -> ifMtu = 1500
--base: Walking ifSpeed
--snpd: public@localhost: -> 1 -> ifSpeed = 10000000
--snpd: public@localhost: -> 2 -> ifSpeed = 0
--snpd: public@localhost: -> 3 -> ifSpeed = 0
# ls /var/www/mrtg/
index.html mrtg-l.png mrtg-m.png mrtg-r.png

Create index file for web server:

# indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html

The last thing to do is create a VirtualHost file for the site.

# vim /etc/apache2/sites-available/mrtg.conf

Add the following to this new file:

Alias /mrtg "/var/www/mrtg/"
​<Directory "/var/www/mrtg/">
 ​Options None
​ AllowOverride None
​ Require all granted

Enable the site and reload apache service.

# sudo a2ensite mrtg

Reload Apache service:

# systemctl reload apache2

Accessing MRTG UI

Head over to http://ip-address/mrgt or http://servername/mrtg tp access MRTG web interface. Interface monitoring should start showing traffic:

There is also default daily graph for an interface by default.

You can configure MRTG to monitor other things like RAM, CPU usage, Swap, Disk e.t.c.

Configure MRTG to poll metrics every X minutes.

I'm going to create a cron job which checks for changes on the configuration file and generate new png images that can be viewed on the web interface. Make sure to create the cron job as the root user.

# crontab -e
*/5 * * * * root LANG=C LC_ALL=C /usr/bin/mrtg /etc/mrtg.cfg

This will be executed every five minutes.

4 Comments... add one

  1. I think I have everything configured correctly. Getting no errors that I can see. Problem is when I go to the website by IP addr I get a completely blank screen.

    What am I doing wrong?


Leave a Comment