#! /usr/bin/ksh
#
# ident	"@(#)sap_fm_start.shi	1.6	98/07/24 SMI"
#
# Copyright (c) 1997 by Sun Microsystems, Inc.
# All rights reserved.
#

#
#	Copyright 11/18/96 Sun Microsystems, Inc.  All Rights Reserved.
#
#
#pragma ident "@(#)ds_boiler	1.1 97/06/12 SMI"
#
#ident "@(#)ds_boiler		1.7	96/11/18 SMI"
#
# common boiler for HA Internet Pro 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
. ds_utilities

# add the ha-service specific clust_progs
expr "$prog_path" : '.*/clust_progs' >/dev/null 2>&1
if [ $? -eq 0 ]; then
	PATH=${prog_path}:${PATH}
else
	PATH=${prog_path}:${prog_path}/../clust_progs:${PATH}
fi

# add the ha-service specific fault_progs
expr "$prog_path" : '.*/fault_progs' >/dev/null 2>&1
if [ $? -eq 0 ]; then
    PATH=${prog_path}:${PATH}
else
    PATH=${prog_path}:${prog_path}/../fault_progs:${PATH}
fi

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

if [ $? -ne 0 ]; then
	logerr `gettext "Cannot obtain name of local host"`
	exit 1
fi
# compute hostnames of remote nodes
PHYS_HOSTS="`haget -f all_physical_hosts`"
for i in $PHYS_HOSTS; do
	if [ "$i" != "$LOCALHOST" ]; then
		REMOTEHOSTS="$REMOTEHOSTS $i"
	fi
done
#! /bin/sh 
#
# pragma ident "@(#)do_service	1.5 98/01/14 SMI"
#
#

ARG_MASTERED=$1
ARG_NOT_MASTERED=$2
SYSLOG_PREFIX="SUNWcluster.ha.sap"

# Replace comma with space to form an sh word list
MASTERED="`echo $ARG_MASTERED | tr ',' ' '`"
NOT_MASTERED="`echo $ARG_NOT_MASTERED | tr ',' ' '`"

HASAP_CONFIG_FILE=/etc/opt/SUNWscsap/hadsconf

#
# Call the parser to handle the config file.
#
if [ ! -f $HASAP_CONFIG_FILE ]; then
	logerr `gettext "$HASAP_CONFIG_FILE doesn't exist"`
	exit 1
fi

. ds_utilities.1.3

source_env_file $HASAP_CONFIG_FILE

if [ $? -ne 0 ]; then
	# source_env_file logs error message if it fails.
	# No need to log another; just exit.
	exit 1
fi

#
# Timeout to waiting for SIGTERM to stop a process
# This should be in the config file
#
STOP_TIMEOUT=15
#
# bundle_do_svc <action>
#
# is called for each instance
#
bundle_do_svc ()
{
	action=$1

	SAP_START=${_INST_BASE_DIR}/hasap_start_net
	SAP_STOP=${_INST_BASE_DIR}/hasap_stop_net

	prefix="$SYSLOG_PREFIX.$action"

	case $action in

	'start')

		# First do some error checking.

		if [ ! -x $SAP_START ]; then
			logerr "$prefix.4000" \
				`gettext "<$SAP_START> is not executable."`
			exit 1
		fi

		# The process monitor facility calls the start program,
		# passing to it the instance-specific information it needs.
		# Note that we're using pmf to start/stop, but not to probe.

		if [ ${_INST_RETRY} = "n" ]; then
			pmfadm -c ${_INST_NAME} $SAP_START
		else
			pmfadm -c ${_INST_NAME}          \
			       -n ${_INST_RETRY_TIMES}    \
			       -t ${_INST_RETRY_INTERVAL} \
			       -a ${_INST_PROBE_CALLBACK_1} \
			       $SAP_START
		fi

		if [ $? -ne 0 ]; then
			logerr "$prefix.4001" \
				`gettext "pmfadm failed to start SAP instance ${_INST_NAME}"`
			exit 1
		else
			lognotice "$prefix.2000" \
				`gettext "Started SAP instance ${_INST_NAME}"`

		fi
	;;

	'stop' | 'abort')

		# delete from queue, but don't kill
		pmfadm -s ${_INST_NAME}
		if [ $? -ne 0 ]; then
			logerr "$prefix.4002" \
				`gettext "pmfadm failed to delete ${_INST_NAME} from queue"`
			exit 1
		fi

		# use Netscape's stop script to stop nshttp instance
		if [ ! -x $SAP_STOP ]; then
			logerr "$prefix.4003" `gettext "<$SAP_STOP> is not executable"`
			exit 1
		fi

		$SAP_STOP

		if [ $? -ne 0 ]; then
			logerr "$prefix.4004" `gettext "$SAP_STOP execution failed."`
		else
			lognotice "$prefix.2001" \
			    `gettext "Stopped SAP instance ${_INST_NAME}"`
		fi

		ha_svc_not_running ${_INST_NAME}

		if [[ $? -ne 0 ]]; then
        	# Now kill any processes left out
        	pmfadm -s ${_INST_NAME} KILL
        	if [ $? -ne 0 ]; then
            	logerr "$prefix.4009" \
                	`gettext "pmfadm failed to kill ${_INST_NAME}'s process and its sub-processes (if any) : pmfadm returned $?"`
            	exit 1
        	fi
		fi

	;;

	'fm_start')

		# XXX
		need_to_run_probe ${_INST_LOGICAL_HOST} ${LOCALHOST}

		if [ $? -ne 0 ]; then
			exit 0
		fi

		# pmf starts nshttp_probe
		# nshttp_probe runs until nshttp_fm_stop kills it.
		# Don't start probe if diskset is in maintenance mode.

		# If this SAP instance's diskset is in maint mode, exit now.
		MAINT=`haget -f is_maint -h ${_INST_LOGICAL_HOST}`
		if [ "$MAINT" = "1" ]; then
			exit 0
		fi

		pmfadm -c ${_INST_NAME}.probe ${_INST_PROBE_PROG_1} \
		    ${_INST_NAME}

		if [ $? -ne 0 ]; then
			logerr "$prefix.4005" \
	`gettext "pmfadm failed to start SAP probe for instance ${_INST_NAME}"`
			exit 1
		else
			lognotice "$prefix.2002" \
		`gettext "Started SAP probe instance ${_INST_NAME}.probe"`
		fi
	;;

	'fm_stop')

		# If probe not running, do nothing
		ha_svc_not_running ${_INST_NAME}.probe && exit 0

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

	'fm_check_this_host_ok')

		# If the HA-SAP logical host for this instance
		# is not currently mastered by this machine, exit now.

		is_member "${_INST_LOGICAL_HOST}" "$MASTERED"
		if [ $? -ne 0 ]; then
			exit 0
		fi

		# Otherwise, probe Web service now.
		# If dead, request will time out in
		# ${_INST_PROBE_TIMEOUT_1} secs.

		SAPPROBEFILE=/tmp/${_INST_NAME}.probe.$$

		hatimerun -t ${_INST_PROBE_TIMEOUT_1} \
		    /usr/bin/telnet ${_INST_LOGICAL_HOST} \
		    ${_INST_PORT} <<EOF > $SAPPROBEFILE 2>&1

