#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

SCLOGGER=/usr/cluster/lib/sc/scds_syslog
PKG=SUNWsctomcat
METHOD=`basename $0`
SCHA_RESOURCE_GET=/usr/cluster/bin/scha_resource_get
SCHA_RESOURCEGROUP_GET=/usr/cluster/bin/scha_resourcegroup_get
TASK_COMMAND=""
RESOURCE_PROJECT_NAME=""


syslog_tag()
{
        #
        # Data Service message format
        #

        $SET_DEBUG

        print "SC[${PKG:-??}.${METHOD:-??}]:${RESOURCEGROUP:-??}:${RESOURCE:-??}"
}

scds_syslog()
{

        #
        # Log a message
        #

        $SET_DEBUG

        $SCLOGGER "$@" &
}

debug_message()
{
        #
        # Output a debug message to syslog if required
        #

        if [ "$DEBUG" = "$RESOURCE" -o "$DEBUG" = "ALL" ]
        then
                SET_DEBUG="set -x"

                DEBUG_TEXT=$1

                scds_syslog -p daemon.debug -t $(syslog_tag) -m \
                        "%s" "$DEBUG_TEXT"
        else
                SET_DEBUG=
        fi
}

log_message()
{
        #
        # Output a message to syslog as required
        #

        debug_message "Function: log_message - Begin"
        $SET_DEBUG

        if [ -s "${LOGFILE}" ]
        then
                PRIORITY=$1
                HEADER=$2

                if [ "${PRIORITY}" = "err" ]
                then
                        echo "The following command was executed - pgrep -fl -u $User" >> $LOGFILE
                        pgrep -fl -u $User >> $LOGFILE
                fi

		# wipe out non printable characters

		strings ${LOGFILE} >${LOGFILE}.copy

                while read MSG_TXT
                do
                        scds_syslog -p daemon.${PRIORITY} -t $(syslog_tag) -m \
				"%s - %s" \
                                "${HEADER}" "${MSG_TXT}"
                done < ${LOGFILE}.copy

                su - $User -c "cat /dev/null > $LOGFILE" > /dev/null
		cat /dev/null > ${LOGFILE}.copy
        fi

        debug_message "Function: log_message - End"
}

standard_resource_get()
{
        debug_message "Function: standard_resource_get - Begin"
        $SET_DEBUG

	PROPERTY=$1

	${SCHA_RESOURCE_GET} -R ${RESOURCE} -G ${RESOURCEGROUP} -O ${PROPERTY}

	s1=$?

	if [ "${s1}" -ne 0 ]; then
		debug_message "standard_resource_get - Retrievment of property ${PROPERTY} returned ${s1}"
	fi

        debug_message "Function: standard_resource_get - End"
	return ${s1}
}

standard_resourcegroup_get()
{
        debug_message "Function: standard_resourcegroup_get - Begin"
        $SET_DEBUG

	PROPERTY=$1

	${SCHA_RESOURCEGROUP_GET} -R ${RESOURCE} -G ${RESOURCEGROUP} -O ${PROPERTY}

	s1=$?

	if [ "${s1}" -ne 0 ]; then
		debug_message "standard_resource_get - Retrievment of property ${PROPERTY} returned ${s1}"
	fi

        debug_message "Function: standard_resourcegroup_get - End"

	return ${s1}
}

