From e18c93b30560e68d91bd34f298dc9d8903929b97 Mon Sep 17 00:00:00 2001 From: Michael Gernoth Date: Thu, 17 Dec 2009 16:02:01 +0100 Subject: [PATCH 1/1] Initial perl grapher using rrdtool --- upsgraph.pl | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100755 upsgraph.pl diff --git a/upsgraph.pl b/upsgraph.pl new file mode 100755 index 0000000..745a44c --- /dev/null +++ b/upsgraph.pl @@ -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 'USV status'; + print HTML ''; + + 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 ""; + } + + print HTML "\n"; + print HTML "
Generated on: " . localtime(time()); + + close(HTML); + + rename("${outdir}/index.html.new", "${outdir}/index.html"); + + sleep(${step}/2); +} -- 2.39.5