#!/bin/bash
#
# phantomctl v1.2
# (c)2000-2005 Marek Uher <marek.uher@t-mobile.cz>
#
# Control script for the Phantom Application Server II
#

#
# Include Phantom environment
#
PHANTOM_BASE=/services/www/phantom
if [ -f ${PHANTOM_BASE}/env/phantom.env ]; then
	. ${PHANTOM_BASE}/env/phantom.env
else
	echo "Can't load Phantom environment!"
	exit 0
fi

#
# Include Oracle environment
#
if [ -f ${PHANTOM_BASE}/env/oracle_${PHANTOM_ORACLE_VERSION}.env ]; then
	. ${PHANTOM_BASE}/env/oracle_${PHANTOM_ORACLE_VERSION}.env
else                                                                            
	echo "Can't load Oracle ${PHANTOM_ORACLE_VERSION} environment!"
	exit 0
fi
                                                                                
#
# Include JAVA environment
#
if [ -f ${PHANTOM_BASE}/env/java.env ]; then
	. ${PHANTOM_BASE}/env/java.env
else
	echo "Can't load JAVA environment!"
fi

#
# Include ANT environment
#
if [ -f ${PHANTOM_BASE}/env/ant.env ]; then
	. ${PHANTOM_BASE}/env/ant.env
else
	echo "Can't load ANT environment!"
fi

#
# Global settings
#
NAME=phantom
OWNER=${PHANTOM_USER}
PATH=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=${PHANTOM_HOME}/sbin/${NAME}
APACHECTL=${PHANTOM_HOME}/sbin/apachectl
PIDFILE=${PHANTOM_HOME}/var/${NAME}.pid
SERVER_ROOT=${PHANTOM_HOME}
PHANTOM_BANNER="Phantom Application Server II"

export LANG=C
export PATH

test -f ${DAEMON} || exit 0

#
# Set phantomctl logging
#
PHANTOMCTL_LOG="${PHANTOM_HOME}/logs/phantom/error/global/${NAME}ctl-`/bin/date '+%Y-%m-%d'`.log"
PHANTOMCTL_SYMLINK="${PHANTOM_HOME}/logs/phantom/error/global/${NAME}ctl.log"
if [ ! -f ${PHANTOMCTL_LOG} ]; then
        touch ${PHANTOMCTL_LOG}
fi
rm -f ${PHANTOMCTL_SYMLINK} > /dev/null 2>&1
ln -s ${PHANTOMCTL_LOG} ${PHANTOMCTL_SYMLINK} > /dev/null 2>&1

#
# Set PHP logging
#
PHANTOM_PHP_LOG="${PHANTOM_HOME}/var/php/php-`/bin/date '+%Y-%m-%d'`.log"
PHANTOM_PHP_SYMLINK="${PHANTOM_HOME}/var/php/php.log"
if [ ! -f ${PHANTOM_PHP_LOG} ]; then
	touch ${PHANTOM_PHP_LOG}
fi
rm -f ${PHANTOM_PHP_SYMLINK} >> ${PHANTOMCTL_LOG} 2>&1
ln -s ${PHANTOM_PHP_LOG} ${PHANTOM_PHP_SYMLINK} >> ${PHANTOMCTL_LOG} 2>&1

#
# Destroy Phantom Application Server II IPC objects
#
destroy_phantom_ipc_objects ( ) {

	echo -n "Destroying Phantom IPC objects: "
	IPC_MEMORY=`ipcs -m | grep ${PHANTOM_USER} | awk '/^0x/{ print $2 }' | sed 's/'"${PHANTOM_USER}"'//g'`
	IPC_SEMAPHORES=`ipcs -s | grep ${PHANTOM_USER} | awk '/^0x/{ print $2 }' | sed 's/'"${PHANTOM_USER}"'//g'`
	IPC_MESSAGES=`ipcs -q | grep ${PHANTOM_USER} | awk '/^0x/{ print $2}' | sed 's/'"${PHANTOM_USER}"'//g'`

	for mem in ${IPC_MEMORY}; do
		ipcrm shm $shm >> ${PHANTOMCTL_LOG} 2>&1
	done
	echo -n "shm "
	for sem in ${IPC_SEMAPHORES}; do
		ipcrm sem $sem >> ${PHANTOMCTL_LOG} 2>&1
	done
	echo -n "sem "
	for msg in ${IPC_SEMAPHORES}; do
		ipcrm msg $msg >> ${PHANTOMCTL_LOG} 2>&1
	done
	echo "msg."
}

#
# Destroy Phantom Application Server II loggers
#
destroy_phantom_loggers ( ) {

	echo -n "Destroying Phantom loggers: "
	for SIGNAL in 15 9; do
		killall -${SIGNAL} cronolog >> ${PHANTOMCTL_LOG} 2>&1
	done
	echo -n "cronolog "
	for SIGNAL in 15 9; do
		killall -${SIGNAL} logger >> ${PHANTOMCTL_LOG} 2>&1
	done
	echo "logger."
}

