Install and configure MRTG on Raspbian

This article covers initial installation and configuration of MRTG on a Raspi running *bian Linux distribution.

Additional information on adding custom sensors and monitoring a FritzBox router will be published in separate tutorials.

Before we begin, let’s consider where this setup makes sense. If you have a relatively new Raspi with 2GB or more memory, think about installing more flexible network monitoring products such as Cacti, Munin, Nagios, etc. But if you have an ancient Raspi with only 256 or 512MB memory, you may consider a low-footprint, non-scalable installation like this one.

First off, update your system. Debian requires this to be executed as root, otherwise it errors out with inaccessible .lock file. Some people recommend enabling root, setting password and adding it to the SSH users. Much simpler way for me was to impersonate root while in your sudoer session like that:

# Update the operating system

sudo su

apt-get update & apt-get upgrade

After that, you will most probably need to reboot. Then, continue with installing SMTP:

# Install SNMP

sudo apt-get install snmp snmpd

# Initial SNMP configuration

sudo nano /etc/snmp/snmpd.conf

# Add the line ‘rocommunity public localhost’ before ‘rocommunity  public default -V systemonly’

# You can comment the two below lines if you like:

# rocommunity  public default -V systemonly

# rocommunity6 public default -V systemonly

# Restart SNMP

sudo systemctl restart snmpd

At this moment you can test if the SNMP client and server are ready. The two most important client commands are snmpget and snmpwalk. The below syntax should be obvious; the example should return ‘iso.3.6.1.2.1.25.1.7.0 = INTEGER: 0’:

snmpget -v 1 -c <Community-String> <IP-Address> <OID>

snmpget -v 1 -c public localhost 1.3.6.1.2.1.25.1.7.0

Before you install MRTG, keep in mind one important thing. Its monitoring interval is set to 5 minutes. This is not very current where modern network monitoring systems have a default of 60 seconds and go down to 1 second. The problem is that if you want to go below that (above is not a problem), the only official way is to implement RRDTool. According to MRTG online documentation, "Note that unless you are using rrdtool you can not set Interval to less than 5 minutes". OK, again, unofficially though, you can still decrease the cron job interval to 1 minute. If you inspect it with ‘cat /etc/cron.d/mrtg’, you will see that it is set to /5       root    if [ -x /usr/bin/mrtg ] (etc.)’. In fact, you may change the value to/1    and this will work as long as you have sensors of data type ‘gauge’. But do not enter ‘Interval: 1’ in the mrtg.cfg file. This will error out. And don’t use any sensors calculating or accumulating their values.

With that said, let’s jump into the MRTG installation.

# Install MRTG

sudo apt-get install mrtg

# Configure MRTG (mutiple steps)

# Make sure the WorkDir line is set to ‘WorkDir: /var/www/mrtg’

sudo cat /etc/mrtg.cfg

# When done, create the new directory and back-up the original .cfg file

sudo mkdir /var/www/mrtg

sudo cp /etc/mrtg.cfg /etc/mrtg.cfg.back

# Use cfgmaker to rebuild the configuration file using ‘public’ as community and ‘localhost’ as target.

sudo cfgmaker public@localhost –output=”/etc/mrtg.cfg”

# Finally, generate the HTML index file

sudo indexmaker –output=”/var/www/mrtg/index.html” /etc/mrtg.cfg

If you don’t receive any errors, the first part is completed. The cfgmaker has created a new configuration file with one sensor pointing to your eth0; and the indexmaker has created the index.html file pointing to the visual presentation of this sensor.

We need to serve these static HTML files with a web server. Contrary to another online articles, you do not need to install the whole LAMP package. MRTG doesn’t make use of PHP and MariaDB. All you need to install is Apache, or a lightweight web server like lighttpd or nginx. Here we stick to Apache only because the majority of the online posts are based on this server.

# Install apache2

sudo apt-get install apache2

# Check if apache2 is installed and you can access the default page at:

# http://<your-raspi-IP>/

# Configure apache2 (multiple steps)

sudo nano /etc/apache2/sites-available/mrtg.conf

# This will create a new file to which you need to add the below lines:

Alias “/mrtg” “/var/www/mrtg/”

   <Directory “/var/www/mrtg/”>

      Require all granted

   </Directory>

# Enable the new site

sudo a2ensite mrtg

# Restart apache2

sudo systemctl restart apache2

# Check if you can reach the new subsite at:

# http://<your-raspi-IP>/mrtg

With this, we have completed the second and final part of this tutorial. At this moment you should be presented with something similar to this visual output:

In the next tutorial "Adding custom MRTG Sensors" we will add local system sensors monitoring the CPU and Memory Loads, and a Ping sensor.

Reference:

How to install MRTG on Ubuntu

MRTG online documentation

https://oss.oetiker.ch/mrtg/doc/mrtg-reference.en.html

RRDTool online documentation

https://oss.oetiker.ch/rrdtool/

Monitoring The Raspberry Pi with MRTG:

https://www.satsignal.eu/raspberry-pi/monitoring.html