#! /bin/sh
# Copyright (c) 2003-2014 SUSE Linux GmbH, Nuernberg, Germany.
# All rights reserved.
# Author: Lars Mueller <lmuelle@suse.com>
#
# /etc/init.d/monit
#   and its symbolic link
# /usr/sbin/rcmonit
#
### BEGIN INIT INFO
# Provides:          monit
# Required-Start:    $syslog $remote_fs
# Required-Stop:     $syslog
# Should-Start:      $syslog $time $named $remote_fs
# 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="/run/monit/monit.pid"
MONIT_SYSCONFIG_FILE="/etc/sysconfig/monit"

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

if test "$1" != '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 test -f "${MONIT_SYSCONFIG_FILE}"; then
	. "${MONIT_SYSCONFIG_FILE}"
fi

if test ! -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 test -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 test "$1" != '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 test "$1" != '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 test "$1" != '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 "
		mkdir -p -m0700 "/var/run/monit"
		if test "yes" = "${MONIT_VIA_INITTAB}"; then
			echo "via /etc/inittab"
			"${MONIT_MODIFY_INITTAB}" --add && echo -en "${esc}[1A"
		else
            PID_DIR=$(dirname "$MONIT_PID_FILE")
            [ -n "$PID_DIR" ] && mkdir -p -m0700 "$PID_DIR"
			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
		;;
	stop)
		echo -n "Shutting down monit "
		if test "yes" = "${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
		;;
	status)
		echo -n "Checking for service monit "
		test "yes" = "${MONIT_VIA_INITTAB}" && \
			echo -n "from /etc/inittab "
		checkproc -p "${MONIT_PID_FILE}" "${MONIT_BIN}"
		rc_status -v
		;;
	restart)
		if test "yes" = "${MONIT_VIA_INITTAB}"; then
			FORCE_REINIT="no"
			if test -e "${MONIT_PID_FILE}"; then
				echo -n "Restarting monit from /etc/inittab "
				kill $( cat "${MONIT_PID_FILE}")
			else
				echo "Warning: No pid file, ${MONIT_PID_FILE} found.  Do not know which process to stop.  Calling stop and start instead."
				FORCE_REINIT="yes"
			fi
		fi
		if test "${MONIT_VIA_INITTAB}" != "yes" -o "${FORCE_REINIT}" = "yes"; then
			$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
		;;
	poll)
		echo -n "Poll all monit services "
		killproc -p "${MONIT_PID_FILE}" -SIGUSR1 "${MONIT_BIN}"
		rc_status -v
		;;
	*)
		echo "Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload|poll}"
		exit 1
esac
rc_exit
