#!/bin/ksh
#
# pragma ident  "@(#)apache_probe.shi 1.7     01/03/27 SMI"
#
#Copyright 12/20/96 Sun Microsystems, Inc.  All Rights Reserved.
#

# Usage: apache_probe <instance name> <local_flag>
# Started up in the background via pmfd in apache_fm_start
# during reconfiguration.

#
# Add the path to framework binaries, since the probe is not called in the
# context of the methods
#
PATH=$PATH:/opt/SUNWcluster/bin:/opt/SUNWcluster/ha/apache
export PATH

INST_NAME=$1
LOCAL=$2

#
#pragma ident "@(#)ds_boiler	1.3	98/09/15 SMI"
#
# common boiler for HA data services
#
#


ARGV0=`basename $0`
LOGGER=logger
HA_SLOGFACILITY=`haget -f syslog_facility`
HA_SLOGTAG=hadf
prog_path=`dirname $0`

# source in ha-services common utilities
. hads_utilities

# add the ha-service directory to the path
PATH=${prog_path}:${PATH}

#
# for use by subsequent hactl command, get hostnames of local and remote hosts
#
LOCALHOST=`uname -n`

#!/bin/ksh
#
#	Copyright 06/24/99 Sun Microsystems, Inc.  All Rights Reserved.
#
# pragma ident  "@(#)do_service 1.19     00/06/19 SMI"
#
# do_service 
# Standard-name file that defines the standard-name routine bundle_do_svc(), 
# which implements the HA framework-defined methods for this data service.
# The _boiler file in this directory sources this file when the data
# service method scripts execute.


SYSLOG_PREFIX="SUNWcluster.ha.apache"

# Read in and interpret the data service environment configuration file
source_env_file /etc/opt/SUNWscapc/hadsconf || exit 1
STOP_PROBE_TIMEOUT=15

