#!/bin/bash
### BEGIN INIT INFO
# Provides:          nagios
# Required-Start:    ndo
# Required-Stop:
# Default-Start:     2 5
# Default-Stop:      0 1 6
# Description:       Nagios Monitoring Solution
### END INIT INFO
# 
# chkconfig: 345 99 01
#
# File : nagios
#
# Author : Jorge Sanchez Aymar (jsanchez@lanchile.cl)
# 
# Changelog :
#
# 1999-07-09 Karl DeBisschop <kdebisschop@infoplease.com>
#  - setup for autoconf
#  - add reload function
# 1999-08-06 Ethan Galstad <nagios@nagios.org>
#  - Added configuration info for use with RedHat's chkconfig tool
#    per Fran Boon's suggestion
# 1999-08-13 Jim Popovitch <jimpop@rocketship.com>
#  - added variable for nagios/var directory
#  - cd into nagios/var directory before creating tmp files on startup
# 1999-08-16 Ethan Galstad <nagios@nagios.org>
#  - Added test for rc.d directory as suggested by Karl DeBisschop
# 2000-07-23 Karl DeBisschop <kdebisschop@users.sourceforge.net>
#  - Clean out redhat macros and other dependencies
# 2003-01-11 Ethan Galstad <nagios@nagios.org>
#  - Updated su syntax (Gary Miller)
#
# Description: Starts and stops the Nagios monitor
#              used to provide network services status.
#
  
status_nagios ()
{

	if test ! -f $NagiosRunFile; then
		echo "No lock file found in $NagiosRunFile"
		return 1
	fi

	NagiosPID=`head -n 1 $NagiosRunFile`
	if test -x $NagiosCGI/daemonchk.cgi; then
		if $NagiosCGI/daemonchk.cgi -l $NagiosRunFile; then
		        return 0
		else
			return 1
		fi
	else
		if ps -p $NagiosPID; then
		        return 0
		else
			return 1
		fi
	fi

	return 1
}


killproc_nagios ()
{

	if test ! -f $NagiosRunFile; then
		echo "No lock file found in $NagiosRunFile"
		return 1
	fi

	NagiosPID=`head -n 1 $NagiosRunFile`
	kill $2 $NagiosPID

}


# Source function library
# Solaris doesn't have an rc.d directory, so do a test first
if [ -f /etc/rc.d/init.d/functions ]; then
	. /etc/rc.d/init.d/functions
elif [ -f /etc/init.d/functions ]; then
	. /etc/init.d/functions
fi

prefix=/opt/openitc/nagios
exec_prefix=${prefix}
NagiosBin=${exec_prefix}/bin/nagios
NagiosCfgFile=/etc/openitcockpit/nagios.cfg
NagiosStatusFile=${prefix}/var/status.dat
NagiosTempFile=${prefix}/var/nagios.tmp
NagiosRetentionFile=${prefix}/var/retention.dat
NagiosCommandFile=${prefix}/var/rw/nagios.cmd
NagiosVarDir=${prefix}/var
NagiosRunFile=${prefix}/var/nagios.lock
NagiosLockDir=/var/lock/subsys
NagiosLockFile=nagios
NagiosCGIDir=${exec_prefix}/sbin
NagiosUser=nagios
NagiosGroup=nagios
          

# Check that nagios exists.
if [ ! -f $NagiosBin ]; then
    echo "Executable file $NagiosBin not found.  Exiting."
    exit 1
fi

# Check that nagios.cfg exists.
if [ ! -f $NagiosCfgFile ]; then
    echo "Configuration file $NagiosCfgFile not found.  Exiting."
    exit 1
fi
          
# See how we were called.
case "$1" in

	start)
		echo "Starting network monitor: nagios"
		$NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
		if [ $? -eq 0 ]; then
			export ORACLE_HOME="/usr/lib/oracle/11.1.0.1/client64/lib"
			export LD_LIBRARY_PATH="/usr/lib/oracle/11.1.0.1/client64/lib"
			export TNS_ADMIN="/usr/lib/oracle/11.1.0.1/client64/admin/net"
			su - $NagiosUser -c "touch $NagiosVarDir/nagios.log $NagiosRetentionFile"
			rm -f $NagiosCommandFile
			touch $NagiosRunFile
			chown $NagiosUser:$NagiosGroup $NagiosRunFile
			$NagiosBin -d $NagiosCfgFile
			if [ -d $NagiosLockDir ]; then touch $NagiosLockDir/$NagiosLockFile; fi
			#sleep 1
			#status_nagios nagios
			exit 0
		else
			echo "CONFIG ERROR!  Start aborted.  Check your Nagios configuration."
			exit 1
		fi
		;;

	stop)
		echo "Stopping network monitor: nagios"
		killproc_nagios nagios

 		# now we have to wait for nagios to exit and remove its
 		# own NagiosRunFile, otherwise a following "start" could
 		# happen, and then the exiting nagios will remove the
 		# new NagiosRunFile, allowing multiple nagios daemons
 		# to (sooner or later) run - John Sellens
		echo -n 'Waiting for nagios to exit .'
 		for i in 1 2 3 4 5 6 7 8 9 10 ; do
 		    if status_nagios > /dev/null; then
 			echo -n ' .'
 			sleep 1
 		    else
 			break
 		    fi
 		done
 		if status_nagios > /dev/null; then
 		    echo ''
 		    echo 'Warning - running nagios did not exit in time'
 		else
 		    echo ' done.'
 		fi

		rm -f $NagiosStatusFile $NagiosTempFile $NagiosRunFile $NagiosLockDir/$NagiosLockFile $NagiosCommandFile
		;;

	status)
		echo $"Checking for $prog daemon: "
		status_nagios nagios
		;;

	restart)
		printf "Running configuration check..."
		$NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
		if [ $? -eq 0 ]; then
			echo "done"
			$0 stop
			$0 start
		else
			#$NagiosBin -v $NagiosCfgFile
			echo " FAILED!  Restart aborted.  Check your Nagios configuration."
			exit 1
		fi
		;;

	reload|force-reload)
		printf "Running configuration check..."
		$NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
		if [ $? -eq 0 ]; then
			echo "done"
			if test ! -f $NagiosRunFile; then
				$0 start
			else
				NagiosPID=`head -n 1 $NagiosRunFile`
				if status_nagios > /dev/null; then
					printf "Reloading nagios configuration..."
					killproc_nagios nagios -HUP
					echo "done"
				else
					$0 stop
					$0 start
				fi
			fi
		else
			#$NagiosBin -v $NagiosCfgFile
			echo " FAILED!  Reload aborted.  Check your Nagios configuration."
			exit 1
		fi
		;;

	*)
		echo "Usage: nagios {start|stop|restart|reload|force-reload|status}"
		exit 1
		;;

esac
  
# End of this script
