#!@@PERL@@ -w
# -*- perl -*-
# vim: ft=perl
#
# Copyright (C) 2004 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#
# Plugin to fetch fan data from the ServerView SNMP agent on Fujitsu
# Simens servers
#
#%# family=snmpauto
#%# capabilities=snmpconf

use strict;

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

# The OIDs we're after
my $fanBase = '1.3.6.1.4.1.231.2.10.2.2.5.2.2.1';

# Subtables
my $fanCabinetId           = 1;
my $fanNumber              = 2;
my $fanStatus              = 3;
my $fanPurpose             = 4;
my $fanCurrentSpeed        = 8;
my $fanNominalMaximumSpeed = 9;
my $fanCurrentMaximumSpeed = 10;
my $fanDesignation         = 16;

# Magic values
my $fanUnknown = 1;
my $fanDisabled = 2;
my $fanUnavailable = 99;

if (defined $ARGV[0] and $ARGV[0] eq 'snmpconf') {
    print "require $fanBase.\n";
    # Require known, enabled and available fans
    print "require $fanBase.$fanStatus. ^[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 $fans =  $session->get_hash(-baseoid => $fanBase,
				-cols    => { $fanCabinetId          => 'cabinet',
					      $fanNumber             => 'number',
					      $fanStatus             => 'status',
					      $fanPurpose            => 'purpose',
					      $fanCurrentSpeed       => 'value',
					      $fanDesignation        => 'label',
					    },
			       ) or die $session->error();

for my $key (keys %$fans) {
    my $fan = $fans->{$key};
    $fan->{info} = "Cabinet $fan->{cabinet} fan $fan->{number}";
    # Delete sensors with unknown, disabled or unavailable status
    delete $fans->{$key}
      if $fan->{status} == $fanUnknown ||
	$fan->{status} == $fanDisabled ||
	$fan->{status} == $fanUnavailable;
}

if (defined $ARGV[0] and $ARGV[0] eq 'config') {
    print <<EOM;
graph_title Fans
graph_args -l 0
graph_vlabel RPM
graph_category sensors
EOM
    print 'graph_order ', join(' ', map { get_id($_) } oid_lex_sort keys %$fans), "\n";
    print 'host_name ', $session->hostname(), "\n"
      unless $session->hostname eq 'localhost';
      
    for my $fan (keys %$fans) {
	my $id = get_id($fan);
	for my $key (qw(label info)) {
	    print "$id.$key $fans->{$fan}{$key}\n";
	}
	print "$id.type GAUGE\n";
    }
} else {
    print get_id($_), '.value ', $fans->{$_}{value}, "\n"
      for keys %$fans;
}

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