How To Install MRTG Without SNMP On FreeBSD

A step by step guide on how to install MRTG Without SNMP on FreeBSD

A lot of people like to have a graphical overview of what’s going on my self included. But for many Snmp is simply to complicated and to geeky to install, configure and get running.

That’s why I put up this guide. Once you are done you will be able to show of Mrtg graph’s like the one shown below.

The graph shown below is a “realtime” graph as you can see from the time stamp on the image. It’s from one of my less loaded servers and updates every 5 minutes. You can plot nearly all the stuff you want with Mrtg.

This guide sets up Mrtg along with Qmailmrtg so you can begin to show some nice graphs. At the end of this guide I’ve added some info on how to plot other things like system load and so forth.

You can easily adjust this guide to suit your own special needs if you have some. And tune it to plot everything you need from ram consumption, network usage, cpu fan speed and more.

Right we will need a few things for this to work.

Mrtg by Tobi Oetiker
Qmailmrtg by Inter7
Libstatgrab by i-scream

As root

cd /usr/ports/net-mgmt/mrtg
make install clean

When the options screen pops up don’t check anything. The default works fine.

As root

cd /usr/ports/mail/qmailmrtg7
make install clean

As root

cd /usr/ports/devel/libstatgrab
make install clean

I have created some scripts, a configuration file and some working directories that Mrtg will use. All of this assumes that Mrtg stores it’s data in /usr/local/www/mrtg if you want this to be in another directory you will need to modify the configuration file as well as the command to run mrtg and the settings in httpd.conf.

This is by far easier to do once you get familiar with how Mrtg works. I strongly suggest you stick with this guide until you get a hang of it.

We will store the Mrtg configuration file all data, logs, images and working directories in /usr/local/www/mrtg the configuration file I created is set up for this directory

As root

cd /usr/local/www

Now I got a few scripts that launches from within the Mrtg configuration file and plots a few extra mail related graphs. These are as follows. Actually I have a lot more but the others are more or less server and hardware specific.

Clam Anti virus
Invalid RCPT TO
Valid RCPT TO
Sender listed in Spamhaus
Jgrey Listing
Spam Current
Spam Accumulated

These scripts will store the values needed for Mrtg in /tmp if you don’t have this directory you can either create it or modify the scripts to use another directory.

Now you don’t have to use the scripts supplied here you can write your own scripts to handle this. But these scripts work fine.

Now that this is sorted out let’s grab the scripts the configuration file and some default directories where Mrtg will store data.

As root

fetch http://www.xfiles.dk/files/mrtg/mrtgfiles.tar.gz
tar -zxvf mrtgfiles.tar.gz
rm mrtgfiles.tar.gz

We need to set the right permissions on the scripts

As root

cd /usr/local/www/mrtg/mrtg-modules
chmod 755 *

If you look at the configuration file in /usr/local/www/mrtg/mrtg-config you’ll find out that most of what’s going on is a lot of grep, sed and awk stuff and the scripts being executed that you just downloaded. Once you get a hang of it you will quickly find yourself adding more stuff to be plotted by mrtg.

Before we start this up we need to supply mrtg with some initial values for the scripts we just downloaded. Mrtg will produce some errors if we don’t. I supplied a script you can run in the mrtg-modules directory. So let’s do that.

As root

sh /usr/local/www/mrtg/mrtg-modules/clearstat.sh

To get things running you will need to issue the following command.

As root

/usr/local/bin/mrtg /usr/local/www/mrtg/mrtg-config/mrtg.cfg

The first time you run it you will get a warning from Rateup it will look something like below. It tells you that Rateup can’t read the logfiles that it needs and is unable to read the backup logfiles as well and also is unable to remove the backup logfiles to be used as logfiles instead. Now this is normal behaviour since there is no data to work with in the /usr/local/www/mrtg directory.

Output of first run

Rateup WARNING: /usr/local/bin/rateup could not read the primary log file for msg
Rateup WARNING: /usr/local/bin/rateup The backup log file for msg was invalid as well
Rateup WARNING: /usr/local/bin/rateup Can’t remove msg.old updating log file
Rateup WARNING: /usr/local/bin/rateup Can’t rename msg.log to msg.old updating log file
Rateup WARNING: /usr/local/bin/rateup could not read the primary log file for queue-size
Rateup WARNING: /usr/local/bin/rateup The backup log file for queue-size was invalid as well
Rateup WARNING: /usr/local/bin/rateup Can’t remove queue-size.old updating log file
Rateup WARNING: /usr/local/bin/rateup Can’t rename queue-size.log to queue-size.old updating log file
Rateup WARNING: /usr/local/bin/rateup could not read the primary log file for concurrency
Rateup WARNING: /usr/local/bin/rateup The backup log file for concurrency was invalid as well
Rateup WARNING: /usr/local/bin/rateup Can’t remove concurrency.old updating log file
Rateup WARNING: /usr/local/bin/rateup Can’t rename concurrency.log to concurrency.old updating log file

