943fc98
#!/bin/bash
943fc98
#
abc90b3
### BEGIN INIT INFO
659f369
# chkconfig: 2345 91 10
659f369
# processname: munin-node
659f369
# config: /etc/munin/munin-node.conf
659f369
# pidfile: /var/run/munin-asyncd.pid
abc90b3
# Provides: munin-asyncd
659f369
# Required-Start: $local_fs $remote_fs $network munin-node
abc90b3
# Required-Stop: $local_fs $remote_fs $network
abc90b3
# Default-Start: 2 3 4 5
abc90b3
# Default-Stop: 0 1 6
abc90b3
# Short-Description: Starts and stops munin-asyncd
abc90b3
# Description: munin-asyncd enables asyncronous fetching of
abc90b3
#              metrics from munin-node in a Munin monitoring setup.
abc90b3
### END INIT INFO
abc90b3
abc90b3
ASYNC_BIN=munin-asyncd
abc90b3
PATH=/sbin:/bin:/usr/sbin:/usr/bin
9ad6e7b
DAEMON=/usr/sbin/${ASYNC_BIN}
97061fb
PIDFILE=/var/run/munin/${ASYNC_BIN}.pid
abc90b3
abc90b3
is_running () {
abc90b3
	if [ ! -e "$PIDFILE" ]; then
abc90b3
		return 3 # No PID file exists, ${ASYNC_BIN} must not be running
abc90b3
	else
abc90b3
		kill -0 $(cat $PIDFILE) 2> /dev/null
abc90b3
		if [ "$?" -eq "0" ]; then
abc90b3
			return 0 # PID file exists, and PID found in file is in use
abc90b3
		else
abc90b3
			rm $PIDFILE 2> /dev/null
abc90b3
			if [ "$?" -eq "0" ]; then
abc90b3
				return 1 # PID file exists, but no process with the PID was found. Deletion of the PID file was a success
abc90b3
			else
abc90b3
				return 2 # PID file exists, but no process with the PID was found. Deletion of the PID file failed
abc90b3
			fi
abc90b3
		fi
abc90b3
	fi
abc90b3
}
abc90b3
abc90b3
check_root () {
abc90b3
	operation=$1
abc90b3
	if [ "$(id -u)" -ne "0" ]; then
abc90b3
		echo "Root permissions are required in order to perform the \"$operation\" operation."
abc90b3
		exit 4
abc90b3
	fi
abc90b3
}
abc90b3
abc90b3
sanity_check () {
abc90b3
	if [ ! "$(type -p pidof)" ]; then
abc90b3
		echo "The program \"pidof\" is required in order for this script to work properly, and will not run without it."
abc90b3
		exit 1
abc90b3
	fi
abc90b3
	
abc90b3
	if [ ! -d "$(dirname $PIDFILE)" ]; then
abc90b3
		mkdir -p $(dirname $PIDFILE) 2> /dev/null
abc90b3
		if [ "$?" -ne "0" ]; then
abc90b3
			echo "The directory for a PID file is missing ($(dirname $PIDFILE)), and could not be created automatically. Please correct this and try again."
abc90b3
			exit 1
abc90b3
		fi
abc90b3
	fi
abc90b3
	
abc90b3
	if [ ! -x "$DAEMON" ]; then
abc90b3
		echo "${ASYNC_BIN} was expected at $DAEMON, but was not found. Are you sure Munin is correctly installed?"
abc90b3
		exit 5
abc90b3
	fi
abc90b3
}
abc90b3
abc90b3
case $1 in
abc90b3
	start)
abc90b3
		check_root $1
abc90b3
		sanity_check
abc90b3
		
abc90b3
		echo -n "Starting ${ASYNC_BIN} ... "
abc90b3
		
abc90b3
		is_running
abc90b3
		if [ "$?" -eq "0" ]; then
abc90b3
			echo "Already running (PID: $(cat $PIDFILE))."
abc90b3
			exit 0
abc90b3
		fi
abc90b3
		
abc90b3
		$DAEMON > /dev/null 2> /dev/null < /dev/null &
abc90b3
		
