#!/bin/bash
#
# Written By: Pierre SOURDEAU to AMF company [FRANCE]
# Created: 23/04/2009
# Description: Check SNMP v 1, 2c, 3 for Packeteer PS, and return :
### Mode 1 - PsInfo ###
# 	- 8 system checks : ShapingStatusAdmin, ShapingStatusOper, SiteRouter, PowerSystem, Fan, ConfigFile, MeasurementEngine, StandbyStatus
#
### Mode 2 - partitionsize-1-min ###
# 	- return partition min size for inbound
# 	- select scale (octet|kilo|mega|giga)
#
### Mode 3 - partitionsize-1-max ###
# 	- return partition max size for inbound (burst)
# 	- select scale (octet|kilo|mega|giga)
#
### Mode 4 - partitionsize-2-min ###
# 	- return partition min size for outbound
# 	- select scale (octet|kilo|mega|giga)
#
### Mode 5 - partitionsize-2-max ###
# 	- return partition max size for outbound (burst)
# 	- select scale (octet|kilo|mega|giga)
#
### Mode 6 - class ###
# 	- check specific class (% used on link, SYN Flood attacks blocked, TCP data packet and % of retransmitted packets)
# 	- check HTTP response messages in option for specific class
# 	- list all class if you don't know name of specific class
#
# Each mode included graphic return to Centreon application
#
# Check succesfuly on PacketShaper� 1700 
#
# 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.0(updated 19/12/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_snmp_packeteer_ps.sh -H hostname -V version -M mode [-w|-c|-C|-l|-u|-a|-s|-n|-r|-d|-h]
### PARAMETERS ### 
-H <Hostname> (IP adresse or DNS name)
-V <Version> (1|2c|3)
-M <Mode> (psinfo|partitionsize-1-min|partitionsize-1-max|partitionsize-2-min|partitionsize-2-max|class) 1=inbound, 2=outbound
### OPTIONNAL ###
-w <Warning_Level> *** Use on partitionsize-* or class mode ***
-c <Critical_Level> *** Use on partitionsize-* or class 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 ***
-s <Scale> (octet|kilo|mega|giga-number) - example kilo-2 return 16,14kilo to 16142octets *** Use on partitionsize-* mode ***
-n <ClassFullName> *** Use on class mode ***
-r Check HTTP errors *** Use on class mode ***
-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=""
partitionsize_com=""
walk_param=""
debug=0
sum_power=0
sum_fan=0
nb_partitions=0
http_ckeck=0
min_packets=10000

# MIB - PS info
mib_psShapingStatusAdmin=SNMPv2-SMI::enterprises.2334.2.1.6.4.0
mib_psShapingStatusOper=SNMPv2-SMI::enterprises.2334.2.1.1.1.0
mib_psAlarmSiteRouterStatus=SNMPv2-SMI::enterprises.2334.2.1.5.6.0
mib_psAlarmPowerSystemOneStatus=SNMPv2-SMI::enterprises.2334.2.1.5.8.0
mib_psAlarmPowerSystemTwoStatus=SNMPv2-SMI::enterprises.2334.2.1.5.10.0
mib_psAlarmFanOneStatus=SNMPv2-SMI::enterprises.2334.2.1.5.12.0
mib_psAlarmFanTwoStatus=SNMPv2-SMI::enterprises.2334.2.1.5.14.0
mib_psAlarmFanThreeStatus=SNMPv2-SMI::enterprises.2334.2.1.5.22.0
mib_psAlarmFanFourStatus=SNMPv2-SMI::enterprises.2334.2.1.5.24.0
mib_psAlarmCfgFileStatus=SNMPv2-SMI::enterprises.2334.2.1.5.16.0
mib_psAlarmMeasurementEngineStatus=SNMPv2-SMI::enterprises.2334.2.1.5.18.0
mib_psAlarmStandbyStatus=SNMPv2-SMI::enterprises.2334.2.1.5.20.0

