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 the owner of this directory is 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 [email protected] > /etc/mrtg.cfg
--base: Get Device Info on [email protected]:
--base: Vendor Id: Unknown Vendor -
--base: Populating confcache
--base: Get Interface Info
--base: Walking ifIndex
--snpd: [email protected]: -> 1 -> ifIndex = 1
--snpd: [email protected]: -> 2 -> ifIndex = 2
--snpd: [email protected]: -> 3 -> ifIndex = 3
--base: Walking ifType
--snpd: [email protected]: -> 1 -> ifType = 24
--snpd: [email protected]: -> 2 -> ifType = 6
--snpd: [email protected]: -> 3 -> ifType = 6
--base: Walking ifAdminStatus
--snpd: [email protected]: -> 1 -> ifAdminStatus = 1
--snpd: [email protected]: -> 2 -> ifAdminStatus = 1
--snpd: [email protected]: -> 3 -> ifAdminStatus = 1
--base: Walking ifOperStatus
--snpd: [email protected]: -> 1 -> ifOperStatus = 1
--snpd: [email protected]: -> 2 -> ifOperStatus = 1
--snpd: [email protected]: -> 3 -> ifOperStatus = 1
--base: Walking ifMtu
--snpd: [email protected]: -> 1 -> ifMtu = 65536
--snpd: [email protected]: -> 2 -> ifMtu = 1500
--snpd: [email protected]: -> 3 -> ifMtu = 1500
--base: Walking ifSpeed
--snpd: [email protected]: -> 1 -> ifSpeed = 10000000
--snpd: [email protected]: -> 2 -> ifSpeed = 0
--snpd: [email protected]: -> 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.

Josphat Mutai 2:38 am

About Josphat Mutai

Red Hat Certified Engineer with more than 2 years experience managing critical Cloud Infrastructures and Services. Passionate about Technology and loves testing and reviewing gadgets and Open-source tools.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.