#!/usr/bin/perl
## Script written by Noah Guttman and Copyright (C) 2011 Noah Guttman. This script is released and distributed under the terms of the GNU General Public License

##########################
# 25/08/2015 - Daniel Ortiz
# Necessario especificar 1 paramentro adicional "-D"
#########################

#Libraries to use
#use strict;
use lib "/usr/local/opmon/libexec/";
use Getopt::Std;
use Data::Dumper;

# Check for proper args....

use vars qw($iopt_h $opt_H $opt_U $opt_P $opt_M $opt_D);
my $currentStatus;
my $exitcode=3;
my @statusList;
my $val;

if ($#ARGV le 0) {
	$opt_h = true;
} else {
        getopts('hH:U:P:M:D:');
}


## Display Help
if ($opt_h){
        print "::Dell Server Check via iDRAC IPMI Instructions::\n\n";
        print " -h,             Display this help information\n";
        print " -H,             Hostname or IP to check\n";
        print " -M,             Specify a message to return on failure\n";
        print " -U,             Username to connect\n";
        print " -P,             Password to connect\n";
        print " -D,             Driver to connect (LAN or SERIAL) \n";
	print "This script has been tested on iDRAC 6 & 7 and requires /usr/sbin/ipmi-sensors\n";
        print "Script written by Noah Guttman and Copyright (C) 2011 Noah Guttman.\n";
        print "This script is released and distributed under the terms of the GNU\n";
        print "General Public License.     >>>>    http://www.gnu.org/licenses/\n";
        print "";
        print "This program is free software: you can redistribute it and/or modify\n";
        print "it under the terms of the GNU General Public License as published by\n";
        print "the Free Software Foundation.\n\n";
        print "This program is distributed in the hope that it will be useful,\n";
        print "but WITHOUT ANY WARRANTY; without even the implied warranty of\n";
        print "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n";
        print "GNU General Public License for more details.\n";
        print ">>>>    http://www.gnu.org/licenses/\n";
        exit 0;
}

#Get the data

if ($opt_D le 0) {
	$currentStatus = `/usr/sbin/ipmi-sensors -h $opt_H -u $opt_U -p $opt_P -Q |grep -v "State Deasserted" |grep -v Unknown | grep -v "Presence" |grep -v Interconnect |grep -v OEM |grep -v Interrupt |grep -v "Non-Critical" |grep -v "Upper Critical Threshold"`;
} else {

	$currentStatus = `/usr/sbin/ipmi-sensors -h $opt_H -u $opt_U -p $opt_P -D $opt_D -Q |grep -v "State Deasserted" |grep -v Unknown | grep -v "Presence" |grep -v Interconnect |grep -v OEM |grep -v Interrupt |grep -v "Non-Critical" |grep -v "Upper Critical Threshold"`;
}

@statusList = split("\n", $currentStatus);

#Check if there are any Errors, Warnings or Criticals
if ($currentStatus =~ /OK/i){
        $exitcode =0;
}

if ($currentStatus =~ /Error/i){
        $exitcode =3;
        print "ERROR:";
        foreach my $val (@statusList) {
                if ($val =~ /ERROR/i){
                        print $val;
                }
        }

}

if ($currentStatus =~ /Warning/i){
        $exitcode =1;
        print "WARNING:";
        foreach my $val (@statusList) {
                if ($val =~ /WARNING/i){
                        print $val;
                }
        }
	
}

if ($currentStatus =~ /Critical/i){
	$exitcode =2;
	print "CRITICAL:";
	foreach my $val (@statusList) {
		if ($val =~ /CRITICAL/i){
			print $val;
		}
	}
	print "/n$opt_M";
}
if ($currentStatus =~ /OK/i){
	print "OK: The remaining checks were all OK|";
	foreach my $val (@statusList){
		@resultArray = split(":", $val);
		if (((($val =~ /Current/i) || ($val =~ /Fan/i)) || ($val =~ /Temperature/i)) || ($val =~ /Voltage/i)){
			if ((($val =~/THERMTRIP/i) || ($val =~/Redundancy/i)) || ($val =~/Interf/i )){

			}else{
				@performanceArray = split(" ", @resultArray[2]);
				@resultArray[1] = substr(@resultArray[1],1);
				@resultArray[1] =~ s/ /_/g;
				print "@resultArray[1]".@resultArray[0]."_".@performanceArray[1]."=".@performanceArray[0].";;;; ";
			}
		}

	} 
}else{
	print ("UNKNOWN: cCheck seems to have timed out. Error: $currentStatus - $opt_M\n");
	print $opt_D;

}


print "\n";
exit $exitcode; 


