Skip to content

Instantly share code, notes, and snippets.

@WanderingStar
Created March 8, 2014 23:03
Show Gist options
  • Save WanderingStar/9440362 to your computer and use it in GitHub Desktop.
Save WanderingStar/9440362 to your computer and use it in GitHub Desktop.
Munin plugin to calculate power draw, based on apc_nis plugin
#!/usr/bin/perl -w
# -*- perl -*-
=head1 NAME
apc_watts - Plugin to monitor APC UPS via the nis port of apcupsd
=head1 CONFIGURATION
The following configuration parameters are used by this plugin
[apc_watts]
env.host - hostname to connect to
env.port - port number to connect to
=head2 DEFAULT CONFIGURATION
[apc_watts]
env.host 127.0.0.1
env.port 3551
=head1 MAGIC MARKERS
#%# family=contrib
#%# capabilities=autoconf
=cut
use IO::Socket;
use strict;
if($ARGV[0] and $ARGV[0] eq "autoconf") {
print "yes\n";
exit 0;
}
my $host = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1";
my $port = exists $ENV{'port'} ? $ENV{'port'} : "3551";
my $sock = new IO::Socket::INET (
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp'
);
die "Could not create socket: $!\n" unless $sock;
my $buf = pack("CC", 0, 6);
print $sock $buf;
print $sock "status\n";
if($ARGV[0] and $ARGV[0] eq "config") {
# Test for some capabilities.
my $has_temperature = 0, my $line_volt_min, my $line_volt_max;
my $line;
do {
$line = <$sock>;
chomp($line);
if ($line =~ /\WITEMP /) {
$has_temperature = 1;
} elsif ($line =~ /\WLOTRANS /) {
$line =~ s/.* (\d+.\d+).*/$1/;
$line_volt_min = $line;
} elsif ($line =~ /\WHITRANS /) {
$line =~ s/.* (\d+.\d+).*/$1/;
$line_volt_max = $line;
}
} while(!($line =~ /END APC/));
close($sock);
print "graph_title APC UPS wattage\n";
print "graph_args -l 0 --base 1000\n";
print "graph_vlabel Watts\n";
print "graph_info Values received for apcupsd available at $host:$port\n";
print "watt.label current load\n";
print "watt.type GAUGE\n";
print "watt.max 650\n";
exit 0;
}
my $line;
my $loadpct;
my $nompower;
do {
$line = <$sock>;
chomp($line);
if($line =~ /\WNOMPOWER /) {
$line =~ s/.* (\d+.\d+).*/$1/;
$nompower = $line;
print "watt.value " . ($loadpct / 100 * $nompower) . "\n" if defined($loadpct);
} elsif($line =~ /\WLOADPCT /) {
$line =~ s/.* (\d+.\d+).*/$1/;
$loadpct = $line;
print "watt.value " . ($loadpct / 100 * $nompower) . "\n" if defined($nompower);
}
} while(!($line =~ /END APC/));
close($sock);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment