#!/usr/bin/perl
# Author : Roderick Derks (roderick@r71.nl)
# Geschreven voor het St. Elisabeth Ziekenhuis te Tilburg
# Date : 08/10/2007
# check_snmp_apc_env 
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
#################################################################
#
# globale variabelen:
use vars qw($opt_h $opt_H $opt_C $opt_t $opt_p $opt_a $opt_b $opt_c $opt_d $opt_g);
use Getopt::Std;

&getopts("h:H:C:t:p:a:b:c:d:g")||die "ERROR: Onbekende optie. -help voor help\n";
if ($opt_h) { 
    &print_usage;
   }

sub print_usage {
    print "check_snmp_apc_env -H [ IP|HOSTNAME ] -C SNMPCOMMUNITY -a warning_temp -b critical_temp -c warning_humidity -d critical_humidity\n";
    exit $STATE_UNKNOWN;
}

$PROGNAME = "check_snmp_apc_env";

$STATE_CRITICAL = 2;
$STATE_WARNING = 1;
$STATE_UNKNOWN = 3;
$STATE_OK = 0;

$IP=$opt_H;
$COMMUNITY=$opt_C;
$type=$opt_t;
$probe=$opt_p;
$temp_warning=$opt_a;
$temp_critical=$opt_b;
$hum_warning=$opt_c;
$hum_critical=$opt_d;

if ( $temp_warning > $temp_critical ) {
   print "optie -a is groter dan -b, niet logisch\n";
   &print_usage;
   }
if ( $hum_warning > $hum_critical ) {
   print "optie -c $hum_warning is groter dan -d ${hum_critical}, niet logisch\n";
   &print_usage;
   }

#print "temp_warning=$opt_a temp_critical=$opt_b hum_warning=$opt_c hum_critical=$opt_d\n";

$result_temp_probe1      =`snmpwalk -v 1 -c $COMMUNITY $IP enterprises.318.1.1.2.1.1.0`;
$result_humidity_probe1  =`snmpwalk -v 1 -c $COMMUNITY $IP enterprises.318.1.1.2.1.2.0`;
$result_temp_probe2      =`snmpwalk -v 1 -c $COMMUNITY $IP enterprises.318.1.1.2.1.3.0`;
$result_humidity_probe2  =`snmpwalk -v 1 -c $COMMUNITY $IP enterprises.318.1.1.2.1.4.0`;

chomp $result_temp_probe1;
chomp $result_temp_probe2;
chomp $result_humidity_probe1;
chomp$result_humidity_probe2;

#print "temp_probe1 = $result_temp_probe1\nhumidity_probe1 $result_humidity_probe1\ntemp_probe2 = $result_temp_probe2\nhumidity_probe2 $result_humidity_probe2\n";

# SNMPv2-SMI::enterprises.318.1.1.2.1.1.0 = Gauge32: 20

if ( $result_temp_probe1 ) {
     $result_temp_probe1 =~s/SNMPv2-SMI::enterprises.318.1.1.2.1.1.0 = Gauge32: //g;
   } 
else {
    print "Unknown  : No response while querying for the temperature of probe 1 of $IP\n";
    exit $STATE_UNKNOWN;
}

if ( $result_humidity_probe1 ) {
     $result_humidity_probe1 =~s/SNMPv2-SMI::enterprises.318.1.1.2.1.2.0 = Gauge32: //g;
   } 
else {
    print "Unknown  : No response while querying for the humidity of probe 1 of $IP\n";
    exit $STATE_UNKNOWN;
}

if ( $result_temp_probe2 ) {
     $result_temp_probe2 =~s/SNMPv2-SMI::enterprises.318.1.1.2.1.3.0 = Gauge32: //g;
   } 
else {
    print "Unknown  : No response while querying for the temperature of probe 1 of $IP\n";
    exit $STATE_UNKNOWN;
}

if ( $result_humidity_probe2 ) {
     $result_humidity_probe2 =~ s/SNMPv2-SMI::enterprises.318.1.1.2.1.4.0 = Gauge32: //g ;
   } 
else {
    print "Unknown  : No response while querying for the humidity of probe 1 of $IP\n";
    exit $STATE_UNKNOWN;
}


#print "temp_probe1 = $result_temp_probe1\nhumidity_probe1 $result_humidity_probe1\ntemp_probe2 = $result_temp_probe2\nhumidity_probe1 $result_humidity_probe1\n";

if ( "$result_temp_probe1" >= "$temp_critical" or "$result_temp_probe2" >= "$temp_critical" ) {
     $status_temp = "critical";
}
elsif ( "$result_temp_probe1" < "$temp_warning" and "$result_temp_probe2" < "$temp_warning" ) {
     $status_temp = "ok";
}
else {
     $status_temp = "warning";
}

if ( "$result_humidity_probe1" >= "$hum_critical" or "$result_humidity_probe2" >= "$hum_critical" ) {
     $status_hum = "critical";
}
elsif ( "$result_humidity_probe1" < "$hum_warning" and "$result_humidity_probe2" < "$hum_warning" ) {
     $status_hum = "ok";
}
else {
     $status_hum = "warning";
}


#print "status_temp $status_temp status_hum $status_hum\n";

$OUTPUT="TEMP_P1=$result_temp_probe1 degr TEMP_P2=$result_temp_probe2 degr HUMIDITY_P1=${result_humidity_probe1}% HUMIDITY_P2=${result_humidity_probe2}%|TEMP_P1=$result_temp_probe1 TEMP_P2=$result_temp_probe2 HUMIDITY_P1=${result_humidity_probe1} HUMIDITY_P2=${result_humidity_probe2}";

if ( $status_temp eq "critical" or $status_hum eq "critical" ) {
     print "SNMP_ENVIRONMENT CRITICAL : $OUTPUT\n";
     exit $STATE_CRITICAL;
}
elsif ( $status_temp eq "warning" or $status_hum eq "warning" ) {
     print "SNMP_ENVIRONMENT WARNING : $OUTPUT\n";
     exit $STATE_WARNING;
}
else {
     print "SNMP_ENVIRONMENT OK : $OUTPUT\n";
     exit $STATE_OK;
}



#	print "OK : TEMP \n";
#	exit $STATE_OK;
 print " $result_temp_probe1 and $result_temp_probe2 < $temp_warning dan ok\n ";
 print " $result_temp_probe1 or $result_temp_probe2 >= $temp_critical dan critical\n ";


