#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                     
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2022.  All rights reserved.         	
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                       
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/utilities/clcheck_server.sh 1.10.4.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1998,2013 
# 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 
# @(#)  7529259 43haes/usr/sbin/cluster/utilities/clcheck_server.sh, 61aha_r726, 2205A_aha726, May 12 2022 10:16 PM
#

###############################################################################
#
#  Name:  check_server_extended
#
#  This routine is called when the main routine has found an active
#  server. This routine performs extended checks based on the particular
#  server.
#
#  If the server being checked is grpsvcs:
#	grpsvcs can be active for either an HACMP or GPFS domain.
#	As this routine is only used by HACMP utilities, check for grpsvcs
#	active for HACMP by checking the clients connected to grpsvcs:
#	if one of the clients is "CLSTRMGR_*" then the hacmp clstrmgr is
#	connected to grpsvcs and the domain is hacmp.
#
#  If the server being checked is not listed above:
#	return 1 - main routine has already determined server is active
#
#  Arguments:    SERVER - Server to check
#
#  Returns:     1 - daemon active
#               0 - daemon not active, according to the conditions defined above
#               255 - routine was unable to determine extended server state
#
#  Note that the return value from this routine follows the same convention
#  as the main routine.
#
###############################################################################
check_server_extended () {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    
    # server name 
    typeset SERVER=$1
    typeset STATUS=1    # caller already determined subsystem is active
    
    case $SERVER in
    grpsvcs)
        # if grpsvcs is active and the hacmp clstrmgr is connected 
        # (indicating grpsvcs is being used by hacmp) then the string
        # CLSTRMGR_ and CLRESMGRD_ will appear in the "Group name" listing
        # section of the src long listing
            
        if ! LC_ALL=C lssrc -ls ${SERVER} | grep -q "CLSTRMGR_"
        then
            STATUS=0
        fi
        ;;
        
    cthags)
        # if cthags is active and the hacmp clstrmgr is connected 
        # (indicating grpsvcs is being used by hacmp) then the string
        # CLSTRMGR_ and CLRESMGRD_ will appear in the "Group name" listing
        # section of the src long listing
        
        if ! LC_ALL=C lssrc -ls ${SERVER} | grep -q "CLSTRMGR_"
        then
            STATUS=0
        fi
        ;;
    esac
    
    echo $STATUS
    return 
    
}




###############################################################################
#
#  Name:  clcheck_server
#
#  This routine checks the status of a daemon using the lssrc command and
#  returns a 1 if it is active, or a 0 if it is not active.
#
#  Arguments:    SERVER - Server to check
#
#  Returns:     1 - daemon active
#               0 - daemon not active or does not exist
#               255 - routine was unable to determine server state
#
#  Note that the return value from this routine makes its usage somewhat
#  obtuse. For example:
#    if clcheck_server foo        # then foo is INACTIVE
#    if ! clcheck_server foo        # then foo is ACTIVE
#
###############################################################################

PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
[[ "$VERBOSE_LOGGING" == "high" ]] && set -x
[[ "$VERBOSE_LOGGING" == "high" ]] && version='1.10.4.2'
HA_DIR="$(cl_get_path)"

SERVER=$1
STATUS=0    # daemon not active
FATAL_ERROR=255 # fatal error
integer retries=0

if [[ -n "${SERVER}" ]]
then
    # cthags is not defined until a CAA cluster is created, so if
    # no CAA clyster is defined, then cthags can not be active
    
    rc=$(LC_ALL=C lssrc -s $SERVER | grep 'not on file' | wc -l)
    
    if (( $rc == 1 ))
    then
        exit 0
    fi
    
    # lssrc may encounter temporary problems which result in a non-zero
    # return code from lssrc.  If this occurs, we will retry 3 times
    # if lssrc gives a zero return code, we will "return" inside the loop
    # and will not complete the retries

    while [[ $retries < 3 ]]
    do

        # first see if the server even exists
        if lssrc -s ${SERVER} 1>/dev/null 2>/dev/null
        then
        # If the server exists, then check to see if is inoperative.
        # Becuase of the possibility of lssrc reporting an erroneous
        # "inoperative" state at the same time as a valid "active"
        # state, we have to check for all the non-inoperative states:
        #
        # 1. active
        # 2. warned to stop
        # 3. stopping
        #
        # If none of them are present, then the server must be
        # "inoperative".

        check_if_down=$(LC_ALL=C lssrc -s ${SERVER} | egrep "stop|active")

        # If any of the non-inoperative states are found, then return 1
        # indicating that the server is not yet inoperative. Otherwise
        # return 0 indicating it is inoperative.
        if [[ -z "${check_if_down}" ]] 
        then
            # No non-inoperative state was found,
            # but check one more time to be sure we
            # didn't just catch SRC between states
            sleep 1
            check_if_down=$(LC_ALL=C lssrc -s ${SERVER} | egrep "stop|active")
            if [[ -z "${check_if_down}" ]] 
            then
            # No non-inoperative state was found,
            # so the server must be inoperative.
            return $STATUS
            else
            # A non-inoperative state was found,
            # so the server is not yet inoperative.
            STATUS=$(check_server_extended ${SERVER})
            return $STATUS
            fi
        else
            # A non-inoperative state was found,
            # so the server is not yet inoperative.
        STATUS=$(check_server_extended ${SERVER})
            return $STATUS
        fi
        else
            retries=$retries+1
            dspmsg scripts.cat 9999 "$PROGNAME has encountered lssrc problems.  Retrying.\n" $PROGNAME 1>&2
            sleep 3
    fi
    done
    # If lssrc fails 3 times in a row, assume unresolvable problems with lssrc

    # If the server being checked is the clstrmgr, attempt another way to
    # determine if the clstrmgr is up or not.  This secondary way is to use
    # the cllsstbys command, which returns with an rc=0 as long as there is
    # a boot adapter defined (which should cover all clusters).

    if [ "$SERVER" = "clstrmgrES" ]
    then
        dspmsg scripts.cat 9999 "$PROGNAME has encountered successive lssrc problems.\n\
        Trying alternative method to determine clstrmgr status.\n" $PROGNAME 1>&2

        # if cllsstbys returns anything except 2, clstrmgr is up,
        # so return 1.  otherwise, the clstrmgr is down, so return 0

        cllsstbys 1>/dev/null 2>/dev/null
        if (( $? != 2 ))
        then
            STATUS=1
            return $STATUS
        else
            return $STATUS
        fi
    else
        # If the server being checked is not the clstrmgr, don't try anything
        # additional.  Those cases should not cause extreme problems.
        dspmsg scripts.cat 9999 "$PROGNAME has encountered successive lssrc problems.  Aborting.\n" $PROGNAME 1>&2
        STATUS=$FATAL_ERROR
        return $STATUS
    fi

fi
return $STATUS
