#!/bin/sh

# Multipath Alerting Daemon

# location of the executable:
MPATHALERT="@BASE_PATH@/bin/mpathalert"

#delay in seconds between 2 consecutive alerts:
DELAY="120"

# pidfile:
PID_FILE="/var/run/mpathalert.pid"

# lock file
SUBSYS_FILE="/var/lock/subsys/mpathalert"

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

start() {
	echo -n $"Starting the multipath alerting daemon: "
	
	if [ -e ${SUBSYS_FILE} ]; then
		if [ -e ${PID_FILE} ] && [ -e /proc/`cat ${PID_FILE}` ]; then
			echo -n $"cannot start mpathalert: already running."
			failure $"cannot start mpathalert: already running."
			echo
			return 1
		fi
	fi
	
	${MPATHALERT} -daemon -delay ${DELAY} -pidfile ${PID_FILE} >/dev/null 2>&1 </dev/null
	
	touch $SUBSYS_FILE
	success
	echo
	return 0
}

stop() {
	echo -n $"Stopping the multipath alerting daemon: "

	if [ ! -e ${SUBSYS_FILE} ]; then
		echo -n $"cannot stop mpathalert: mpathalert is not running."
		failure $"cannot stop mpathalert: mpathalert is not running."
		echo
		return 1;
	fi

	killproc mpathalert
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/perfmon;
	return $RETVAL
}

restart() {
	stop
	start
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	*)
		echo $"Usage: $0 {start|stop|restart}"
		exit 1
esac