#
# bundle_do_svc <action>
#
# called for each instance
#
bundle_do_svc ()
{

	action=$1
	prefix="$SYSLOG_PREFIX.$action"

	LOGICAL_HOST=`get_config_param ${_INST_NAME} LOGICAL_HOST`
	BIN_DIR=`get_config_param ${_INST_NAME} PRIV_BIN_DIR`
	CONF_DIR=`get_config_param ${_INST_NAME} PRIV_CONF_DIR`
	APACHE_SERVER=$BIN_DIR/httpd
	CONF_FILE=$CONF_DIR/httpd.conf

	case $action in
	'start')
		# launch the daemon using the process monitor
		ha_svc_not_running ${_INST_NAME}
		if [ $? -eq 0 ]; then
			#make sure the httpd is present and executable
			if [ ! -x $APACHE_SERVER ]; then
				logerr "$prefix.4000" `gettext "$APACHE_SERVER is not executable for instance ${_INST_NAME}"`
				exit 1
			fi
			
			#check to see if the httpd.conf file is there
			if [ ! -f $CONF_FILE ]; then
				logerr "$prefix.4099" `gettext "$CONF_FILE could not be found for instance ${_INST_NAME}"`
				exit 1
			fi

			# check whether the conf file looks ok to apache
			# the webserver doesnt actually start here,
			# the -T option only tests the syntax of the config file
			# -f specifies the config file to be examined
			$APACHE_SERVER -f $CONF_FILE -T >/dev/null 2>&1
			if [ $? -ne 0 ]; then
				logerr "$prefix.4098" `gettext "Apache web server found errors in $CONF_FILE for instance ${_INST_NAME}"`
				exit 1
			fi

			# the -f option instructs the web server to use the
			# specified config file rather than the compiled
			# in default
			pmfadm -c ${_INST_NAME} $APACHE_SERVER -f $CONF_FILE
			if [ $? -ne 0 ]; then
				logerr "$prefix.4001" `gettext "Failed to start Apache web server instance ${_INST_NAME}"`
				exit 1
			else
				lognotice "$prefix.2000" `gettext "Started Apache web server instance ${_INST_NAME}"`
			fi
		fi
		;;

	'start_net')
		ha_svc_not_running ${_INST_NAME}
		if [ $? -eq 0 ]; then
			logerr "$prefix.4002" `gettext "start_net: instance ${_INST_NAME} is down"`
			exit 1
		fi
		;;

	'stop' )
		ha_svc_not_running ${_INST_NAME}
		if [ $? -ne 0 ]; then
			method_timeout=`hareg -q apache -T stop`
			if [ -z "$method_timeout" ]; then
				method_timeout=60
				lognotice "$prefix.2001" `gettext "Instance ${_INST_NAME} failed to obtain stop timeout value. Using a default of $method_timeout"`
			fi

			#calculate the time left for us to do a clean stop
			#save 5 seconds in case a KILL is needed
			#SECONDS tells us how much time has elapsed since
			#this script started (its a ksh variable)
			method_timeout=`expr $method_timeout - 5 - $SECONDS`

			if [ $method_timeout -gt 0 ] ; then
				#attempt an orderly shutdown
				pmfadm -s ${_INST_NAME} -w $method_timeout TERM
			fi

			# Now kill any process which still remains
			ha_svc_not_running ${_INST_NAME}
			if [ $? -ne 0 ]; then
				pmfadm -k ${_INST_NAME}
				if [ $? -ne 0 ]; then
					logerr "$prefix.4005" \
					`gettext "pmfadm failed to kill ${_INST_NAME}'s process"`
					exit 1
				fi
			else
				lognotice "$prefix.2002" \
				`gettext "Stopped APACHE instance ${_INST_NAME}"`
			fi
		fi
		;;

	'abort' | 'abort_net')
		#do nothing if the instance is not running
                ha_svc_not_running ${_INST_NAME} && exit 0

		# kill everything, and do it fast
		pmfadm -s ${_INST_NAME} KILL
		if [ $? -ne 0 ]; then
			logerr "$prefix.4006" `gettext "Failed to abort instance ${_INST_NAME}"`
			exit 1
		fi
		;;

	'fm_start')
		#do nothing if the probe is already running
		ha_svc_not_running ${_INST_NAME}.probe || exit 0
		
		maint=`haget -f is_maint -h $LOGICAL_HOST`
		if [ $? -ne 0 ]; then
			logerr "$prefix.4007" `gettext "haget(1M) failed for logical host $LOGICAL_HOST"`
			exit 1
		elif  [ $maint -eq 0 ]; then
			if is_member "$LOGICAL_HOST" "$MASTERED_LOGICAL_HOSTS" ; then
				local=yes
			else
				local=no
			fi
			
			# Launch a probe using the process monitor.
			pmfadm -c ${_INST_NAME}.probe /bin/sh -c "${_INST_PROBE_PROG_1} ${_INST_NAME} $local >/dev/null 2>&1"
			if [ $? -ne 0 ]; then
				logerr "$prefix.4008" `gettext "failed to start probe ${_INST_PROBE_PROG_1} for instance ${_INST_NAME}"`
				exit 1
			fi
		fi
		;;

	'fm_stop')
		# Kill the probe that is associated with this instance,
		# but only if it running
                ha_svc_not_running ${_INST_NAME}.probe && exit 0

                # PMF kills apache_probe
		pmfadm -s ${_INST_NAME}.probe -w ${STOP_PROBE_TIMEOUT} TERM || pmfadm -s ${_INST_NAME}.probe KILL
		if [ $? -ne 0 ]; then
                        logerr "$prefix.4009" \
    `gettext "pmfadm failed to stop ${_INST_NAME}.probe"`
                        exit 1
                else
                        lognotice "$prefix.2003" \
                        `gettext "Stopped probe for instance ${_INST_NAME}"`
                fi
		;;

	esac

	exit 0
}

#include_boiler

set_inst_name ${INST_NAME}

prefix="SUNWcluster.ha.apache.probe"

if [ -z "$INST_NAME" ]; then
	logerr "$prefix.4010" `gettext "Usage: $ARGV0 <instance> <local_flag>"`
	exit 1
fi

if [ -z "$LOCAL" ]; then
	logerr "$prefix.4011" `gettext "Usage: $ARGV0 <instance> <local_flag>"`
	exit 1
fi

MASTERED_LOGICAL_HOSTS="`haget -f mastered`"

APACHE_PORT=`get_config_param $INST_NAME PORT`
if [ -z "$APACHE_PORT" ]; then
	logerr "$prefix.4012" \
	    `gettext "Port value not set for instance $INST_NAME"`
	exit 1
