Repository
Munin (master)
Last change
2020-05-24
Graph Categories
Family
contrib
Capabilities
Language
Perl

apc_nis

Name

apc_nis - Plugin to monitor APC UPS via the nis port of apcupsd

Configuration

The following configuration parameters are used by this plugin

[apc_nis]
 env.host     - hostname to connect to
 env.port     - port number to connect to

Default Configuration

[apc_nis]
 env.host 127.0.0.1
 env.port 3551

Magic Markers

#%# family=contrib
#%# capabilities=autoconf
#!/usr/bin/perl -w

=head1 NAME

apc_nis - Plugin to monitor APC UPS via the nis port of apcupsd

=head1 CONFIGURATION

The following configuration parameters are used by this plugin

 [apc_nis]
  env.host     - hostname to connect to
  env.port     - port number to connect to

=head2 DEFAULT CONFIGURATION

 [apc_nis]
  env.host 127.0.0.1
  env.port 3551

=head1 MAGIC MARKERS

 #%# family=contrib
 #%# capabilities=autoconf

=cut

use IO::Socket;
use strict;

my $host = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1";
my $port = exists $ENV{'port'} ? $ENV{'port'} : "3551";

# We connect early: the connection is used for every operation: autoconf, config and data fetch.
my $sock = new IO::Socket::INET (
    PeerAddr => $host,
    PeerPort => $port,
    Proto => 'tcp'
    );

if($ARGV[0] and $ARGV[0] eq "autoconf") {
    if ($sock) {
        # we successfully opened a connected to the configured daemon
        print "yes\n";
    } else {
        print "no (failed to connect to $host:$port)\n";
    }
    exit 0;
}

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 $has_freq = 0;
    my $nom_power = 0;
    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;
        } elsif($line =~ /\WLINEFREQ /) {
            $line =~ s/.* (\d+.\d+).*/$1/;
            $has_freq = 1;
        } elsif($line =~ /\WNOMPOWER /) {
            $line =~ s/.* (\d+.\d+).*/$1/;
            $nom_power = $line;
        }
    } while(!($line =~ /END APC/));

    close($sock);

    print "graph_title APC UPS measurements\n";
    print "graph_args -l 0 --base 1000\n";
    print "graph_vlabel A bit of all (Volt, time, %, Watt)\n";
    print "graph_info Values received for apcupsd available at $host:$port\n";
    print "graph_category sensors\n";
    print "battery_volt.label batt volt (V)\n";
    print "battery_volt.type GAUGE\n";
    print "battery_volt.max 300\n";
    print "battery_charge.label batt charge (%)\n";
    print "battery_charge.type GAUGE\n";
    print "battery_charge.max 100\n";
    print "line_volt.label line (V)\n";
    print "line_volt.type GAUGE\n";
    print "line_volt.max 300\n";
    print "line_volt.warning ${line_volt_min}:${line_volt_max}\n";
    if ($has_freq) {
        print "line_freq.label line (Hz)\n";
        print "line_freq.type GAUGE\n";
        print "line_freq.max 65\n";
    }
    print "load.label ups load (%)\n";
    print "load.type GAUGE\n";
    if ($nom_power) {
        print "load_watt.label ups load (W)\n";
        print "load_watt.type GAUGE\n";
        print "load_watt.max $nom_power\n";
    }
    print "time_left.label time left (min)\n";
    print "time_left.type GAUGE\n";
    if ($has_temperature) {
        print "temperature.label internal temperature (°C)\n";
        print "temperature.type GAUGE\n";
    }
    exit 0;
}

my $load_percent, my $nom_power = 0;
my $line;
do {
    $line = <$sock>;
    chomp($line);
    if($line =~ /\WBATTV /) {
        $line =~ s/.* (\d+.\d+).*/$1/;
        print "battery_volt.value $line\n";
    } elsif($line =~ /\WLINEV /) {
        $line =~ s/.* (\d+.\d+).*/$1/;
        print "line_volt.value $line\n";
    } elsif($line =~ /\WLINEFREQ /) {
        $line =~ s/.* (\d+.\d+).*/$1/;
        print "line_freq.value $line\n";
    } elsif($line =~ /\WLOADPCT /) {
        $line =~ s/.* (\d+.\d+).*/$1/;
        print "load.value $line\n";
        $load_percent = $line;
    } elsif($line =~ /\WNOMPOWER /) {
        $line =~ s/.* (\d+.\d+).*/$1/;
        $nom_power = $line;
    } elsif($line =~ /\WBCHARGE /) {
        $line =~ s/.* (\d+.\d+).*/$1/;
        print "battery_charge.value $line\n";
    } elsif($line =~ /\WTIMELEFT /) {
        $line =~ s/.* (\d+.\d+).*/$1/;
        print "time_left.value $line\n";
    } elsif($line =~ /\WITEMP /) {
        $line =~ s/.* (\d+.\d+).*/$1/;
        print "temperature.value $line\n";
    }
} while(!($line =~ /END APC/));

if ($nom_power) {
    print "load_watt.value " . ($nom_power / 100 * $load_percent) . "\n";
}

close($sock);