#!/bin/sh
PATH=/usr/bin:$PATH
EG_HOME=/opt/egurkha
export PATH EG_HOME
#============================================================================
# stop_agent: shell script that starts discovery
#=============================================================================
#./eGAgentmon stop > /dev/null 2>/dev/null
$EG_HOME/bin/eGAgentmon stop > /dev/null 2>/dev/null
$EG_HOME/bin/kill_ds > /dev/null 2>/dev/null

#pid_list=`ps -aef -opid,args | grep "EgMain" | grep -v grep | grep "java " | awk '{print $1}'` 

#code to get the list of pids of the zone in which agent is currently running. ie. for Solaris Zone machines
isZone=0
zonename=""
if [ -f "/sbin/zonename" ] || [ -f "/usr/bin/zonename" ]
then
	zonename=`zonename`
	isZone=1
fi
if [ $isZone -eq 1 ]
then
	pid_list=`ps -z $zonename -o pid,args | grep "EgMain" | grep -v grep | grep "java " | awk '{print $1}'` 
	if [ -z "$pid_list" ]
	then
	pid_list=`ps -z $zonename -o pid,args | grep " /opt/egurkha" | grep "java " | grep -v "\"\-Xnoclassgc\"" | grep -v grep | grep -v "EgCheckAgent" | awk '{print $1}'` 
	fi
	if [ -z "$pid_list" ]
	then
		if [ -f "/usr/ucb/whoami" ]
		then
			whoami=`/usr/ucb/whoami`
			pid_list=`/usr/ucb/ps -gauxwww | grep "EgMain" | grep $whoami | grep -v grep | grep "java " | cut -c1-300 | awk '{print $2}'`
		fi
	fi
else 
	pid_list=`ps -aef -opid,args | grep "EgMain" | grep -v grep | grep "java " | awk '{print $1}'` 
	#get the list of pids 
	if [ -z "$pid_list" ]
	then
		if [ -f "/usr/ucb/ps" ]
		then
		pid_list=`/usr/ucb/ps -gauxwww | grep "EgMain" | grep java | grep -v grep | grep "java " | cut -c1-300 | awk '{print $2}'`
		fi
	fi
fi

if [ "$pid_list" ] 	# if EgMainAgent is running
then
	for pid in $pid_list    # for each pid
	do
		# kill each child
		kill -9 $pid > /dev/null 2>&1 
	done
fi


#code to get the list of pids of the zone in which agent is currently running. ie. for Solaris Zone machines
if [ $isZone -eq 1 ]
then
	pid_list=`ps -z $zonename -o pid,args | grep "java EgHttp" | grep -v "grep" | awk '{print $1}'` 
	if [ -z "$pid_list" ]
	then
	pid_list=`ps -z $zonename -o pid,args | grep " /opt/egurkha" | grep jre | grep -v "EgCheckAgent" | grep -v grep | awk '{print $1}'` 
	fi
	if [ -z "$pid_list" ]
	then
		if [ -f "/usr/ucb/whoami" ]
		then
			whoami=`/usr/ucb/whoami`
			pid_list=`/usr/ucb/ps -gauxwww | grep " /opt/egurkha" | grep $whoami | grep -v "eg_manager" | grep "jre"  | grep -v "EgCheckAgent" | grep -v grep | cut -c1-300 | awk '{print $2}'`

		fi
	fi
else 
	pid_list=`ps -e -o pid,args | grep "java EgHttp" | grep -v "grep" | awk '{print $1}'`
	#get the list of pids
	if [ -z "$pid_list" ]
	then
		if [ -f "/usr/ucb/ps" ]
		then
		pid_list=`/usr/ucb/ps -gauxwww | grep "java EgHttp" |grep -v grep | awk '{print $2}'`
		fi
	fi
fi
#echo $pid_list
if [ "$pid_list" ]      # if EgHttpGet is running
then
        for pid in $pid_list    # for each pid
        do
                # kill each child
                kill -9 $pid > /dev/null 2>&1
        done
fi

