#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2020,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/usr/sbin/cluster/sa/sap/sbin/cl_sapMonitorSCS.sh 1.3.1.4 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2008,2015 
# 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/sa/sap/sbin/cl_sapMonitorSCS.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

##
## NAME:    cl_sapMonitorSCS
##
## PURPOSE:
##          Monitoring script for SCS instance(s).
##
##
## ARGUMENTS:
##
##  Type:
##      -a Application ID
##
##
## OUTPUT:
## 
## 
## RETURNS:
##      0 on success
##      1 on failure
##

. /usr/es/lib/ksh93/func_include

#----------------------------------------------------------------------------
# Global Definitions
#----------------------------------------------------------------------------

. /usr/es/sbin/cluster/sa/sap/etc/SAPGlobals
. /usr/es/sbin/cluster/sa/sap/sbin/SAPUtilities

typeset PROGRAM=${0##*/}
PATH=$PATH:/usr/es/sbin/cluster/sa/sbin
PATH=$(/usr/es/sbin/cluster/utilities/cl_get_path all)

#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# Function:
#   checkIsSCSRunning
#
# Purpose:
#   To Monitor SAP SCS instance processes using sapcontrol for healthiness.
#
# Arguments:
#   1) Instance Number
#   2) Virtual IP used by the instance
#   3) Instance Name
#
# Returns:
#   0 For sucess
#   exit with 1 For failure
#

function checkIsSCSRunning {
    if [[ $VERBOSE_LOGGING == 'high' ]]
    then
	PS4_FUNC=checkIsSCSRunning
	set -x
    fi
    typeset instance_num virtual_ip
    typeset processDisplayStatus processNames    
    instance_num=$1
    virtual_ip=$2
    instance_name=$3
    out=$(/usr/bin/su - $SAPADMNUSR -c \
          "env LANG=C sapcontrol -host $virtual_ip -nr $instance_num \
          -function GetProcessList -format script" 2>/dev/null | grep "[0-9] " \
          | awk -F":" '/dispstatus:/ {print $2}')

    [[ -z $out ]] && {
        user_msg 50 7 $PROGRAM
        exit 1
    }
    processDisplayStatus=(${out})
    out=$(/usr/bin/su - $SAPADMNUSR -c \
          "env LANG=C sapcontrol -host $virtual_ip -nr $instance_num \
          -function GetProcessList -format script" 2>/dev/null | grep "[0-9] " \
          | awk -F":" '/name:/ {print $2}')

    [[ -z $out ]] && {
        user_msg 50 8 $PROGRAM
        exit 1
    }

    processNames=(${out})    
    for count in "${!processDisplayStatus[@]}"; do
        [[ ${processDisplayStatus[$count]} == "GREEN" ]] && { 
            user_msg 50 9 $instance_name ${processNames[$count]} \
                          ${processDisplayStatus[$count]}
        } || {
            user_msg 50 10 $instance_name ${processNames[$count]} \
                           ${processDisplayStatus[$count]}
            exit 1                    
        }
    done
    return 0
}

#-------------------------------------------------------------------------------
#Main
#-------------------------------------------------------------------------------
typeset ret SID instances inst_names virt_ips inst_nos
typeset instance_start_profile inst_profile

if [[ -z $ODMDIR ]]; then
    ODMDIR=/usr/es/sbin/cluster/etc/objrepos/active/
fi
export ODMDIR

eval export $(cllsparam -n $(clodmget -f nodename HACMPcluster))

if [[ $VERBOSE_LOGGING == 'high' ]]
then
    PS4_TIMER=true
    set -x
    version='1.3.1.4'
fi

while getopts a: option
do
    case $option in
    a)
        APPLICATION_ID=$OPTARG
    ;;
    esac
done

[[ -z $APPLICATION_ID ]] && {
    user_msg 30 30 $PROGRAM
    exit 1
}
set -a
eval $(clquerysaapp -a $APPLICATION_ID | grep SAPSYSTEMNAME)
set +a

export SAPSYSTEMNAME

[[ -z $SAPSYSTEMNAME ]] && {
    user_msg 30 34 $PROGRAM
    exit 1
}

user_msg 40 10 $SAPSYSTEMNAME

setSAPGlobalEnv $SAPSYSTEMNAME
ret=$?
(( $ret != 0 )) && {
    user_msg 30 12 
    exit 1
}


inst_names=($(clquerysaapp -a $APPLICATION_ID \
                 | awk -F"=" '/INSTANCE_NAMES/  {print $2}' 2>/dev/null))

[[ -z $inst_names ]] && {
    user_msg 30 31 $PROGRAM
    exit 1
}

virt_ips=($(clquerysaapp -a $APPLICATION_ID \
                 | awk -F"=" '/VIRTUAL_IPS/  {print $2}' 2>/dev/null))

[[ -z $virt_ips ]] && {
    user_msg 30 32 $PROGRAM
    exit 1
}

inst_nos=($(clquerysaapp -a $APPLICATION_ID \
                 | awk -F"=" '/INSTANCE_NUMBERS/  {print $2}' 2>/dev/null))

[[ -z $inst_nos ]] && {
    user_msg 30 33 $PROGRAM
    exit 1
}

for count in "${!inst_names[@]}"; do

   # Set START_PROFILE
        instance_start_profile="/usr/sap/"$SAPSYSTEMNAME"/SYS/profile/START_"${inst_names[$count]}"_"${virt_ips[$count]}
   # Set INSTANCE_PROFILE
        inst_profile="/usr/sap/"$SAPSYSTEMNAME"/SYS/profile/"$SAPSYSTEMNAME"_"${inst_names[$count]}"_"${virt_ips[$count]}

    if [ -f ${instance_start_profile} ]; then
      profile=${instance_start_profile};
    else
      profile=${inst_profile};
    fi

    checkForRunningSapstartsrv $profile
    ret=$?
    (( ret == 0 )) && {
        user_msg 50 13 ${inst_names[$count]} 
    } || {
        user_msg 50 12 ${inst_names[$count]}
        exit 1
    }
    checkIsSCSRunning ${inst_nos[$count]} ${virt_ips[$count]} \
                      ${inst_names[$count]}
done
exit 0
