#!/usr/bin/perl -w
#
# Jean-Samuel Reynaud <js.reynaud@free.fr>
# base on snmp__if_ from Jimmy Olsen, Dagfinn Ilmari Mannsaaker
#
# 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.
#
#
# Usage:
# ln -s /usr/share/munin/node/plugins-auto/snmp__ups2_ /etc/munin/node.d/snmp_ups.address.loc_mode
# with mode : batteryVoltage, batteryTemperature,inputVoltage,inputCurrent,inputFrequency,outputVoltage,outputCurrent,outputPower,outputPercentLoad,ChargeRemaining
#

#%# family=snmpauto
#%# capabilities=snmpconf



use strict;
use Net::SNMP;

my $DEBUG = 0;

my $host      = $ENV{host}      || undef;
my $port      = $ENV{port}      || 161;
my $community = $ENV{community} || "public";
my $mode     = $ENV{mode} || undef;

my $response;

if (defined $ARGV[0] and $ARGV[0] eq "snmpconf")
{
    print "require .1.3.6.1.2.1.33.1.2.7 [0-9]\n"; # Bat temp
    exit 0;
}


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



my $graphs = {
    'batteryVoltage' => { 'title' => 'Battery Voltage',
                          'category' => 'network',
                          'unit' => 'Volt',
                              'value' => '.1.3.6.1.2.1.33.1.2.5'},
    'batteryTemperature' => { 'title' => 'Battery Temperature',
                              'category' => 'network',
                              'unit' => 'DegC',
                              'value' => '.1.3.6.1.2.1.33.1.2.7'},
    'inputVoltage' => { 'title' => 'Input Voltage',
                        'category' => 'network',
                        'unit' => 'Volt',
                        'list' => '.1.3.6.1.2.1.33.1.3.2',
                        'value' => '.1.3.6.1.2.1.33.1.3.3.1.3'},
    'inputCurrent' => { 'title' => 'Input Current',
                        'category' => 'network',
                        'unit' => 'Watt',
                        'list' => '.1.3.6.1.2.1.33.1.3.2',
                        'value' => '.1.3.6.1.2.1.33.1.3.3.1.4'},
    'inputFrequency' => { 'title' => 'Input Frequency',
                        'category' => 'network',
                        'list' => '.1.3.6.1.2.1.33.1.3.2',
                        'unit' => 'Hz',
                        'value' => '.1.3.6.1.2.1.33.1.3.3.1.2'},

    'outputVoltage' => { 'title' => 'Output Voltage',
                        'category' => 'network',
                        'list' => '.1.3.6.1.2.1.33.1.4.3',
                        'unit' => 'Hz',
                        'value' => '.1.3.6.1.2.1.33.1.4.4.1.2'},
    'outputCurrent' => { 'title' => 'Output Current',
                        'category' => 'network',
                        'list' => '.1.3.6.1.2.1.33.1.4.3',
                        'value' => '.1.3.6.1.2.1.33.1.4.4.1.3'},
    'outputPower' => { 'title' => 'Output Power',
                        'category' => 'network',
                        'list' => '.1.3.6.1.2.1.33.1.4.3',
                        'unit' => 'Hz',
                        'value' => '.1.3.6.1.2.1.33.1.4.4.1.4'},

    'outputPercentLoad' => { 'title' => 'Output PercentLoad',
                             'category' => 'network',
                             'list' => '.1.3.6.1.2.1.33.1.4.3',
                             'unit' => '%',
                             'value' => '.1.3.6.1.2.1.33.1.4.4.1.5'},

   'ChargeRemaining' => { 'title' => 'Charge remaining',
                          'unit' => 'Seconds',
                          'category' => 'network',
                          'value' => '.1.3.6.1.2.1.33.1.2.4'}
};


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

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

my $count_data = 1;
if (defined $graphs->{$mode}->{'list'}  &&
    defined ($response = $session->get_request($graphs->{$mode}->{'list'} . ".0"))) {
    $count_data = $response->{$graphs->{$mode}->{'list'} . ".0"};

}

if ($ARGV[0] and $ARGV[0] eq "config")
{
	print "host_name $host\n";
    printf "graph_title %s \n",$graphs->{$mode}->{'title'};
#	print "graph_args --base 1000\n";
	print "graph_category sensors\n";


    for(my $i=0;$i<$count_data;$i++) {
        printf "line%u.label Line %u\n",$i+1,$i+1;
        printf "line%u.type GAUGE\n",$i+1;
        printf "line%u.info %s\n",$i+1,$graphs->{$mode}->{'unit'};
    }
	exit 0;
}

for(my $i=0;$i<$count_data;$i++) {
    my $l_current = $i+1;
    if ($count_data == 1) {
        $l_current = 0;
    }
    if (defined ($response = $session->get_request($graphs->{$mode}->{'value'} . sprintf(".%u",$l_current)))) {
        printf "line%u.value %u\n",$i+1,$response->{$graphs->{$mode}->{'value'} . sprintf(".%u",$l_current)};
    } else {
        printf "line%u.value U\n",$i+1;
    }

}
