Getting MRTG to show disk space

For a long time, I have been trying to get MRTG to show how much space there was left/used on a harddrive in my server. I have done this before, but that was on another server, running Windows 2003 server. Now I have changed to a HP Microserver N54L, and installed Debian on it.

After a lot time used on Google, a lot of failed attempts to configure SNMP to return info for the harddrives I want to monitor, and after breaking and fixing it all multiple times, I gave up… Until I had an idea…

I already got a script to extract a value from csv format in a file, and I am not sure why I haven’t got this idea before, but with a few modifications, that script could run a command to get info about the disk, calculate that into a percentage, and return that to MRTG.

And here it is, it is pretty simple. All it does is to run “df” and return the line defined by “$ARGV”, then take the total and used values, and calculate that into the percentage used on the drive.

I saved this in “/etc/mrtg/” with the filename “script_hdd.sh”, make sure to allow it to be executed.

#!/bin/bash

ARGV=("$@")

if [ -z "$ARGV" ]; then
	echo "Target not defined";
	exit;
fi

hddinfo=`df | grep $ARGV`

hddtotal=`echo $hddinfo |awk '{print $2}'`
hddused=`echo $hddinfo |awk '{print $3}'`

echo 0;
awk "BEGIN {print ($hddused/$hddtotal)*10000}";
echo 0;
echo 0;

If it all works as expected, it should return something like,

0
5656.8
0
0

And as you probably have noticed, it is way over the maximal 100% that should be possible, but we get to that in a second.

In the mrtg configuration, we define it like this, to make it execute the script and use the values from it.

As you see, after “/etc/mrtg/script_hdd.sh”, I have put “/dev/sdb1” in, this is the disk MRTG is going to keep track of.

To find out what you need to put in, run the “df” command, and look at the first part of the lines that is returned.

Remember that 5656.8% we got earlier? We are going to get that scaled down, so it fits within the 0-100% range, and we are doing that by setting YTicsFactor and Factor to 0.01. This will divide the returned values, so instead of 5656.8, it will be used as 56.568%, the reason for doing it like this, is to get a better resolution on the cart. If we just multiplied by 100 in the script to get the percentage, MRTG would only have 100 steps to show on the chart, which would make it less precise and also look bad.

Target[disk_sdb1]: `/etc/mrtg/script_hdd.sh /dev/sdb1`
Options[disk_sdb1]: growright, gauge
MaxBytes[disk_sdb1]: 10000
ShortLegend[disk_sdb1]: %
YLegend[disk_sdb1]: Disk used %
Title[disk_sdb1]: Disk used
PageTop[disk_sdb1]: <H1>Disk used</H1>
Unscaled[disk_sdb1]: ymwd
TimeStrPos[disk_sdb1]: RU
YTicsFactor[disk_sdb1]: 0.01
Factor[disk_sdb1]: 0.01

If everything is done right, it should return something like this.

hp1_disk_sdb1-day

Leave a Reply