MrtgTricks
Overview
Below is a collection of scripts to make MRTG graph different things. Some of this can be done over snmp, but sometimes, snmp can't be installed.
Attached to this document are some of the scripts used.
Run MRTG as Daemon
Add the below to the config file.
Interval: 10
NoDetach: No
RunAsDaemon: Yes
HDD Temps
Graphs the HDD Temp as long as the HDD is active and not in sleep mode. Gaps will appear when the drive is in sleep mode.
Example
mrtg.cfg
Target[ds-hda-temp]: `run-temp /dev/hda`
Options[ds-hda-temp]: gauge,integer,nopercent,growright
MaxBytes[ds-hda-temp]: 60
YLegend[ds-hda-temp]: Temp C
ShortLegend[ds-hda-temp]:
LegendI[ds-hda-temp]: Temp C:
LegendO[ds-hda-temp]:
Title[ds-hda-temp]: Analysis for hda Temp
PageTop[ds-hda-temp]: <H1>Analysis for hda Temp</H1>
Note: HDD do not generate stats when in standby mode.<BR>
script
run-temp
#!/bin/bash
HDD=$1
#HDD="/dev/hdc"
ACTIVE=`hdparm -C $HDD | grep active`
if [ ! -n "$ACTIVE" ]; then # -n tests to see if the argument is non empty
# HDD is not active. Don't do anything.
echo 0
echo 0
exit
fi
HDDTEMP=`/usr/sbin/smartctl -a $HDD |grep Temperature | /bin/awk '{print $10}'`
HDDPOWER=`smartctl -a $HDD | grep -m 1 "Extended off" | /bin/awk '{print $9}'`
HDDID=`smartctl -a $HDD | grep -m 1 "Model"`
echo $HDDTEMP
echo $HDDTEMP
echo $HDDPOWER
echo $HDDID
HDD Space Used/Free
Graphs the free and used space. Does not wake the drive out of sleep mode. Does not report anything if the drive is in sleep mode. MRTG assumes that if nothing is reported, then the data is the same as last time. So this generates a solid graph.
Example

Month Graph
mrtg.cfg
Note For the graph to have the correct scale, the
MaxBytes? should be set to the max size of the HDD as reported by
df and padded with a few zeros.
Target[ds-hda-free]: `run-free /dev/hda`
Options[ds-hda-free]: gauge,integer,nopercent,growright
MaxBytes[ds-hda-free]: 277737980000
YLegend[ds-hda-free]: Disk Space
ShortLegend[ds-hda-free]:
LegendI[ds-hda-free]: used:
LegendO[ds-hda-free]: free:
Title[ds-hda-free]: Analysis for hda free
PageTop[ds-hda-free]: <H1>Analysis for hda Free Space</H1>
Note: HDD do not generate stats when in standby mode.<BR>
script
#!/bin/bash
HDD=$1
#HDD="/dev/hdc"
ACTIVE=`hdparm -C $HDD | grep active`
if [ ! -n "$ACTIVE" ]; then # -n tests to see if the argument is non empty
# HDD is not active. Don't do anything. We assume nothing has changed.
exit
fi
HDDFREE=`df |grep $HDD | tail -n 1 | /bin/awk '{print $4}'`
HDDUSED=`df |grep $HDD | tail -n 1 | /bin/awk '{print $3}'`
HDDPOWER=`smartctl -a $HDD | grep -m 1 "Extended off" | /bin/awk '{print $9}'`
HDDID=`smartctl -a $HDD | grep -m 1 "Model"`
HDDTOTAL=`df -h |grep $HDD | tail -n 1| /bin/awk '{print $2}'`
echo $[$HDDUSED*1000]
echo $[$HDDFREE*1000]
echo $HDDPOWER
echo "$HDDID <br> Total Size [formatted]:$HDDTOTAL"
CPU Load
This graphs the system load. It does a poor job of parsing the uptime command and tends to mess up the 5 min avg unless the system has been up for over a day. But all my systems I track with this are, so it's not something worth the time to fix.
Example
mrtg.cfg
run-load script
Target[ds-load]: `run-load`
Options[ds-load]: gauge,integer,nopercent,growright
MaxBytes[ds-load]: 500
Unscaled[ds-load]: n
YLegend[ds-load]: System Load
ShortLegend[ds-load]:
LegendI[ds-load]: 1 min avg:
LegendO[ds-load]: 5 min avg:
Title[ds-load]: Analysis for System Load
PageTop[ds-load]: <H1>Analysis for System Load</H1>
script
#!/bin/bash
UPTIME=`uptime | /bin/awk -F , '{print $1}'`
ONEMIN=`uptime | /bin/awk '{print $10}' | /bin/awk -F, '{print $1}' `
FIVEMIN=`uptime | /bin/awk '{print $11}' | /bin/awk -F, '{print $1}' `
echo $ONEMIN*100|bc
echo $FIVEMIN*100|bc
echo $UPTIME
echo localhost
Multiple host ping graph
This set of scripts is designed to ping multiple systems and generate a graph showing the ping times. This script generates it's own mrtg.cfg files if none are found. This makes it easy to add and remove hosts to monitor. Unlike other mrtg scripts, this runs from a looping shell script. The script is set to run every 40 seconds. Due to the huge amount of data this script can generate over a few weeks of operation, it's not recommended to run this script forever. MRTG was designed to run every 10 minutes, not 40 seconds. This script should just be used to debug a connection.
- Get first host from list
- If mrtg.cfg is not found for this host, make one.
- Call MRTG with the mrtg.cfg file
- MRTG reads the config file, then calls a small ping script that pings the host
- MRTG generates the graph.
- Script loops and does next host
- Once every host is done, wait X amount of time, then do it again.
After every loop, the script checks for the existence of a blank file called "run". If this file does not exist, then the script quits. This makes it easy to kill off the script without knowing it's PID. Aka, crontab or just something like "sleep 300 && rm ./run" will end the script in 5 minutes.
To install, extract into a directory, change the SCRIPTPATH to the directory it's in, add the hosts to monitor, and change the path to MRTG. Then run "masterping".
Example
scripts
--
PorchLight - 03 Sep 2007