#!/usr/bin/perl
#
#  rrd_UPS.pl
#	Uninterrupable power supply collection routine for KnoppMyth
#
########################################################################
# Configuration:
my $dbf = 'ups';
$ups = 'mythups@localhost';
########################################################################
use RRDs;

my $rrdlog = '/var/lib/rrd';
my $graphs = '/data/srv/httpd/htdocs/rrd';

sub create {
    #	$_[0] = filename
    if (! -e "$rrdlog/$_[0].rrd") {
	print "Create db for $_[0] => $rrdlog/$_[0].rrd\n";
	RRDs::create( "$rrdlog/$_[0].rrd", "-s 300",
	    "DS:load:GAUGE:600:0:U",
	    "DS:input:GAUGE:600:0:U",
	    "DS:charge:GAUGE:600:0:U",
	    "RRA:AVERAGE:0.5:1:576",
	    "RRA:AVERAGE:0.5:6:672",
	    "RRA:AVERAGE:0.5:24:732",
	    "RRA:AVERAGE:0.5:144:1460");
	$ERROR = RRDs::error;
	print "Error: RRDs::create failed for '$_[0]' : $ERROR\n" if $ERROR;
    }
}

sub gather {
    my $status = '/var/log/apcupsd.status';
    if(-e "/usr/bin/upsc") { # Network UPS Tools
        open F, "upsc $ups |";
        while(<F>) {
            if($_ =~ m/battery\.charge:\s+(\w+)/) {
                $charge = $1;
            }
            if($_ =~ m/input\.voltage:\s+(\w+)/) {
                $input = $1;
            }
            if($_ =~ m/ups\.load:\s+(\w+)/) {
                $load = $1;
            }
        }
        close F;
    } elsif(-e $status) { # apcupsd
        open F, $status;
        while(<F>) {
            if($_ =~ m/BCHARGE\s*:\s*(\d+\.\d+)\s+/) {
                $charge = $1;
            }
            if($_ =~ m/LINEV\s*:\s*(\d+\.\d+)\s+/) {
                $input = $1;
            }
            if($_ =~ m/LOADPCT\s*:\s*(\d+\.\d+)\s+/) {
                $load = $1;
            }
        }
        close F;
    }
    print "$dbf: load='$load' input='$input' charge='$charge'\n";
}

sub update {
    #	$_[0] = filename
    RRDs::update( "$rrdlog/$_[0].rrd", "-t",
	"load:input:charge",
	"N:$load:$input:$charge");
    $ERROR = RRDs::error;
    print "Error: RRDs::update for '$_[0]' : $ERROR\n" if $ERROR;
}

sub graph {
    #	$_[0] = time interval (ie: day...)
    #	$_[1] = filename suffix.
    RRDs::graph( "$graphs/$dbf-$_[1].png", "-s -1$_[0]", "-aPNG",
	"--alt-y-grid", "-w 600", "-h 100", "-l 0", "-E",
	"--color", "SHADEA#EAE9EE",
        "--color", "SHADEB#EAE9EE",
        "--color", "BACK#EAE9EE",	"-t UPS Data :: $_[0]",
	"DEF:load=$rrdlog/$dbf.rrd:load:AVERAGE",
	"DEF:input=$rrdlog/$dbf.rrd:input:AVERAGE",
	"DEF:charge=$rrdlog/$dbf.rrd:charge:AVERAGE",
	"LINE2:load#0000FF:Load\\:",
	"GPRINT:load:LAST:currently %.1lf ",
	"LINE2:input#00FF00:Input\\:",
	"GPRINT:input:LAST:currently %.1lf ",
	"LINE2:charge#FF0000:Charge\\:",
	"GPRINT:charge:LAST:currently %.1lf\\j");
    $ERROR = RRDs::error;
    print "Error: RRDs::graph failed for '$_[0]' : $ERROR\n" if $ERROR;
}
########################################################################
create "$dbf";
gather;
update "$dbf";
graph 'day',   'day';
graph 'week',  'week';
graph 'month', 'month';
graph 'year',  'year';
########################################################################
# vim: sw=4 ts=8
# End
