#!/bin/sh
###############################################
#
# Nagios script to check the number of concurrent requests being processed by Apache
#
# NOTE: You may need to tweak your apache config to allow "apachectl fullstatus" to 
#       return information to the localhost or <hostname> for the server you are running this on
#
# e.g.
#	<Location /server-status>
#		SetHandler server-status
#		Order deny,allow
#		Deny from all
#		Allow from localhost <hostname> ...
#	</Location>
# 
# Also requires lynx (or links change variable at top of script)
#
# See usage for command line switches
# 
# Created: 2006-08-11 (i.yates@uea.ac.uk)
#
###############################################

. /usr/local/nagios/libexec/utils.sh


VERSION="1.0"
CHECKNAME="HTTP requests"

HOME=/tmp
LYNX=/usr/bin/lynx
AWK=/bin/awk
GREP=/bin/grep

STATUSURL="http://localhost:80/server-status"

FLAG_VERBOSE=FALSE
LEVEL_WARN=""
LEVEL_CRIT=""
RESULT=""
EXIT_STATUS=$STATE_OK


###############################################
#
## FUNCTIONS 
#

## Print usage
usage() {
	echo " check_apacheconcreq $VERSION - Nagios Apache concurrent HTTP requests check script"
	echo ""
	echo " Usage: check_apacheconcreq -w <warning level> -c <critical level> [ -v ] [ -h ]"
	echo ""
	echo "		 -w  Number of concurrent requests at which a warning is triggered"
	echo "		 -c  Number of concurrent requests at which a critical is triggered"
	echo "		 -v  Verbose output (ignored for now)"
	echo "		 -h  Show this page"
	echo ""
}
 
## Process command line options
doopts() {
	if ( `test 0 -lt $#` )
	then
		while getopts w:c:vh myarg "$@"
		do
			case $myarg in
				h|\?)
					usage
					exit;;
				w)
					LEVEL_WARN=$OPTARG;;
				c)
					LEVEL_CRIT=$OPTARG;;
				v)
					FLAG_VERBOSE=TRUE;;
				*)	# Default
					usage
					exit;;
			esac
		done
	else
		usage
		exit
	fi
}


# Write output and return result
theend() {
	echo $RESULT
	exit $EXIT_STATUS
}


#
## END FUNCTIONS 
#

#############################################
#
## MAIN 
#


# Handle command line options
doopts $@

# Do the do
OUTPUT=`$LYNX -dump $STATUSURL | $GREP 'requests currently being processed' | $AWK '{print $1}'`
if test -z "$OUTPUT" ; then
	RESULT="$CHECKNAME WARNING - query returned no output!"
	EXIT_STATUS=$STATE_WARNING
else
	if test "$OUTPUT" -lt "$LEVEL_WARN" ; then
		RESULT="$CHECKNAME OK - $OUTPUT concurrent requests"
		EXIT_STATUS=$STATE_OK
	else
		if test "$OUTPUT" -gt "$LEVEL_CRIT" ; then 
			RESULT="$CHECKNAME CRITICAL - $OUTPUT concurrent requests"
			EXIT_STATUS=$STATE_CRITICAL
		else
			if test "$OUTPUT" -gt "$LEVEL_WARN" ; then 
				RESULT="$CHECKNAME WARNING - $OUTPUT concurrent requests"
				EXIT_STATUS=$STATE_WARNING
			fi
		fi
	fi
fi

# Quit and return information and exit status
theend
