#!/bin/bash
#David Waugh RSA
#This script will display the current maximum value of the sessionsBehind for any aggregating source of the ESA

E_OK=0
E_WARNING=1
E_CRITICAL=2
E_UNKNOWN=3


#Get the stream names
Perfdata=""
Output=""

   behind=$(echo -e "cd /Workflow/Source/nextgenAggregationSource\ndump\nexit" | /opt/rsa/esa/client/bin/esa-client |grep sessionsBehind | cut -d ":" -f 2 |cut -d " " -f 2 |cut -d "," -f 1 |sort -nr |head -n 1)
   Output+="Sessions behind: "
   Output+=": "
   Output+=$behind
   Output+=" "
   Perfdata+="Sessions_behind"
   Perfdata+=\=
   Perfdata+=$behind
   Perfdata+=""

#echo $Output\|$Perfdata\;\;
#exit 0

show_help() {
        echo "$0 -w VALUE -c VALUE | -h"
        echo
        echo "This plug-in is used to be alerted when maximum ESA behind sessions is reached"
        echo
        echo "  -w/c Sessions behind integer"
	echo " To warn when 200 sessions behind and critical when 300 sessions behind"
        echo " example: $0  -w 200 -c 300"
}

# process args
while [ ! -z "$1" ]; do
        case $1 in
                -w)     shift; WARNING=$1 ;;
                -c)     shift; CRITICAL=$1 ;;
                -h)     show_help; exit 1 ;;
        esac
        shift
done

# check input parameters so we can continue !
sanitize() {

        # check thresholds
        if [ -z "$WARNING" ]; then
		WARNING=500
                #echo "Need warning threshold"
                #exit $E_UNKNOWN
        fi
        if [ -z "$CRITICAL" ]; then
		CRITICAL=1000
                #echo "Need critical threshold"
                #exit $E_UNKNOWN
        fi
}
# check args
sanitize

OUTPUT=""
EXITCODE=$E_OK
# check behind
if [ $behind -gt $WARNING ]; then
        if [ $behind -gt $CRITICAL ]; then
                OUTPUT="CRITICAL behind (>$CRITICAL), "
                EXITCODE=$E_CRITICAL
        else
                OUTPUT="WARNING behind (>$WARNING), "
                EXITCODE=$E_WARNING
	fi

else
	OUTPUT="OK"
fi


echo "${OUTPUT}  ${Output}|${Perfdata};;;;"
exit $EXITCODE
