Skip to content

Multi Router Traffic Grapher

MRTG - an age-old graphing tool used by network administrators all over the world. But it's not just for networking systems. I've used it to monitor various things. In this how-to, I'll walk you through how you can create a simple MRTG system to track pretty much anything.

Monitoring internet uptime

In this example, I will be using MRTG to monitor the availability of my internet link. I will do this by pinging an address on the internet and recording the number of pings that were able to be received in a 60-second period. The idea is that I should be getting 60 pings a second (one every second).

mrtg-internet.sh

Start by creating a script that will do the checking. You'll notice the script is returning 4 values. The first two will be plotted on the graph. The 3rd is the system uptime, and the 4th is the hostname. This is a requirement for MRTG to consume the data.

#!/bin/sh
HOST=8.8.8.8
COUNT=60
DTE=$(date '+%Y-%m-%d %H:%M:%S')
VAR=$(ping $HOST -c $COUNT | grep "packets transmitted" | awk {'print $4'})
echo $VAR
echo $COUNT
uptime -s
echo $HOST

mrtg.conf

Next, we have to tell MRTG to use the script. Create the config file.

WorkDir: /var/www/html/mrtg/

Options[_]: gauge,nopercent,growright
YLegend[_]: Ping Count:
Title[_]: Total Ping Count
MaxBytes[_]: 60

Target[internet]: `sh mrtg-internet.sh`
PageTop[internet]: <h1>Total Ping count internet</h1>

indexmaker

indexmaker will read the config file, and create an HTML page for you.

indexmaker mrtg.conf --output /var/www/html/mrtg/index.html

mrtg.sh

I like to wrap the execution in another script. This makes scheduling the task in cron a bit easier.

#!/bin/sh
env LANG=c /usr/bin/mrtg /root/mrtg.conf

crontab -e

We scheduled the job to run through a crontab.

* * * * * /root/mrtg.sh > /tmp/mrtg.log 2>&1

View it

If everything worked correctly, you should be able to view your newly created MRTG files on your web server. Navigate to the /mrtg path of your web server to view the page.

Don't be surprised if the page does not update straight away. Provided the cron job is executed, you'll see an empty graph. It will take a bit of time before the graph will show any data.