#!/usr/bin/perl
#
# Copyright (c) 2009 - Rune Nordbøe Skillingstad
# Copyright (c) 2009 - Kai Ove Gran
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

#	snmp_host_apcpdu - For APC PDUs. Graphs total current throughput.
#
#%# family=snmpauto
#%# capabilities=snmpconf

use strict;
use Net::SNMP;

my $DEBUG     = $ENV{DEBUG}     || 0;
my $host      = $ENV{host}      || undef;
my $port      = $ENV{port}      || 161;
my $community = $ENV{community} || "public";
my $iface     = $ENV{interface} || undef;
my $timeout   = $ENV{timeout}   || 1;
my $snmp_ver  = $ENV{version}   || 1;

if($0 =~ /^(?:|.*\/)snmp_([^_]+)_apcpdu$/) {
    $host = $1;
    if($host =~ /^([^:]+):(\d+)$/) {
	$host = $1;
	$port = $2;
    }
} elsif(!defined($host)) {
    die "# Error: couldn't understand what I'm supposed to monitor."; 
}

my @oidsList =  (
    '1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1',
    '1.3.6.1.4.1.318.1.1.12.2.2.1.1.3.1',
    '1.3.6.1.4.1.318.1.1.12.1.7.0'    
);

if(defined $ARGV[0] and $ARGV[0] eq "snmpconf") {
    for(my $i = 0; $i < @oidsList; $i++) {
	print "require " . $oidsList[$i] . "[0-9]\n"; 
    }
    exit 0;
}

my ($session, $error) = Net::SNMP->session(
    -hostname  => $host,
    -community => $community,
    -port      => $port,
    -timeout   => $timeout,
    -version   => $snmp_ver,
);

if(!defined ($session)) {
    die "# Croaking: $error";
}

if($ARGV[0] and $ARGV[0] eq "config") {
    my $modell = &get_single($session, '1.3.6.1.4.1.318.1.1.4.1.4.0');
    if($modell) {
	print "host_name $host\n" unless $host eq 'localhost';
	print "graph_title Current, $modell\n";
	print "graph_args --base 1000 -l 0\n";
	print "graph_vlabel Amps\n";
	print "graph_category Ups\n";
	print "graph_info This graph shows the total throughput of the PDU";
	print "graph_order load threshold rating\n";
	print "load.label Load\n";
	print "load.type GAUGE\n";
	print "load.info Current load in amps.\n";
	print "load.draw LINE2\n";
	print "threshold.label Threshold\n";
	print "threshold.type GAUGE\n";
	print "threshold.info Near overload threshold.\n";
	print "threshold.draw LINE2\n";
	print "rating.label Rating\n";
	print "rating.type GAUGE\n";
	print "rating.info Rating (Max amps).\n";
	print "rating.draw LINE2\n";
    }
    exit 0;				    
}

my @response = &get_multiple($session, @oidsList);

printf "load.value %.02f\n", $response[0] / 10;
printf "threshold.value %d\n", $response[1];
printf "rating.value %d\n", $response[2];


sub get_multiple {
    my $handle = shift;
    my @oids = @_;

    my @result;
    foreach my $oid (@oids) {
	push(@result, &get_single($handle,$oid));
    }
    chomp @result;
    return @result;
}

sub get_single {
    my ($handle, $oid) = @_;
    
    my $response = $handle->get_request ($oid);
    if(!defined $response->{$oid}) {
	print "# No response\n" if $DEBUG;
	return "";
    } else {
	print "# Got response \"".$response->{$oid}."\"\n" if $DEBUG;
	return $response->{$oid};
    }
}
