#! /bin/sh
#
# ident	"@(#)nsldap_fm_stop.shi	1.3	97/06/09 SMI"
#
# Copyright (c) 1996-1997 by Sun Microsystems, Inc.
# All rights reserved.
#

#
# Stop fault monitoring.
# 

#
#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`

#
#	Copyright 06/24/99 Sun Microsystems, Inc.  All Rights Reserved.
#
#ident "@(#)do_service		1.13	99/06/24 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.nsldap"

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

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

# XXX
#set -x
	action=$1
	prefix="$SYSLOG_PREFIX.$action"

#lognotice "bundle_do_svc: called with action '$action' for instance '$_INST_NAME' "

	LOGICAL_HOST=`get_config_param $_INST_NAME LOGICAL_HOST`
	BASE_DIR=`get_config_param $_INST_NAME BASE_DIR`
        if [ -f $BASE_DIR/start-slapd ] ;
        then
                LDAP_START=${BASE_DIR}/start-slapd
                LDAP_STOP=${BASE_DIR}/stop-slapd
        else
                LDAP_START=${BASE_DIR}/start
                LDAP_STOP=${BASE_DIR}/stop
        fi

	case $action in
	'start')

		if [ ! -x $LDAP_START ]; then
			logerr "$prefix.4040" `gettext "Unable to execute $BASE_DIR/start"`
			exit 1
		fi

		#
		# launch the daemon using the process monitor
		#
		ha_svc_not_running $_INST_NAME
		if [ $? -eq 0 ]; then
			pmfadm -c $_INST_NAME $LDAP_START
			if [ $? -ne 0 ]; then
				logerr "$prefix.4000" `gettext "failed to start Netscape Directory Server instance $_INST_NAME"`
				exit 1
			else
				lognotice "$prefix.2000" `gettext "Started Netscape Directory Server instance $_INST_NAME"`
			fi
		fi
		;;

	'start_net')
		#
		# START_NET is always called after START, so at this point
		# the daemon should be running (we're doing the actual start 
		# at START time). The bundles framework, as part of the idempotency
		# check, will notice that the daemon for this instance is running,
		# and will not call bundle_do_svc(), so this code should never 
		# be executed.
		#
		lognotice "$prefix.2010" `gettext "start_net method called for instance $_INST_NAME"`
		ha_svc_not_running $_INST_NAME
		if [ $? -eq 0 ]; then
			logerr "$prefix.3002" `gettext "start_net: instance $_INST_NAME is down"`
			exit 1
		fi
		;;

	'stop' )
		ha_svc_not_running $_INST_NAME
		if [ $? -ne 0 ]; then
			pmfadm -s $_INST_NAME
			if [ $? -ne 0 ]; then
				logerr "$prefix.4001" `gettext "pmfadm failed to delete  $_INST_NAME from queue"`
				exit 1
			fi
			if [ ! -x $LDAP_STOP ]; then   
				logerr "$prefix.4002"  \
			`gettext "$LDAP_STOP is not executable"` 
				exit 1
			fi		
		
			method_timeout=`hareg -q nsldap -T stop`
			hatimerun -t $method_timeout $LDAP_STOP
			if [ $? -ne 0 ]; then
				logerr "$prefix.4003" \
	`gettext "pmfadm failed to kill ${_INST_NAME}'s process"`
				exit 1
			else
				lognotice "$prefix.2030" \
				`gettext "Stopped LDAP instance ${_INST_NAME}"`
			fi
		fi
		;;

	'stop_net' )
		#
		;;

	'abort' | 'abort_net')
		pmfadm -s $_INST_NAME KILL
		if [ $? -ne 0 ]; then
			logerr "$prefix.4024" `gettext "failed to abort/net instance $_INST_NAME"`
			exit 1
		fi
		;;

	'fm_init')
		lognotice "$prefix.2005" `gettext "fm_init not implemented "`
		;;

	'fm_start')
		#
		# Check if we are about to probe a logical host we master (local
		# probe),  or one which another host masters (remote probe), or
		# one in maintenance mode (don't probe)
		#
		maint=`haget -f is_maint -h $LOGICAL_HOST`
		if [ $? -ne 0 ]; then
			logerr "$prefix.4030" `gettext "haget(1M) failed; could be an error is logical host name $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.
			# We are using the process monitor just to start and tag
			# the probe, without the retry feature of 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.4020" `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.
		#
                # If probe not running, do nothing
                ha_svc_not_running ${_INST_NAME}.probe && exit 0

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

	'fm_check_this_host_ok')
		#
		# Yap, we are OK
		#
		lognotice "$prefix.2021" `gettext "This host is OK for instance $_INST_NAME"`
		;;

	esac

	exit 0
# XXX
#set +x

}

#include_boiler

# Pass args explicitly to preserve null ones.
# The return code of generic_svc will be the exit code of the method
generic_svc fm_stop "$1" "$2" "$3"
