Using Rrdtool and NUT to generate graphics about UPS battery charge, runtime and ups load.
The purpose of this post is to show how to use Rrdtool and NUT (Network Ups Tools) command line tools to generate graphics for the last 72 hours (give or take) on battery charge, it's runtime and UPS load.
As you know, Rrdtool is based on a database model. The database is used to generate the graphic.
To create the rrdtool database:
Code:
# /usr/bin/rrdtool create /var/lib/rrd/ups.rrd -s 60 \
DS:upsBat:GAUGE:600:0:U \
DS:upsLoad:GAUGE:600:0:U \
DS:upsBatRun:GAUGE:600:0:U \
RRA:AVERAGE:0.5:1:4320 \
RRA:AVERAGE:0.5:1440:3 \
RRA:MIN:0.5:1440:3 \
RRA:MAX:0.5:1440:3
/usr/bin/rrdtool is the Rrdtool binary and
/var/lib/rrd/ups.rrd is the rrdtool database that will contain the values for specific attribute, for specific moments.
Now, the database needs updating every 60 seconds (-s 60) so crontab can easily be used.
Updating the rrdtool database:
Code:
# upsBat=`upsc apc | grep 'battery.charge:' | awk '{print $NF}'`
# upsLoad=`upsc apc | grep 'ups.load:' | awk '{print $NF}'`
# upsBatRun=`upsc apc | grep 'battery.runtime:' | awk '{print $NF}'`
# /usr/bin/rrdtool update /var/lib/rrd/ups.rrd N:$upsBat:$upsLoad:$upsBatRun;;
And now generating the graphic:
Code:
# /usr/bin/rrdtool graph $TRAF/ups.png \
--start "-3day" \
-c "BACK#000000" \
-c "SHADEA#000000" \
-c "SHADEB#000000" \
-c "FONT#DDDDDD" \
-c "CANVAS#202020" \
-c "GRID#666666" \
-c "MGRID#AAAAAA" \
-c "FRAME#202020" \
-c "ARROW#FFFFFF" \
-u 1.1 -l 0 -v "UPS" -w 1100 -h 250 -t "UPS - `/bin/date +%A", "%d" "%B" "%Y`" \
DEF:upsBatA=/var/lib/rrd/ups.rrd:upsBat:AVERAGE \
DEF:upsLoad=/var/lib/rrd/ups.rrd:upsLoad:AVERAGE \
DEF:upsBatRun=/var/lib/rrd/ups.rrd:upsBatRun:AVERAGE \
"CDEF:upsBat=upsBatA,10,*" \
AREA:upsBat\#FFFF00:"Battery %" \
GPRINT:upsBat:MIN:"Min\: %3.2lf " \
GPRINT:upsBat:MAX:"Max\: %3.2lf" \
GPRINT:upsBat:LAST:"Current\: %3.2lf\j" \
COMMENT:"\\n" \
AREA:upsBatRun\#00FF00:"Battery Runtime" \
GPRINT:upsBatRun:MIN:"Min\: %3.2lf " \
GPRINT:upsBatRun:MAX:"Max\: %3.2lf" \
GPRINT:upsBatRun:LAST:"Current\: %3.2lf\j" \
COMMENT:"\\n" \
AREA:upsLoad\#FF0000:"UPS Load" \
GPRINT:upsLoad:MIN:"Min\: %3.2lf " \
GPRINT:upsLoad:MAX:"Max\: %3.2lf" \
GPRINT:upsLoad:LAST:"Current\: %3.2lf\j"
This can easily be incorporated into a script which would be ran from crontab:
Code:
# cat ups.sh
#!/bin/bash
TRAF=/directory/to/contain/png/graphic/files
case $1 in (create)
/usr/bin/rrdtool create /var/lib/rrd/ups.rrd -s 60 \
DS:upsBat:GAUGE:600:0:U \
DS:upsLoad:GAUGE:600:0:U \
DS:upsBatRun:GAUGE:600:0:U \
RRA:AVERAGE:0.5:1:4320 \
RRA:AVERAGE:0.5:1440:3 \
RRA:MIN:0.5:1440:3 \
RRA:MAX:0.5:1440:3;;
(update)
upsBat=`upsc apc | grep 'battery.charge:' | awk '{print $NF}'`
upsLoad=`upsc apc | grep 'ups.load:' | awk '{print $NF}'`
upsBatRun=`upsc apc | grep 'battery.runtime:' | awk '{print $NF}'`
/usr/bin/rrdtool update /var/lib/rrd/ups.rrd N:$upsBat:$upsLoad:$upsBatRun;;
(graph)
/usr/bin/rrdtool graph $TRAF/ups.png \
--start "-3day" \
-c "BACK#000000" \
-c "SHADEA#000000" \
-c "SHADEB#000000" \
-c "FONT#DDDDDD" \
-c "CANVAS#202020" \
-c "GRID#666666" \
-c "MGRID#AAAAAA" \
-c "FRAME#202020" \
-c "ARROW#FFFFFF" \
-u 1.1 -l 0 -v "UPS" -w 1100 -h 250 -t "UPS - `/bin/date +%A", "%d" "%B" "%Y`" \
DEF:upsBatA=/var/lib/rrd/ups.rrd:upsBat:AVERAGE \
DEF:upsLoad=/var/lib/rrd/ups.rrd:upsLoad:AVERAGE \
DEF:upsBatRun=/var/lib/rrd/ups.rrd:upsBatRun:AVERAGE \
"CDEF:upsBat=upsBatA,10,*" \
AREA:upsBat\#FFFF00:"Battery %" \
GPRINT:upsBat:MIN:"Min\: %3.2lf " \
GPRINT:upsBat:MAX:"Max\: %3.2lf" \
GPRINT:upsBat:LAST:"Current\: %3.2lf\j" \
COMMENT:"\\n" \
AREA:upsBatRun\#00FF00:"Battery Runtime" \
GPRINT:upsBatRun:MIN:"Min\: %3.2lf " \
GPRINT:upsBatRun:MAX:"Max\: %3.2lf" \
GPRINT:upsBatRun:LAST:"Current\: %3.2lf\j" \
COMMENT:"\\n" \
AREA:upsLoad\#FF0000:"UPS Load" \
GPRINT:upsLoad:MIN:"Min\: %3.2lf " \
GPRINT:upsLoad:MAX:"Max\: %3.2lf" \
GPRINT:upsLoad:LAST:"Current\: %3.2lf\j";;
(*)
echo "Invalid option.";;
and the crontab line:
Code:
* * * * * root (/path/to/ups.sh update && /path/to/ups.sh graph) >/dev/null 2>&1
The database needs to be created (/path/to/ups.sh create) before setting up cron.