#!/bin/bash
#
# Written By: Pierre SOURDEAU to AMF company [FRANCE]
# Created: 28/10/2008
# Description: Check SNMP v 1, 2c, 3 on ToIP gateway, and return :
# 	- number of total B-Channel 
# 	- number of B-Channel used
# 	- number of B-Channel free
#		=> warning or critical exit if B-Channel free is lower
#	- include graphic return
#
# Check succesfuly on Cisco 2811 and 2821
#
# License: This nagios plugin comes with ABSOLUTELY NO WARRANTY. You may redistribute copies of
# the plugins under the terms of the GNU General Public License. For more information about these 
# matters, see the GNU General Public License.
# Version: 2.3 (updated 09/04/2009)


############################################################
# Varialbles
############################################################
PROGNAME=$0
#`/bin/basename $0` #no basename on Mac OS X 10.3
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$PROGNAME: 2.3 $' | sed -e 's/[^0-9.]//g'`
SNMPGET="/usr/bin/snmpget";
SNMPWALK="/usr/bin/snmpwalk";

. $PROGPATH/utils.sh

USAGE="Usage: check_isdn_calls.sh -H hostname -V version -M bchannels [-w|-c|-C|-l|-u|-a|-d|-h]
### PARAMETERS ### 
-H Hostname (IP adresse or DNS name)
-V Version (1|2c|3)
-M Mode (bchannels)
### OPTIONNAL ###
-w Warning_Level (number of free channels) *** Use on bchannels mode ***
-c Critical_Level (number of free channels) *** Use on bchannels mode ***
-C Community (name) *** Use on Version 1|2 ***
-l Login (NoAuthNoPriv | AuthNoPriv | AuthPriv) *** Use on Version 3 ***
-u Username *** Use on Version 3 ***
-a Password *** Use on Version 3 ***
-d Debug mode
-h Help (print command usage, and quit)"

sum_value_Ok=0
sum_value_Warning=0
sum_value_Critical=0
sum_value_Unknwon=0

comm_final=""
walk_param=""

mib_num_B_channel=".1.3.6.1.2.1.10.20.1.3.2.1.6.1"
mib_cpmActiveDS0s=".1.3.6.1.4.1.9.10.19.1.1.4.0"
mib_cpmPPPCalls=".1.3.6.1.2.1.10.20.1.3.3.1.5.1"
mib_cpmV120Calls=".1.3.6.1.4.1.9.10.19.1.1.6.0"
mib_cpmV110Calls=".1.3.6.1.4.1.9.10.19.1.1.7.0"
mib_Used_B_channel_2=".1.3.6.1.4.1.9.10.19.1.1.3.0"


############################################################
# Global Functions
############################################################
# Check if value is numeric or not
check_num()
{
if ! let $1 2> /dev/null
then
	if [ $1 != 0 ]
	then
		echo "Error - Not numeric value : $2 = $1"
		exit $STATE_UNKNOWN
	fi
fi
}

# Check Parameters
check_param()
{
if [ "$hostname" == "" ] || [ "$version" == "" ] || [ "$mode" == "" ]
then
	echo "Error - Missing parameters - hostname = $hostname : version = $version : mode = $mode"
	echo "$USAGE"
	exit $STATE_UNKNOWN
fi

case $version in
		1 | 2c)
			if [ "$community" == "" ]
			then
				echo "Error - Missing parameters - community = $community"
				echo "$USAGE"
				exit $STATE_UNKNOWN
			fi
			walk_param="-v $version -c $community $hostname";;
		3) 
			if [ "$login" == "" ] || [ "$user" == "" ] || [ "$password" == "" ]
			then
				echo "Error - Missing parameters - login = $login : user = $user : password = $password"
				echo "$USAGE"
				exit $STATE_UNKNOWN
			fi
			walk_param="-v $version -l $login -u $user -A $password $hostname";;
esac
}

# Command used to debug
debug()
{
echo "*********************************************************************"
echo "### Mode B-Channels ###"
echo "Number of B-Channels : $num_B_channel"
echo "Number of used B-Channel(s) 1 - DS0 : $Used_B_channel_1"
echo "Number of used B-Channel(s) 2 - ISDN : $Used_B_channel_2"
echo "Number of used B-Channel(s) : $Used_B_channel (method = $Used_B_channel_meth)"
echo "Number of free B-Channel(s) : $Free_B_channel"
echo "cpmActiveDS0s : $cpmActiveDS0s"
echo "cpmPPPCalls : $cpmPPPCalls"
echo "cpmV120Calls : $cpmV120Calls"
echo "cpmV110Calls : $cpmV110Calls"
echo "### Global Values ###"
echo "Sum values : Ok = $sum_value_Ok ; Warning = $sum_value_Warning ; Critical = $sum_value_Critical ; Unknown = $sum_value_Unknwon"
echo "Nagios return : Ok = $STATE_OK ; Warning = $STATE_WARNING ; Critical = $STATE_CRITICAL ; Unknown = $STATE_UNKNOWN"
echo "Parameters : Hostname = $hostname ; Version = $version ; Community = $community ; Mode = $mode ; warning = $warning ; critical = $critical ; login = $login ; user = $user ; password = $password"
echo "*********************************************************************"
}