srm_function()
{

	USER=$1

        debug_message "Function: srm_function - Begin"
        $SET_DEBUG

   
	# 
	# If Solaris 8 just return
	#

	if [ `/usr/bin/uname -r` = "5.8" ];
	then
		return 0
	fi

	#
	# Retrieve RESOURCE_PROJECT_NAME
	#

	RESOURCE_PROJECT_NAME=`standard_resource_get RESOURCE_PROJECT_NAME`

	#
	# Retrieve RG_PROJECT_NAME if RESOURCE_PROJECT_NAME is not set
	#

	if [ -z "${RESOURCE_PROJECT_NAME}" ]; then

		RESOURCE_PROJECT_NAME=`standard_resourcegroup_get RG_PROJECT_NAME`
	fi

	#
	# Return if no projects are defined
	#

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

	#
	# Validate that $USER belongs to the project defined by
	# ${RESOURCE_PROJECT_NAME}
	#

	PROJ_MEMBER=`/usr/bin/projects ${USER} | /usr/bin/grep -w ${RESOURCE_PROJECT_NAME}`

	if [ -z "${PROJ_MEMBER}" ];
        then
             scds_syslog -p daemon.error -t $(syslog_tag) -m \
                        "Validate - The user %s does not belongs to project %s" \
                        "${USER}" "${RESOURCE_PROJECT_NAME}" 
                return 1
        else
                debug_message "Validate - User ${USER} belongs to project ${RESOURCE_PROJECT_NAME}"
        fi

	#
	# Set TASK_COMMAND
	#

	TASK_COMMAND="/usr/bin/newtask -p ${RESOURCE_PROJECT_NAME}"

        debug_message "Function: srm_function - End"

	return 0
}

validate_options()
{
        #
        # Ensure all options are set
        #

        SCRIPTNAME=`basename $0`
        for i in RESOURCE RESOURCEGROUP PARFILE
        do
                case $i in
                        RESOURCE)
                        if [ -z $RESOURCE ]; then
   				scds_syslog -p daemon.err -t $(syslog_tag) -m \
                                "validate_options: %s Option -R not set" \
				"${SCRIPTNAME}"
                                exit 1
                        fi;;

                        RESOURCEGROUP)
                        if [ -z $RESOURCEGROUP ]; then
   				scds_syslog -p daemon.err -t $(syslog_tag) -m \
                                "validate_options: %s Option -G not set" \
				"${SCRIPTNAME}"
                                exit 1
                        fi;;

                        PARFILE)
                        if [ -z $PARFILE ]; then
   				scds_syslog -p daemon.err -t $(syslog_tag) -m \
                                "validate_options: %s Option -N not set" \
				"${SCRIPTNAME}"
                                exit 1
                        fi;;
                esac
        done
}

