| 1 | #!/usr/bin/perl
|
| 2 | #
|
| 3 | # rrd_UPS.pl
|
| 4 | # Uninterrupable power supply collection routine for KnoppMyth
|
| 5 | #
|
| 6 | ########################################################################
|
| 7 | # Configuration:
|
| 8 | my $dbf = 'ups';
|
| 9 | $ups = 'mythups@localhost';
|
| 10 | ########################################################################
|
| 11 | use RRDs;
|
| 12 |
|
| 13 | my $rrdlog = '/var/lib/rrd';
|
| 14 | my $graphs = '/data/srv/httpd/htdocs/rrd';
|
| 15 |
|
| 16 | sub create {
|
| 17 | # $_[0] = filename
|
| 18 | if (! -e "$rrdlog/$_[0].rrd") {
|
| 19 | print "Create db for $_[0] => $rrdlog/$_[0].rrd\n";
|
| 20 | RRDs::create( "$rrdlog/$_[0].rrd", "-s 300",
|
| 21 | "DS:load:GAUGE:600:0:U",
|
| 22 | "DS:input:GAUGE:600:0:U",
|
| 23 | "DS:charge:GAUGE:600:0:U",
|
| 24 | "RRA:AVERAGE:0.5:1:576",
|
| 25 | "RRA:AVERAGE:0.5:6:672",
|
| 26 | "RRA:AVERAGE:0.5:24:732",
|
| 27 | "RRA:AVERAGE:0.5:144:1460");
|
| 28 | $ERROR = RRDs::error;
|
| 29 | print "Error: RRDs::create failed for '$_[0]' : $ERROR\n" if $ERROR;
|
| 30 | }
|
| 31 | }
|
| 32 |
|
| 33 | sub gather {
|
| 34 | my $status = '/var/log/apcupsd.status';
|
| 35 | if(-e "/usr/bin/upsc") { # Network UPS Tools
|
| 36 | open F, "upsc $ups |";
|
| 37 | while(<F>) {
|
| 38 | if($_ =~ m/battery\.charge:\s+(\w+)/) {
|
| 39 | $charge = $1;
|
| 40 | }
|
| 41 | if($_ =~ m/input\.voltage:\s+(\w+)/) {
|
| 42 | $input = $1;
|
| 43 | }
|
| 44 | if($_ =~ m/ups\.load:\s+(\w+)/) {
|
| 45 | $load = $1;
|
| 46 | }
|
| 47 | }
|
| 48 | close F;
|
| 49 | } elsif(-e $status) { # apcupsd
|
| 50 | open F, $status;
|
| 51 | while(<F>) {
|
| 52 | if($_ =~ m/BCHARGE\s*:\s*(\d+\.\d+)\s+/) {
|
| 53 | $charge = $1;
|
| 54 | }
|
| 55 | if($_ =~ m/LINEV\s*:\s*(\d+\.\d+)\s+/) {
|
| 56 | $input = $1;
|
| 57 | }
|
| 58 | if($_ =~ m/LOADPCT\s*:\s*(\d+\.\d+)\s+/) {
|
| 59 | $load = $1;
|
| 60 | }
|
| 61 | }
|
| 62 | close F;
|
| 63 | }
|
| 64 | print "$dbf: load='$load' input='$input' charge='$charge'\n";
|
| 65 | }
|
| 66 |
|
| 67 | sub update {
|
| 68 | # $_[0] = filename
|
| 69 | RRDs::update( "$rrdlog/$_[0].rrd", "-t",
|
| 70 | "load:input:charge",
|
| 71 | "N:$load:$input:$charge");
|
| 72 | $ERROR = RRDs::error;
|
| 73 | print "Error: RRDs::update for '$_[0]' : $ERROR\n" if $ERROR;
|
| 74 | }
|
| 75 |
|
| 76 | sub graph {
|
| 77 | # $_[0] = time interval (ie: day...)
|
| 78 | # $_[1] = filename suffix.
|
| 79 | RRDs::graph( "$graphs/$dbf-$_[1].png", "-s -1$_[0]", "-aPNG",
|
| 80 | "--alt-y-grid", "-w 600", "-h 100", "-l 0", "-E",
|
| 81 | "--color", "SHADEA#EAE9EE",
|
| 82 | "--color", "SHADEB#EAE9EE",
|
| 83 | "--color", "BACK#EAE9EE", "-t UPS Data :: $_[0]",
|
| 84 | "DEF:load=$rrdlog/$dbf.rrd:load:AVERAGE",
|
| 85 | "DEF:input=$rrdlog/$dbf.rrd:input:AVERAGE",
|
| 86 | "DEF:charge=$rrdlog/$dbf.rrd:charge:AVERAGE",
|
| 87 | "LINE2:load#0000FF:Load\\:",
|
| 88 | "GPRINT:load:LAST:currently %.1lf ",
|
| 89 | "LINE2:input#00FF00:Input\\:",
|
| 90 | "GPRINT:input:LAST:currently %.1lf ",
|
| 91 | "LINE2:charge#FF0000:Charge\\:",
|
| 92 | "GPRINT:charge:LAST:currently %.1lf\\j");
|
| 93 | $ERROR = RRDs::error;
|
| 94 | print "Error: RRDs::graph failed for '$_[0]' : $ERROR\n" if $ERROR;
|
| 95 | }
|
| 96 | ########################################################################
|
| 97 | create "$dbf";
|
| 98 | gather;
|
| 99 | update "$dbf";
|
| 100 | graph 'day', 'day';
|
| 101 | graph 'week', 'week';
|
| 102 | graph 'month', 'month';
|
| 103 | graph 'year', 'year';
|
| 104 | ########################################################################
|
| 105 | # vim: sw=4 ts=8
|
| 106 | # End
|