Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides
https://forum.ivorde.com/

Using Rrdtool and NUT to generate graphics about UPS battery charge, runtime and ups load.
https://forum.ivorde.com/using-rrdtool-and-nut-to-generate-graphics-about-ups-battery-charge-runtime-and-ups-load-t98.html
Page 1 of 1

Author:  debuser [ Sun Jun 06, 2010 9:20 am ]
Post subject:  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.

Author:  debuser [ Sun Jun 06, 2010 9:22 am ]
Post subject:  Re: Using Rrdtool and NUT to generate graphics about UPS battery charge, runtime and ups load.

Below is a sample of output from upsc utility:
Code:
# upsc apc
battery.charge: 100
battery.charge.low: 10
battery.charge.warning: 50
battery.date: not set
battery.mfr.date: 2009/11/29
battery.runtime: 690
battery.runtime.low: 120
battery.type: PbAc
battery.voltage: 13.5
battery.voltage.nominal: 12.0
driver.name: usbhid-ups
driver.parameter.pollfreq: 30
driver.parameter.pollinterval: 2
driver.parameter.port: auto
driver.version: 2.2.2
driver.version.data: APC HID 0.92
driver.version.internal: 0.33
input.transfer.high: 266
input.transfer.low: 180
input.voltage: 232.0
input.voltage.nominal: 230
ups.beeper.status: enabled
ups.delay.shutdown: 20
ups.delay.start: 30
ups.firmware: 871.O1 .I
ups.firmware.aux: O1
ups.load: 51
ups.mfr: APC
ups.mfr.date: 2009/11/29
ups.model: Back-UPS ES 700G
ups.productid: 0002
ups.serial: 3B0948X82191
ups.status: OL
ups.timer.reboot: 0
ups.timer.shutdown: -1
ups.vendorid: 051d

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/