#! /usr/bin/perl -w
#
# This plugin will graph the chassis fan 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_fan_speed to this file. E.g.
#
#   ln -s /usr/share/node/node/plugins/omreport_fan_speed /etc/munin/plugins/omreport_fan_speed
#
# 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";
        }
        else {
                print "no ($omreport does not exist)\n";
                exit(1);
        }
} # end if
else {
        my $cmd = "$omreport chassis fans";
        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;
                }
                elsif ($field eq "Probe Name") {
                        $value =~ s/ RPM//g;
                        $val{$index}{$field} = "$value";
                }
                elsif ($field eq "Reading") {
                        $value =~ s/ RPM//g;
                        $val{$index}{"$field"} = "$value";
                }
                elsif ($field eq "Maximum Warning Threshold") {
                        $value =~ s/ RPM//g;
                        $val{$index}{"Warning Threshold"} = "$value";
                }
                elsif ($field eq "Maximum Failure Threshold") {
                        $value =~ s/ RPM//g;
                        $val{$index}{"Critical Threshold"} = "$value";
                }
        }

        if ($ARGV[0] && $ARGV[0] eq "config") {
                print "graph_title OpenManage - Fan Probes\n";
                print "graph_args --base 1000 -l 0\n";
                print "graph_vlabel Speed in RPMs\n";
                print "graph_category Sensors\n";
                foreach my $j (sort keys %val) {
                        print "fan_$j.label $val{$j}{\"Probe Name\"}\n";
                        if ($val{$j}{"Warning Threshold"} !~ m/\[N\/A\]/i) {
                                print "fan_$j.warning " .
                                      "$val{$j}{\"Warning Threshold\"}\n";
                        }
                        if ($val{$j}{"Critical Threshold"} !~ m/\[N\/A\]/i) {
                                print "fan_$j.critical " .
                                      "$val{$j}{\"Critical Threshold\"}\n";
                        }
                }
        }
        else {
                foreach my $j (sort keys %val) {
                        print "fan_$j.value $val{$j}{\"Reading\"}\n";
                }
        }
}
exit(0);
