#! /usr/bin/ksh
#
# Copyright (c) 1998-2002 by Sun Microsystems, Inc.
# All rights reserved.

#pragma ident	"@(#)sybase_monitor_stop.ksh	1.2	02/01/03 SMI"
#
# HA Sybase ASE Monitor_start method
# 
# Parameters passed:  -R <resource name> -T <type> -G <group>
#
#
#pragma ident	"@(#)sybase_lib.ksh	1.6	02/04/24 SMI"
#
# Copyright 1998-2002 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#


# Treat undefined variables as null.
# Disable wildcard expansion
# Script is running in suid mode.
set -ufp

#
# Set defaults, which could be overridden later
#
MYDIR=`/bin/dirname $0`
BINDIR=`/bin/dirname $0`
ETCDIR="$BINDIR/../etc"
DEBUG_LEVEL=0  # Sri 

#unset PATH
#export PATH="/bin:/usr/cluster/bin"


#
# These are currently hardcoded. Could choose to use an extension property. 
#
DEBUG_LOGFILE="/opt/SUNWscsyb/log/message_log."

#Maximum size i.e. 512 KB
DEBUG_LOG_SIZE=524288

ARGUMENTS="$0 ${@:-""}"
RESOURCE_NAME=""
RESOURCE_TYPE=""
RESOURCE_GROUP=""
SCLOGGER=/usr/cluster/lib/sc/scds_syslog


MONITOR_BASE=/opt/SUNWscsyb/bin/sybase_monitor
MONITOR_ACTIONS=/opt/SUNWscsyb/etc/sybase_monitor_actions
MONITOR_ONLINE_CHECK=/opt/SUNWscsyb/etc/sybase_monitor_online_check
MONITOR_RESTART_HISTORY=/opt/SUNWscsyb/log/restart_history


# The time that the start method actually "waits" before subsequent
# checks if any ASE server is up 
# is START_METHOD_TIMEOUT/SERVER_START_SLEEP_FACTOR
# 
SERVER_START_SLEEP_FACTOR=60


SLEEP_TILL_SERVER_STOP=5

# Variables set in in Sybase ENVIRONMENT_FILE
SYBASE=""
SYBASE_ASE=""
SYBASE_FTS=""



#Should be dynamically set
#MONITOR_SERVER_INSTALLED="Y"
#TEXT_SERVER_INSTALLED="Y"

# Used by the fault monitor
SYBASE_MONITOR_USER=""
SYBASE_MONITOR_PASSWORD=""
MONITOR=""

TIMER="/opt/SUNWscsyb/bin/sybase_monitor"
HATIMER="/usr/cluster/bin/hatimerun -v -t"
PMFADM="/usr/cluster/bin/pmfadm"


#
# Assumption: The SC 3.0 environment predefines the PATH and LD_LIBRARY_PATH env 
# variables. No checks are done for undefined variables
#
ORIGPATH=`echo $PATH`
ORIG_LD_LIBRARY_PATH=`echo $LD_LIBRARY_PATH`

TRAPSIGNALS="TERM KILL"
trap "trap - $TRAPSIGNALS; /usr/bin/pkill -g $$;exit 1" $TRAPSIGNALS


#############################################################
# syslog_tag
#
#	prints syslog_tag as SC[pkg.method]: GROUP:RESOURCE
#
#############################################################
syslog_tag()
{
	print "SC[${pkg:-??}.${method:-??}]:${RESOURCE_GROUP:-??}:${RESOURCE_NAME:-??}" 
}

#############################################################
#  scds_syslog <priority> <formattext> <parameters> 
#############################################################

scds_syslog()
{

	typeset priority=""
	typeset msg_format=""
	typeset tag=""
	typeset msg

	$SCLOGGER "$@" & 

	while getopts :p:t:m: opt
	do
  	case $opt in
		p)      priority="$OPTARG";;
		t)      tag="$OPTARG";;
		m)      msg_format="$OPTARG";;
		*)	;;
  	esac
	done

	shift `expr $OPTIND - 1`

	msg=$(/usr/bin/printf "${msg_format}" ${@:-} )

	[ -z "${TRACE_INIT:-""}" ] && init_trace

	[ "${DEBUG_LOGFILE}"  != "/dev/console" ] && \
	    print -r -- "$(date '+%b %d %T') $tag: $msg" >> $DEBUG_LOGFILE
}

