#!/bin/bash
#
# nagios       Starts and stops the Nagios monitor
#              used to provide network services status.
#
# chkconfig: 345 99 01
# description: Nagios network monitor
# pidfile: /var/run/nagios/nagios.pid
# config: /etc/nagios/nagios.cfg

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

[ -f /etc/nagios/nagios.cfg ] || exit 0
[ -f /usr/sbin/nagios ] || exit 0

nagios_cmd=`grep "command_file" /etc/nagios/nagios.cfg|cut -d= -f2` 
nagios_pidfile=`grep "lock_file" /etc/nagios/nagios.cfg|cut -d= -f2` 

# See how we were called.
case "$1" in
start)
	[ -f $nagios_cmd ] || rm -f $nagios_cmd 
        if [ -n "`/sbin/pidof nagios`" ]; then
            action "nagios: already running: " /bin/false
            exit 1
	fi
	echo -n "Starting nagios daemon: "
	export LC_ALL=C
	daemon nagios -d /etc/nagios/nagios.cfg
	echo
	touch /var/lock/subsys/nagios
	;;
stop)
	echo -n "Shutting down nagios daemon: "
	killproc nagios
	echo
	rm -f /var/lock/subsys/nagios
	[ -f $nagios_cmd ] || rm -f $nagios_cmd
	[ -f $nagios_pidfile ] || rm -f $nagios_pidfile
	;;
status)
	status nagios
	;;
restart|reload)
	$0 stop
	$0 start
	;;
condrestart)
	[ -f /var/lock/subsys/nagios ] && restart
	;;
configtest)
        echo -n "Testing the nagios configuration: "
        nagios -v /etc/nagios/nagios.cfg
        echo
        ;;

  *)
	echo "Usage: nagios {start|stop|status|restart|condrestart|reload|configtest}"
	exit 1
esac

exit 0
