#!/usr/bin/perl
# Author : Roderick Derks (roderick@r71.nl)
# Written for the St. Elisabeth Hospital in Tilburg, The Netherlands
# Date : februari 5, 2008
# check_snmp_mge_ups
#
# 20080908 - roderick - minor bugs removed
#
# 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_g);
use Getopt::Std;

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

sub print_usage {
    print "check_snmp_barracuda -H [ IP|HOSTNAME ] -C SNMPCOMMUNITY\n";
    exit $STATE_UNKNOWN;
}

$PROGNAME = "check_snmp_mge_ups";

$STATE_CRITICAL = 2;
$STATE_WARNING = 1;
$STATE_UNKNOWN = 3;
$STATE_OK = 0;
$STATE = $STATE_OK;  # used this later on in the script

$IP=$opt_H;
$COMMUNITY=$opt_C;
#$warning=$opt_w;
#$critical=$opt_c;

$warning_backup_time	= 600;
$warning_charge_level	= 60;
$warning_output_load	= 80;
$critical_backup_time	= 300; #300
$critical_charge_level	= 70; #40
$critical_output_load	= 90; #40


# mailqueues in, out and bounced
$backup_time		=`snmpwalk -v 1 -c $COMMUNITY $IP enterprises.705.1.5.1.0`;
$batt_charge_level	=`snmpwalk -v 1 -c $COMMUNITY $IP enterprises.705.1.5.2.0`;
$output_load		=`snmpwalk -v 1 -c $COMMUNITY $IP enterprises.705.1.7.2.1.4.1`;

chomp $backup_time;
chomp $batt_charge_level;
chomp $output_load;

#print "$backup_time\n";
#print "$batt_charge_level\n";
#print "$output_load\n";

# FILTER
if ( $backup_time ) {
     $backup_time =~s/SNMPv2-SMI::enterprises.705.1.5.1.0 = INTEGER: //g;
   }
else {
    print "Unknown  : No response while querying for the the amount of backup time left on host $IP\n";
    exit $STATE_UNKNOWN;
}
if ( $batt_charge_level ) {
     $batt_charge_level =~s/SNMPv2-SMI::enterprises.705.1.5.2.0 = INTEGER: //g;
   }
else {
    print "Unknown  : No response while querying for the percentage of battery charge level on host $IP\n";
    exit $STATE_UNKNOWN;
}
if ( $output_load ) {
     $output_load =~s/SNMPv2-SMI::enterprises.705.1.7.2.1.4.1 = INTEGER: //g;
   }
else {
    print "Unknown  : No response while querying for the percentage of output load on host $IP\n";
    exit $STATE_UNKNOWN;
}

#$OUTPUT="backuptime left:${backup_time}seconds - batt charge level:${batt_charge_level}% - Output load:${output_load}%|BCKP_TIME=$backup_time BATT_CHARGE=$batt_charge_level OUTPUT_LOAD=$output_load";

$REGEX="BCKP_TIME=$backup_time BATT_CHARGE=$batt_charge_level OUTPUT_LOAD=$output_load";

#if ( $backup_time <= $critical_backup_time or $batt_charge_level <= $critical_charge_level or $output_load <= $critical_output_load ) {
#    print "Status is CRITICAL. $OUTPUT\n";
#    exit $STATE_CRITICAL;
#   }


if ( $backup_time <= $critical_backup_time ) {
     $C_STATE = 2;
     $backup_time = "*${backup_time}*";
   }
elsif ( $backup_time <= $warning_backup_time ) {
     $W_STATE = 1;
     $backup_time = "*${backup_time}*";
   }
if ( $batt_charge_level <= $critical_charge_level ) {
     $CSTATE = 2;
     $batt_charge_level = "*${batt_charge_level}*";
   }
elsif ( $batt_charge_level <= $warning_charge_level ) {
     $W_STATE = 1;
     $batt_charge_level = "*${batt_charge_level}*";
   }
if ( $output_load >= $critical_output_load ) {
     $C_STATE = 2;
     $output_load = "*${output_load}*";
   }
elsif ( $output_load >= $warning_output_load ) {
     $W_STATE = 1;
     $output_load = "*${output_load}*";
   }

#print "STATE $STATE\n";

$OUTPUT="backuptime left: ${backup_time} seconds - batt charge level: ${batt_charge_level}% - Output load: ${output_load}%|$REGEX";

if ( $W_STATE == 1 ) {
    print "Status is WARNING: $OUTPUT\n";
    exit $STATE_WARNING;
   }
if ( $C_STATE == 2 ) {
    print "Status is CRITICAL: $OUTPUT\n";
    exit $STATE_CRITICAL;
   }
else {
    print "Status is OK. $OUTPUT\n";
    exit $STATE_OK;
    }