# Values - PS info
psShapingStatusAdmin_value=([1]=on [2]=off [3]=bypass [4]=other [5]=watch)
psShapingStatusOper_value=([1]=ok [2]=not-ok)
psAlarmSiteRouterStatus_value=([1]=ready [2]=gone-no_arp_received_by_router [3]=error-inside [4]=none_router_configured)
psAlarmPowerSystemStatus_value=([1]=ok [2]=not-ok [3]=non-existant)
psAlarmFanStatus_value=([0]=non-existant [1]=ok [2]=not-ok [3]=non-existant)
psAlarmCfgFileStatus_value=([1]=valid [2]=corrupted)
psAlarmMeasurementEngineStatus_value=([1]=initiallyStarting [2]=starting [3]=running [4]=errorOnVolume [5]=disabled [6]=waitToStart [7]=suspended [8]=backup [9]=restore)
psAlarmStandbyStatus_value=([1]=not-configured [2]=active [3]=passive [4]=down [5]=direct [6]=disconnected [7]=leak [8]=disable)

# MIB - PartitionsSize
mib_partitionIndex=SNMPv2-SMI::enterprises.2334.2.1.3.2.1.1
mib_partitionName=SNMPv2-SMI::enterprises.2334.2.1.3.2.1.2
mib_partitionLinkDirection=SNMPv2-SMI::enterprises.2334.2.1.3.2.1.3
mib_partitionMinimumBps=SNMPv2-SMI::enterprises.2334.2.1.3.2.1.24
mib_partitionBurstLimit=SNMPv2-SMI::enterprises.2334.2.1.3.2.1.25
mib_partitionRate=SNMPv2-SMI::enterprises.2334.2.1.3.2.1.26

# Values - PartitionsSize
partitionLinkDirection_value=([1]="inbound" [2]="outbound")

# MIB - Class
mib_classIndex=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.1
mib_className=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.2
mib_classLinkDirection=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.4
mib_classDataPkts=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.8
mib_classReTxs=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.9
mib_classReTxTosses=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.10
mib_classPeakTcpConns=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.14
mib_classHits=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.25
mib_classRate=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.27
mib_classPeakGuaranteedRateFlows=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.29
mib_classFullName=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.36
mib_classCurrentRate=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.37
mib_classClientFloodBlock=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.41
mib_classServerFloodBlock=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.42
mib_classWebResponse2XX=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.48
mib_classWebResponse3XX=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.49
mib_classWebResponse4XX=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.50
mib_classWebResponse5XX=SNMPv2-SMI::enterprises.2334.2.1.4.2.1.51
mib_linksize=SNMPv2-SMI::enterprises.2334.2.1.2.2.1.23

# Values - Class
classLinkDirection_value=([1]="inbound" [2]="outbound")

############################################################
# 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 "*********************************************************************"
	case $mode in
	psinfo)
		echo "### Mode psinfo ###"
		echo "psShapingStatusAdmin = $psShapingStatusAdmin_status ($psShapingStatusAdmin_status_trans)"
		echo "psShapingStatusOper = $psShapingStatusOper_status ($psShapingStatusOper_status_trans)"
		echo "psAlarmSiteRouterStatus = $psAlarmSiteRouterStatus_status ($psAlarmSiteRouterStatus_status_trans)"
		echo "Number of PowerSystem = $sum_power"
		echo "psAlarmPowerSystemOneStatus = $psAlarmPowerSystemOneStatus_status ($psAlarmPowerSystemOneStatus_status_trans)"
		echo "psAlarmPowerSystemTwoStatus = $psAlarmPowerSystemTwoStatus_status ($psAlarmPowerSystemTwoStatus_status_trans)"
		echo "Number of Fan = $sum_fan"
		echo "psAlarmFanOneStatus = $psAlarmFanOneStatus_status ($psAlarmFanOneStatus_status_trans)"
		echo "psAlarmFanTwoStatus = $psAlarmFanTwoStatus_status ($psAlarmFanTwoStatus_status_trans)"
		echo "psAlarmFanThreeStatus = $psAlarmFanThreeStatus_status ($psAlarmFanThreeStatus_status_trans)"
		echo "psAlarmFanFourStatus = $psAlarmFanFourStatus_status ($psAlarmFanFourStatus_status_trans)"
		echo "psAlarmCfgFileStatus = $psAlarmCfgFileStatus_status ($psAlarmCfgFileStatus_status_trans)"
		echo "psAlarmMeasurementEngineStatus = $psAlarmMeasurementEngineStatus_status ($psAlarmMeasurementEngineStatus_status_trans)"
		echo "psAlarmStandbyStatus = $psAlarmStandbyStatus_status ($psAlarmStandbyStatus_status_trans)"
		;;
		
	class)
		echo "### Mode class ###"
		echo "ID = ${classtableau[$classid,0]} : FullName = ${classtableau[$classid,1]} : LinkDir = ${classtableau[$classid,2]} (${classtableau[$classid,3]}) : Rate = ${classtableau[$classid,4]} / ${classtableau[$classid,5]} (${classtableau[$classid,6]}%) : SYN Flood attacks blocked = ${classtableau[$classid,9]} (${classtableau[$classid,7]} client side + ${classtableau[$classid,8]} server side) : TCP data packet = ${classtableau[$classid,10]} (${classtableau[$classid,11]} retransmit + ${classtableau[$classid,12]} tossed retransmit = ${classtableau[$classid,13]} (${classtableau[$classid,14]}%)) : HTTP response messages = ${classtableau[$classid,15]}(2xx) + ${classtableau[$classid,16]}(3xx) + ${classtableau[$classid,17]}(4xx) + ${classtableau[$classid,18]}(5xx) => ${classtableau[$classid,20]}% on error" 
		;;
	esac
	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 ; devicetype = $devicetype ; login = $login ; user = $user ; password = $password"
	echo "*********************************************************************"
}


