#!/bin/bash
##################################################################################################
#
#	Check SNMP INVERTER 
#
#	Date: 27.08.2010
#	Version: 0.5
#	Author: Mihai Radoveanu
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#	Description:
#
# The purpose of this plug-in is to invert the output of the "check_snmp" plug-in
#when you need to check an inverted scale. For example, one needs to know when the input voltage of an UPS
#drops to zero => an error, as the power line is in fault, but not having problems when the same check is
#220V in normal conditions.
#  With the normal "check_snmp" you can't check this limits, as the check will always be in error.
#
#
#
# Usage: ./check_snmp_inverter.sh -H <remote_host_IP> -C <comunity_string> -o <OID> -w <warning value> 
#-c <critical value> -u <measure block> -l <label> 
#
# The critical value is the real value that one needs to set, for example: 0
# and the warning value greater than the critical - ex: 240.
#
# User needs to set the "home" variable to the Nagios plugin directory
#
# Notice:
# - At the moment the script does not work with outputs that do not give 
# performance data (ex: some time related SNMP checks "SNMP OK - Timeticks: (427168862) 49 days, 10:34:48.62")
##################################################################################################


usage="Usage: ./check_snmp_inverter.sh -H <remote_host_IP> -C <comunity_string> -o <OID> -w <warning value> -c <critical value> -u <measure block> -l <label> \n\n The critical value is the real value that one needs to set for example 0 and the warning value is greater then the critical - ex: 240."
home=/usr/local/nagios/libexec

#getting the options list -> if not exit!
while getopts H:o:C:w:c:u:l? options;
do

	case $options in
		C ) community="$OPTARG";;
		H ) IP="$OPTARG";;
		o ) oid="$OPTARG";;
		w ) warning="$OPTARG";;
		c ) critical="$OPTARG";;
		u ) um="$OPTARG";;
		l ) label="$OPTARG";;
		? ) echo -e $usage
			exit 1;;#if we don't find the options
	esac

done

#check if the important options are set

if [ -z $IP ] || [ -z $community ] || [ -z $oid ] || [ -z $warning ] || [ -z $critical ]; 
then echo -e "$usage"
	exit 2
fi


snmp_check=`$home/check_snmp -H $IP -C $community -o $oid | cut -d "=" -f2 | cut -d " " -f1`

if [ "$snmp_check" -le "$critical" ];
							then echo "$label SNMP CRITICAL - $snmp_check $um | $oid=$snmp_check";
								exit 2;
							else if [ "$snmp_check" -le "$warning" ];
									then echo "$label SNMP WARNING - $snmp_check $um | $oid=$snmp_check";
										exit 1;
									else echo "$label SNMP OK - $snmp_check $um | $oid=$snmp_check";
										exit 0;
								fi
fi
