]> git.zerfleddert.de Git - upsgraph/commitdiff
Initial perl grapher using rrdtool
authorMichael Gernoth <michael@gernoth.net>
Thu, 17 Dec 2009 15:02:01 +0000 (16:02 +0100)
committerMichael Gernoth <michael@gernoth.net>
Thu, 17 Dec 2009 15:02:01 +0000 (16:02 +0100)
upsgraph.pl [new file with mode: 0755]

diff --git a/upsgraph.pl b/upsgraph.pl
new file mode 100755 (executable)
index 0000000..745a44c
--- /dev/null
@@ -0,0 +1,173 @@
+#!/usr/bin/perl -w
+
+if (@ARGV != 3) {
+       print STDERR "Syntax: ${0} host /path/to/file.rrd /output/directory/\n";
+       exit(1);
+}
+
+my $host=$ARGV[0];
+my $rrdfile=$ARGV[1];
+my $outdir=$ARGV[2];
+my $community="public";
+my $step=60;
+
+my @fields = ( 'inputV', 'outputV', 'inputHZ', 'outputHZ', 'battT', 'battC', 'load', 'ambT', 'ambH', 'timeR', 'timeO' );
+
+my $vars = {
+       'inputV' => {
+               'name' => 'Input Voltage',
+               'oidtext' => 'PowerNet-MIB::upsAdvInputLineVoltage.0',
+               'oid' => '1.3.6.1.4.1.318.1.1.1.3.2.1.0',
+               'min' => '180',
+               'max' => '280',
+       },
+       'outputV' => {
+               'name' => 'Output Voltage',
+               'oidtext' => 'PowerNet-MIB::upsAdvOutputVoltage.0',
+               'oid' => '1.3.6.1.4.1.318.1.1.1.4.2.1.0',
+               'min' => '180',
+               'max' => '280',
+       },
+       'inputHZ' => {
+               'name' => 'Input Frequency',
+               'oidtext' => 'PowerNet-MIB::upsAdvInputFrequency.0',
+               'oid' => '1.3.6.1.4.1.318.1.1.1.3.2.4.0',
+               'min' => '40',
+               'max' => '60',
+       },
+       'outputHZ' => {
+               'name' => 'Output Frequency',
+               'oidtext' => 'PowerNet-MIB::upsAdvOutputFrequency.0',
+               'oid' => '1.3.6.1.4.1.318.1.1.1.4.2.2.0',
+               'min' => '40',
+               'max' => '60',
+       },
+       'battT' => {
+               'name' => 'Battery Temperature',
+               'oidtext' => 'PowerNet-MIB::upsAdvBatteryTemperature.0',
+               'oid' => '1.3.6.1.4.1.318.1.1.1.2.2.2.0',
+               'min' => '0',
+               'max' => '100',
+       },
+       'battC' => {
+               'name' => 'Battery Capacity',
+               'oidtext' => 'PowerNet-MIB::upsAdvBatteryCapacity.0',
+               'oid' => '1.3.6.1.4.1.318.1.1.1.2.2.1.0',
+               'min' => '0',
+               'max' => '110',
+       },
+       'load' => {
+               'name' => 'UPS Load',
+               'oidtext' => 'PowerNet-MIB::upsAdvOutputLoad.0',
+               'oid' => '1.3.6.1.4.1.318.1.1.1.4.2.3.0',
+               'min' => '0',
+               'max' => '110',
+       },
+       'ambT' => {
+               'name' => 'Ambient Temperature',
+               'oidtext' => 'PowerNet-MIB::mUpsEnvironAmbientTemperature.0',
+               'oid' => '1.3.6.1.4.1.318.1.1.2.1.1.0',
+               'min' => '0',
+               'max' => '60',
+       },
+       'ambH' => {
+               'name' => 'Ambient Humidity',
+               'oidtext' => 'PowerNet-MIB::mUpsEnvironRelativeHumidity.0',
+               'oid' => '1.3.6.1.4.1.318.1.1.2.1.2.0',
+               'min' => '0',
+               'max' => '100',
+       },
+       'timeR' => {
+               'name' => 'Time Remaining',
+               'oidtext' => 'PowerNet-MIB::upsAdvBatteryRunTimeRemaining.0',
+               'oid' => '1.3.6.1.4.1.318.1.1.1.2.2.3.0',
+               'factor' => 1/6000,
+               'min' => '0',
+               'max' => '360',
+       },
+       'timeO' => {
+               'name' => 'Time On Battery',
+               'oidtext' => 'PowerNet-MIB::upsBasicBatteryTimeOnBattery.0',
+               'oid' => '1.3.6.1.4.1.318.1.1.1.2.1.2.0',
+               'factor' => 1/6000,
+               'min' => '0',
+               'max' => '360',
+       },
+};
+
+use Net::SNMP;
+
+if (! -e "${rrdfile}") {
+       my $cmd = "rrdtool create \"${rrdfile}\" ";
+       foreach my $var (@fields) {
+               $cmd .= "DS:${var}:GAUGE:600:" .
+                       $vars->{$var}->{'min'} . ":" .
+                       $vars->{$var}->{'max'} . " ";
+       }
+       $cmd .= "RRA:AVERAGE:0.5:1:10080 --step ${step}";
+
+       print "Creating ${rrdfile}...\n";
+       print `${cmd}`;
+}
+
+while(1) {
+       ($session,$error) = Net::SNMP->session(Hostname => $host,
+                       Community => $community);
+
+       die "session error: $error" unless ($session);
+
+       $session->translate(0);
+
+       foreach my $var (@fields) {
+               delete $vars->{$var}->{'value'};
+
+               my $result = $session->get_request($vars->{$var}->{'oid'});
+               next unless (defined $result);
+
+               $vars->{$var}->{'value'} = $result->{$vars->{$var}->{'oid'}};
+               if (defined($vars->{$var}->{'factor'})) {
+                       $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
+               }
+       }
+
+       $session->close;
+
+       my $cmd = "rrdtool update \"${rrdfile}\" \"N";
+       foreach my $var (@fields) {
+               if (!(defined($vars->{$var}->{'value'}))) {
+                       $vars->{$var}->{'value'} = 'U';
+               }
+               print $vars->{$var}->{'name'}.": ".$vars->{$var}->{'value'}."\n";
+               $cmd .= ":" . $vars->{$var}->{'value'};
+       }
+       $cmd .= "\"";
+       print `${cmd}`;
+
+       open(HTML, ">${outdir}/index.html.new");
+
+       print HTML '<html><head><meta http-equiv="refresh" content="60"/><meta http-equiv="cache-control" content="no-cache"/><meta http-equiv="pragma" content="no-cache"/><meta http_equiv="expires" content="Sat, 26 Jul 1997 05:00:00 GMT"/><title>USV status</title></head>';
+       print HTML '<body bgcolor="#ffffff">';
+
+       foreach my $var (@fields) {
+               my $graphdef = "-t \"" . $vars->{$var}->{'name'} . "\" \"DEF:${var}=${rrdfile}:${var}:AVERAGE\" \"LINE1:${var}#FF0000\"";
+               my $cmd = "rrdtool graph \"${outdir}/${var}.png.new\" -w 720 ${graphdef}";
+               my $size = `$cmd`;
+               rename("${outdir}/${var}.png.new", "${outdir}/${var}.png");
+               (my $width, my $height) = split(/x/,$size);
+
+               my $cmd2 = "rrdtool graph \"${outdir}/${var}.long.png.new\" -w 1008 -e now -s end-1w ${graphdef}";
+               my $size2 = `$cmd2`;
+               rename("${outdir}/${var}.long.png.new", "${outdir}/${var}.long.png");
+
+               print HTML "<a href=\"${var}.long.png\"><img src=\"${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a>";
+       }
+
+       print HTML "</body></html>\n";
+       print HTML "<br>Generated on: " . localtime(time());
+
+       close(HTML);
+
+       rename("${outdir}/index.html.new", "${outdir}/index.html");
+
+       sleep(${step}/2);
+}
Impressum, Datenschutz