#!/bin/ksh
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2021.  All rights reserved.
#
#  ALTRAN_PROLOG_END_TAG
#
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/server_restart.sh 1.20 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1999,2011 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)  7d4c34b 43haes/usr/sbin/cluster/events/server_restart.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#########################################################################
#                                                                       #
#       Name:           server_restart                                  #
#                                                                       #
#       Description:    This event script is called when one of the     #
#			application monitoring needs to restart an      #
#                       application server.                             #
#                                                                       #
#       Called by:      resource manager                                #
#                                                                       #
#       Calls to:       None                                            #
#                                                                       #
#       Arguments:      nodename, application server resource id        #
#									#
#                       nodename - name of the node where the	        # 
#			 	   application failed. 		        #
#			application server resource id:  the id         #
#                                  of the HACMPresource object          #
#                                  containing the application server.   #
#                                                                       #
#       Returns:        0       success                                 #
#                       1       failure                                 #
#                       2       bad argument                            #
#                                                                       #
#########################################################################

#
# The following lines create the appropriate trace output
#
PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
if [[ $VERBOSE_LOGGING == high ]]
then
    set -x
    version='%I%'
fi

#
# Check for minimal invocation
#
if [ $# -gt 3 ]
then
    cl_log  10408 "Usage: $PROGNAME nodename resource_id [monitor name]\n"  $PROGNAME
    exit 2
fi

MONITOR=""
STATUS=0
EXIT_STATUS=0

#
: args are the event node, id of the server and an optional monitor name
#
NODENAME=$1
ID=$2
[[ $# == 3 ]] && MONITOR=$3

#
: check if the LOCALNODENAME is the same as the event node name
: If not, exit cleanly, otherwise continue processing.
#
[[ -z $LOCALNODENAME ]] && LOCALNODENAME=$(get_local_nodename)
if [ $NODENAME != $LOCALNODENAME ]
then
    # 
    : remote event - all done
    #
    exit 0
fi

typeset WPARNAME EXEC WPARDIR groupName

WPARNAME=""
EXEC=""
WPARDIR=""


groupName=$(clodmget -q"id=$ID" -f group -n HACMPresource)
[[ -z $GROUPNAME ]] && export GROUPNAME=$groupName

WPARNAME=$(clwparname $groupName)
if (( $? == 0 )) && [[ -n ${WPARNAME} ]]
then
    WPARDIR=$(clwparroot ${WPARNAME})
    if (( $? != 0 ))
    then
        echo "$0 ERROR: Failed to get the WPAR directory for ${WPARNAME}"
        exit 1
    fi

    EXEC="clwparexec ${WPARNAME}"
fi

#
: Set the Run-Time Parameter values and export them
: to all successive scripts.
#
set -a
eval $(cllsparam -n $LOCALNODENAME)
set +a


set -u

#
: Find what kind of resource this "server" is
#
typeset RESOURCE=$(clodmget -q"id=$ID" -f name -n HACMPresource)


#
: Find is it is USERDEFINED resource type
#
typeset RESTYPE=$(clodmget -q"id=$ID" -f type -n HACMPresource)

#
: check the type - only app servers and userdefined resources are 
: supported by this script
#
if [[ -z $RESOURCE || ( $RESOURCE != APPLICATIONS && $RESTYPE != USERDEFINED ) ]]
then
    cl_log 6161 "Bad resource id $ID.  Neither an application server nor an user defined resource.\n" $ID
    exit 2
fi

#
: Find the name
#

typeset APPNAME=$(clodmget -q"id=$ID" -f value HACMPresource)

#
: A resource can have multiple monitors associated with it - get the list
#
if [[ -z $MONITOR ]]; then
   MONITORS=$(clodmget -q"name = RESOURCE_TO_MONITOR AND value = $APPNAME" -f monitor -n HACMPmonitor)
else
    #
    : if passed a specific monitor, only run methods for that one
    #
    MONITORS=$MONITOR
fi

for monitor in $MONITORS
do

    #
    : Lookup any user defined notification method and execute it
    #
    NOTIFY=$(clodmget -q"monitor=$monitor and name=NOTIFY_METHOD" -f value -n HACMPmonitor | sed 's-\\"-"-g')
    print -- $NOTIFY | read NOTIFY_SCRIPT rest

    if [[ ( -n ${WPARNAME} && -n ${NOTIFY_SCRIPT} && -x ${WPARDIR}/${NOTIFY_SCRIPT} ) || \
          ( -z ${WPARNAME} && -n ${NOTIFY_SCRIPT} && -x ${NOTIFY_SCRIPT} ) ]]
    then
        dspmsg scripts.cat 9333 \
        "$PROGNAME: Calling user specified notify method $NOTIFY\n" \
        $PROGNAME $NOTIFY
        ${EXEC} $NOTIFY &
        if [ $? -ne 0 ]
        then
        cl_log 6162 "Failure in user-defined script $NOTIFY.\n" \
                        $NOTIFY
        # no exit here, must still try to clean the application up.
        fi
    fi
    #
    : Look for the user defined cleanup method and execute it if it exists
    #
    CLEANUP=$(clodmget -q"monitor=$monitor and name=CLEANUP_METHOD" -f value -n HACMPmonitor | sed 's-\\"-"-g')

    print -- $CLEANUP | read CLEANUP_SCRIPT rest


    if [[ ( -n ${WPARNAME} && -n $CLEANUP_SCRIPT && -x ${WPARDIR}/$CLEANUP_SCRIPT ) || \
          ( -z ${WPARNAME} && -n $CLEANUP_SCRIPT && -x $CLEANUP_SCRIPT ) ]]
    then
  	dspmsg scripts.cat 9334 "$PROGNAME: Calling user specified cleanup method $CLEANUP\n" $PROGNAME $CLEANUP
  	${EXEC} $CLEANUP

	STATUS=$?
	if [ $STATUS -ne 0 ]
	then
  	    cl_log 6162	"Failure in user-defined script $CLEANUP.\n" $CLEANUP
            #
	    : save the exit status and continue to process methods for the next monitor
            #
  	    EXIT_STATUS=$STATUS
	fi

    fi
done

exit $EXIT_STATUS

