Page 1 of 1

[HOWTO] Use RRDTool with MRTG

Posted: 14 Sep 2014 21:35
by Snunn1
[HOWTO] Use RRDTool with MRTG

Tested on:
NAS4Free 9.2.0.1 - Shigawire (rev 943 & 972)
Platform x64-Embedded on 1GB USB Flash Stick on HP Microserver N40L (AMD64)

Continuing on from my previous guide, [HOWTO] Install MRTG SNMP Monitoring Inside Jail, this guide will focus on using MRTG with RRDTool. For this, we will be using the mrtg-rrd.cgi file included in this guide which will be accessible through Apache. This mrtg-rrd.cgi is one I have modified slightly to use the default MRTG configuration file and to provide nicer graphs. The original version can be found at the MRTG-RRD homepage HERE, please click HERE for example graphs using the original version.

This guide assumes you have a system with a jail called 'mrtg' with an IP address of 192.168.1.251, fully working Internet access, SSH and a file editor (Nano). This jail will also have a working installation of MRTG and Apache 2.x as per my previous guide. Please note that this will be using Apache 2.4 which has a few config differences in comparison to Apache 2.2.

Before I begin, the sources I give credit to are the following:

Tobi Oetiker's MRTG Homepage
Jan "Yenya" Kasprzak's MRTG-RRD Homepage

First, we need to enter the jail:

Code: Select all

nas4free: ~ # jexec 1 csh
root@mrtg:/ #
Next, if you're using the FreeBSD Ports Collection, you'll want to make sure it's up to date:

Code: Select all

portsnap fetch update
Then, we need to install the RRDTool port:

Code: Select all

cd /usr/ports/databases/rrdtool/
make install clean
If you're using packages as opposed to ports, use this:

Code: Select all

pkg install databases/rrdtool
This will give MRTG the means to use RRDTool and will convert its logfiles to RRD database files.

We now need to edit the MRTG config to tell it to use RRDTool:

Code: Select all

nano /usr/local/etc/mrtg/mrtg.cfg
To do this, add the following to the existing 'Global Defaults' section:

Code: Select all

LogFormat: rrdtool
WorkDir: /usr/local/www/apache24/data/mrtg
PathAdd: /usr/local/bin/
LibAdd: /usr/local/bin/
IconDir: /mrtg
RunAsDaemon: Yes
Once complete, we need to stop any existing MRTG processes and restart so it picks up the config changes:

Code: Select all

env LANG=C /usr/local/bin/mrtg --logging /var/log/mrtg.log --user mrtg --group mrtg --daemon /usr/local/etc/mrtg/mrtg.cfg
This should be the response you see:

Code: Select all

2014-09-01 21:48:16 -- Started mrtg with config '/usr/local/etc/mrtg/mrtg.cfg'
Daemonizing MRTG ...
MRTG should now create and write to new RRD files, so check the timestamps to confirm this is happening:

Code: Select all

ls -ltr /usr/local/www/apache24/data/mrtg/*.rrd
Now, we need to change the ownership of the image and HTML files so Apache can write to them:

Code: Select all

chown www:www *.png; chown www:www *.html; chown mrtg:mrtg *.rrd
Next, we need to configure Apache to work with CGI files so we can use mrtg-rrd.cgi. For this, you'll need to edit Apache's main configuration file - httpd.conf:

Code: Select all

nano /usr/local/etc/apache24/httpd.conf
Search for the following line:

Code: Select all

#LoadModule cgi_module libexec/apache24/mod_cgi.so
And unhash it so it looks like this:

Code: Select all

LoadModule cgi_module libexec/apache24/mod_cgi.so
Next, make sure the following line is mentioned just before the <Directory> section so Apache knows where to look and how to work with CGI files:

Code: Select all

ScriptAlias /cgi-bin/ "/usr/local/www/apache24/cgi-bin/"
We now need to set tell Apache who can access it and what it's for so ensure this section is below the ScriptAlias line:

Code: Select all

<Directory "/usr/local/www/apache24/cgi-bin">
    AllowOverride None
    Options +ExecCGI
    Require all granted
</Directory>
If you're using Apache 2.2, you'll need this instead:

Code: Select all

<Directory "/usr/local/www/apache22/cgi-bin">
    AllowOverride None
    Options +ExecCGI
    Order allow,deny
    Allow from all
</Directory>
Once complete, you'll need to save and exit the file. Next step is to download, decompress and move the mrtg-rrd.cgi to the cgi-bin directory and set its permissions and ownership:
MRTG-RRD.tgz

Code: Select all

tar -zxvf mrtg-rrd.tgz
mv mrtg-rrd.cgi /usr/local/www/apache24/cgi-bin
chown www:www mrtg-rrd.cgi; chmod 755 mrtg-rrd.cgi
Please note that if your existing mrtg.cfg file is anywhere but the default, you'll need to edit the mrtg-rrd.cgi so it knows where to look.

The final step is to restart Apache so it picks up the config changes and uses mrtg-rrd.cgi:

Code: Select all

/usr/local/etc/rc.d/apache24 restart
By now, you should be able to enter http://192.168.1.251/cgi-bin/mrtg-rrd.cgi into your browser and the index page should show up with your new RRD-drawn graphs! Click on the graph to give you daily/weekly/monthly/yearly usage.
N4F - MRTG-RRD.jpg
If Apache displays an error such as 403 Forbidden, check the Apache error logs for clues, this may be due to permissions:

Code: Select all

less /var/log/httpd-error.log
If your graphs aren't updating, you'll need to check that the image files are owned by Apache:

Code: Select all

ls -ltr /usr/local/www/apache24/data/mrtg/*.img
And the RRD files are owned by MRTG:

Code: Select all

ls -ltr /usr/local/www/apache24/data/mrtg/*.rrd
And that's it! If it's up and working, you'll now have much prettier, slicker graphs! If you want to create your own RRD graphs with multiple lines and remove MRTG altogether, Haroon Rafique's RRD.CGI would be worth investigating. This will require some understanding of how RRDTool works so is worth doing if you're willing to learn.

I hope some of you find this guide useful, any corrections or recommendations are welcome.