converter_octet()
{
	diviseur_name=`echo $2 | cut -d '-' -f1`
	virgule=`echo $2 | cut -d '-' -f2`
	check_num $virgule virgule_on_scale_parameter
	case $diviseur_name in
		octet)
			diviseur_val=1
			unit=o;;
		kilo)
			diviseur_val=1000
			unit=Ko;;
		mega)
			diviseur_val=1000000
			unit=Mo;;
		giga)
			diviseur_val=1000000000
			unit=Go;;
		*)
			echo "Error - Invalid parameter - scale = $scale"
			echo "$USAGE"
			exit $STATE_UNKNOWN;;
	esac
	
	result=""
	result=`echo "scale=$virgule; $1/$diviseur_val" | bc | sed 's/^[.*]/0./'`
}


function_class-fullname-to-id()
{
	classid=""
	classid=`$SNMPWALK $walk_param $mib_classFullName | egrep "\"$1\"" | sed -e "s/$mib_classFullName.//g" | cut -d ' ' -f1`
	if [ "$classid" == "" ]
	then
		echo "Error - ClassFullName do not exist : $1 - List of existing ClassFullName :"
		$SNMPWALK $walk_param $mib_classFullName | cut -d ' ' -f4 | sed -e "s/\"//g"
		exit $STATE_UNKNOWN
	fi
}


