#!/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_down.sh 1.14 
#  
# 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_down.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#########################################################################
#
#                                                                       #
#       Name:           server_down                                     #
#                                                                       #
#       Description:    This event script is called when an application #
#                       that is monitored fails.  It runs the notify    #
#                       script for that monitor, if one is defined.     #
#                                                                       #
#       Called by:      resource manager                                #
#                                                                       #
#       Calls to:       None                                            #
#                                                                       #
#       Arguments:      nodename resource_id                            #   
#                                                                       #
#                       nodename - name of the node where the           # 
#                                  application failed.                  #
#                                                                       #
#                       resource_id - the resource id of the            # 
#                                     application server.               #  
#                                                                       #
#       Returns:        0       success                                 #
#                       1       failure                                 #
#                       2       bad argument                            #
#                                                                       #
#########################################################################

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


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


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

#
# Event emulation support
#
if [ ! -n "$EMULATE" ]
then
   EMULATE="REAL"
fi

#
# Find what kind of resource this "server" is
#
typeset RESOURCE=$(odmget -q"id=$ID" HACMPresource | egrep "name =" \
                | cut -f2 -d\")

#
# Find is it is USERDEFINED resource type
#
typeset RESTYPE=$(odmget -q"id=$ID" HACMPresource | egrep "type =" \
                | cut -f2 -d\")

#
# check the type 
#
if [[ -z $RESOURCE || (  $RESOURCE != APPLICATIONS && $RESTYPE != USERDEFINED  && \
                         $RESOURCE != VOLUME_GROUP && $RESOURCE != SERVICE_LABEL )  ]]
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=$(odmget -q"id=$ID" HACMPresource | egrep "value =" \
                | cut -f2 -d\")

dspmsg scripts.cat 9332 "$PROGNAME: Called for $RESOURCE $APPNAME on node $NODENAME\n" $PROGNAME $RESOURCE $APPNAME $NODENAME


#
# A resource can have multiple monitors associated with it - get the list
#
if [[ -z $MONITOR ]]; then
    MONITORS=$(odmget -q"name = RESOURCE_TO_MONITOR AND value = $APPNAME" HACMPmonitor |
        grep "monitor =" | cut -f2 -d'"')
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=$(odmget -q"monitor=$monitor and name=NOTIFY_METHOD" HACMPmonitor |
         grep "value =" | cut -d'"' -f2)

    NOTIFY_SCRIPT=$(echo $NOTIFY | cut -d' ' -f1) # in case of embedded blank

    if [[ -n $NOTIFY_SCRIPT && -x $NOTIFY_SCRIPT ]]
    then
    	dspmsg scripts.cat 9333 "$PROGNAME: Calling user specified notify method $NOTIFY_SCRIPT\n" $PROGNAME $NOTIFY_SCRIPT
  	ODMDIR=/etc/objrepos $NOTIFY &
    fi
done

#
# Note to users: to add any user defined processing during a server_down event,
# define a pre or post event (do not modify this script).
# 

exit 0

