#!/bin/bash
#
# Licensed Materials - Property of IBM
#
# 5724O4800
#
# (C) Copyright IBM Corp. 1997, 2006. All Rights Reserved
#
# US Government Users Restricted Rights - Use, duplication
# or disclosure restricted by GSA ADP Schedule Contract
# with IBM Corp.
#
#
# RedHat/SuSE Linux startup script
#
# Ident: $Id: nco 1.9 2003/08/13 14:06:20 nick Development $
#
### RedHat Service Information follows:
#
# By default, install service for runlevels 3 and 5, with
# start priority of 81 and stop priority of 65.
#
# chkconfig: 35 81 65
# description: Netcool/OMNIbus Process Control
#
### SuSE Service Information follows:
### BEGIN INIT INFO
# Provides: nco
# Required-Start: $local_fs $network $syslog
# Should-Start:
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 4 6
# Short-Description: Netcool/OMNIbus
# Description: Netcool/OMNIbus Process Control
### END INIT INFO

### REDHAT ONLY
# Source function library.
. /etc/init.d/functions

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

# Check that networking is up? If it is not we exit here.
[ ${NETWORKING} = "no" ] && exit 0
### END REDHAT ONLY
### SUSE ONLY
. /etc/rc.status
### END SUSE ONLY

# Variables that can be changed
START_NCO=Y			# Start nco_pad
NCHOME=__NCHOME__		# Set directory for $NCHOME
OMNIHOME=__OMNIHOME__		# Set directory for $OMNIHOME
NCO_PA="__PROCESSAGENT__"	# Set Process Agent's name
SECURE=__SECURE__		# Y/N run Process Agent in secure mode
NETCOOL_LICENSE_FILE=__NETCOOL_LICENSE_FILE__ # Points to flex license server

export NCHOME OMNIHOME NCO_PA NETCOOL_LICENSE_FILE

if [ ! -d $OMNIHOME/bin ]; then
	# $OMNIHOME/bin doesnt exist
	echo "directory $OMNIHOME/bin: does not exist, nco not starting"
	exit
fi

# Omnibus 3.5: 
# The PAD used to be shutdown using nco_pa_shutdown utility with -option STOP.
# However, there was a security requirement to remove the password on the
# nco_pa_shutdown command from this script.
# As a result the following function was added to terminate all PADs and their
# children using the 'kill' command.

killpad() {
	# Find all PAD processes
	# ps -e gets basic details on all processes
	pad_pids=`ps -e |
			awk '$4 == "nco_pad" {print $1}' |
			sort -n`

	# NOTE: If this script is run as non-root, we have the potential
	# for an endless loop here.
	# That is because the effective user may not have permission
	# to terminate some of the PADs on the system.
	# Added count to terminate loop after 10 iterations.

	count=0
	while [ "$pad_pids" != "" -a $count -lt 10 ]
	do

		# Find all the process group id's of PAD children

 		child_list=""		

		for parent_pid in $pad_pids
		do
			# ps -e j (not "-ej" or "ej") gets groups details
			children=`ps -e j |
				awk '$1 == pid && $10 != "nco_pad" {print $3}' pid=$parent_pid |
				sort -nru`
			child_list="$child_list $children"
		done

		# Kill PADs first to prevent them from re-starting children

		kill $pad_pids
	
		# Kill child processes and all other processes that share
		# their process group id's.

		for gid in $child_list
		do
			[ "$gid" != "" ] && kill -TERM -$gid
		done

		# In case PAD daemons weren't killed in the right order
		# get the ones that have come alive again.
		# This will happen if the orginal PAD pid's span
		# across pid wrap around.

		count=`expr $count + 1`

		sleep 10
 
		# ps -e gets basic details on all processes
		pad_pids=`ps -e |
			awk '$4 == "nco_pad" {print $1}' |
			sort -n`
	done
}
# Start/stop NCO processes

case "$1" in
'start')
	# Start the required processes.
	case "$START_NCO" in
		'Y')
			if [ -x $OMNIHOME/bin/nco_pad ]; then
				grep ${NCO_PA} $NCHOME/etc/omni.dat >/dev/null 2>/dev/null
				if [ $? -eq 0 ]; then
					[ "$BOOTUP" != "verbose" ] && echo -n "Netcool/OMNIbus : Starting Process Control ..."
					if [ "$SECURE" = "Y" ]; then
						${OMNIHOME}/bin/nco_pad -name ${NCO_PA} -authenticate PAM -secure > /dev/null 2> /dev/null
					else
						${OMNIHOME}/bin/nco_pad -name ${NCO_PA} -authenticate PAM > /dev/null 2> /dev/null
					fi
					sleep 2
					PID=`ps auxwww | grep nco_pad | grep ${NCO_PA} | grep -v grep | awk '{print $2}'`
### SUSE ONLY
					[ "$PID" != "" ]
					rc_status -v
					rc_exit
### END SUSE ONLY
### REDHAT ONLY
					if [ "$PID" != "" ]; then
						echo_success;
					else
						echo_failure;
					fi
					[ "$BOOTUP" != "verbose" ] && echo 
### END REDHAT ONLY
				else
					[ "$BOOTUP" != "verbose" ] && echo "Netcool/OMNIbus : Process Control not configured"
				fi
			else
				echo "Netcool/OMNIbus : nco_pad is not executable"
			fi
			;;
		'N')
			[ "$BOOTUP" != "verbose" ] && echo "Netcool/OMNIbus : Process Control not starting"
			;;
		*)
			echo "Netcool/OMNIbus : START_NCO incorrectly set"
			;;
	esac
	;;

'stop')

	PID=`ps auxwww | grep nco_pad | grep -v grep | awk '{print $2}'`
	if [ "$PID" != "" ]; then
		[ "$BOOTUP" != "verbose" ] && echo -n "Netcool/OMNIbus : Stopping Process Control ..."
		killpad
### SUSE ONLY
		rc_status -v
		rc_exit
### END SUSE ONLY
### REDHAT ONLY
		if [ $? -eq 0 ]; then
			echo_success
		else
			echo_failure
		fi
		[ "$BOOTUP" != "verbose" ] && echo
### END REDHAT ONLY
	else
		echo "Netcool/OMNIbus : Process Control not running ..."
	fi

	;;
restart|reload)
	$0 stop
	$0 start
	;;
*)
	echo "Usage: /etc/init.d/nco { start | stop | restart | reload }"
	;;
esac
