#! /bin/sh
# Copyright (c) 2003 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
# Author: Lars Mueller <lmuelle@suse.de>
#
# /etc/init.d/monit
#   and its symbolic link
# /usr/sbin/rcmonit
#
### BEGIN INIT INFO
# Provides:          monit
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# Short-Description: Manages and monitors processes, files, directories, devices
# description:  Monit is a utility for managing and monitoring processes,
#	        files, directories, and devices on a Unix system.
### END INIT INFO

MONIT_BIN="/usr/bin/monit"
MONIT_MODIFY_INITTAB="/usr/share/monit/monit-modifyinittab"
MONIT_PID_FILE="/var/run/monit/monit.pid"
MONIT_SUBSYS_FILE="/var/lock/subsys/monit"
MONIT_SYSCONFIG_FILE="/etc/sysconfig/monit"

# Status shell functions
. /etc/rc.status
# Reset status of this service
rc_reset

if [ x"$1" != x'stop' -a ! -f ${MONIT_SYSCONFIG_FILE} ]; then
	echo -n "Monit configuration file, ${MONIT_SYSCONFIG_FILE} does not exists. "
	# Tell the user this has skipped
	rc_status -s
	exit 6
elif [ -f ${MONIT_SYSCONFIG_FILE} ]; then
	. ${MONIT_SYSCONFIG_FILE}
fi

if [ ! -x ${MONIT_BIN} ]; then
	echo -n "Monit binary, ${MONIT_BIN} is not installed. "
	# Tell the user this has skipped
	rc_status -s
	exit 5
fi

if [ -z "${MONIT_RC_FILE}" ]; then
	MONIT_RC_FILE="/etc/monitrc"
	echo "MONIT_RC_FILE not set in configuration file, ${MONIT_SYSCONFIG_FILE}."
	echo "Using ${MONIT_RC_FILE} instead."
fi
if [ x"$1" != x'stop' -a ! -f "${MONIT_RC_FILE}" ]; then
	echo -n "Monit configuration file, ${MONIT_RC_FILE} does not exist. "
	# Tell the user this has skipped
	rc_status -s
	exit 6
fi
if [ x"$1" != x'stop' ] && ! grep -qv '^[[:space:]]*#' "${MONIT_RC_FILE}"; then
	echo -n "Monit is not configured in ${MONIT_RC_FILE}. "
	# Tell the user this has skipped
	rc_status -s
	exit 6
fi
MONIT_OUT=$( ${MONIT_BIN} -t -I 2>&1)
if [ x"$1" != x'stop' -a $? -ne 0 ]; then
	echo ${MONIT_OUT}
	echo -n "Monit is not configured well in ${MONIT_RC_FILE}. "
	# Tell the user this has skipped
	rc_status -s
	exit 6
fi

case "$1" in
	start)
		echo -n "Starting monit "
		if [ x"yes" = x${MONIT_VIA_INITTAB} ]; then
			echo "via /etc/inittab"
			${MONIT_MODIFY_INITTAB} --add && echo -en "${esc}[1A"
		else
			checkproc -p ${MONIT_PID_FILE} ${MONIT_BIN} && \
				echo -n " Warning: monit already running. "
			startproc -p ${MONIT_PID_FILE} ${MONIT_BIN} -c "${MONIT_RC_FILE}" ${MONIT_ARGS}
		fi
		rc_status -v
		test $? -eq 0 && touch ${MONIT_SUBSYS_FILE}
		;;
	stop)
		echo -n "Shutting down monit "
		if [ x"yes" = x${MONIT_VIA_INITTAB} ]; then
			echo "from /etc/inittab"
			${MONIT_MODIFY_INITTAB} --remove && echo -en "${esc}[1A"
		else
			checkproc -p ${MONIT_PID_FILE} ${MONIT_BIN} || \
				echo -n " Warning: monit not running. "
			killproc -p ${MONIT_PID_FILE} ${MONIT_BIN}
		fi
		rc_status -v
		test $? -eq 0 && rm -f ${MONIT_SUBSYS_FILE}
		;;
	status)
		echo -n "Checking for service monit "
		test x"yes" = x${MONIT_VIA_INITTAB} && \
			echo -n "from /etc/inittab "
		checkproc -p ${MONIT_PID_FILE} ${MONIT_BIN}
		rc_status -v
		;;
	restart)
		if [ x"yes" = x${MONIT_VIA_INITTAB} ]; then
			echo -n "Restarting monit from /etc/inittab "
			killproc -p ${MONIT_PID_FILE} ${MONIT_BIN}
		else
			$0 stop
			$0 start
		fi
		rc_status -v
		;;
	try-restart)
		# Do a restart only if the service was active before.
		$0 status >/dev/null && $0 restart
		rc_status
		;;
	reload|force-reload)
		echo -n "Reload service monit "
		killproc -p ${MONIT_PID_FILE} -HUP ${MONIT_BIN}
		rc_status -v
		test $? -eq 0 && touch ${MONIT_SUBSYS_FILE}
		;;
	poll)
		echo -n "Poll all monit services "
		killproc -p ${MONIT_PID_FILE} -SIGUSR1 ${MONIT_BIN}
		rc_status -v
		test $? -eq 0 && touch ${MONIT_SUBSYS_FILE}
		;;
	*)
		echo "Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload|poll}"
		exit 1
esac
rc_exit
