#!/usr/bin/perl -w
### check_ipmi_sensors.pl
#
# Icinga plugin using the ipmitool.
#
# checks status of IPMI sensors
#
# License: GPL
#
# Changelog:
#
# TODO 
# Currently the  script supports only upper level limits for Temp and voltage.
# 
##############################################################################
use strict;
use warnings;
use lib "/usr/local/nagios/libexec";
use vars qw($VERSION $PROGNAME  $verbose $warn $critical $timeout $result $SDR $SDR_Param $IPMITOOL $SDR );
use vars qw(@PARAM $size $stat $bash $RUN $argnum $Sreading $reading_data2 $reading_data1 @result @reading @status $Sstatus );
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_l $opt_w $opt_c);
use utils qw(&print_revision %ERRORS) ;

sub print_help () {
        print "Usage:\n";
        print "  $PROGNAME <sensor name> \n";
        print "  $PROGNAME [-h | --help] \n";
        print "  $PROGNAME [-l | --list] \n";
        print "  <Sensor name> An IPMi Sensor , Examples are :Temp ,Ambient Temp ,DIMM Fan ,CMOS Battery \n";
        print "  etc`, If the name is more then one word then the name should be encapsulated in Qutation marks example \"Planar Temp\". \n";
        print "  The script will test the IPMI reading and report the value and the status in the following format:  Sensor Status  : ok , Sensor Reading : 42 C  \n";
        print "\n";
        print "  -l , --list : generates a list of the server IPMI Sensors \n";
        print "  -h , --help : Display this help data \n";
        print "\n";
        print "Copyright (c) 2009 Assaf Flatto\n\n";
}

$VERSION = 0.1;
$PROGNAME = "check_ipmi_sensors";
$IPMITOOL = "ipmitool";
$SDR = " sensor get " ;

Getopt::Long::Configure('bundling');
GetOptions ("V"   => \$opt_V, "version"    => \$opt_V,
  "h"   => \$opt_h, "help"       => \$opt_h,
  "w=s" => \$opt_w, "warning=s"  => \$opt_w,
  "l"   => \$opt_l, "list"       => \$opt_l,
  "c=s" => \$opt_c, "critical=s" => \$opt_c);

#Show Version
if ($opt_V){
  print "$PROGNAME" . " $VERSION \n" ;
  exit $ERRORS{'UNKNOWN'};
}

# Show Help
if ($opt_h) {
  print_help();
  exit $ERRORS{'UNKNOWN'};
}

# Show Sensor list
if ($opt_l) {
         print " Generating IPMI Sensor List \n";
         $SDR = " sensor list" ;
         $RUN = $IPMITOOL . $SDR ;
         @result = `$RUN` ;
         print  @result ;
         exit 0 ;
}

print_help() unless $ARGV[0] ;

@PARAM = split /\s+/,$ARGV[0];
$size = @PARAM ;

if ($size == 1) {
     $SDR = " sensor get " . $PARAM[0] ;
     $RUN = $IPMITOOL . $SDR ;
} else {
    foreach $argnum (0 .. $#ARGV) {
        chomp $ARGV[$argnum];
        $SDR_Param = "" . "$ARGV[$argnum]";
}
$RUN = $IPMITOOL . $SDR  . "\"$SDR_Param\"";
}
@result = `$RUN` ;
$bash = $? >> 8;

if ($bash == 0 )
{
foreach (@result )
{
        if( $_ =~ /Sensor Reading/ )
{
             @reading = split /\s+/,$_ ;
             $size = @reading ;
             $Sreading = $reading[4];
            if ( $reading[4] >= $opt_w ) {
                $stat = 1 ;
}
            if ($reading[4] >= $opt_c) {
                $stat = 2 ;
}else {
                $stat = 0 ;
}
             $reading_data2 = $reading[$size-1] ;
}

        if( $_ =~ /Status/ )
{
             @status = split /\s+/,$_ ;
             $Sstatus = $status[3] ;
}
}
}


if (($bash == 0)  && ($stat == 0) ) {
    print "OK :" . " Sensor Status : $Sstatus " . " Sensor Reading :$Sreading $reading_data2\n" ;
    exit $ERRORS{OK};
} elsif (($bash == 1 ) || ($stat == 1 )) {
    print "Warning :" . " Sensor Status : Warning , Sensor Reading :$Sreading $reading_data2\n" ;
    exit $ERRORS{WARNING};
} elsif (($bash == 2 ) ||($stat == 2 )) {
     print "Critical :" . " Sensor Status : Critical , Sensor Reading :$Sreading $reading_data2\n" ;
    exit $ERRORS{CRITICAL};
} elsif (($bash == 3 ) || ($stat == 3 )) {
    print "Unkonwn :" . " Data not Found \n" ;
    exit $ERRORS{UNKNOWN};
}

