#!@@PERL@@ -w
# -*- perl -*-
# vim: ft=perl

=head1 NAME

snmp__sensors_fsc_bx_temp - Plugin to fetch temperature data from the
SNMP agent on the management blade on Fujitsu Simens BX series blade
servers

=head1 CONFIGURATION

The following environment variables are used by this plugin:

 host      - SNMP host (default taken from plugin link name)
 port      - SNMP port (default 161)
 community - SNMP community (default "private")
 version   - SNMP version to use (default: Net::SNMP default)

For acceptable and default SNMP versions, see the Net::SNMP
documentation.  Per Net::SNMP version 5.2.0, the default is SNMP
version 1.

=head1 AUTHOR

Copyright (C) 2004 Dagfinn Ilmari Mannsåker

=head1 LICENSE

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., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

=head1 MAGIC MARKERS

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

=cut

use strict;

use Net::SNMP qw(oid_lex_sort);
use Munin::Plugin::SNMP;

# The OIDs we're after
my $tempBase = '1.3.6.1.4.1.7244.1.1.1.4.6.1.1';

# Subtables
my $tempCabinetId          = 1;
my $tempSensorNumber       = 2;
my $tempSensorStatus       = 3;
my $tempSensorDesignation  = 4;
my $tempUpperWarningLevel  = 5;
my $tempUpperCriticalLevel = 6;
my $tempCurrentValue       = 7;

# Magic values
my $tempSensorUnknown = 1;
my $tempSensorDisabled = 2;
my $tempSensorUnavailable = 99;

if (defined $ARGV[0] and $ARGV[0] eq 'snmpconf') {
    print "require $tempBase.\n";
    # Require known, enabled and available sensors
    print "require $tempBase.$tempSensorStatus. ^[3-9]|[1-8][0-9]|9[0-8]\$\n";

    exit 0;
}


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

if ($error) {
    die "# Error: $error\n";
}

my $temps =  $session->get_hash(-baseoid => $tempBase,
				-cols    => { $tempCabinetId          => 'cabinet',
					      $tempSensorNumber       => 'number',
					      $tempSensorStatus       => 'status',
					      $tempUpperWarningLevel  => 'warning',
					      $tempUpperCriticalLevel => 'critical',
					      $tempCurrentValue       => 'value',
					      $tempSensorDesignation  => 'label',
					    },
			       ) or die "# Error: ". $session->error();

for my $key (keys %$temps) {
    my $temp = $temps->{$key};
    $temp->{info} = "Blade $temp->{cabinet} sensor $temp->{number}";
    $temp->{label} = "Blade $temp->{cabinet} $temp->{label}";
    # Delete sensors with unknown, disabled or unavailable status
    delete $temps->{$key}
      if $temp->{status} == $tempSensorUnknown ||
	$temp->{status} == $tempSensorDisabled ||
	$temp->{status} == $tempSensorUnavailable;
}

if (defined $ARGV[0] and $ARGV[0] eq 'config') {
    print <<EOM;
graph_title Temperatures
graph_args -l 0
graph_vlabel degrees Celcius
graph_category sensors
EOM
    print 'graph_order ', join(' ', map { get_id($_) } oid_lex_sort keys %$temps), "\n";
    print 'host_name ', $session->hostname(), "\n"
      unless $session->hostname eq 'localhost';

    for my $sensor (keys %$temps) {
	my $id = get_id($sensor);
	for my $key (qw(label warning critical info)) {
	    print "$id.$key $temps->{$sensor}->{$key}\n";
	}
	print "$id.type GAUGE\n";
    }
} else {
    print get_id($_), '.value ', $temps->{$_}{value}, "\n"
      for keys %$temps;
}

sub get_id {
    (my $id = shift) =~ tr/\./_/;
    return 'temp'.$id;
}
