#!/bin/sh

### BEGIN INIT INFO
# Provides:          gmond
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts gmond 
# Description:       starts the ganglia monitoring plugin using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/mgmond
DAEMON_OPTS="" 
NAME=mgmond
DESC=mgmond

test -x $DAEMON || exit 0

thisstart() {
    for cluster_conf in /etc/gmond/*.conf
    do
        if ! pgrep -U nobody -f $cluster_conf > /dev/null
        then    
            $DAEMON -c $cluster_conf
	        echo -e "\t$cluster_conf"
        fi
    done
}

thisstop() {
    shift
    if [ $# -gt 0 ]
    then
        for i in $@
        do 
            pkill -TERM -f /etc/gmond/gmond-$i.conf
        done
    else
        pkill -TERM -f /etc/gmond/gmond
    fi

}

thisreload() {
    pkill -HUP -f /etc/gmond/gmond
}


# Include nrpe defaults if available
if [ -f /etc/default/mgmond ] ; then
	. /etc/default/mgmond
fi

set -e

case "$1" in
  start)
	echo -n "Starting $DESC: "
    thisstart
	;;
  stop)
	echo -n "Stopping $DESC: "
    thisstop $@
	echo "$NAME."
	;;
  restart|force-reload)
	echo -n "Restarting $DESC: "
	thisstop $@
	sleep .5
	thisstart
	echo "$NAME."
	;;
#  reload)
#      echo -n "Reloading $DESC configuration: "
#      thisreload
#      echo "$NAME."
#      ;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