#############################################################
#  write_trace <text> <parameters> 
#   Other information: TAG format 
#############################################################
write_trace()
{
	typeset msg_text=${1:-""}
	typeset msg

	shift

	msg=$(/usr/bin/printf "${msg_text}" ${@:-} )
	[ -z "${TRACE_INIT:-""}" ] && init_trace
	[ "${DEBUG_LOGFILE}"  != "/dev/console" ] && \
	    print -r -- "$(date '+%b %d %T') $(syslog_tag): $msg" >> $DEBUG_LOGFILE
}

#############################################################
#  read_arguments()
#  
#  Set Globals variables:
#     RESOURCE_NAME, RESOURCE_TYPE RESOURCE_GROUP
#
#############################################################
read_arguments()
{

	typeset rc=0
	typeset invalid_option=""
	typeset save_opt=""
	typeset save_optarg=""

	while getopts :R:T:G: opt
	do
	case $opt in
        R)      	RESOURCE_NAME="$OPTARG";;
        T)      	RESOURCE_TYPE="$OPTARG";;
        G)      	RESOURCE_GROUP="$OPTARG";;
        *)      	invalid_option="YES"; 
			save_opt="$opt"
			save_optarg="$OPTARG"
		 ;;
	esac
	done

	if [ -z "$RESOURCE_NAME" \
		-o -z "$RESOURCE_TYPE" \
		-o -z "$RESOURCE_GROUP"  \
		-o -n "$invalid_option" ]; then

		scds_syslog -p error -t $(syslog_tag) -m \
	 		"Initialization failed. Invalid command line  %s %s." \
			"$save_optarg" "$save_opt"
		rc=1;
	fi
	
	return $rc

}
#############################################################
# check_resource_setup
#
#############################################################
check_resource_setup()
{



        if [ -z "$RESOURCE_NAME" \
                -o -z "$RESOURCE_TYPE" \
                -o -z "$RESOURCE_GROUP" ]; then
		return 1
	else
		return 0
	fi
}


#############################################################
#
# error_exit()
#
#############################################################
error_exit()
{
	typeset exit_code="${1:-0}"
	exit $exit_code
}

#############################################################
# extract_property_value()
#
#  For extended properties, API returns data type followed by value
#  This function discards first line (data type) and prints 
#	remaining lines.
#
#############################################################
extract_property_value()
{
	typeset type
	typeset value
	typeset rc=0

	
	read type 
	rc=$?
	[ $rc -ne 0 ] && return $rc

	while read value
	do

		print $value
	done
		
	rc=$?
	return $rc
}
#############################################################
#
# read_property_value()
#
# grabs the values by reading the regular/extension props
#
#############################################################
read_property_value()
{

	typeset api_command
	typeset rc
	typeset log_flag="YES"
	typeset option_tag
	typeset extension=""

	case "$1" in
		R_description| \
		Type| \
		On_off_switch| \
		Monitored_switch| \
		Resource_state| \
		Cheap_probe_interval| \
		Thorough_probe_interval| \
		Retry_count| \
		Retry_interval| \
		Failover_mode| \
		Resource_dependencies| \
		Resource_dependencies_weak| \
		Logical_hostnames_used| \
		Status| \
		START_TIMEOUT| \
		STOP_TIMEOUT| \
		PRIMARIES_CHANGED_TIMEOUT | \
		VALIDATE_TIMEOUT| \
		UPDATE_TIMEOUT| \
		INIT_TIMEOUT| \
		FINI_TIMEOUT| \
		BOOT_TIMEOUT| \
		MONITOR_START_TIMEOUT| \
		MONITOR_STOP_TIMEOUT| \
		MONITOR_CHECK_TIMEOUT| \
		Status_Node| \
		Resource_state_Node)
		option_tag="$1"
		extension=""
		;;
	DEBUG_LOGFILE| \
	DEBUG_LOG_SIZE| \
	DEBUG_LEVEL) 
		log_flag=""
		option_tag="Extension"
		extension="$*"
		;;
	*)
		option_tag="Extension"
		extension="$*"
		;;
	esac

	if [ -z "$extension" ]; then
		scha_resource_get -O $option_tag \
	 	-R "$RESOURCE_NAME" -G "$RESOURCE_GROUP"
	else
		scha_resource_get -O $option_tag \
		 -R "$RESOURCE_NAME" -G "$RESOURCE_GROUP" $extension \
		| extract_property_value
	fi

	rc=$?
	if [ $rc -ne 0 ]; then
                 if [ "$log_flag" == "YES" ]; then
			scds_syslog -p error -t $(syslog_tag) -m \
                	    "Error (%s) when reading property %s." "$rc" "$1" 
		fi
	fi	
	return $rc
}

