#! /bin/sh
#
# init script for the nagios monitor
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nagios
NAME=nagios
DESC=nagios
NICENESS=5
PIDFILE=/var/run/nagios/nagios.pid
CONFIG=/etc/nagios/nagios.cfg
CGICFG=/etc/nagios/cgi.cfg

test -f $DAEMON || exit 0

# LC_NUMERIC must be set to POSIX for locales that use ',' in floating point 
# numbers, otherwise some plugins will break.
export LC_NUMERIC
LC_NUMERIC=POSIX

set -e

if [ ! -d /var/run/nagios ]; then
	mkdir /var/run/nagios
	chown nagios:www-data /var/run/nagios
fi

#
#	check_started()
#
# checks not only for the pid (which could be done by start-stop-daemon), 
# but also for whether or not nagios is running according to the same
# nagios_check_command as defined by the web interface in $CGICFG.  this
# should make things easier for the db-flavored nagioses, or other
# nagioses where the status log is in a different location too.
#
check_started () {
	check_cmd=`get_config "nagios_check_command" "$CGICFG"`
	if [ ! "$check_cmd" ]; then
		echo "unable to determine nagios_check_command from $CGICFG!" >&2
		return 1
	fi

	eval $check_cmd >/dev/null
		
	if [ -f $PIDFILE ]; then
		pid=`cat $PIDFILE`
		if [ "$pid" ] && ps $pid >/dev/null; then
			return 0    # Is started
		fi
	fi
	return 1	# Isn't started
}

#
#	get_config()
#
#	grab a config option from nagios.cfg (or possibly another nagios config
#	file if specified).  everything after the '=' is echo'd out, making
#	this a nice generalized way to get requested settings.
#
get_config () {
	if [ "$2" ]; then
    	set -- `grep ^$1 $2 | sed 's@=@ @'`
	else
    	set -- `grep ^$1 $CONFIG | sed 's@=@ @'`
	fi
	shift
    echo $*
}

check_config () {
    if $DAEMON -v $CONFIG >/dev/null 2>&1 ; then
	# First get the user/group etc Nagios is running as
	nagios_user=`get_config nagios_user`
	nagios_group=`get_config nagios_group`
	log_file=`get_config log_file`
	log_dir=`dirname $log_file`

	# Chown the log directory to this user/group so that
	# Nagios can write there.
	chown -R ${nagios_user}:$nagios_group $log_dir

	return 0    # Config is ok
    else
    	# config is not okay, so let's barf the error to the user
        $DAEMON -v $CONFIG
	return 1    # Config not ok
    fi
}

check_named_pipe () {
	nagiospipe=`get_config command_file`
	if [ -p "$nagiospipe" ]; then
		return 1   # a named pipe exists
	else
		return 0   # no named pipe exists
	fi
}

case "$1" in
start)
	echo -n "Starting $DESC: "
	if ! check_started; then
		if ! check_named_pipe; then
			echo "named pipe exists - removing"
			rm -f $nagiospipe
		fi

		if check_config; then
			start-stop-daemon --start --quiet \
			                  --nicelevel $NICENESS \
			                  --pidfile $PIDFILE \
			                  --make-pidfile --background \
			                  --exec $DAEMON -- $CONFIG
			echo "$NAME."
		else
			echo "errors in config!"
		fi
	else
		echo "already running"
	fi
	;;
stop)
	echo -n "Stopping $DESC: "
	start-stop-daemon --stop --quiet --pidfile $PIDFILE \
		--oknodo --exec $DAEMON
	echo "$NAME."
	rm -f $PIDFILE
	if ! check_named_pipe; then
		rm -f $nagiospipe
	fi
	;;
  reload|force-reload)
	echo "Reloading $DESC configuration files."
	# Check first
	if check_config; then
	    if check_started; then
		start-stop-daemon --stop --signal 1 --quiet --pidfile \
		    $PIDFILE --exec $DAEMON
	    else
		echo "Not running."
	    fi
	else
	    echo "Errors in config file. Not reloading."
	fi
  	;;
  restart)
	$0 stop
	sleep 2
	$0 start
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