# Results sent to Nagios
function_exit()
{
if [ $sum_value_Unknwon != 0 ]
then
	echo "Unknown $comm_final"
	exit $STATE_UNKNOWN
fi

if [ $sum_value_Critical != 0 ]
then
	echo "Critical $comm_final"
	exit $STATE_CRITICAL
fi

if [ $sum_value_Warning != 0 ]
then
	echo "Warning $comm_final"
	exit $STATE_WARNING
fi

if [ $sum_value_Ok != 0 ]
then
	echo "OK $comm_final"
	exit $STATE_OK
fi
}


############################################################
# Mode BFree Functions
############################################################
# Results sent to Nagios
bchannels_sum()
{
if [ $Free_B_channel -gt $warning ]
then
	sum_value_Ok=$(( $sum_value_Ok + 1 ))
fi

if [ $Free_B_channel -le $warning ] && [ $Free_B_channel -gt $critical ]
then
	sum_value_Warning=$(( $sum_value_Warning + 1 ))
fi

if [ $Free_B_channel -le $critical ] && [ $Free_B_channel -ge 0 ]
then
	sum_value_Critical=$(( $sum_value_Critical + 1 ))
fi

if [ $Free_B_channel -gt $num_B_channel ] && [ $Free_B_channel -lt 0 ]
then
	sum_value_Unknwon=$(( $sum_value_Unknwon + 1 ))
fi

comm_final="$comm_final - $num_B_channel channels : $Free_B_channel free : $Used_B_channel used (method = $Used_B_channel_meth) | Max=$num_B_channel Current_Used=$Used_B_channel"
}

# The total number of B channels (bearer channels) managed by this signaling channel
isdnSignalingBchannelCount()
{
	num_B_channel=`$SNMPGET $walk_param $mib_num_B_channel | cut -d' ' -f4`
	check_num $num_B_channel num_B_channel
}

# The number of incoming and outgoing calls on this interface which were actually connected (additional method)
Used_B_channel_count_1()
{
	cpmActiveDS0s=`$SNMPGET $walk_param $mib_cpmActiveDS0s | cut -d' ' -f4`
	check_num $cpmActiveDS0s cpmActiveDS0s
	cpmPPPCalls=`$SNMPGET $walk_param $mib_cpmPPPCalls | cut -d' ' -f4`
	check_num $cpmPPPCalls cpmPPPCalls
	cpmV120Calls=`$SNMPGET $walk_param $mib_cpmV120Calls | cut -d' ' -f4`
	check_num $cpmV120Calls cpmV120Calls
	cpmV110Calls=`$SNMPGET $walk_param $mib_cpmV110Calls | cut -d' ' -f4`
	check_num $cpmV110Calls cpmV110Calls
	Used_B_channel_1=$(( ${cpmActiveDS0s} + ${cpmPPPCalls} + ${cpmV120Calls} + ${cpmV110Calls} ))
}

# The number of incoming and outgoing calls on this interface which were actually connected (direct method)
Used_B_channel_count_2()
{
	Used_B_channel_2=`$SNMPGET $walk_param $mib_Used_B_channel_2 | cut -d' ' -f4`
	check_num $Used_B_channel_2 Used_B_channel_2
}

# Check Used_B_channel_count_1 and Used_B_channel_count_2, return correct value
# $1 = min ; $2 = max ; $3 = Used_B_channel_1 ; $4 = Used_B_channel_2
Used_B_channel_count()
{
	if [ $3 -ge $1 ] && [ $3 -le $2 ] && [ $4 -ge $1 ] && [ $4 -le $2 ]
	then
		if [ $3 -ge $4 ]
		then
			Used_B_channel=$3
			Used_B_channel_meth="DS0"
		else
			Used_B_channel=$4
			Used_B_channel_meth="ISDN"
		fi
	fi
	
	if [ $3 -ge $1 ] && [ $3 -le $2 ]
	then
		Used_B_channel=$3
		Used_B_channel_meth="DS0"
	else
		if [ $4 -ge $1 ] && [ $4 -le $2 ]
		then
			Used_B_channel=$4
			Used_B_channel_meth="ISDN"
		else
			echo "Error - Free channels : $3 or $4	: min = $1 : max = $2"
			exit $STATE_UNKNOWN
		fi
	fi
}


############################################################
# Main Method
############################################################
main()
{
	# Get Options and check parameters
	while getopts H:V:C:M:w:c:l:u:a:dh option;
	do
		case $option in
			H) hostname=$OPTARG;;
			V) version=$OPTARG;;
			C) community=$OPTARG;;
			M) mode=$OPTARG;;
			w) warning=$OPTARG;;
			c) critical=$OPTARG;;
			l) login=$OPTARG;;
			u) user=$OPTARG;;
			a) password=$OPTARG;;
			d) debug=1;;
			h) echo "$USAGE"
			exit $STATE_UNKNOWN;;
		esac
	done

	check_param

	### Mode 1 - B-Channels ### 
	if [ "$mode" == "bchannels" ]
	then
		# Parameters Checking
		check_num $warning warning
		check_num $critical critical

		# Functions lunch
		isdnSignalingBchannelCount
		Used_B_channel_count_1
		Used_B_channel_count_2
		Used_B_channel_count 0 $num_B_channel $Used_B_channel_1 $Used_B_channel_2
		Free_B_channel=$(( ${num_B_channel} - ${Used_B_channel} ))
		bchannels_sum
	fi

	if [ "$debug" == 1 ]
	then
		debug
	fi

	function_exit
}

main $*

# Default exit
echo "Unknown - No result sent to nagios - End of script"
exit $STATE_UNKNOWN