validate()
{
	#
	# Validate Tomcat
	#
	
        debug_message "Function: validate - Begin"
	$SET_DEBUG

	rc_validate=0

	#
	# Validate that ${filename} exists
	#

	if [ ! -f "${PARFILE}" ]; then
   		scds_syslog -p daemon.err -t $(syslog_tag) -m \
			"validate - file %s does not exist" \
			"${PARFILE}"
		rc_validate=1
	else
		debug_message "Validate - ${filename} exists"

		# check if the parameter file is syntax and semantic is correct

		ksh -n ${PARFILE} >/dev/null 2>&1
		if [ $? -ne 0 ]; then
   			scds_syslog -p daemon.err -t $(syslog_tag) -m \
	                "validate: there are syntactical errors in the parameterfile %s " \
			"${PARFILE}"
			rc_validate=1
			return $rc_validate
	        fi
			
		# Test if all the mandatory variables are included and set correctly in the parameterfile 

		PARLIST="EnvScript User Basepath Host Port TestCmd ReturnString Startwait"
		PARAMETERS=`cat ${PARFILE} |grep -v "^#"|grep -v "^ "|nawk -F= '{print $1}'`


		for i in $PARLIST
		do
			if ! `echo $PARAMETERS |grep $i > /dev/null `; then
   				scds_syslog -p daemon.err -t $(syslog_tag) -m \
			        "validate: %s is not set in the parameterfile %s but it is required" \
				"${i}" "${PARFILE}"
				rc_validate=1
			fi
		done	

		# test the semantic of the parameters

		. ${PARFILE}
		for i in `cat ${PARFILE} |grep -v "^#"|grep -v "^ "|nawk -F= '{print $1}'`
		do
			case $i in
	                        EnvScript)
	
				# Test environment script
	                      
	                        if [ -z $EnvScript ]; then
   					scds_syslog -p daemon.err -t $(syslog_tag) -m \
	                                "validate: EnvScript not set but it is required"
					rc_validate=1
	                        fi
	                        if [ ! -f $EnvScript ]; then
   					scds_syslog -p daemon.err -t $(syslog_tag) -m \
	                                "validate: EnvScript %s does not exist but it is required" \
					"${EnvScript}"
					rc_validate=1
	                        fi;;
	
	                        User)
	
				# Test tomcat user
	                      
	                        if [ -z $User ]; then
   					scds_syslog -p daemon.err -t $(syslog_tag) -m \
	                                "validate: User is not set but it is required"
					rc_validate=1
	                        fi
				id ${User} >/dev/null 2>&1
				if [ $? -ne 0 ]; then
   					scds_syslog -p daemon.err -t $(syslog_tag) -m \
	                                "validate: User %s does not exist but it is required" \
					"${User}"
					rc_validate=1
	                        fi;;
	
	                        Basepath)
	
				# Test tomcat basepath 
	                      
	                        if [ -z $Basepath ]; then
   					scds_syslog -p daemon.err -t $(syslog_tag) -m \
	                                "validate: Basepath is not set but it is required"
					rc_validate=1
	                        fi
	                        if [ ! -d $Basepath ]; then
   					scds_syslog -p daemon.err -t $(syslog_tag) -m \
	                                "validate: Directory %s does not exist but it is required" \
					"${Basepath}"
					rc_validate=1
	                        fi;;
	
	                        Host)
	
				# Test tomcat testhost
	                      
	                        if [ -z $Host ]; then
   					scds_syslog -p daemon.err -t $(syslog_tag) -m \
	                                "validate: Host is not set but it is required"
					rc_validate=1
	                        fi
				grep -w $Host /etc/hosts >/dev/null 2>&1
	                        if [ $? -ne 0 ]; then
   					scds_syslog -p daemon.err -t $(syslog_tag) -m \
	                                "validate: Host %s is not found in /etc/hosts but it is required" \
					"${Host}"
					rc_validate=1
	                        fi;;

	
	                        Port)
	
				# Test tomcats Port
	                      
	                        if [ -z $Port ]; then
   					scds_syslog -p daemon.err -t $(syslog_tag) -m \
	                                "validate: Port is not set but it is required"
					rc_validate=1
	                        fi;;
	
	                        TestCmd)
	
				# Test tomcats Test Command
	                      
	                        if [ -z $TestCmd ]; then
   					scds_syslog -p daemon.err -t $(syslog_tag) -m \
	                                "validate: TestCmd is not set but it is required"
					rc_validate=1
	                        fi;;
	
	                        ReturnString)

				# Test tomcats Returnstring
	                      
	                        if [ -z $ReturnString ]; then
   					scds_syslog -p daemon.err -t $(syslog_tag) -m \
	                                "validate: Return String is not set but it is required"
					rc_validate=1
	                        fi;;
	
	                        Startwait)
	
				# Test tomcats Startwait Option
	                      
	                        if [ -z $Startwait ]; then
   					scds_syslog -p daemon.err -t $(syslog_tag) -m \
	                                "validate: Startwait is not set but it is required"
					rc_validate=1
	                        fi;;
	
	                        *)
	
				# All other Variables are invalid
	                      
   				scds_syslog -p daemon.err -t $(syslog_tag) -m \
	                        "validate: An invalid option entered or # is not the first character"
				rc_validate=1
	                        ;;
			esac
		done
	fi

	debug_message "Function: validate - End"

}

start_Tomcat()
{
	#
	# Start Tomcat
	#

        debug_message "Function: start_Tomcat - Begin"
	$SET_DEBUG
	
        # Get the project name for the resource

        srm_function $User

        if [ $? -ne 0 ]; then
                # The error message is out of by the function
                TASK_COMMAND=""
        fi

	if getent passwd ${User} | awk -F: '{print $7}' | grep csh > /dev/null
	then
        	su - ${User} -c "source ${EnvScript}; $TASK_COMMAND ${Basepath}/bin/startup.sh >& ${LOGFILE}" > /dev/null
		rc_start_command=$?
	else
        	su - ${User} -c ". ${EnvScript}; $TASK_COMMAND ${Basepath}/bin/startup.sh >${LOGFILE} 2>&1" > /dev/null
		rc_start_command=$?
	fi

	sleep $Startwait

	debug_message "Function: start_Tomcat - End"
}

