#!/bin/sh
#
# Nagios	Host/service/network monitoring daemon
#
# chkconfig:	345 85 24
# description:	Host/service/network monitoring daemon which uses snort as NIDS
#

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

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/nagios ] && . /etc/sysconfig/nagios

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down "Nagios"
		exit 1
	fi
else
	exit 0
fi

cfg_file=/etc/nagios/nagios.cfg

# value to use for $HOME
# as pld initscript resets HOME=/tmp, plugins may inherit bad value
nagios_home=/var/lib/nagios

# check for precache
precached_object_file=$(awk -F= '/^precached_object_file/{print $2}' $cfg_file)

# nagios pid file
pid_file=$(awk -F= '/^lock_file/{print $2}' $cfg_file)
pid_file=${pid_file:-/var/lib/nagios/nagios.pid}

# configtest itself
configtest() {
	/usr/sbin/nagios ${precached_object_file:+-p} -v $cfg_file
}

# wrapper for configtest:
checkconfig() {
	local details=${1:-0}

	if [ $details = 1 ]; then
		# run config test and display report (status action)
		show "Checking %s configuration" "Nagios"; busy
		local out
		out=`configtest 2>&1`
		RETVAL=$?
		if [ $RETVAL = 0 ]; then
			ok
		else
			fail
		fi
		[ "$out" ] && echo >&2 "$out"
	else
		# run config test and abort with nice message if failed
		# (for actions checking status before action).
		configtest >/dev/null 2>&1
		RETVAL=$?
		if [ $RETVAL != 0 ]; then
			show "Checking %s configuration" "Nagios"; fail
			nls 'Configuration test failed. See details with %s "checkconfig"' $0
			exit $RETVAL
		fi
	fi
}

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/nagios ]; then
		msg_already_running "Nagios"
		return
	fi

	checkconfig
	msg_starting "Nagios"

	# remove stale cmd pipe (or nagios won't start if it exists)
	rm -f /var/lib/nagios/rw/nagios.cmd

	# we're safe to use -x as we did verify config prior startup
	# precached object file also is created in configtest.
	daemon env -i PATH="$PATH" HOME="$nagios_home" /usr/sbin/nagios ${precached_object_file:+-u} -x -d $cfg_file
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		touch /var/lock/subsys/nagios
	fi
}

stop() {
	# Stop daemons.
	if [ ! -f /var/lock/subsys/nagios ]; then
		msg_not_running "Nagios"
		return
	fi

	msg_stopping "Nagios"
	killproc --pidfile $pid_file nagios
	rm -f /var/lock/subsys/nagios > /dev/null 2>&1
}

reload() {
	if [ ! -f /var/lock/subsys/nagios ]; then
		msg_not_running "Nagios"
		RETVAL=7
		return
	fi

	checkconfig
	msg_reloading "Nagios"

	# NOTE: precached object file is created in configtest.
	killproc --pidfile $pid_file nagios -HUP
	RETVAL=$?
}

condrestart() {
	if [ ! -f /var/lock/subsys/nagios ]; then
		msg_not_running "Nagios"
		RETVAL=$1
		return
	fi

	checkconfig
	stop
	start
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	checkconfig
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  reload|force-reload)
  	reload
	;;
  checkconfig|configtest)
	checkconfig 1
	;;
  status)
	status --pidfile $pid_file nagios
	RETVAL=$?
	;;
*)
	msg_usage "$0 {start|stop|restart|try-restart|reload|force-reload|checkconfig|status}"
	exit 3
esac

exit $RETVAL