# 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 PSinfo Functions
############################################################
# Status
psinfo_status()
{
	psShapingStatusAdmin_status=`$SNMPGET $walk_param $mib_psShapingStatusAdmin | cut -d' ' -f4`
	check_num $psShapingStatusAdmin_status psShapingStatusAdmin_status
	psShapingStatusAdmin_status_trans=${psShapingStatusAdmin_value[$psShapingStatusAdmin_status]}
	
	psShapingStatusOper_status=`$SNMPGET $walk_param $mib_psShapingStatusOper | cut -d' ' -f4`
	check_num $psShapingStatusOper_status psShapingStatusOper_status
	psShapingStatusOper_status_trans=${psShapingStatusOper_value[$psShapingStatusOper_status]}
	
	psAlarmSiteRouterStatus_status=`$SNMPGET $walk_param $mib_psAlarmSiteRouterStatus | cut -d' ' -f4`
	check_num $psAlarmSiteRouterStatus_status psAlarmSiteRouterStatus_status
	psAlarmSiteRouterStatus_status_trans=${psAlarmSiteRouterStatus_value[$psAlarmSiteRouterStatus_status]}
	
	psAlarmPowerSystemOneStatus_status=`$SNMPGET $walk_param $mib_psAlarmPowerSystemOneStatus | cut -d' ' -f4`
	check_num $psAlarmPowerSystemOneStatus_status psAlarmPowerSystemOneStatus_status
	psAlarmPowerSystemOneStatus_status_trans=${psAlarmPowerSystemStatus_value[$psAlarmPowerSystemOneStatus_status]}
	
	psAlarmPowerSystemTwoStatus_status=`$SNMPGET $walk_param $mib_psAlarmPowerSystemTwoStatus | cut -d' ' -f4`
	check_num $psAlarmPowerSystemTwoStatus_status psAlarmPowerSystemTwoStatus_status
	psAlarmPowerSystemTwoStatus_status_trans=${psAlarmPowerSystemStatus_value[$psAlarmPowerSystemTwoStatus_status]}
	
	psAlarmFanOneStatus_status=`$SNMPGET $walk_param $mib_psAlarmFanOneStatus | cut -d' ' -f4`
	check_num $psAlarmFanOneStatus_status psAlarmFanOneStatus_status
	psAlarmFanOneStatus_status_trans=${psAlarmFanStatus_value[$psAlarmFanOneStatus_status]}
	
	psAlarmFanTwoStatus_status=`$SNMPGET $walk_param $mib_psAlarmFanTwoStatus | cut -d' ' -f4`
	check_num $psAlarmFanTwoStatus_status psAlarmFanTwoStatus_status
	psAlarmFanTwoStatus_status_trans=${psAlarmFanStatus_value[$psAlarmFanTwoStatus_status]}
	
	psAlarmFanThreeStatus_status=`$SNMPGET $walk_param $mib_psAlarmFanThreeStatus | cut -d' ' -f4`
	check_num $psAlarmFanThreeStatus_status psAlarmFanThreeStatus_status
	psAlarmFanThreeStatus_status_trans=${psAlarmFanStatus_value[$psAlarmFanThreeStatus_status]}
	
	psAlarmFanFourStatus_status=`$SNMPGET $walk_param $mib_psAlarmFanFourStatus | cut -d' ' -f4`
	check_num $psAlarmFanFourStatus_status psAlarmFanFourStatus_status
	psAlarmFanFourStatus_status_trans=${psAlarmFanStatus_value[$psAlarmFanFourStatus_status]}
	
	psAlarmCfgFileStatus_status=`$SNMPGET $walk_param $mib_psAlarmCfgFileStatus | cut -d' ' -f4`
	check_num $psAlarmCfgFileStatus_status psAlarmCfgFileStatus_status
	psAlarmCfgFileStatus_status_trans=${psAlarmCfgFileStatus_value[$psAlarmCfgFileStatus_status]}

	psAlarmMeasurementEngineStatus_status=`$SNMPGET $walk_param $mib_psAlarmMeasurementEngineStatus | cut -d' ' -f4`
	check_num $psAlarmMeasurementEngineStatus_status psAlarmMeasurementEngineStatus_status
	psAlarmMeasurementEngineStatus_status_trans=${psAlarmMeasurementEngineStatus_value[$psAlarmMeasurementEngineStatus_status]}
	
	psAlarmStandbyStatus_status=`$SNMPGET $walk_param $mib_psAlarmStandbyStatus | cut -d' ' -f4`
	check_num $psAlarmStandbyStatus_status psAlarmStandbyStatus_status
	psAlarmStandbyStatus_status_trans=${psAlarmStandbyStatus_value[$psAlarmStandbyStatus_status]}
}

# Sum
psinfo_sum()
{
case $psShapingStatusAdmin_status in
	1)
		case $psShapingStatusOper_status in
			1)
				sum_value_Ok=$(( $sum_value_Ok + 1 ))
				;;
			2)
				sum_value_Critical=$(( $sum_value_Critical + 1 ))
				;;
			*)
				sum_value_Unknwon=$(( $sum_value_Unknwon + 1 ))
				;;
		esac
		;;
	2 | 3 | 5)
		sum_value_Warning=$(( $sum_value_Warning + 1 ))
		;;
	*)
		sum_value_Unknwon=$(( $sum_value_Unknwon + 1 ))
		;;
esac


case $psAlarmSiteRouterStatus_status in
	1)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		;;
	4)
		sum_value_Warning=$(( $sum_value_Warning + 1 ))
		;;
	2 | 3)
		sum_value_Critical=$(( $sum_value_Critical + 1 ))
		;;
	*)
		sum_value_Unknwon=$(( $sum_value_Unknwon + 1 ))
		;;
esac


case $psAlarmPowerSystemOneStatus_status in
	1)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		sum_power=$(( $sum_power + 1 ))
		;;
	3)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		;;
	2)
		sum_value_Critical=$(( $sum_value_Critical + 1 ))
		;;
	*)
		sum_value_Unknwon=$(( $sum_value_Unknwon + 1 ))
		;;
esac

