#!/bin/bash
# *************************************************************************
# NAME              : check_snmp_hp5900_mem.sh
# LANGUAGE          : Bash
# *************************************************************************
# AUTHOR           	: Gabriel MACHADO
# MAIL				: gabriel.machado@orange.fr
# VERSION          	: 1.0
# RELEASE DATE 		: 15/07/2015
# *************************************************************************
# DESCRIPTION      	: HP5900 SWITCH MEMORY MONITORING
#
#**************************************************************************

# Nagios variables
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

# Environment variables
HOST=$1
COMMUNITY=$2
USERNAME=$3
PASSWORD=$4
WARNING=$5
CRITICAL=$6
OID_CPU=.1.3.6.1.4.1.25506.2.6.1.1.1.1.6
OID_MEM=.1.3.6.1.4.1.25506.2.6.1.1.1.1.8
state=${STATE_OK}
oids_file="/var/lib/centreon/centplugins/cpu_oid_$HOST" # File containing cpus oid

if [ ! -s ${oids_file} ]
then
	snmpwalk -v 3  -c $COMMUNITY -u $USERNAME -A $PASSWORD $HOST ${OID_CPU} | grep -v "INTEGER: 0" | cut -f15 -d'.' | cut -d" " -f1 > ${oids_file}

	if [ $? -ne 0 ]; then
		echo "CRITICAL: Snmp request failed"
	exit ${STATE_CRITICAL}
	fi
fi



cpu_id=0
while read oid
do
	value=`snmpget -OEqv -v 3  -c $COMMUNITY -u $USERNAME -A $PASSWORD $HOST ${OID_MEM}.$oid`
	
	dataperf+="mem_${cpu_id}=${value}% "
	
	if [ $value -ge $CRITICAL ]
	then 
		msg+="CRITICAL : "
		state=${STATE_CRITICAL}
	else
		if [ $value -ge $WARNING -a $state -ne ${STATE_CRITICAL} ]
		then
			msg+="WARNING : "
			state=${STATE_WARNING}
		else
			msg+="OK : "
		fi
	fi
	msg+="mem_${cpu_id}=${value}% "
	cpu_id=`expr ${cpu_id} + 1`  
done<${oids_file}

output=$msg
output+='|'
output+=$dataperf
echo $output
exit $state
