#!/bin/bash
# Script to check total processes
#
#!/bin/bash

critical=-1
warning=-1
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

function usage() {
	cat <<EOU
`basename $0` -w|--warning <warninglevel> -c|--critical <criticallevel>

where
    <warninglevel> is the number of process at which the check returns warning state
    <criticallevel> is the number of process at which the check returns critical state
EOU
}

# Ensure enough parameters were passed
if [ $# -lt 4 ] ; then
	echo ""
	echo "Incorrect Syntax: `basename $0` $*"
	echo ""
	usage
	echo ""
	exit $STATE_UNKNOWN
fi

# Read the passed parameters
while [ $# -gt 0 ] ; do
	case "$1" in
	-w|--warning)
		shift
		warning=$1
		;;
	-c|--critical)
		shift
		critical=$1
		;;
	-h|-?|--help)
		usage
		exit $STATE_UNKNOWN
		;;
	*)
		echo ""
		echo "Unknown command line parameter: $1"
		echo ""
		usage
		echo ""
		exit $STATE_UNKNOWN
		;;
	esac
	shift
done

# Verify the comand line parameters
if [ $warning -gt $critical ]
then
	echo ""
	echo "Warning level must be less than critical level"
	echo ""
	usage
	echo ""
	exit $STATE_UNKNOWN
fi

tmpfile="/tmp/tmp_custom_check_procs"
os=`uname -s`
case $os in
	Linux)
		procs_cmd=`ps axo state > $tmpfile`

		# Total Processes
		total_procs=`cat $tmpfile | wc -l | sed -e 's/ //g'`
		output1="Total=$total_procs"
		output2="total=$total_procs"

		# Running or Runnable
		running_procs=`cat $tmpfile | grep R | wc -l | sed -e 's/ //g'`
		output1="$output1, Running=$running_procs"
		output2="$output2 running=$running_procs"

		# Interruptable Sleep
		isleep_procs=`cat $tmpfile | grep S | wc -l | sed -e 's/ //g'`
		output1="$output1, Sleep(I)=$isleep_procs"
		output2="$output2 isleep=$isleep_procs"

		# Uninterruptable Sleep
		usleep_procs=`cat $tmpfile | grep D | wc -l | sed -e 's/ //g'`
		output1="$output1, Sleep(U)=$usleep_procs"
		output2="$output2 usleep=$usleep_procs"

		# Stopped
		stopped_procs=`cat $tmpfile | grep T | wc -l | sed -e 's/ //g'`
		output1="$output1, Stopped=$stopped_procs"
		output2="$output2 stopped=$stopped_procs"

		# Paging
		paging_procs=`cat $tmpfile | grep W | wc -l | sed -e 's/ //g'`
		output1="$output1, Paging=$paging_procs"
		output2="$output2 paging=$paging_procs"

		# Zombie
		zombie_procs=`cat $tmpfile | grep Z | wc -l | sed -e 's/ //g'`
		output1="$output1, Zombie=$zombie_procs"
		output2="$output2 zombie=$zombie_procs"
		;;
	SunOS)
		procs_cmd=`ps -efo s > $tmpfile`

		# Total Processes
		total_procs=`cat $tmpfile | wc -l | sed -e 's/ //g'`
		output1="Total=$total_procs"
		output2="total=$total_procs"

		# Running
		running_procs=`cat $tmpfile | grep O | wc -l | sed -e 's/ //g'`
		output1="$output1, Running=$running_procs"
		output2="$output2 running=$running_procs"

		# Runnable
		running_procs=`cat $tmpfile | grep R | wc -l | sed -e 's/ //g'`
		output1="$output1, Runable=$running_procs"
		output2="$output2 runable=$running_procs"

		# Sleeping
		sleep_procs=`cat $tmpfile | grep S | wc -l | sed -e 's/ //g'`
		output1="$output1, Sleep=$sleep_procs"
		output2="$output2 sleep=$sleep_procs"

		# Stopped
		stopped_procs=`cat $tmpfile | grep T | wc -l | sed -e 's/ //g'`
		output1="$output1, Stopped=$stopped_procs"
		output2="$output2 stopped=$stopped_procs"

		# Waiting
		waiting_procs=`cat $tmpfile | grep W | wc -l | sed -e 's/ //g'`
		output1="$output1, Waiting=$waiting_procs"
		output2="$output2 waiting=$waiting_procs"

		# Zombie
		zombie_procs=`cat $tmpfile | grep Z | wc -l | sed -e 's/ //g'`
		output1="$output1, Zombie=$zombie_procs"
		output2="$output2 zombie=$zombie_procs"
		;;
	*)
		echo "The operating system $os is not supported"
		exit $STATE_UNKNOWN
		;;
esac
rm -f $tmpfile

if [ $total_procs -ge $critical ] ; then
	label="CRITICAL"
	retval=$STATE_CRITICAL
elif [ $total_procs -ge $warning ] ; then
	label="WARNING"
	retval=$STATE_WARNING
else
	label="OK"
	retval=$STATE_OK
fi

echo "Procs $label: $output1 | $output2"
exit $retval