#
# Destroy Phantom Application Server II permormance monitor
#
destroy_phantom_performance_monitor ( ) {

	PHANTOM_PERFORMANCE_MONITOR_CONFIG=${PHANTOM_HOME}/conf/admin/performance_monitoring.conf
	if [ -e ${PHANTOM_PERFORMANCE_MONITOR_CONFIG} ]; then
		echo -n "Destroying Phantom performance agent(s): "
		PHANTOM_PERFORMANCE_MONITOR_AGENT=`mawk -F':' '/BenchmarkWithSysInfo/{ PM_ARGC = split ( $2, PM_ARGV, "\/" ); print PM_ARGV[PM_ARGC] }' ${PHANTOM_PERFORMANCE_MONITOR_CONFIG}`
		for SIGNAL in 15 9; do
			killall -${SIGNAL} ${PHANTOM_PERFORMANCE_MONITOR_AGENT} >> ${PHANTOMCTL_LOG} 2>&1
		done
		PHANTOM_PERFORMANCE_MONITOR_SOCKET=`awk -F':' '/BenchmarkWithSysInfo/{ print $3 }' ${PHANTOM_PERFORMANCE_MONITOR_CONFIG}`
		rm -f ${PHANTOM_PERFORMANCE_MONITOR_SOCKET} >> ${PHANTOMCTL_LOG} 2>&1
		echo -n "`echo ${PHANTOM_PERFORMANCE_MONITOR_AGENT} | awk -F '/' '{ print $NF }'` "
		PHANTOM_PERFORMANCE_STAT_AGENT=`mawk -F':' '/BenchmarkWithStatProg/{ PM_ARGC = split ( $2, PM_ARGV, "\/" ); print PM_ARGV[PM_ARGC] }' ${PHANTOM_PERFORMANCE_MONITOR_CONFIG}`
		for SIGNAL in 15 9; do
			killall -${SIGNAL} ${PHANTOM_PERFORMANCE_STAT_AGENT} >> ${PHANTOMCTL_LOG} 2>&1
		done
		PHANTOM_PERFORMANCE_MONITOR_SOCKET=`awk -F':' '/BenchmarkWithStatProg/{ print $3 }' ${PHANTOM_PERFORMANCE_MONITOR_CONFIG}`
		rm -f ${PHANTOM_PERFORMANCE_MONITOR_SOCKET} >> ${PHANTOMCTL_LOG} 2>&1
		echo "`echo ${PHANTOM_PERFORMANCE_STAT_AGENT} | awk -F '/' '{ print $NF }'`."
	fi
}

#
# Phantom Application Server II usage
#
phantom_usage ( ) {

	echo "Usage: $0 {start|stop|restart|reload|force-reload|kill|status|fullstatus|info|configtest}"
}

#
# Start Phantom Application Server II
#
phantom_start ( ) {

	echo -en "Starting ${PHANTOM_BANNER} [build: #${PHANTOM_VERSION}]: "
	${APACHECTL} start >> ${PHANTOMCTL_LOG} 2>&1
	if [ "$?" == 0 ]; then
		echo "started."
	else
		echo "failed."
	fi
}

#
# Stop Phantom Application Server II
#
phantom_stop ( ) {

	echo -en "Stopping ${PHANTOM_BANNER} [build: #${PHANTOM_VERSION}]: "
	${APACHECTL} stop >> ${PHANTOMCTL_LOG} 2>&1
	if [ "$?" == 0 ]; then
		echo "stopped."
	else
		echo "failed."
	fi
	destroy_phantom_loggers
	destroy_phantom_ipc_objects
	destroy_phantom_performance_monitor
}

#
# Reload Phantom Application Server II
#
phantom_reload ( ) {

	echo -en "Reloading ${PHANTOM_BANNER} [build: #${PHANTOM_VERSION}] configuration: "
	${APACHECTL} graceful$2 >> ${PHANTOMCTL_LOG} 2>&1
	if [ "$?" == 0 ]; then
		echo "reloaded."
	else
		echo "failed."
	fi
}

#
# Restart Phantom Application Server II
#
phantom_restart ( ) {

	echo -en "Restarting ${PHANTOM_BANNER} [build: #${PHANTOM_VERSION}]: "
	${APACHECTL} restart >> ${PHANTOMCTL_LOG} 2>&1
	if [ "$?" == 0 ]; then
		echo "restarted."
	else
		echo "failed."
	fi
}

#
# Force reload Phantom Application Server II
#
phantom_force_reload ( ) {

	echo -en "Forcing reload of ${PHANTOM_BANNER} [build: #${PHANTOM_VERSION}]: "
	${APACHECTL} stop >> ${PHANTOMCTL_LOG} 2>&1
	sleep 10
	${APACHECTL} start >> ${PHANTOMCTL_LOG} 2>&1
	if [ "$?" == 0 ]; then
		echo "force reloaded."
	else
		echo "failed."
	fi
}