case $psAlarmPowerSystemTwoStatus_status in
	1)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		sum_power=$(( $sum_power + 1 ))
		;;
	3)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		;;
	2)
		sum_value_Critical=$(( $sum_value_Critical + 1 ))
		;;
	*)
		sum_value_Unknwon=$(( $sum_value_Unknwon + 1 ))
		;;
esac


case $psAlarmFanOneStatus_status in
	1)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		sum_fan=$(( $sum_fan + 1 ))
		;;
	0 | 3)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		;;
	2)
		sum_value_Critical=$(( $sum_value_Critical + 1 ))
		;;
	*)
		sum_value_Unknwon=$(( $sum_value_Unknwon + 1 ))
		;;
esac


case $psAlarmFanTwoStatus_status in
	1)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		sum_fan=$(( $sum_fan + 1 ))
		;;
	0 | 3)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		;;
	2)
		sum_value_Critical=$(( $sum_value_Critical + 1 ))
		;;
	*)
		sum_value_Unknwon=$(( $sum_value_Unknwon + 1 ))
		;;
esac


case $psAlarmFanThreeStatus_status in
	1)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		sum_fan=$(( $sum_fan + 1 ))
		;;
	0 | 3)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		;;
	2)
		sum_value_Critical=$(( $sum_value_Critical + 1 ))
		;;
	*)
		sum_value_Unknwon=$(( $sum_value_Unknwon + 1 ))
		;;
esac


case $psAlarmFanFourStatus_status in
	1)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		sum_fan=$(( $sum_fan + 1 ))
		;;
	0 | 3)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		;;
	2)
		sum_value_Critical=$(( $sum_value_Critical + 1 ))
		;;
	*)
		sum_value_Unknwon=$(( $sum_value_Unknwon + 1 ))
		;;
esac


case $psAlarmCfgFileStatus_status in
	1)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		;;
	2)
		sum_value_Critical=$(( $sum_value_Critical + 1 ))
		;;
	*)
		sum_value_Unknwon=$(( $sum_value_Unknwon + 1 ))
		;;
esac


case $psAlarmMeasurementEngineStatus_status in
	3 | 8)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		;;
	1 | 2 | 6)
		sum_value_Warning=$(( $sum_value_Warning + 1 ))
		;;
	4 | 5 | 7 | 9)
		sum_value_Critical=$(( $sum_value_Critical + 1 ))
		;;
	*)
		sum_value_Unknwon=$(( $sum_value_Unknwon + 1 ))
		;;
esac


case $psAlarmStandbyStatus_status in
	1 | 2 | 3 | 5 | 8)
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
		;;
	4 | 6 | 7)
		sum_value_Critical=$(( $sum_value_Critical + 1 ))
		;;
	*)
		sum_value_Unknwon=$(( $sum_value_Unknwon + 1 ))
		;;
esac


comm_final="$comm_final - ShapingStatusAdmin $psShapingStatusAdmin_status_trans - ShapingStatusOper $psShapingStatusOper_status_trans - SiteRouter $psAlarmSiteRouterStatus_status_trans - $sum_power PowerSystem ($psAlarmPowerSystemOneStatus_status_trans,$psAlarmPowerSystemTwoStatus_status_trans) - $sum_fan Fan ($psAlarmFanOneStatus_status_trans,$psAlarmFanTwoStatus_status_trans,$psAlarmFanThreeStatus_status_trans,$psAlarmFanFourStatus_status_trans) - ConfigFile $psAlarmCfgFileStatus_status_trans - MeasurementEngine $psAlarmMeasurementEngineStatus_status_trans - StandbyStatus $psAlarmStandbyStatus_status_trans | Ok=$sum_value_Ok"
}