#code to get the list of pids of the zone in which agent is currently running. ie. for Solaris Zone machines
if [ $isZone -eq 1 ]
then
	pid_list=`ps -z $zonename -o pid,args | grep "EgMain" | grep -v grep | grep java |  awk '{print $1}'` 
	if [ -z "$pid_list" ]
	then
		pid_list=`ps -z $zonename -o pid,args | grep " /opt/egurkha" | grep "java " | grep -v "EgCheckAgent" | grep -v grep | awk '{print $1}'` 
	fi
	if [ -z "$pid_list" ]
	then
		if [ -f "/usr/ucb/whoami" ]
		then
			whoami=`/usr/ucb/whoami`
			pid_list=`/usr/ucb/ps -gauxwww | grep "EgMain" | grep $whoami | grep -v grep | grep java | awk '{print $2}'`
		fi
	fi
else 
	pid_list=`ps -aef -opid,args | grep "EgMain" | grep -v grep | grep java |  awk '{print $1}'` 
	if [ -z "$pid_list" ]
	then
		if [ -f "/usr/ucb/ps" ]
		then
		pid_list=`/usr/ucb/ps -gauxwww | grep "EgMain" | grep java | grep -v grep | grep "java " | awk '{print $2}'`
		fi
	fi
fi
#get the list of pids 
if [ "$pid_list" ] 	# if EgMainAgent is running
then
	for pid in $pid_list    # for each pid
	do
		# kill each child
		kill -9 $pid > /dev/null 2>&1 
	done
fi

if [ -z "$1" ]
then
	echo "*************************************************"
	echo "The eG agent has been stopped successfully."
	echo "*************************************************"
fi

#code to get the list of pids of the zone in which agent is currently running. ie. for Solaris Zone machines
if [ $isZone -eq 1 ]
then
	pid_list=`ps -z $zonename -o pid,args | grep "EgMain" | grep -v "grep" | grep "java " | awk '{print $1}'` 
	if [ -z "$pid_list" ]
	then
	pid_list=`ps -z $zonename -o pid,args | grep " /opt/egurkha" | grep "java " | grep -v "EgCheckAgent" | grep -v grep | awk '{print $1}'` 
	fi
	if [ -z "$pid_list" ]
	then
		if [ -f "/usr/ucb/whoami" ]
		then
			whoami=`/usr/ucb/whoami`
			pid_list=`/usr/ucb/ps -gauxwww | grep "EgMain" | grep $whoami | grep "java " | grep -v grep  | awk '{print $2}'`
		fi
	fi
else 
	pid_list=`ps -e -o pid,args | grep "EgMain" | grep "java " | grep -v "grep" | awk '{print $1}'`
	#get the list of pids
	if [ -z "$pid_list" ]
	then
		if [ -f "/usr/ucb/ps" ]
		then
		pid_list=`/usr/ucb/ps -gauxwww | grep "EgMain" | grep "java " | grep -v grep | awk '{print $2}'`
		fi
	fi
fi
#echo $pid_list
if [ "$pid_list" ]      # if EgHttpGet is running
then
        for pid in $pid_list    # for each pid
        do
                # kill each child
                kill -9 $pid > /dev/null 2>&1
        done
fi

#code to get the list of pids of the zone in which agent is currently running. ie. for Solaris Zone machines
if [ $isZone -eq 1 ]
then
	pid_list=`ps -z $zonename -o pid,args | grep "traceroute" | grep -v "grep" | awk '{print $1}'` 
	if [ -z "$pid_list" ]
	then
		if [ -f "/usr/ucb/whoami" ]
		then
			whoami=`/usr/ucb/whoami`
			pid_list=`/usr/ucb/ps -gauxwww | grep "traceroute" | grep $whoami | grep -v "grep" | awk '{print $2}'`
		fi
	fi
else 
	pid_list=`ps -e -o pid,args | grep "traceroute" | grep -v "grep" | awk '{print $1}'`
	#get the list of pids
	if [ -z "$pid_list" ]
	then
		if [ -f "/usr/ucb/ps" ]
		then
		pid_list=`/usr/ucb/ps -gauxwww | grep "traceroute" |grep -v grep | awk '{print $2}'`
		fi
	fi
fi
#echo $pid_list
if [ "$pid_list" ]      # if traceroute is running
then
        for pid in $pid_list    # for each pid
        do
                # kill each child
                kill -9 $pid > /dev/null 2>&1
        done
fi