stop_Tomcat()
{
	#
	# Stop Tomcat
	#

        debug_message "Function: stop_Tomcat - Begin"
	$SET_DEBUG

        # Get the project name for the resource

        srm_function $User

        if [ $? -ne 0 ]; then
                # The error message is out of by the function
                TASK_COMMAND=""
        fi


	if getent passwd ${User} | awk -F: '{print $7}' | grep csh > /dev/null
	then
        	su - ${User} -c "source ${EnvScript}; $TASK_COMMAND ${Basepath}/bin/shutdown.sh >& ${LOGFILE} ; sleep 30" > /dev/null
		rc_stop_command=$?
	else
       		su - ${User} -c ". ${EnvScript}; $TASK_COMMAND ${Basepath}/bin/shutdown.sh >${LOGFILE} 2>&1 ; sleep 30" > /dev/null
		rc_stop_command=$?
	fi

	debug_message "Function: stop_Tomcat - End"
}

check_Tomcat()
{
	# 
	# Probe Tomcat
	#

        debug_message "Function: check_Tomcat - Begin"
	$SET_DEBUG

	rc_check_Tomcat=0

	# determine, wether the start command is running, this indicates that the probe is fired during
	# wait for online algorythm of the gds

	# if wait_for_online will be set to 1 no diagnostic message will be dropped to syslog

	wait_for_online=0
	ps -ef|grep -v grep |grep /opt/SUNWsctomcat/bin/start_sctomcat |grep -w $RESOURCE >/dev/null

	if [ $? -eq 0 ]; then
		wait_for_online=1
	fi 

	# test if monitor host is reachable

	/usr/sbin/ping ${Host} 2 >/dev/null
	rc_ping_command=$?

	# test if Tomcat is operational

	if [ $rc_ping_command -eq 0 ]; then
		mconnect -p ${Port} ${Host} 2>&1 << EOF|grep "${ReturnString}" >/dev/null  
${TestCmd}
EOF
		if [ $? -ne 0 ]; then

			if [ $wait_for_online -eq 0 ]; then

	# drop a message for the first probe

       		         	scds_syslog -p daemon.notice -t $(syslog_tag) -m \
               		 	"first probe was unsuccessful, try again in 5 seconds" 
			fi

			sleep 5

	# try again

			mconnect -p ${Port} ${Host} 2>&1 << EOF2|grep "${ReturnString}" >/dev/null  
${TestCmd}
EOF2
			if [ $? -ne 0 ]; then
				if [ $wait_for_online -eq 0 ]; then

	# drop a message for that Tomcat is unavailable

                			scds_syslog -p daemon.notice -t $(syslog_tag) -m \
                			"last probe failed, Tomcat considered as unavailable" 
				fi
				rc_check_Tomcat=1
			fi
		fi
	else
		rc_check_Tomcat=1
	fi


	debug_message "Function: check_Tomcat - End"
}

set_status()
{
        #
        # Set appropriate resource status
        #

        debug_message "Function: set_status - Begin"
        $SET_DEBUG

        STATUS_TYPE=$1

        scha_resource_setstatus -R $RESOURCE -G $RESOURCEGROUP -s $STATUS_TYPE

        rc_scha_resource_setstatus=$?

        if [ "$rc_scha_resource_setstatus" != 0 ]
        then
                scds_syslog -p daemon.err -t $(syslog_tag) -m \
                "set_status - rc<%s>  type<%s>" \
                "${rc_scha_resource_setstatus}" "${STATUS_TYPE}"
        fi

        debug_message "Function: set_status - End"
}