############################################################
# Mode PartitionSize Functions
############################################################
# Status
partitionsize_status()
{
	while read line
	do
		nb_partitions=$(( $nb_partitions + 1 ))
		i=`echo $line | cut -d ' ' -f1`
		
		partitiontableau[$nb_partitions,0]=0
		partitiontableau[$nb_partitions,1]=0
		partitiontableau[$nb_partitions,2]=0
		partitiontableau[$nb_partitions,3]=0
		partitiontableau[$nb_partitions,4]=0
		
		partitiontableau[$nb_partitions,0]=`$SNMPGET $walk_param $mib_partitionLinkDirection.$i | cut -d' ' -f4`
		check_num ${partitiontableau[$nb_partitions,0]} "partitiontableau[$nb_partitions,0]"
				
		if [ "${partitiontableau[$nb_partitions,0]}" == $choiseinout ] && [ "$i" != 1 ] && [ "$i" != 2 ]
		then
			partitiontableau[$nb_partitions,1]=$i
			check_num ${partitiontableau[$nb_partitions,1]} partitiontableau[$nb_partitions,1]
		
			partitiontableau[$nb_partitions,2]=`$SNMPGET $walk_param $mib_partitionName.$i | cut -d' ' -f4 | sed -e "s/\"//g"`
			
			if [ $choiseminmax == "min" ]
			then
				partitiontableau[$nb_partitions,3]=`$SNMPGET $walk_param $mib_partitionMinimumBps.$i | cut -d' ' -f4`
				check_num ${partitiontableau[$nb_partitions,3]} "partitiontableau[$nb_partitions,3]"
				converter_octet ${partitiontableau[$nb_partitions,3]} $scale
				partitiontableau[$nb_partitions,3]=$result
				partitionsize_com="$partitionsize_com${partitiontableau[$nb_partitions,2]}=${partitiontableau[$nb_partitions,3]} "
			fi
			
			if [ $choiseminmax == "max" ]
			then
				partitiontableau[$nb_partitions,4]=`$SNMPGET $walk_param $mib_partitionBurstLimit.$i | cut -d' ' -f4`
				check_num ${partitiontableau[$nb_partitions,4]} "partitiontableau[$nb_partitions,4]"
				converter_octet ${partitiontableau[$nb_partitions,4]} $scale
				partitiontableau[$nb_partitions,4]=$result
				partitionsize_com="$partitionsize_com${partitiontableau[$nb_partitions,2]}=${partitiontableau[$nb_partitions,4]} "
			fi
		fi
	done <<-EOF
	$($SNMPWALK $walk_param $mib_partitionIndex | cut -d ' ' -f4)
	EOF
}


# Sum
partitionsize_sum()
{
	converter_octet 999 $scale
	comm_final="$comm_final - ${partitionLinkDirection_value[$choiseinout]} check $choiseminmax partition size - Unit in $unit - $partitionsize_com | $partitionsize_com"

	if [ "$partitionsize_com" != "" ] && [ "$nb_partitions" != 0 ]
	then
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
	else
		sum_value_Unknwon=$(( $sum_value_Unknwon + 1 ))
	fi
}


