#! /usr/bin/perl -w
#
# This plugin will graph the chassis temp sensors on a Dell PowerEdge Server
# via the omreport tool. It has been tested on the following chassis:
# 
#   PE2650/6650
#   PE2850/6850
#   PE2950
#
# To enable, link omreport_temps to this file. E.g.
#
#   ln -s /usr/share/node/node/plugins/omreport_temps /etc/munin/plugins/omreport_temps
#
# Configuration parameters for /etc/munin/plugin-conf.d/munin-node
#
# [omreport_*]
#   user 	 - User that has permissions to run the omreport binary
#   env.omreport - Path to the omreport binary
#
# Parameters:
#
#   config
#   autoconf
#
# Author: Justin Shepherd <galstrom21@gmail.com>
# Revision: 1.0  2007/10/22
#
#%# family=auto
#%# capabilities=autoconf

use strict;

my $omreport = $ENV{"omreport"} || "/usr/bin/omreport";

if ($ARGV[0] && $ARGV[0] eq "autoconf") {
        if (-f $omreport) {
                print "yes\n";
        } # end if
        else {
                print "no ($omreport does not exist)\n";
                exit(1);
        } # end else
} # end if
else {
        my $cmd = "$omreport chassis temps";
        my @result = `$cmd`;
        my (%val, $index);
        foreach my $line (@result) {
                $line =~ s/\s+/ /g;
                $line =~ s/\s$//g;
                next if ($line eq "");
                my ($field, $value) = split(/ \: /, $line);
                if ($field eq "Index") {
                        $val{$value} = {};
                        $index = $value;
                } # end if
                elsif ($field eq "Probe Name") {
                        $value =~ s/ Temp//g;
                        $val{$index}{$field} = "$value";
                } # end elsif
                elsif ($field eq "Reading") {
                        $value =~ s/ C//g;
                        $val{$index}{"$field"} = "$value";
                } # end elsif
                elsif ($field eq "Maximum Warning Threshold") {
                        $value =~ s/ C//g;
                        $val{$index}{"Warning Threshold"} = "$value";
                } # end elsif
                elsif ($field eq "Maximum Failure Threshold") {
                        $value =~ s/ C//g;
                        $val{$index}{"Critical Threshold"} = "$value";
                } # end elsif
        } # end foreach

        if ($ARGV[0] && $ARGV[0] eq "config") {
                print "graph_title OpenManage - 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";
                } # end foreach
        } # end if
        else {
                foreach my $j (sort keys %val) {
                        print "probe_$j.value $val{$j}{\"Reading\"}\n";
                } # end foreach
        } # end else
} # end else
exit(0);

