#!/bin/sh

### Make sure you add the following to your visudo file
### nagios  ALL = (root) NOPASSWD: /sbin/service * status
###

. /usr/lib/nagios/plugins/utils.sh

SERVICE=""
EXIT_STATUS=$STATE_OK
EXIT_STATUS_TPS=$STATE_OK
EXIT_STATUS_READS=$STATE_OK
EXIT_STATUS_WRITES=$STATE_OK

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

## Print usage
usage() {
	echo " check_servicestatus"
	echo ""
	echo " Usage: check_servicestatus -s service ] [ -h ]"
	echo ""
	echo "		-s  service name"
	echo "		-h  Show this page"
	echo ""
}
 
## Process command line options
doopts() {
	if ( `test 0 -lt $#` )
	then
		while getopts :s: myarg "$@"
		do
			case $myarg in
				h|\?)
					usage
					exit;;
				s)
					SERVICE=$OPTARG;;
				*)	# 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=`sudo /usr/sbin/service $SERVICE status`
OUTPUTSTATUS=`echo $?`
if test -z "$OUTPUT" ; then
	RESULT="service UNKNOWN - query returned no output! - Make sure the proper changed in the notes is added to the sudoer file"
	EXIT_STATUS=$STATE_UNKNOWN
else
	if test $OUTPUTSTATUS -eq 0 ; then
		RESULT="OK: Service $SERVICE is running"
		EXIT_STATUS=$STATE_OK
	else
		RESULT="CRITICAL: Service $SERVICE is not running"
		EXIT_STATUS=$STATE_CRITICAL
	fi
fi


# Quit and return information and exit status
theend
