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/20.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 install apache2
Install snmp packages for Ubuntu:
$ sudo apt install snmpd snmp
To configure SNMP, I like enabling full access from localhost:
$ sudo vim /etc/snmp/snmpd.conf
Add the following snmp community string to snmpd.conf file:
rocommunity public localhost

You need to restart snmpd service for the change to be effected.
$ sudo systemctl restart snmpd
By default, SNMP listens to localhost. You can verify what port or IP to which SNMP is listening using the ss command.
Install MRTG on Ubuntu 18.04/20.04
All prereqs are okay, so we can install MRTG on Ubuntu by running the command:
$ sudo apt 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
$ sudo 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.
$ sudo chown -R www-data:www-data /var/www/mrtg
Then edit /etc/mrtg.conf to set Working directory:
$ sudo cat /etc/mrtg.cfg | grep -v "^#"
Output:
WorkDir: /var/www/mrtg
WriteExpires: Yes
Title[^]: Traffic Analysis for
Rebuild MRTG configuration from modified file:
$ sudo cfgmaker public@localhost > /etc/mrtg.cfg
Output:
--base: Get Device Info on public@localhost:
--base: Vendor Id: Unknown Vendor - 1.3.6.1.4.1.8072.3.2.10
--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:
$ sudo indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
The last thing to do is create a VirtualHost file for the site.
$ sudo 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
</Directory>
Enable the site and reload apache service.
$ sudo sudo a2ensite mrtg
Reload Apache service:
$ sudo systemctl reload apache2
Accessing MRTG UI
Head over to http://ip-address/mrgt or http://servername/mrtg to 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.
Conclusion
In this tutorial, we learned how to install and configure MRTG on Ubuntu 18.04/20.04.
Thanks for reading, please provide your feedback and suggestion in the comment section.
hi
my indexmaker output is
ERROR: did not find any matching data in cfg file
how i can solve this error?
what you have in snmpd.conf file and configured correctly?
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?
Hi Gregory,
Run the following command to see any errors.
$ sudo mrtg //mrtg.cfg
$ sudo cfgmaker public@localhost > /etc/mrtg.cfg
-bash: /etc/mrtg.cfg: Permission denied
Resolved by following command:
$ sudo bash -c 'cfgmaker public@localhost > /etc/mrtg.cfg'