#
# Kill Phantom Application Server II
#
phantom_kill ( ) {

	echo -en "Killing ${PHANTOM_BANNER} [build: #${PHANTOM_VERSION}]: "
	for SIGNAL in 15 9; do
		ps -ef | fgrep "${DAEMON}" | fgrep -v grep | awk '{ print $2 }' | xargs kill -${SIGNAL} >> ${PHANTOMCTL_LOG} 2>&1
		sleep 3
	done
	echo "killed."
	destroy_phantom_loggers
	destroy_phantom_ipc_objects
	destroy_phantom_performance_monitor
}

#
# Phantom Application Server II info
#
phantom_info ( ) {

	echo -e "${PHANTOM_BANNER} [build: #${PHANTOM_VERSION}] info:"
	echo " "
	echo "Phantom Application Server II core:"
	${PHANTOM_HOME}/sbin/phantom -V | awk '{ printf "  %s\n", $0 }'
	${PHANTOM_HOME}/sbin/phantom -l | awk '{ printf "  %s\n", $0 }'
	echo -e "\n${PHANTOM_BANNER} build-in clients:"
	echo "  MySQL client:		`dpkg -l | grep mysql-client | sort | tail -1 | awk '{ print $3 }'`"
	echo "  PostgreSQL client:	`dpkg -l | grep postgresql | tail -1 | awk '{ print $3 }'`"
	echo "  ORA client:		${ORACLE_VERSION}"
	echo "  ANT client:		${ANT_VERSION}"
	echo "  JDK client:		${JAVA_VERSION}"
	echo " "
	echo "Phantom Application Server II PHP:"
	${PHANTOM_HOME}/extensions/php/bin/php -v | awk '{ printf "  %s\n", $0 }'
	echo " "
	echo "Phantom Application Server II PHP config:"
	${PHANTOM_HOME}/extensions/php/bin/php-config --version | awk '{ printf "  php-config: %s\n", $0 }'
	echo " "
	echo "Phantom Application Server II PEAR packages:"
	${PHANTOM_HOME}/extensions/php/bin/pear list | egrep -v "^(Installed|===|INSTALLED|PACKAGE)" | awk '{ printf "  %s\n", $0 }'
	echo " "
}

#
# Phantom Application Server II staus
#
phantom_status ( ) {

	echo -n "${PHANTOM_BANNER} [build: #${PHANTOM_VERSION}] status: "
	PHANTOM_STATUS=`ps -ef | fgrep "${DAEMON}" | fgrep -v grep`
	if [ "${PHANTOM_STATUS}" = "" ]; then
		echo "not running."
	else
		PHANTOM_PROCESSES=`ps -ef | fgrep "${DAEMON}" | fgrep -v grep | wc -l | sed 's/[ ,\t]//g'`
		echo "${PHANTOM_PROCESSES} running process(es)."
	fi
}

#
# Phantom Application Server II full staus
#
phantom_fullstatus ( ) {

	echo "Full status of ${PHANTOM_BANNER} [build: #${PHANTOM_VERSION}] [process table]:"
	HTTP_ENG_02_STATUS=`ps auxf | fgrep ${OWNER} | fgrep "${SERVER_ROOT}/sbin/${NAME}" | fgrep -v grep`
	if [ "${HTTP_ENG_02_STATUS}" = "" ]; then
		echo -e "Processes table for server ${NAME} is empty. Server not running."
	else
		echo -e " Process owner\tProcess ID\tParent ID\tServer instance"
		echo -e " -------------\t----------\t---------\t---------------"
		ps -ef | fgrep ${OWNER} | grep "\/sbin\/${NAME}" | fgrep -v grep | awk '{ printf " %s\t%d\t\t%s\t\t%s\n", "'"${OWNER}"'", $2, $3,"'"${NAME}"'" }' | sort
		echo -e " =============\t==========\t=========\t==============="
		echo -e "End of process table for ${PHANTOM_BANNER}.\n"
	exit 0
	fi
}

#
# Phantom Application Server II configuration test
#
phantom_configtest ( ) {

	${APACHECTL} configtest
}

#
# See how we were called
#
case $1 in

	start)
		phantom_start
		;;

	stop)
		phantom_stop
		;;

	reload)
		phantom_reload
		;;

	restart)
		phantom_restart
		;;

	force-reload)
		phantom_force_reload
		;;

	kill)
		phantom_kill
		;;

	status)
		phantom_status
		;;

	fullstatus)
		phantom_fullstatus
		;;

	info)
		phantom_info
		;;

	configtest)
		phantom_configtest
		;;

	*)
		phantom_usage
		;;

esac
exit 0

# /* End of file: phantomctl */