EOF
		if [ $? -eq 99 ]; then
			# timeout
logerr "$prefix.4007" \
	`gettext "This server is supposed to be providing SAP service for instance <${_INST_NAME}>, but request timed out"`
			exit 1
		fi
		grep refused $SAPPROBEFILE > /dev/null 2>&1
		if [ $? -eq 0 ]; then
logerr "$prefix.4008" \
	`gettext "This server is supposed to be providing SAP service for instance <${_INST_NAME}>, but isn't"`
			exit 1
		fi
	;;

	esac

	exit 0
}
#include_boiler

# Usage: hasap_fm_start <mastered list> <not mastered list> <timeout>
# Called by Solstice HA framework during reconfiguration.
# Starts up dns fault probing.  

ARGV0=`basename $0`
PATH0=`dirname $0`
SYSLOG=`haget -f syslog_facility`
SAPCONF=/etc/opt/SUNWscsap/hadsconf
MYNAME=`uname -n`
HA_DEBUG=1

#
# take this out before fcs
#
if [ $HA_DEBUG -ne 0 ];then
  logger -p ${SYSLOG}.info \
   "$0" "$1" "$2"
fi

prog_path=`dirname $0`

# add the ha-service specific clust_progs path since
# it will not co-reside with the framework clust_progs.
# if it's a fault monitor method, add also fault_progs.
expr "$prog_path" : '.*/clust_progs' >/dev/null 2>&1
if [ $? -eq 0 ]; then
	PATH=${prog_path}:${PATH}
else
	PATH=${prog_path}:${prog_path}/../clust_progs:${PATH}
fi

# source in ha-services common utilities
. ds_utilities.1.3

source_env_file $SAPCONF


#
# SAP variables
#
NOTAVAIL=${_INST_1_PRIV_NOT_AVAIL}
SAPSID=${_INST_1_PRIV_YOUR_SAP_SID}
DBSID=${_INST_1_PRIV_YOUR_DB_SID}
CI=${_INST_1_PRIV_CI_LOGICAL_HOSTNAME}
DB=${_INST_1_PRIV_DB_LOGICAL_HOSTNAME}
NFS=${_INST_1_PRIV_NFS_LOGICAL_HOSTNAME}
SAPADM=${_INST_1_PRIV_SAP_ADMIN_LOGIN_NAME}
DBADM=${_INST_1_PRIV_DB_ADMIN_LOGIN_NAME}
SAPMNT1=${_INST_1_PRIV_USR_SAP_TRANS_MOUNT_POINT}
SAPMNT2=${_INST_1_PRIV_SAP_MOUNT_POINT_B}
SAPMNT3=${_INST_1_PRIV_SAP_MOUNT_POINT_C}
SAPMNT4=${_INST_1_PRIV_SAP_MOUNT_POINT_D}
AUTO_NFSMOUNT=${_INST_1_PRIV_AUTO_NFS_MOUNT}


generic_svc fm_start "$1" "$2" "$3"
