#! /usr/bin/perl -w
#
# This plugin will graph the chassis temp sensors on a Dell PowerEdge Server
# via the ipmi-sensors tool. It has been tested on the following chassis:
# 
#   PE1850
#
# To enable: 
#
#   ln -s /usr/share/munin/plugins/ipmi-therm /etc/munin/plugins/ipmi-therm
#
# Configuration parameters for /etc/munin/plugin-conf.d/munin-node
#
# [ipmi_therm]
#   user 	 - User that has permissions to run ipmi-sensors
#   env.category sensors
#
# Parameters:
#
#   config
#   autoconf
#
# Author: Alexx Roche <munin-ipmi-plugin@alexx.net>
# Built on the work of Justin Shepherd <galstrom21@gmail.com>
# Revision: 1.3  2011/01/10
#
#%# family=auto
#%# capabilities=autoconf

use strict;

my $IPMI;
if(-f "/usr/sbin/ipmi-sensors"){ $IPMI='/usr/sbin/ipmi-sensors'; }
unless($IPMI){
   $IPMI = `which ipmi-sensors 2>/dev/null|sed 's/.*no ipmi-sensors//'`; 
   #$IPMI = `echo -n \$(which ipmi-sensors)`;
}
chomp($IPMI);
unless($IPMI){ exit 1; }

if ($ARGV[0] && $ARGV[0] eq "autoconf"){
    if (-f $IPMI){
	print "yes\n";
    }else{
	print "no ($IPMI does not exist)\n";
	exit(1);
    }
}else{
        my $cmd = "$IPMI|grep Temp";
        my @result = `$cmd`;
        my (%val, $index);
	$index=0;
	my $count=0;
	#Four of these seem to be unlabled, I'm going to guess that they are the CPU(s) and HDD(s)
	my @unknown = ('CPU0','CPU1','HDD0','HDD1');
        foreach my $line (@result) {
                $line =~ s/\s+/ /g;
                $line =~ s/\s$//g;
                next if ($line eq "");
                my ($list, $field, $value, $state) = split(/\: /, $line);
		#print "L: $list F: $field V: $value S: $state\n";
                if($field=~m/^(Temp|Ambient|Planar|Riser)/) {
		    my $f=$1;
		    if($f eq 'Temp'){
			$f = $unknown[$count];
			$count++;
		    }
                    my @data = split / /, $value;
		    $data[2]=~s/^\(//;
		    $data[2]=~s/\)$//;
		    if($f){
		    	my($warn,$crit) = split/\//, $data[2];
			unless($warn=~m/\d+/){ $warn = 0; }
			unless($crit=~m/\d+/){ $crit = 200; }
                    	$val{$index}{'Probe Name'} = "$f";
                    	$val{$index}{'Reading'} = "$data[0]";
                    	$val{$index}{'Warning Threshold'} = ($crit - $warn);
                    	$val{$index}{'Critical Threshold'} = "$crit";
		  	$index++;
		    }
                }
        }

        if ($ARGV[0] && $ARGV[0] eq "config") {
                print "graph_title IPMI sensors - Temperature Probes\n";
                print "graph_args --base 1000 -l 0\n";
                print "graph_vlabel Temperature in Celsius\n";
                print "graph_category Sensors\n";
                foreach my $j (sort keys %val) {
                        print "probe_$j\.label $val{$j}{'Probe Name'}\n";
                        print "probe_$j\.warning $val{$j}{'Warning Threshold'}\n";
                        print "probe_$j\.critical $val{$j}{'Critical Threshold'}\n";
                }
        }else{
             foreach my $j (sort keys %val) {
                if($val{$j}{'Reading'}){
		   print "probe_$j.value $val{$j}{'Reading'}\n";
		}
             }
        }
}
exit(0);