#############################################################
# check_user_env
#
# verifies 'ENVIRONMENT_FILE' is set 
#############################################################
check_user_env()
{
	typeset rc=0

        if [ -z "${ENVIRONMENT_FILE:-""}" ]; then
		return $rc
	fi

	if [ ! -f "$ENVIRONMENT_FILE" ]; then
		scds_syslog -p error -t $(syslog_tag) -m \
	 	    "file specified in ENVIRONMENT_FILE parameter %s does not exist." \
		    $ENVIRONMENT_FILE
		rc=1
        fi
	return $rc
}

#############################################################
# set_user_env
#
# Function which will read the ENVIRONMENT_FILE and set 
# all environment variables here. Maybe a check for an 
# erroneous <FILE>.csh instead of <FILE>.sh could be done
#
# LD_LIBRARY_PATH and LM_LICENSE_FILE are the only env variables
# whose existing values ( if any ) are appended. 
#############################################################
set_user_env()
{

        typeset param
        typeset val
        typeset aa_line

        typeset rc=0


        [ -z "${ENVIRONMENT_FILE:-""}" ] && return 0

        check_user_env

	rc=$?

	if [ $rc -ne 0 ]; then

		rc=1

		return $rc

	fi


        while read aa_line
        do
                [ -z ${aa_line:-""} ] && continue

                if [[ "$aa_line" == \#* ]]; then
			continue
                fi



		# File SYBASE.sh has the 'export $param' specifications which we ignore since 
		# we issue the export directive ourselves
		# 

                if [[ "$aa_line" == export* ]]; then
                  continue
                fi

		# Now ensure that the line read is of the form
		# VARIABLE=VALUE

		if [[ "$aa_line" != *\=* ]]; then
	   		scds_syslog -p warning -t $(syslog_tag) -m \
		   	    "Incorrect syntax in Environment_file.Ignoring %s"\
			    "${aa_line}"
			continue
                fi


                val="${aa_line##*=}"
                param="${aa_line%%=*}"


		# The following substitutions are because eval itself does not do the sub
		#
		if [[ "$param" == "PATH" ]]; then

			#write_trace "Orig path is $ORIGPATH"
			#write_trace "val is $val"

			# Strip the $PATH and substitute with it's actual value
			val="`echo $val|/usr/bin/sed -e 's/$PATH//'`"

			val=$val$ORIGPATH
			
			#write_trace "New val is $val"
		fi 

		if [[ "$param" == "LD_LIBRARY_PATH" ]]; then

			#write_trace "Orig library path is $ORIG_LD_LIBRARY_PATH"
			#write_trace "val is $val"

			val="`echo $val|/usr/bin/sed -e 's/$LD_LIBRARY_PATH//'`"

			val=$val$ORIG_LD_LIBRARY_PATH
			
			#write_trace "New val is $val"
		fi 

		if [[ "$param" == "LM_LICENSE_FILE" ]]; then




			# Check if the LM_LICENSE_FILE env variable is empty/not set
			if [[ -z "${LM_LICENSE_FILE:-""}" ]]; then

				write_trace "LM_LICENSE_FILE is assumed null" 

				val="`echo $val|/usr/bin/sed -e 's/:$LM_LICENSE_FILE//'`"

			else 
				write_trace "LM_LICENSE_FILE is not assumed null" 

				ORIG_LM_LICENSE_FILE=`echo $LM_LICENSE_FILE`

				#write_trace "Orig license file is $ORIG_LM_LICENSE_FILE"
				#write_trace "val is $val"

				val="`echo $val|/usr/bin/sed -e 's/$LM_LICENSE_FILE//'`"

				val=$val$ORIG_LM_LICENSE_FILE
			
				#write_trace "New val is $val"

			fi

		fi 

 
		# Ensure that Variable name is a valid KSH variable name

                if [[ "$param" != @([A-Z]|[a-z]|_)*([A-Z]|[a-z]|[0-9]|_) ]];then
	   		scds_syslog -p warning -t $(syslog_tag) -m \
		   	    "Invalid variable name in Environment_file. \
Ignoring %s" "${aa_line}"
			continue
                fi

 
                eval ${param}=\"${val}\"
                export $param
                TRACE_MESSAGE="Setting environment variable: %s=%s"
                write_trace "${TRACE_MESSAGE}" "${param}" "${val}"
        done < $ENVIRONMENT_FILE


}

#############################################################
#
# init_trace()
#
#############################################################
init_trace()
{
	typeset debugdir

        #[ ${DEBUG_LEVEL} -eq -1 ] && DEBUG_LEVEL=$(read_property_value DEBUG_LEVEL)

	[ -z "${DEBUG_LEVEL}" ] && DEBUG_LEVEL=$(read_property_value DEBUG_LEVEL) 

	TRACE_INIT="done"

	if [ -z "${DEBUG_LOGFILE}" ]; then
		DEBUG_LOGFILE="/dev/console"
		return
	fi

	debugdir=$(/bin/dirname "${DEBUG_LOGFILE}") 

	[ -n "$debugdir" ] && /bin/mkdir -p "$debugdir"

	if [[ "$DEBUG_LOGFILE" == *\. ]];then
   		DEBUG_LOGFILE="${DEBUG_LOGFILE}${RESOURCE_NAME:-""}"
	fi

	[ -z "$DEBUG_LOG_SIZE" ] && DEBUG_LOG_SIZE=300000 

	touch $DEBUG_LOGFILE 2> /dev/null

	rotate_log 

       	exec 2>> $DEBUG_LOGFILE

}

#############################################################
# rotate_log()
#
#############################################################
rotate_log()
{
        typeset log_size=0

	[ ! -f "$DEBUG_LOGFILE" ] && return

        log_size=$(/bin/ls -l "$DEBUG_LOGFILE" | /bin/awk '{print $5}')
        [ $log_size -lt $DEBUG_LOG_SIZE ] && return

        [ -f ${DEBUG_LOGFILE}.1 ] &&  /bin/rm -f "${DEBUG_LOGFILE}.1"
        [ -f ${DEBUG_LOGFILE}.0 ] && /bin/mv ${DEBUG_LOGFILE}.0 ${DEBUG_LOGFILE}.1
        /bin/mv "${DEBUG_LOGFILE}" "${DEBUG_LOGFILE}.0"
        /bin/touch "${DEBUG_LOGFILE}"
}

#############################################################
# set_status
#   Sets resource status
#	Parameter 1: <status> (not validated)
#		 OK,  DEGRADED,  FAULTED, UNKNOWN, or OFFLINE. 
#	Parameter 2: <message> (optional)
#############################################################
set_status()
{
	typeset rstatus="${1:-UNKNOWN}"
	typeset rmsg=${2:-""}
	typeset prev_status

	prev_status=$(read_property_value Status)

	# WARNING: Assumption here is that all previous scha_resource_setstatus
	# commands did NOT use the -m option. If it was ever used, more
	# parsing of prev_status is required here
	
	if [ "$prev_status" = "$rstatus" ]; then
		return 0
	fi

	if [ -n "${rmsg}" ]; then
		scha_resource_setstatus -s $rstatus -m "$rmsg" \
			-R $RESOURCE_NAME -G $RESOURCE_GROUP
	else
		scha_resource_setstatus -s $rstatus \
			-R $RESOURCE_NAME -G $RESOURCE_GROUP
	fi
}
############################################################
#
# initialize
############################################################
initialize()
{

	#
	# uncomment to turn on tracing in all functions
	# typeset -ft $(typeset +f)
	#
	

	#
	pkg=SUNWscsyb


	# DEFAULT_ACTION_FILE=

	# ONLINE_CHECK_CONFIG_FILE=
        
	# SUPPORT_FILE=

	DEBUG_LOG_SIZE=300000

	MONITOR_RETRY_COUNT=1

	MONITOR_RETRY_INTERVAL=5

	MONITOR_DELAY=0

}

#############################################################
#
# adaptive_server_running
# 
# return 0 if process is present
#        1 if server is not
# Note: using 'pgrep -f' detects multiple instances of 
# the running process.
#############################################################
adaptive_server_running()
{
	typeset rc

	${PMFADM} -q "$SERVER_TAG"

        rc=$?

	return $rc
}

#############################################################
#
# backup_server_running
#
# return 0 if process is present
#        1 if process is not present
#############################################################
backup_server_running()
{
	typeset rc

	${PMFADM} -q "$BACKUP_SERVER_TAG"

        rc=$?

	return $rc
}
#############################################################
#
# monitor_server_running
#
# return 0 if process is present
#        1 if process is not present
#############################################################
monitor_server_running()
{
	typeset rc

	${PMFADM} -q "$MONITOR_SERVER_TAG"

        rc=$?

	return $rc
}
#############################################################
#
# text_server_running
#
# return 0 if process is present
#        1 if process is not present
#############################################################
text_server_running()
{
	typeset rc

	${PMFADM} -q "$TEXT_SERVER_TAG"

        rc=$?

	return $rc
}

#############################################################
#
# read_parameters
#
#############################################################
read_parameters()
{


	ADAPTIVE_SERVER_NAME=$(read_property_value Adaptive_Server_Name) 
	BACKUP_SERVER_NAME=$(read_property_value Backup_Server_Name) 
	MONITOR_SERVER_NAME=$(read_property_value Monitor_Server_Name) 
	TEXT_SERVER_NAME=$(read_property_value Text_Server_Name) 

	ADAPTIVE_SERVER_LOG_FILE=$(read_property_value Adaptive_Server_Log_File) 
	ENVIRONMENT_FILE=$(read_property_value Environment_File) 
	STOP_FILE=$(read_property_value Stop_File)
	CONNECT_STRING=$(read_property_value Connect_String)
	WAIT_FOR_ONLINE=$(read_property_value Wait_for_online)
	DEBUG_LEVEL=$(read_property_value Debug_level)


	STOP_TIMEOUT=$(read_property_value STOP_TIMEOUT)
	[ -z "$STOP_TIMEOUT" ] && STOP_TIMEOUT=300 

	START_TIMEOUT=$(read_property_value START_TIMEOUT)
	[ -z "$START_TIMEOUT" ] && START_TIMEOUT=300


	write_trace "Adaptive Server Name is $ADAPTIVE_SERVER_NAME"
	write_trace "Backup Server Name is $BACKUP_SERVER_NAME"
	write_trace "Monitor Server Name is $MONITOR_SERVER_NAME"
	write_trace "Text Server Name is $TEXT_SERVER_NAME"

	write_trace "Adaptive Server Log is $ADAPTIVE_SERVER_LOG_FILE"
	write_trace "Environment File is $ENVIRONMENT_FILE"
	write_trace "Stop_File is $STOP_FILE"
	write_trace "Connect_String is $CONNECT_STRING"
	write_trace "Wait_For_Online is $WAIT_FOR_ONLINE"
	write_trace "Debug_Level is $DEBUG_LEVEL"
	


}
#############################################################
#
# validate_parameters 
#
# Do sanity checks on all variables accumulated so far
#############################################################
validate_parameters()
{
	if [ ! -d "$SYBASE" ]; then
		scds_syslog -p error -t "$(syslog_tag)" -m \
		    "Validation failed. SYBASE directory %s does not exist." \
		    $SYBASE
		return 1
	fi

	if [ ! -d "$SYBASE/$SYBASE_ASE" ]; then
		scds_syslog -p error -t "$(syslog_tag)" -m \
		    "Validation failed. ASE directory %s does not exist." \
		    $SYBASE/$SYBASE_ASE
		return 1
	fi


	if [ ! -f "$SYBASE/$SYBASE_ASE/install/startserver" ]; then
		scds_syslog -p error -t "$(syslog_tag)" -m \
		    "Validation failed. SYBASE ASE startserver file not found SYBASE=%s." \
		    $SYBASE
		return 1
	fi

	#Check if the 'startserver' file has executable permissions set. This does not check
	#whether 'startserver' is capable of being executed by user $SYBASE_OWNER  
	if [ ! -x ${SYBASE}/${SYBASE_ASE}/install/startserver ] ; then


		scds_syslog -p error -t "$(syslog_tag)" -m \
		"$SYBASE/$SYBASE_ASE/install/startserver does not have execute permissions set."

        	return 1
        fi



	#
	#Check the existence of the RUN_servers file for the Adaptive Server Only
	#
	if [ ! -f "$SYBASE/$SYBASE_ASE/install/RUN_$ADAPTIVE_SERVER_NAME" ]; then
		scds_syslog -p error -t "$(syslog_tag)" -m \
		    "Validation failed. SYBASE ASE runserver file RUN_%s not found SYBASE=%s." \
		    $ADAPTIVE_SERVER_NAME $SYBASE
		return 1

	fi

	#Check the existence of the STOP_<server> file
	if [ ! -f "$STOP_FILE" ]; then
		scds_syslog -p error -t "$(syslog_tag)" -m \
		    "Validation failed. SYBASE ASE STOP_FILE %s not found." \
		    $STOP_FILE
		return 1

	fi

	#Check the existence of the STOP_FILE file
	if [ ! -x "$STOP_FILE" ]; then
		scds_syslog -p error -t "$(syslog_tag)" -m \
		    "Validation failed. SYBASE ASE STOP_FILE %s is not executable." \
		    $STOP_FILE
		return 1

	fi

	# Verify that the ADAPTIVE_SERVER_LOG file exists
	if [ ! -f "$ADAPTIVE_SERVER_LOG_FILE" ]; then
		scds_syslog -p error -t "$(syslog_tag)" -m \
		    "Validation failed. Adaptive_Server_Log_File %s not found." \
		    $ADAPTIVE_SERVER_LOG_FILE
		return 1

	fi

	#Check that the CONNECT_STRING is set and has the right format ie "STRING/STRING"
	if [ "$CONNECT_STRING" == "" ]; then 

		scds_syslog -p error -t "$(syslog_tag)" -m \
		    "Validation failed. Connect string is NULL."
		return 1

	else 


		SYBASE_MONITOR_USER=`echo $CONNECT_STRING|cut -d"/" -f1 -s`
		SYBASE_MONITOR_PASSWORD=`echo $CONNECT_STRING|cut -d"/" -f2 -s`


		if [ "$SYBASE_MONITOR_USER" == "" ]; then

			scds_syslog -p error -t "$(syslog_tag)" -m \
		    	"Validation failed. Connect string is incomplete." 
			return 1

		fi

		if [ "$SYBASE_MONITOR_PASSWORD" == "" ]; then
			scds_syslog -p error -t "$(syslog_tag)" -m \
		    	"Validation failed. Connect string is incomplete." 
			return 1
		fi


		# Check if the SYBASE_MONITOR_USER is 'sa'. Treat this as an error because its 
		# a security hole is the sa account is used for fault monitoring. Doing so
		# will expose the 'sa' passwerd i.e. SYBASE_MONITOR_PASSWORD
	 	if [ "$SYBASE_MONITOR_USER" == "sa" ]; then

			scds_syslog -p error -t "$(syslog_tag)" -m \
			"Validation failed. Connect string contains 'sa' password." 

			return 1

		fi	


	fi

	#Since the Adaptive Server Name extension property specification never has a default,
	#we assume that there is always a valid ADAPTIVE_SERVER_NAME

	#Set the BACKUP_SERVER_INSTALLED value
	if [ "$BACKUP_SERVER_NAME" == "" ]; then

		BACKUP_SERVER_INSTALLED="N"	

	else 

		BACKUP_SERVER_INSTALLED="Y"	
		#
		# Check the existence of the RUN_servers file for the 
		# Backup server
		#
		if [ ! -f "$SYBASE/$SYBASE_ASE/install/RUN_$BACKUP_SERVER_NAME" ]; then
			scds_syslog -p error -t "$(syslog_tag)" -m \
		    	"Validation failed. SYBASE backup server startup file RUN_%s not found SYBASE=%s." \
		    $BACKUP_SERVER_NAME $SYBASE
			return 1

		fi

	fi

	#Set the MONITOR_SERVER_INSTALLED value
	if [ "$MONITOR_SERVER_NAME" == "" ]; then

		MONITOR_SERVER_INSTALLED="N"	

	else 

		MONITOR_SERVER_INSTALLED="Y"

		#
		# Check the existence of the RUN_server file for the 
		# MONITOR server
		#
		if [ ! -f "$SYBASE/$SYBASE_ASE/install/RUN_$MONITOR_SERVER_NAME" ]; then
			scds_syslog -p error -t "$(syslog_tag)" -m \
		    	"Validation failed. SYBASE monitor server startup file RUN_%s not found SYBASE=%s." \
		    $MONITOR_SERVER_NAME $SYBASE
			return 1
		fi

	fi

	#Set the TEXT_SERVER_INSTALLED value
	if [ "$TEXT_SERVER_NAME" == "" ]; then

		TEXT_SERVER_INSTALLED="N"	

	else 

		TEXT_SERVER_INSTALLED="Y"	
		#
		# Check the existence of the RUN_server file for the 
		# Text server
		#
		if [ ! -f "$SYBASE/$SYBASE_FTS/install/RUN_$TEXT_SERVER_NAME" ]; then
			scds_syslog -p error -t "$(syslog_tag)" -m \
		    	"Validation failed. SYBASE text server startup file RUN_%s not found SYBASE=%s." \
		    $TEXT_SERVER_NAME $SYBASE
			return 1

		fi
	fi

	# Not sure if this needs be located here. Better to locate it in 
	# a separate function
	#
	# Export the $SERVER_NAME because child processes may use it 
	export ADAPTIVE_SERVER_NAME
	export BACKUP_SERVER_NAME
	export MONITOR_SERVER_NAME
	export TEXT_SERVER_NAME

	export STOP_FILE

	# For the fault monitor
	export SYBASE_MONITOR_USER
	export SYBASE_MONITOR_PASSWORD
	export MONITOR_RESTART_HISTORY

        export DEBUG_LOGFILE
        export DEBUG_LOG_SIZE


	return 0

}
#############################################################
#  Validates parameters before starting the ASE monitors
#
#  Return 0 if validations succeeded
#  Return 1 if any errors were detected
#############################################################
validate_sybase_parameters()
{


        set_user_env

        if [ $? -ne 0 ]; then

                return 1

        fi

        validate_parameters

        if [ $? -ne 0 ]; then

                return 1

        fi

        return 0 

}
#############################################################
# monitor_setup
#############################################################
monitor_setup()
{

	setup_pmf_tags

	return 0

}	
#############################################################
# monitor_running
#############################################################
monitor_running()
{
	typeset rc
        ${PMFADM} -q $MONITOR_TAG
        rc=$?

        return $rc

}

#############################################################
#  get_monitor_name
#
#   Input:
#               none
#
#   Output:
#	prints out fault monitor name. Also
#	sets the MONITOR global variable.
#
#   Description:
#	This function returns fault monitor name.
#	32 bit fault monitor can work with Sybase 12.0 32 bit 
#	or 64 bit installation.
#
#############################################################
get_monitor_name()
{
	MONITOR=$MONITOR_BASE
	print "$MONITOR"
}

set_stop_time_intervals()
{

	let aux_count=0
		
        let AUX_NORMAL_STOP_TIME=0
        let AUX_ABNORMAL_STOP_TIME=0
	let PRIM_NORMAL_STOP_TIME=0
	let PRIM_ABNORMAL_STOP_TIME=0

	#STOP_TIMEOUT has already been set during earlier part of validation

	#stop_time_interval=$(read_property_value STOP_TIMEOUT)

	# Total time available is STOP_TIMEOUT - <elapsed time> - 5 seconds
	
	let total_time="$STOP_TIMEOUT - $SECONDS - 5" 
        [ $total_time -le 0 ] && total_time=10

	#Lets assume that 3% overhead for script execution.
	#
	#let total_time=total_time*97/100


	#Find out how many auxilary servers are configured

	if [ "$BACKUP_SERVER_NAME" != "" ]; then

		let aux_count=aux_count+1

	fi

	if [ "$MONITOR_SERVER_NAME" != "" ]; then

		let aux_count=aux_count+1

	fi

	if [ "$TEXT_SERVER_NAME" != "" ]; then

		let aux_count=aux_count+1

	fi



	if  (( aux_count == 0 )); then


		# 95 % of total for primary server ( ASE )

		let PRIM_NORMAL_STOP_TIME=total_time*70/100
		let PRIM_ABNORMAL_STOP_TIME=total_time*25/100

	elif (( aux_count == 1)); then

		# 60 % of total for primary server ( ASE )

		let PRIM_NORMAL_STOP_TIME=total_time*50/100
		let PRIM_ABNORMAL_STOP_TIME=total_time*10/100

		# 35 % of total for 1 aux server 

		let AUX_NORMAL_STOP_TIME=total_time*30/100
		let AUX_ABNORMAL_STOP_TIME=total_time*5/100

	elif (( aux_count == 2)); then

		# 45 % of total for primary server 

		let PRIM_NORMAL_STOP_TIME=total_time*40/100
		let PRIM_ABNORMAL_STOP_TIME=total_time*5/100

		let AUX_NORMAL_STOP_TIME=total_time*20/100
		let AUX_ABNORMAL_STOP_TIME=total_time*5/100

	elif (( aux_count == 3)); then

		# 45 % of total for primary server 
		let PRIM_NORMAL_STOP_TIME=total_time*40/100
		let PRIM_ABNORMAL_STOP_TIME=total_time*5/100

		let AUX_NORMAL_STOP_TIME=total_time*10/100
		let AUX_ABNORMAL_STOP_TIME=total_time*5/100
		
	fi 

	# SERVER_SHUTDOWN_SLEEP_TIME is time in seconds to wait after 
	# completing shutdown command
	
	let SERVER_SHUTDOWN_SLEEP_TIME=3

	write_trace "Number of auxilary servers is $aux_count"
	write_trace "Prim normal   stop time is $PRIM_NORMAL_STOP_TIME"
	write_trace "Prim abnormal stop time is $PRIM_ABNORMAL_STOP_TIME"
	write_trace "Aux  normal   stop time is $AUX_NORMAL_STOP_TIME"
	write_trace "Aux  abnormal stop time is $AUX_ABNORMAL_STOP_TIME"

}

#############################################################
#
# hastorageplus_validation
#
# Arguments: Arguments to be passed to hasp_check program
# 
# This function checks if resource is dependent on SPlus resource and 
# state of SPlus resource.
#
# This function is expected to be called from validate or monitor_check
# methods, if some validations failed.
# 
# If this resource is dependent on SPlus resource, validations 
# are possible only on the node that has SPlus resource online.
# 
# return 0 - if resource depends on HAStoragePlus resource
#		and HASP resource is on-line on some other node
# 
#        1 - All other cases
# 
#############################################################

hastorageplus_validation() 
{
  	typeset rc=0

	#
	# hasp_check return codes 
	# 1 - Internal error
	#
	# 2 - No dependency on HASToragePlus resource
	#
	# 3 - One or more of the SUNW.HAStoragePlus resources
	#     that this resource depends on is in a different RG.
	#
	# 4 - One or more of the SUNW.HAStoragePlus resources
	#     that this resource depends on is not online anywhere.
	#
	# 5 - All the SUNW.HAStoragePlus resources that this 
	#     resource depends on are not online on this node. 
	#
	# 6 - All the SUNW.HAStoragePlus resources that this
	#     resource depends on are online on the local node. 	
	#

	if [ ! -f /usr/cluster/bin/hasp_check ]; then

		# hasp_check binary does not exist
		# return error

		return 1
	fi

	/usr/cluster/bin/hasp_check "$@"
 	hasp_status=$?
 
	case "$hasp_status" in
 
	   1)	rc=1
	   	scds_syslog -p error -t $(syslog_tag) -m \
	    	   "Error in hasp_check. Validation failed."
	   	;;
		   	
	   3)	rc=1
	   	scds_syslog -p error -t $(syslog_tag) -m \
	    	   "This resource depends on a HAStoragePlus resouce \
that is in a different Resource Group. This configuration is not supported." 
	   	;;

	   4)	rc=1
	   	scds_syslog -p error -t $(syslog_tag) -m \
	    	   "This resource depends on a HAStoragePlus resouce \
that is not online. Unable to perform validations."
	   	;;


	   5)	rc=0
	   	scds_syslog -p warning -t $(syslog_tag) -m \
		   "This resource depends on a HAStoragePlus resouce \
that is not online on this node. Ignoring validation errors." 
	   	;; 

	   # Return code 2, 6 or any other.
 	   *) 	rc=1;; 
	esac

	return $rc

}

