#!/usr/bin/perl -w # rrdvolt.pl, Graph line voltage as measured by an APC UPS. # Copyright 2003,2007 by Hans Lambermont # # This program is free software; you can redistribute it and/or modify # it under the same terms as Perl itself. # $Id: rrdvolt.pl,v 1.6 2007/01/07 13:46:11 hans Exp $ # Latest source location: # # Mavetju's page at http://www.mavetju.org/reporting/apcups.php started # this whole thing. # # 1) Create rrdtool database # rrdtool create $HOME/src/apcups-linevoltage/apc.rrd -s 300 \ # DS:line:GAUGE:600:U:U \ # RRA:MIN:0.5:1:2400 RRA:MAX:0.5:1:2400 RRA:AVERAGE:0.5:1:2400 # # 2) Set up a cronjob to measure the line voltage and update the database # */5 * * * * $HOME/src/apcups-linevoltage/crontab-apc.sh # #!/bin/sh # S=`/usr/local/sbin/apcaccess status|grep LINEV|head -1|awk '{ print $3 }'` # /usr/local/bin/rrdtool update $HOME/src/apcups-linevoltage/apc.rrd N:$S # # 3) Set up a cronjob to graph the line voltage in a web page using this script # 0 * * * * $HOME/src/apcups-linevoltage/rrdvolt.pl >/dev/null use strict; my $debug = 0; my $rrdfilepath="/home/hans/apcups-linevoltage"; my $outputpath="/usr/local/www/lambermont.dyndns.org/docs/ups"; sub draw { my $filename = shift; my $period = shift; my $time = shift; my @args = ( "rrdtool", "graph", $filename, "--start", $period, "--vertical-label", "Volt AC", "COMMENT:Legend", "--title", "APC Line Voltage of the $time", "DEF:min=$rrdfilepath/apc.rrd:line:MIN", "DEF:max=$rrdfilepath/apc.rrd:line:MAX", "CDEF:minsane=min,220,240,LIMIT", "CDEF:maxsane=max,220,240,LIMIT", "CDEF:delta=maxsane,minsane,-", "--lower-limit", "220", "--upper-limit", "240", "--rigid", "HRULE:230#00AA00:Target voltage", "LINE1:maxsane#AA0000:Maximum voltage", "LINE1:minsane#0000AA:Minimum voltage", "--imgformat", "PNG"); if ($debug) {print "args=@args\n";} system(@args); } sub createPage { my $filename = shift; local *FH; open(FH, ">$filename") or die "Cannot open $filename: $!\n"; print FH "\n"; print FH "APC UPS Line Voltage\n"; print FH "\n"; print FH "\n"; print FH "\n"; print FH "Last update at: " . localtime() . " CET\n"; printf FH "(%02d:%02d UTC)\n", (gmtime)[2], (gmtime)[1]; print FH "

\n"; print FH "\"day\"\n"; print FH "
\n"; print FH "\"week\"\n"; print FH "
\n"; print FH "\"month\"\n"; print FH "
\n"; print FH "\"year\"\n"; print FH "\n"; close(FH); } draw("$outputpath/apcups-linevoltage-2d.png", "now-2d", "last 2 days"); draw("$outputpath/apcups-linevoltage-1w.png", "now-1w", "last week"); draw("$outputpath/apcups-linevoltage-1m.png", "now-1m", "last month"); draw("$outputpath/apcups-linevoltage-1y.png", "now-1y", "last year"); createPage("$outputpath/apcups-linevoltage.html");