fi

APACHE_HOST=`get_config_param $INST_NAME LOGICAL_HOST`

APACHE_PROBE_INTERVAL=`get_config_param $INST_NAME PROBE_1_INTERVAL`
# parser requires this to be set, but doesn't check for negative values
if [ $APACHE_PROBE_INTERVAL -le 0 ]; then
	lognotice "$prefix.2004" \
`gettext "INTERVAL value is negative for instance $INST_NAME; using 60 seconds"`
	APACHE_PROBE_INTERVAL=60
fi

APACHE_PROBE_TIMEOUT=`get_config_param $INST_NAME PROBE_1_TIMEOUT`
if [ -z "$APACHE_PROBE_TIMEOUT" ]; then
	lognotice "$prefix.2005" \
`gettext "TIMEOUT value not set for instance $INST_NAME; using 60 seconds"`
	APACHE_PROBE_TIMEOUT=60
elif [ $APACHE_PROBE_TIMEOUT -le 0 ]; then
	lognotice "$prefix.2006" \
`gettext "TIMEOUT is <= zero for instance $INST_NAME; resetting to 60 seconds"`
	APACHE_PROBE_TIMEOUT=60
fi

APACHE_TAKEOVER=`get_config_param $INST_NAME PROBE_1_TAKEOVER`
# optional parameter
if [ -z "$APACHE_TAKEOVER" ]; then
	lognotice "$prefix.2007" \
	   `gettext "TAKEOVER value not set for instance $INST_NAME; using 'y'"`
	APACHE_TAKEOVER=y
fi

APACHEGRACE=0
APACHEPROBEFILE=/var/opt/SUNWcluster/run/${INST_NAME}.apache_probe
RETRY=0
FAIL=0

while : ; do

	# Take a nap here, instead of at the end of loop
	# At start-up, this gives more time to server to initialize itself

	sleep $APACHE_PROBE_INTERVAL

	hatimerun -t $APACHE_PROBE_TIMEOUT /usr/bin/telnet $APACHE_HOST $APACHE_PORT <<EOF > $APACHEPROBEFILE 2>&1

EOF
	telnet_result=$?
	grep refused $APACHEPROBEFILE > /dev/null 2>&1
	#refer hatimerun(1M) for meaning of 99/98 result codes
	if [ $? -eq 0 -o $telnet_result -eq 99 -o $telnet_result -eq 98 ]; then
                FAIL=1
		# If running locally, restart it.
		# If it fails to restart successfully, the sibling
		# will eventually take over if TAKEOVER set to 'y'.
		if [ $LOCAL = "yes" ]; then
			logerr "$prefix.4013" `gettext "apache instance $INST_NAME failed locally"`
			RETRY=`expr $RETRY + 1`
			logerr "$prefix.4014" `gettext "restarting apache instance $INST_NAME; restart number $RETRY"`
			apache_svc_start "$MASTERED_LOGICAL_HOSTS" ""
		else
			logerr "$prefix.4015" `gettext "apache instance $INST_NAME failed on sibling"`
			# give sibling chance to restart apache
			# before doing a takeover
			if [ $APACHEGRACE -eq 0 ]; then
				APACHEGRACE=1
				continue
			fi
			if [ "$APACHE_TAKEOVER" = "y" ]; then
				# $REMOTEHOSTS set in ds_boiler
				# hactl is not yet available
				# Get "current master"
				CURRENT_MASTER="`haget -f master -h $APACHE_HOST`"
				pmfadm -c ${INST_NAME}.hactl hactl -t -s apache  -l $APACHE_HOST

        			if [ $? -ne 0 ]; then
            				logerr "$prefix.4016" `gettext "pmfadm failed to launch hactl for ${INST_NAME}"`
            				exit 1
        			else
            				lognotice "$prefix.2008" `gettext "Launched hactl for ${INST_NAME}"`
				fi

			fi
		fi
	else 
		if [ $FAIL -eq 1 ]; then
        		lognotice "$prefix.2009" `gettext "apache instance ${INST_NAME} restarted successfully"`
       		fi
                FAIL=0
		APACHEGRACE=0
	fi

done
