#!/bin/bash

DAEMONCHK="/usr/lib/nagios/plugins/check_procs"

function usage {
	echo "Usage: $0 [-c <critical threshold>] [-w <warning threshold>] [ -r <path to rcron state file>]"
	echo -e "\t-c\tcritical threshold in seconds"
	echo -e "\t-w\twarning threshold in seconds"
	echo -e "\t-r\tpath to rcron state file"
	exit 4
}

args=`getopt -a -o c:,h,w:,r: --long help -- "$@"`
if [ $? != 0 ] ; then
        usage
fi

PARAM=""
eval set -- "$args"
for opt ; do
        case "$opt" in
                -h|--help)
                        usage
                ;;
		-c)
			CRIT=$2
			PARAM="${PARAM} -c ${CRIT}"
			shift 2;
		;;
		-w)
			WARN=$2
			PARAM="${PARAM} -w ${WARN}"
			shift 2;
		;;
		-r)
			RCRON=$2
			shift 2
		;;
		--)
			shift
		;;
	esac
done

if [ -z "${CRIT}" ] && [ -z "${WARN}" ] ; then
	echo "Warning/Critical value not set!"
	exit 4
fi

if [ -x "${DAEMONCHK}" ] ; then
	if [ -n "${RCRON}" ] && [ -f "${RCRON}" ] ; then
		if [ -n "`cat ${RCRON} | grep -i ^active`" ] ; then
			${DAEMONCHK} -C ido2db ${PARAM}
			exit $?
		fi
	else
		${DAEMONCHK} -C ido2db ${PARAM}
		exit $?
	fi
else
	echo "Can't execute ${DAEMONCHK}!"
	exit 4
fi