#############################################################
# setup_pmf_tags
#############################################################
setup_pmf_tags()
{
	typeset ix="${RESOURCE_NAME}"

	SERVER_TAG="sybase_ASE.${RESOURCE_NAME}"
	BACKUP_SERVER_TAG="sybase_backup_server.${RESOURCE_NAME}"
	MONITOR_SERVER_TAG="sybase_monitor_server.${RESOURCE_NAME}"
	TEXT_SERVER_TAG="sybase_text_server.${RESOURCE_NAME}"
        MONITOR_TAG="sybase_ASE_monitor.${RESOURCE_NAME}"

	return 0
}
#############################################################
# pmf_kill <tag> <timeout>
#############################################################
pmf_kill()
{
	typeset tag=${1:-""}
	typeset timeout=${2:-"-1"}

	if [ -z "${tag}" ]; then
		return 0
	fi

	${PMFADM} -q "${tag}"

        if [ $rc -ne 0 ]; then
		# No such tag
		# Return success
		return 0
	fi


	${PMFADM} -s "${tag}" -w ${timeout} KILL

	${PMFADM} -q "${tag}"
        rc=$?

        if [ $rc -eq 0 ]; then
                scds_syslog -p error -t $(syslog_tag) -m \
                "Unable to stop processes running under PMF tag %s." \
                "${tag}"

		return 1
        fi

        return 0
}

#include_sybase_lib

method="monitor_stop"

#############################################################
# stop_monitor
#############################################################
stop_monitor()
{
	
        typeset rc

        monitor_running
        if [ $? -ne 0 ]; then
                write_trace "fault monitor is not running."
                return 0
        fi

        scds_syslog -p info -t $(syslog_tag) -m \
            "Stopping fault monitor using pmf tag %s." \
            "${MONITOR_TAG}"

	pmfadm -s "${MONITOR_TAG}"

	pmfadm -k "${MONITOR_TAG}"

	rc=$?

        if [ $rc -ne 0 ]; then
        	scds_syslog -p error -t $(syslog_tag) -m \
            	"Stop fault monitor using pmfadm failed. tag %s error=%s." \
            	"${MONITOR_TAG}" "$rc"
		
        fi

	return $rc

}
#############################################################
# main
#############################################################
main()
{
        typeset rc=0


        initialize

        read_arguments "${@:-}" || error_exit $?

        write_trace "read_arguments completed "

	#Sets up the MONITOR_TAG to include the resource name
	monitor_setup

        write_trace "Monitor tag is ${MONITOR_TAG}"

        stop_monitor || error_exit $?

        write_trace "stop_monitor completed"

        exit $rc

}

main "${@:-}"