Run the command below the warnings disappear.

As root

/usr/local/bin/mrtg /usr/local/www/mrtg/mrtg-config/mrtg.cfg

I would suggest putting the above command in cron and run it every 5 minutes like this.

Add this line to cron

*/5 * * * * /usr/local/bin/mrtg /usr/local/www/mrtg/mrtg-config/mrtg.cfg >/dev/null 2>&1

Note: it will take some time before Mrtg will start to show any graphs. This can be anything from 10 to 20 minutes

Next we need access from apache so put the following in your httpd.conf file.

As root

Alias /mrtg "/usr/local/www/mrtg/"
<Directory "/usr/local/www/mrtg">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

You are now able to access Mrtg through your browser using www.domainname.com/mrtg

Now the smart reader will have noticed that we did install “libstatgrab” but haven’t used it. Fair enough and absolutely true.

Libstatgrab is a great tool that can create a very comprehensive list of your hardware and generate a configuration file you can use with Mrtg.

Let’s say you wanted to plot the use of inodes, disk usage and all other kinda stuff. Well then you would use libstatgrab to generate such a configuration file.

Now I don’t plot everything thats possible to plot. But I use libstatgrab to generate a configuration file and then just cut out the pieces I want to plot and put them in my Mrtg configuration file.

To invoke libstagrab and make it generate a list of what it can figure out about your system and plot with Mrtg issue the following command.

As root

cd ~
statgrab-make-mrtg-config --no-header > moremrtg

This will generate a configuration file in your root directory called “moremrtg”. You can use portions or all of this file within your Mrtg configuration file.

The file generated by libstatgrab will look something like below (only a few lines are shown here since the file being created is quite long actually.

This shows the configuration for my hardware on one of my servers and is useless to you unless you have the exact same hardware configuration on your system. What you would do now was to cut out the pieces you want to plot and copy it into you Mrtg configuration file.

As root

less moremrtg

Example output

Title[cpu.idle]: CPU idle
PageTop[cpu.idle]: CPU idle
MaxBytes[cpu.idle]: 100
YLegend[cpu.idle]: Idle
ShortLegend[cpu.idle]: %
LegendI[cpu.idle]: idle
Options[cpu.idle]: noo gauge
Target[cpu.idle]: `statgrab -o -p -m cpu.idle const.0`

Title[fs.ad0s1a.used]: Filesystem / space usage
PageTop[fs.ad0s1a.used]: Filesystem / space usage
MaxBytes[fs.ad0s1a.used]: 10397857792
YLegend[fs.ad0s1a.used]: Space used
ShortLegend[fs.ad0s1a.used]: KiB
LegendI[fs.ad0s1a.used]: used
kMG[fs.ad0s1a.used]: Ki,Mi,Gi,Ti
Options[fs.ad0s1a.used]: noo gauge
Target[fs.ad0s1a.used]: `statgrab -K -m fs.ad0s1a.used const.0`

Title[load.min5]: Load average over 5 minutes
PageTop[load.min5]: Load average over 5 minutes
MaxBytes[load.min5]: 100
YLegend[load.min5]: Load average
ShortLegend[load.min5]: running * 1000
LegendI[load.min5]: load
Options[load.min5]: noo gauge
Target[load.min5]: `statgrab -f 1000 -m load.min5 const.0`

Let’s say you want to plot Load average usage you would copy the “load average” section of the “moremrtg” file generated and paste it into your Mrtg configuration file simple as that. You need to copy from Title to Target like the example below.

Example output

Title[load.min5]: Load average over 5 minutes
PageTop[load.min5]: Load average over 5 minutes
MaxBytes[load.min5]: 100
YLegend[load.min5]: Load average
ShortLegend[load.min5]: running * 1000
LegendI[load.min5]: load
Options[load.min5]: noo gauge
Target[load.min5]: `statgrab -f 1000 -m load.min5 const.0`

When Mrtg runs again you will get the RateUP warning again until Mrtg has run a few times.

The plotting for the “load average” you added will first show after 10 to 20 minutes.

Now as you can see from the “moremrtg” file we created theres a lot of things you can plot and if it’s not there simply write a short script for your Mrtg configuration file to process like the few I supplied you with in this guide.

How To Install Apache 2.2 With Php5 On FreeBSD  How To Install Apache 1.3 With Php5 On FreeBSD

Official Qmail Mrtg Site  Official Libstatgrab Site  Official Mrtg Configuration Reference