#!/usr/bin/perl -w
# -*- cperl -*-
#
# Copyright (C) 2012 Andreas Thienemann <andreas@bawue.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published by
# the Free Software Foundation; version 2 only
#
# 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library 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.
#

=head1 NAME

snmp__cyclades_pm_ - Munin plugin to retrive readings from a Cyclades
Power Managemen Distribution Unit connected to a Cyclades console server.

=head1 APPLICABLE SYSTEMS

Avocent/Cyclades PMXX connected to a SNMP enabled Cyclades ACS/TS.

=head1 CONFIGURATION

As a rule SNMP plugins need site specific configuration.  The default
configuration (shown here) will only work on insecure sites/devices.

   [snmp_*]
	env.version 2
        env.community public

In general SNMP is not very secure at all unless you use SNMP version
3 which supports authentication and privacy (encryption).  But in any
case the community string for your devices should not be "public".

Please see 'perldoc Munin::Plugin::SNMP' for further configuration
information.

=head1 INTERPRETATION

The plugin reports the current fan speed readings as reported by the thecusIO
module.

=head1 MIB INFORMATION

Private MIB

=head1 MAGIC MARKERS

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

=head1 VERSION

  0.0.20120307

=head1 BUGS

None known.

=head1 AUTHOR

Copyright (C) 2012 Andreas Thienemann <andreas@bawue.net>

=head1 LICENSE

GPLv2 or (at your option) any later version.

=cut

use strict;
use Munin::Plugin;
use Munin::Plugin::SNMP;

my $DEBUG = $ENV{'MUNIN_DEBUG'};

my ($session, $type, $title, $vlabel, $info, $oid, $format);

sub get_pdu_ports {
	my $result = $session->get_entries(
		-columns => ['1.3.6.1.4.1.2925.4.5.5.1.1']
	);

	return sort(values %$result);
}

sub get_pdu_id {
	my ($port) = @_;
	my $result = $session->get_single("1.3.6.1.4.1.2925.4.5.3.1.11.$port.1");
	return $result;
}

if (defined $ARGV[0] and $ARGV[0] eq "snmpconf") {
    print "number 1.3.6.1.4.1.2925.4.5.1\n";
    exit 0;
}

$session = Munin::Plugin::SNMP->session();

my ($host,undef,$version,$tail) = Munin::Plugin::SNMP->config_session();

($type) = ($tail =~ /cyclades_pm_(.*)$/);

if ($type =~ "volt") {
    $title = "Voltage";
    $vlabel = "Volt";
    $info = "actual value of voltage in the element (V). When element does not have voltage sensor, it returns 0.";
    $oid = "1.3.6.1.4.1.2925.4.5.5.1.7";
    $format = "%i";
} elsif ($type =~ "current") {
    $title = "Current";
    $vlabel = "Ampere";
    $info = "actual value of current in the element (A).";
    $oid = "1.3.6.1.4.1.2925.4.5.5.1.3";
    $format = "%.2f";
} elsif ($type =~ "power") {
    $title = "Power";
    $vlabel = "Watt";
    $info = "actual value of power in the element (W).";
    $oid = "1.3.6.1.4.1.2925.4.5.5.1.5";
    $format = "%i";
} else {
    print "Couldn't get type from $tail\n";
    exit 1;
}


if ($ARGV[0] and $ARGV[0] eq "config") {
    my ($host,undef,$version,$tail) = Munin::Plugin::SNMP->config_session();

    print "host_name $host\n" unless $host eq 'localhost';

    print "graph_title Cyclades PM $title\n";
    print "graph_args --base 1000 -l 0\n";
    print "graph_vlabel $vlabel\n";
    print "graph_info This graph shows the $info\n";
    print "graph_category Sensors\n";

    foreach my $port (get_pdu_ports()) {
        my $id = get_pdu_id($port);
        print lc($id) . ".label $id\n";
    }

    exit(0);
}

foreach my $port (get_pdu_ports()) {
    my $id = get_pdu_id($port);
    my $val = $session->get_single("$oid.$port.1") || "U";
    printf ("%s.value $format\n", lc($id), ($val / 10));
}
