#!/bin/sh

PROGNAME=`basename $0`

print_usage() {
	echo "Usage: $PROGNAME"
}

print_help() {
	echo ""
	print_usage
	echo ""
	echo "This plugin checks the status of services normally started by the init process."
	echo ""
	support
	exit 0
}


case "$1" in
	--help)
		print_help
		exit 0
		;;
	-h)
		print_help
		exit 0
		;;
	*)

		if [ $# -eq 1 ]; then 
			os=`uname -s`
			if [ "$os" = "Linux" -a -x /sbin/service ] ; then
				/sbin/service $1 status
				ret=$?
			elif [ "$os" = "SunOS" ] ; then
				/usr/bin/svcs $1 | tail -1 | grep "online" 
				ret=$?
			elif [ "$os" = "AIX" ] ; then
				/usr/bin/lssrc -s $1 > /dev/null 2> /dev/null
				ret=$?
				if [ $ret -eq 1 ] ; then
					ret=3; # Unknown
				else
					status=`/usr/bin/lssrc -s $1 | /usr/bin/gawk -v src=$1 '$1==src {print $NF}'`
					case $status in
					active)
						ret=0; # OK
						;;
					inoperative)
						ret=2; # Critical
						;;
					inoperative)
						ret=3; # Unknown
						;;
					esac
				fi
			fi
			case "$ret" in
			0)
				exit $ret
				;;
			*)
				exit 2
				;;
			esac
		else
			echo "ERROR: No service name specified on command line"
			exit 3
		fi
		;;
esac