############################################################
# Mode class Functions
############################################################
# Status
class_status()
{
	function_class-fullname-to-id $1
	
	# Initialize classtableau
	j=0
	while [ $j -le 20 ]
	do
		classtableau[$classid,$j]=""
		j=$(( $j + 1 ))
	done
	
	# A unique ID for a traffic class
	classtableau[$classid,0]=$classid
	
	# The complete name of the class
	classtableau[$classid,1]=$1
	
	# The orientation of a class (value)
	classtableau[$classid,2]=`$SNMPGET $walk_param $mib_classLinkDirection.$classid | cut -d' ' -f4`
	check_num ${classtableau[$classid,2]} "classtableau[$classid,2]"
	
	# The orientation of a class (name)
	classtableau[$classid,3]=${classLinkDirection_value[${classtableau[$classid,2]}]}
	
	# The current rate in bits per second
	classtableau[$classid,4]=`$SNMPGET $walk_param $mib_classCurrentRate.$classid | cut -d' ' -f4`
	check_num ${classtableau[$classid,4]} "classtableau[$classid,4]"
	
	# The linksize (bit/s)
	classtableau[$classid,5]=`$SNMPGET $walk_param $mib_linksize.${classtableau[$classid,2]} | cut -d' ' -f4`
	check_num ${classtableau[$classid,5]} "classtableau[$classid,5]"
	
	# % used by class on the link
	if [ ${classtableau[$classid,5]} != 0 ]
	then
		classtableau[$classid,6]=`echo "scale=0; 100*${classtableau[$classid,4]}/${classtableau[$classid,5]}" | bc | sed 's/^[.*]/0./'`
	else 
		classtableau[$classid,6]=0
	fi
	
	# The total number of client side SYN flood attacks blocked
	classtableau[$classid,7]=`$SNMPGET $walk_param $mib_classClientFloodBlock.$classid | cut -d' ' -f4`
	check_num ${classtableau[$classid,7]} "classtableau[$classid,7]"
	
	# The total number of server side SYN flood attacks blocked
	classtableau[$classid,8]=`$SNMPGET $walk_param $mib_classServerFloodBlock.$classid | cut -d' ' -f4`
	check_num ${classtableau[$classid,8]} "classtableau[$classid,8]"
	
	 # The total number of server + client side SYN flood attacks blocked
	classtableau[$classid,9]=$(( ${classtableau[$classid,7]} + ${classtableau[$classid,8]} ))
	check_num ${classtableau[$classid,9]} "classtableau[$classid,9]"
	
	# The TCP data packet count in the class
	classtableau[$classid,10]=`$SNMPGET $walk_param $mib_classDataPkts.$classid | cut -d' ' -f4`
	check_num ${classtableau[$classid,10]} "classtableau[$classid,10]"
	
	# The TCP retransmit packet count in the class
	classtableau[$classid,11]=`$SNMPGET $walk_param $mib_classReTxs.$classid | cut -d' ' -f4`
	check_num ${classtableau[$classid,11]} "classtableau[$classid,11]"
	
	# The TCP tossed retransmit packet count in the class
	classtableau[$classid,12]=`$SNMPGET $walk_param $mib_classReTxTosses.$classid | cut -d' ' -f4`
	check_num ${classtableau[$classid,12]} "classtableau[$classid,12]"
	
	 # Total of TCP retransmit packet count in the class
	classtableau[$classid,13]=$(( ${classtableau[$classid,11]} + ${classtableau[$classid,12]} ))
	check_num ${classtableau[$classid,13]} "classtableau[$classid,13]"
	
	# % of TCP retransmit packet count in the class
	if [ ${classtableau[$classid,10]} != 0 ] && [ ${classtableau[$classid,13]} -ge $min_packets ]
	then
		classtableau[$classid,14]=`echo "scale=0; 100*${classtableau[$classid,13]}/${classtableau[$classid,10]}" | bc | sed 's/^[.*]/0./'`
	else 
		classtableau[$classid,14]=0
	fi
	
	
	if [ "$http_ckeck" == 1 ]
	then
		# The count of HTTP response messages having 2XX status codes (successful)
		classtableau[$classid,15]=`$SNMPGET $walk_param $mib_classWebResponse2XX.$classid | cut -d' ' -f4`
		check_num ${classtableau[$classid,15]} "classtableau[$classid,15]"
		
		# The count of HTTP response messages having 3XX status codes (redirection)
		classtableau[$classid,16]=`$SNMPGET $walk_param $mib_classWebResponse3XX.$classid | cut -d' ' -f4`
		check_num ${classtableau[$classid,16]} "classtableau[$classid,16]"
		
		# The count of HTTP response messages having 4XX status codes (client error)
		classtableau[$classid,17]=`$SNMPGET $walk_param $mib_classWebResponse4XX.$classid | cut -d' ' -f4`
		check_num ${classtableau[$classid,17]} "classtableau[$classid,17]"
		
		# The count of HTTP response messages having 5XX status codes (server error)
		classtableau[$classid,18]=`$SNMPGET $walk_param $mib_classWebResponse5XX.$classid | cut -d' ' -f4`
		check_num ${classtableau[$classid,18]} "classtableau[$classid,18]"
		
		# Total of HTTP response messages having 4XX or 5XXX status codes (client or server error)
		classtableau[$classid,19]=$(( ${classtableau[$classid,17]} + ${classtableau[$classid,18]} ))
		check_num ${classtableau[$classid,19]} "classtableau[$classid,19]"
		
		# % of HTTP response messages having 4XX or 5XXX status codes (client or server error)
		if [ ${classtableau[$classid,15]} != 0 ]
		then
			classtableau[$classid,20]=`echo "scale=0; 100*${classtableau[$classid,19]}/${classtableau[$classid,15]}" | bc | sed 's/^[.*]/0./'`
		else 
			classtableau[$classid,20]=0
		fi
		
	fi
}


