#! /bin/sh
#
# $Id: monit.init,v 1.1 2002/09/17 10:42:19 homyakov Exp $
#
# monit         Monitors processes and restarts them if they exit
#
# chkconfig: 345 98 02
# description: monit is a simple daemon process to restart processes if \
#		they die. It can also check tcp and udp ports to make sure \
#		that they are responding.
# processname: monit
# pidfile: /var/run/monit.pid
# config: /etc/monit.conf

# Source function library.
. /etc/rc.d/init.d/functions

MONIT_EXE=/usr/sbin/monit
MONIT_CONF="/etc/monitrc"

[ -x "$MONIT_EXE" -a -r "$MONIT_CONF" ] || exit

LOCKFILE=/var/lock/subsys/monit

RETVAL=0

start ()
{
	echo -n "Starting monit: "
	daemon $MONIT_EXE 
	RETVAL=$?

	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/monit
	return $RETVAL
}

stop () 
{
	echo -n "Stopping monit: "
	$MONIT_EXE quit
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/monit
	return $RETVAL
}

status () 
{
	$MONIT_EXE status
	return $?
}

restart () 
{
	stop
	start
}

reload ()
{
	echo -n "Reloading monit configuration: "
	killproc monit -HUP
	RETVAL=$?                                                               
	echo                                                                    
	return $RETVAL                                                          
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status
		;;
	restart)
  		restart
		;;
	reload)
  		reload
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	*)
		echo "Usage: monit {start|stop|status|restart|reload}"
		exit 1
esac

exit $RETVAL
