monit.sh 1.71 KB
Newer Older
1 2
#!/bin/sh

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
# script to be run directly from init

dotconfig=/wr/etc/dot-config
monit_pid=/var/run/monit.pid

start() {
    # to start monit, first kill monit running/stopped previously, init will
    # take care of starting it again
    if [ -f $monit_pid ]; then
	kill -9 `cat $monit_pid`
    else
	echo "Unable to find monit's pid"
	exit 1
    fi
}

stop() {
    echo "Stopping monit"
    # Stop monit by sending STOP singal to it. Killing monit will make init
    # to start another instance of monit.
    if [ -f $monit_pid ]; then
	kill -STOP `cat $monit_pid`
    else
	echo "Unable to find monit's pid"
	exit 1
    fi

}

restart() {
    echo "Restarting monit"
    # invoke start
    start
}

loop_forever() {
    # Save pid of current script as monit's (used by start function).
    echo $$ > $monit_pid
    # sleep forever
    while true; do sleep 10000; done
}

init() {
    # First, read dot-config to know if we shall start monit
    if [ -f $dotconfig ]; then
	. $dotconfig
    else
	echo "dot-config not found! Don't start monit"
	loop_forever
    fi

54
    if [ "$CONFIG_MONIT_DISABLE" != "y" ]; then
55 56 57 58 59 60 61 62 63 64 65 66

	if [ -z $CONFIG_WRS_LOG_MONIT ]; then
	    LOG="";
	elif echo "$CONFIG_WRS_LOG_MONIT" | grep / > /dev/null; then
	    # if a pathname, use it
	    LOG="-l $CONFIG_WRS_LOG_MONIT";
	else
	    # not a pathname, monit cannot take facility nor level from command
	    # line, only possible from configuration file
	    LOG="-l syslog"
	fi

67 68
	echo "Start monit"
	# start monit
69
	/usr/bin/monit $LOG
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    else
	echo "Monit disabled in dot-config"
	loop_forever
    fi

}

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