class_sum()
{
	warning_inv=$(( 100 - $critical ))
	critical_inv=$(( 100 - $warning ))
	
	# Current rate - sum
	if [ ${classtableau[$classid,6]} -lt $warning ]
	then
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
	fi

	if [ ${classtableau[$classid,6]} -ge $warning ] && [ ${classtableau[$classid,6]} -lt $critical ]
	then
		sum_value_Warning=$(( $sum_value_Warning + 1 ))
	fi

	if [ ${classtableau[$classid,6]} -ge $critical ]
	then
		sum_value_Critical=$(( $sum_value_Critical + 1 ))
	fi
	
	
	# SYN attack bloqued - sum
	if [ ${classtableau[$classid,9]} != 0 ]
	then
		sum_value_Warning=$(( $sum_value_Warning + 1 ))
	fi
	
	# TCP retransmit packet - sum
	if [ ${classtableau[$classid,14]} -lt $warning_inv ]
	then
		sum_value_Ok=$(( $sum_value_Ok + 1 ))
	fi

	if [ ${classtableau[$classid,14]} -ge $warning_inv ] && [ ${classtableau[$classid,14]} -lt $critical_inv ]
	then
		sum_value_Warning=$(( $sum_value_Warning + 1 ))
	fi

	if [ ${classtableau[$classid,14]} -ge $critical_inv ]
	then
		sum_value_Critical=$(( $sum_value_Critical + 1 ))
	fi
	
	# HTTP response messages having 4XX or 5XXX status codes (client or server error) - sum
	if [ "$http_ckeck" == 1 ]
	then
		if [ ${classtableau[$classid,20]} -lt $warning_inv ]
		then
			sum_value_Ok=$(( $sum_value_Ok + 1 ))
		fi

		if [ ${classtableau[$classid,20]} -ge $warning_inv ] && [ ${classtableau[$classid,20]} -lt $critical_inv ]
		then
			sum_value_Warning=$(( $sum_value_Warning + 1 ))
		fi

		if [ ${classtableau[$classid,20]} -ge $critical_inv ]
		then
			sum_value_Critical=$(( $sum_value_Critical + 1 ))
		fi
	fi
	
	
	if [ "$http_ckeck" == 1 ]
	then
		comm_final="$comm_final - Class ${classtableau[$classid,1]} use ${classtableau[$classid,6]}% on link : SYN Flood attacks blocked = ${classtableau[$classid,7]} (${classtableau[$classid,7]} client side + ${classtableau[$classid,8]} server side) : TCP data packet = ${classtableau[$classid,10]} (${classtableau[$classid,14]}% retransmitted) : HTTP response messages = ${classtableau[$classid,15]}(2xx) + ${classtableau[$classid,16]}(3xx) + ${classtableau[$classid,17]}(4xx) + ${classtableau[$classid,18]}(5xx) => ${classtableau[$classid,20]}% on error | Current_Rate=${classtableau[$classid,4]} Max=${classtableau[$classid,5]}"
	else
		comm_final="$comm_final - Class ${classtableau[$classid,1]} use ${classtableau[$classid,6]}% on link : SYN Flood attacks blocked = ${classtableau[$classid,7]} (${classtableau[$classid,7]} client side + ${classtableau[$classid,8]} server side) : TCP data packet = ${classtableau[$classid,10]} (${classtableau[$classid,14]}% retransmitted) | Current_Rate=${classtableau[$classid,4]} Max=${classtableau[$classid,5]}"
	fi
}

############################################################
# Main Method
############################################################
main()
{
	# Get Options and check parameters
	while getopts H:V:C:M:w:c:l:u:a:s:n:rdh 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;;
			s) scale=$OPTARG;;
			n) classfullname=$OPTARG;;
			r) http_ckeck=1;;
			d) debug=1;;
			h) echo "$USAGE"
			exit $STATE_UNKNOWN;;
		esac
	done

	check_param


	case $mode in
		psinfo)
			psinfo_status
			psinfo_sum
			;;
		partitionsize-1-min | partitionsize-1-max | partitionsize-2-min | partitionsize-2-max)
			choiseinout=`echo $mode | cut -d '-' -f2`
			choiseminmax=`echo $mode | cut -d '-' -f3`
			check_num $choiseinout mode_partition-x
			partitionsize_status
			partitionsize_sum
			;;
		class)
			# Parameters Checking
			check_num $warning warning
			check_num $critical critical
			
			class_status $classfullname
			class_sum
			;;
		*)
			echo "Error - Invalid parameter - mode = $mode"
			echo "$USAGE"
			exit $STATE_UNKNOWN
			;;
	esac
	
	if [ "$debug" == 1 ]
	then
		debug
	fi

	function_exit
}

main $*

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