abc90b3
		sleep 0.5 # Give ${ASYNC_BIN} time to fail, if it fails
abc90b3
		
abc90b3
		pid=$(pidof -o %PPID -x $DAEMON)
abc90b3
		if [ "$?" -eq "0" ]; then
abc90b3
			echo $pid > $PIDFILE
abc90b3
			echo "Done."
abc90b3
			exit 0
abc90b3
		else
abc90b3
			echo "Start failed for some reason, restarting ${ASYNC_BIN} so you can see why:"
abc90b3
			$DAEMON
abc90b3
			echo
abc90b3
			echo "A \"Connection refused\" message typically is because munin-node is not started prior to ${ASYNC_BIN}."
abc90b3
		fi
abc90b3
	;;
abc90b3
	stop)
abc90b3
		check_root $1
abc90b3
		sanity_check
abc90b3
		
abc90b3
		echo -n "Stopping ${ASYNC_BIN} ... "
abc90b3
		
abc90b3
		# If a PID file is not found, we perform an extra check if ${ASYNC_BIN} still should be running, and if it does, then create a PID file
abc90b3
		if [ ! -e "$PIDFILE" ]; then
abc90b3
			pid=$(pidof -o %PPID -x $DAEMON)
abc90b3
			if [ "$?" -eq "0" ]; then
abc90b3
				echo $pid > $PIDFILE
abc90b3
			fi
abc90b3
		fi
abc90b3
		
abc90b3
		is_running
abc90b3
		if [ "$?" -ne "0" ]; then
abc90b3
			echo "Not running."
abc90b3
			exit 0
abc90b3
		fi
abc90b3
		
abc90b3
		PID=$(cat $PIDFILE)
abc90b3
		kill -15 $PID 2> /dev/null
abc90b3
		HALFSECONDS="0"
abc90b3
		while true; do
abc90b3
			kill -0 $PID 2> /dev/null
abc90b3
			if [ "$?" -eq "1" ]; then
abc90b3
				break
abc90b3
			fi
abc90b3
			sleep 0.5
abc90b3
			HALFSECONDS=$(expr $HALFSECONDS + 1)
abc90b3
			if [ "$HALFSECONDS" -ge "10" ]; then
abc90b3
				kill -9 $PID 2> /dev/null
abc90b3
				echo "Not stopping gracefully, killed!"
abc90b3
				exit 0
abc90b3
			fi
abc90b3
		done
abc90b3
		echo "Done."
abc90b3
		exit 0
abc90b3
	;;
abc90b3
	restart|try-restart|force-reload)
abc90b3
		check_root $1
abc90b3
		sanity_check
abc90b3
		
abc90b3
		$0 stop
abc90b3
		$0 start
abc90b3
		
abc90b3
		exit 0
abc90b3
	;;
abc90b3
	reload)
abc90b3
		echo "The \"reload\" operation is not supported by ${ASYNC_BIN}. You probably want to run \"restart\" instead."
abc90b3
		exit 3
abc90b3
	;;
abc90b3
	status)
abc90b3
		is_running
abc90b3
		return="$?"
abc90b3
		if [ "$return" -eq "0" ]; then
abc90b3
			echo "${ASYNC_BIN} is running (PID: $(cat $PIDFILE))."
abc90b3
			exit 0
abc90b3
		elif [ "$return" -eq "1" ]; then
abc90b3
			echo "${ASYNC_BIN} is not running, but PID file still existed (has been removed now)."
abc90b3
			exit 1
abc90b3
		elif [ "$return" -eq "2" ]; then
abc90b3
			echo "${ASYNC_BIN} is not running, but PID file still exists (could not be removed)."
abc90b3
			exit 1
abc90b3
		elif [ "$return" -eq "3" ]; then
abc90b3
			echo "${ASYNC_BIN} is not running."
abc90b3
			exit 3
abc90b3
		fi
abc90b3
		exit 4
abc90b3
	;;
abc90b3
	*)
abc90b3
		echo "Usage: $0 {start|stop|restart|try-restart|force-reload|reload|status}"
abc90b3
		exit 2
abc90b3
	;;
abc90b3
esac