#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/usr/sbin/cluster/sa/sap/sbin/cl_sapMonitor.sh 1.13 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2013,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 
# @(#)57  1.13  src/43haes/usr/sbin/cluster/sa/sap/sbin/cl_sapMonitor.sh, hacmp.assist.sap, 61haes_r720, 1522B_hacmp720 5/25/15 19:24:07 

##
## NAME:    cl_sapMonitor
##
## PURPOSE:
##          Monitor script for SAP NetWeaver instance(s).
##
##
## ARGUMENTS:
##
##  Type:
##      -a Application ID
##
##
## OUTPUT:
## 
## 
## RETURNS:
##      0 on success or degraded functionality
##      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=$PATH:/usr/es/sbin/cluster/utilities
PATH=$PATH:/bin:/usr/bin
export PATH

#-------------------------------------------------------------------------------
#Main
#-------------------------------------------------------------------------------

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

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

typeset script_name="cl_sapMonitor"
typeset -i mycnt=0
typeset -i count=0
typeset -i ctl=0
typeset -i currtime
typeset -i endtime


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

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

[[ -z $APPLICATION_ID ]] && {
    KLIB_SAP_SA_logmsg ERROR 151 73 sapsa.cat "\"%1\$s\": Application ID not passed as input Parameter. Quit immediately with Exit code 1. Please add application ID as input parameter to the PowerHA Smart Assist Application Servers.\n" $script_name
    exit 1
}

## GLUECODE
# Indication from the glue code that we need not monitor the application.
typeset -i temp_var=$(clodmget -n -q "application_id=${APPLICATION_ID} and name=SAP_HA_GLUE_CODE" -f value HACMPsap_connector 2>/dev/null )
if [[ $temp_var != "0" ]]
then
     currtime=$(date +%s)
     endtime=$(clodmget -n -q "application_id=${APPLICATION_ID} and name=END_STABLE_TIME" -f value HACMPsap_connector 2>/dev/null)
     if ((  $endtime != 0 ))
     then
          if ((  $currtime > $endtime  ))
          then
               echo "HACMPsap_connector: \n\
value=0" |/usr/bin/odmchange -o HACMPsap_connector -q "application_id=${APPLICATION_ID} and name=SAP_HA_GLUE_CODE"

           else
	      exit 0
           fi
      else
	exit 0
      fi
fi
## GLUECODE

#Define an array of instances from the specified APPLICATION_ID and get its appropriate SID from ODM
typeset myinst_names=$(clodmget -n -q "application_id=$APPLICATION_ID and name=INSTANCE_NAMES" -f value HACMPsa_metadata 2>/dev/null)
[[ -z $myinst_names ]] && {
    KLIB_SAP_SA_logmsg ERROR 151 74 sapsa.cat "clodmget failed to get value of %1$s.\n" "INSTANCE_NAMES"
    exit 1
}
myinst_names=$(echo $myinst_names | awk '{gsub("\n"," ");print $0}')
# For PHA 712 Style ODM entries like --> "ASCS00_SCS01"
myinst_names=$(echo $myinst_names | awk '{gsub("_"," ");print $0}')

for  i  in $myinst_names
do
   inst_names[$mycnt]=$i
   ((mycnt=mycnt+1))
done

. /usr/es/sbin/cluster/sa/sap/sbin/SAPUtilities_xplatform
while (( $count < ${#inst_names[@]} ))
do

    unset SAPSYSTEMNAME
    setSAPGlobalEnv -i ${inst_names[$count]}
    ret=$?
    (( $ret != 0 )) && {
       KLIB_SAP_SA_logmsg ERROR 151 72 sapsa.cat "\"%1\$s\": Environment setup failed. Quit immediately with Exit code 1.\n" $script_name
       exit 1
     }

. /usr/es/sbin/cluster/sa/sap/etc/SAPInstanceGlobals
     if [[ -z ${virt_ips[$count]} ]]
     then
       ((ctl=count-1))
       if (( $ctl >= 0 )) 
       then
          virt_ips[$count]=${virt_ips[$ctl]}
       fi
     fi

  # Ensure sapstartsrv is started or can be started otherwise exit as we tried to restart.
  KLIB_SAP_SA_logmsg DEBUG 151 30 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Ensure sapstartsrv process is operational or can be started.\n" $script_name ${inst_names[$count]} $SID
  Control_sapstartsrv -A "Prereq_and_start_cmd" -I ${inst_names[$count]} -S ${SAPSYSTEMNAME} -E ${sap_exe_dir[$count]} -V ${virt_ips[$count]} -L "${SAPenv}"    
  [[ $? == $sapstartsrv_failed_to_start ]] && {
    KLIB_SAP_SA_logmsg ERROR 151 31 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The sapstartsrv process failed to start.\n Monitor script exit with \"%4\$s\".\n"  $script_name ${inst_names[$count]} $SID ${exit_code_monitor_sapstartsrv_unavailable}
    [[ $exit_code_monitor_sapstartsrv_unavailable == 0 ]] && [[ $notification_level > 0 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - The sapstartsrv process failed to start.\n Monitor script exit with ${exit_code_monitor_sapstartsrv_unavailable}.\n") 
    exit $exit_code_monitor_sapstartsrv_unavailable
  } 

  KLIB_SAP_SA_logmsg DEBUG 151 32 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Start health check.\n"  $script_name ${inst_names[$count]} $SID 
  Control_instance -A "Check_cmd" -I ${inst_names[$count]} -S ${SAPSYSTEMNAME} -E ${sap_exe_dir[$count]} -V ${virt_ips[$count]} -L "${SAPenv}" -T "0"
  check_instance_rc=$? 
   [[ $check_instance_rc == $GetProcessList_ERROR ]] && exit 1
  #This branch is for Application server instances or CS instances if no ERS is deployed.
  [[ $check_instance_rc == $GetProcessList_GREEN ]] && { 
    #for application server instances
    KLIB_SAP_SA_logmsg DEBUG 151 33 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The instance is running.\n"  $script_name ${inst_names[$count]} $SID
    [[ ${inst_names[$count]} == @(*SCS*) ]] && KLIB_SAP_SA_logmsg DEBUG 151 34 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - There is no enqueue replication server instance in place. \nThis is due to not setting up an ERS instance or if the cluster is running on single node.\n"  $script_name ${inst_names[$count]} $SID
  } 
  #This will be only true for CS and ERS instances
  [[ $check_instance_rc == $EnqGetStatistic_GREEN ]] && {  
    KLIB_SAP_SA_logmsg DEBUG 151 35 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The instance is running and replicating.\n"  $script_name ${inst_names[$count]} $SID
  }
  #instance is running, but replication is broken
  [[ $check_instance_rc > $EnqGetStatistic_GREEN ]] && (( $check_instance_rc < $GetProcessList_not_GREEN )) && { 
    [[ $notification_level > 0 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - The instance is running. Replication is not working well. Returncode: ${check_instance_rc}.\n(${EnqGetStatistic_YELLOW}=replicating but hanging behind, else replication Stopped or broken.)") 
    KLIB_SAP_SA_logmsg WARNING 151 21 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The instance is running. Replication is not working well. Returncode: \"%4\$s\".\n(\"%5\$s\"=replicating but hanging behind, else replication Stopped or broken.)\n" $script_name ${inst_names[$count]} $SID ${check_instance_rc} ${EnqGetStatistic_YELLOW}    	
  }
  #instance is partially running
  [[ $check_instance_rc == $GetProcessList_not_GREEN ]] && { 
    KLIB_SAP_SA_logmsg DEBUG 151 36 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The instance might need to be recovered.\n Start evaluation of severity.\n"  $script_name ${inst_names[$count]} $SID
    Control_eval_severity_instance -I ${inst_names[$count]} -S ${SAPSYSTEMNAME} -E ${sap_exe_dir[$count]} -V ${virt_ips[$count]} -L "${SAPenv}" -T "0"
    typeset -i sev_rc=$?
    #Evaluation for CS Instances
    [[ ${inst_names[$count]} == @(*SCS*) ]] && {
      typeset -i en ms gw
      let "en=(sev_rc%GetProc_en_mod)-(sev_rc%(GetProc_en_mod/10))"
      let "ms=(sev_rc%GetProc_ms_mod)-(sev_rc%(GetProc_ms_mod/10))"
      let "gw=(sev_rc%GetProc_gw_mod)-(sev_rc%(GetProc_gw_mod/10))"
      [[ $en > $GetProc_en_YELLOW ]] || [[ $ms > $GetProc_ms_YELLOW ]] && {
        KLIB_SAP_SA_logmsg ERROR 151 37 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The core processes are inoperative. The accumulated returncode: \"%4\$s\".\n(2|3=error en, 20|30=error ms, 22<->33=both error, >33 also the gw is broken)\n"  $script_name ${inst_names[$count]} $SID ${sev_rc}
        exit 1
      } 
      [[ $gw > $GetProc_gw_YELLOW ]] && {
      	KLIB_SAP_SA_logmsg ERROR 151 38 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The Gateway process is inoperative. The user defined variable was set to trigger a failover. \nThe accumulated returncode: \"%4\$s\".\n"  $script_name ${inst_names[$count]} $SID ${sev_rc}
      	[[ $notification_level > 1 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - The Gateway process is inoperative. The user defined variable was set to trigger a failover. \nThe accumulated returncode: ${sev_rc}.\n")
      	[[ $exit_code_monitor_failover_on_gw_outage == 1 ]] && exit 1       
      }
      [[ $en == $GetProc_en_YELLOW ]] || [[ $ms == $GetProc_ms_YELLOW ]] || [[ $gw == $GetProc_gw_YELLOW ]] && {
        KLIB_SAP_SA_logmsg WARNING 151 39 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The following processes are indicating a state of YELLOW. If this persists manual corrections are required. \nThe accumulated returncode: \"%4\$s\".\n(1=en, 10=ms, 100=gw or accumulation)\n"  $script_name ${inst_names[$count]} $SID ${sev_rc}
        [[ $notification_level > 0 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - The following processes are indicating a state of YELLOW. If this persists manual corrections are required. \nThe accumulated returncode: ${sev_rc}.\n1=en, 10=ms, 100=gw or accumulation.\n")
      }
      KLIB_SAP_SA_logmsg DEBUG 151 43 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Continue to operate.\n"  $script_name ${inst_names[$count]} $SID 
    }
    #Evaluation for ERS Instances
    [[ ${inst_names[$count]} == @(ERS*) ]] && {
      [[ $sev_rc > $GetProc_er_YELLOW ]] && {
        KLIB_SAP_SA_logmsg ERROR 151 41 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The core process is inoperative. The accumulated returncode: \"%4\$s\".\n"  $script_name ${inst_names[$count]} $SID ${sev_rc}
        exit 1
      }
      [[ $sev_rc == $GetProc_er_YELLOW ]] && {
        KLIB_SAP_SA_logmsg WARNING 151 42 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The processes indicates a state of YELLOW. If this persists manual corrections are required. \nThe accumulated returncode: \"%4\$s\".\n"  $script_name ${inst_names[$count]} $SID ${sev_rc}
        [[ $notification_level > 0 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - The processes indicates a state of YELLOW. If this persists manual corrections are required. \nThe accumulated returncode: ${sev_rc}.\n")
      }
      KLIB_SAP_SA_logmsg DEBUG 151 43 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Continue to operate.\n"  $script_name ${inst_names[$count]} $SID 
    }
    #Evaluation for Application Server Instances
    [[ ${inst_names[$count]} != @(*SCS*|ERS*) ]] && {
      GetInstType -I ${inst_names[$count]}  -S ${SAPSYSTEMNAME} -E ${sap_exe_dir[$count]} -V ${virt_ips[$count]} -L "${SAPenv}"
      InstType=$? # 0=JAVA, 1=DUAL, 2=ABAP
      case $InstType in
      	0) #JAVA
      	  typeset -i ser0 ig icm jc sdm
          let "jc=(sev_rc%GetProc_jc_mod)-(sev_rc%(GetProc_jc_mod/10))"
          let "ig=(sev_rc%GetProc_ig_mod)-(sev_rc%(GetProc_ig_mod/10))"
          let "icm=(sev_rc%GetProc_icm_mod)-(sev_rc%(GetProc_icm_mod/10))"
          let "ser=(sev_rc%J2EEGetProc_ser0_mod)-(sev_rc%(J2EEGetProc_ser0_mod/10))"
          let "sdm=(sev_rc%J2EEGetProc_sdm_mod)-(sev_rc%(J2EEGetProc_sdm_mod/10))"
          KLIB_SAP_SA_logmsg DEBUG 151 44 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Evaluate health of a JAVA Application server. The accumulated returncode is: \"%4\$s\".\n"  $script_name ${inst_names[$count]} $SID ${sev_rc}
      	  #eval severity of core processes jc and server.
      	  [[ $jc == $GetProc_jc_GREEN ]] && {
      	    KLIB_SAP_SA_logmsg DEBUG 151 45 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The jcontrol/jstart is running. This process will be able to recover all remaining issues.\n"  $script_name ${inst_names[$count]} $SID	
      	  }
      	  [[ $jc == $GetProc_jc_YELLOW ]] && {
      	  	[[ $ser < $J2EEGetProc_ser0_GRAY ]] && {
      	  	  [[ $notification_level > 0 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - Functionalty degraded. The jc and ser process are effected.\n")
      	  	  KLIB_SAP_SA_logmsg WARNING 151 46 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Functionalty degraded. The jc and ser process are effected.\n"  $script_name ${inst_names[$count]} $SID
      	    }
      	    [[ $ser > $J2EEGetProc_ser0_YELLOW ]] && {
      	      [[ $notification_level > 0 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - Functionalty seriously degraded, maybe broken. The ser0 process is effected.\n")
      	  	  KLIB_SAP_SA_logmsg WARNING 151 47 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Functionalty seriously degraded, maybe broken. The ser0 process is effected.\n"  $script_name ${inst_names[$count]} $SID	
      	    }
      	  }
          [[ $jc > $GetProc_jc_YELLOW ]] && {
      	    KLIB_SAP_SA_logmsg ERROR 151 48 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The jcontrol/jstart is broken and the server Process with ID 0 is stopped or RED.\n Monitor will exit with 1.\n"  $script_name ${inst_names[$count]} $SID
          	exit 1
      	  }
      	  #eval severity of intermediate processes: ig and icm
      	  [[ $ig == $GetProc_ig_YELLOW ]] || [[ $icm == $GetProc_icm_YELLOW ]] && {
      	  	[[ $notification_level > 1 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - Functionalty is degraded. The ig and/or icm are running in state YELLOW.\n")
      	  	KLIB_SAP_SA_logmsg WARNING 151 49 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Functionalty is degraded. The ig and/or icm are running in state YELLOW.\n"  $script_name ${inst_names[$count]} $SID      	     	  
          }
          [[ $ig > $GetProc_ig_YELLOW ]] || [[ $icm > $GetProc_icm_YELLOW ]] && {
      	  	[[ $notification_level > 1 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - The ig and/or icm are not operational.\n")
      	  	KLIB_SAP_SA_logmsg WARNING 151 50 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The ig and/or icm are not operational.\n"  $script_name ${inst_names[$count]} $SID      	     	  
          }
            #log for low priority processes if they are not GREEN   
          [[ $sdm > J2EEGetProc_sdm_GREEN ]] && {
          	KLIB_SAP_SA_logmsg WARNING 151 51 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Functionalty is degraded. The sdm is not running in state GREEN.\n"  $script_name ${inst_names[$count]} $SID
          	[[ $notification_level > 1 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - Functionalty is degraded. The sdm is not running in state GREEN.\n")
          }
      	;;
      	1) #DUAL
      	  typeset -i ser0 ig icm jc dw rest
          let "jc=(sev_rc%GetProc_jc_mod)-(sev_rc%(GetProc_jc_mod/10))"
          let "ig=(sev_rc%GetProc_ig_mod)-(sev_rc%(GetProc_ig_mod/10))"
          let "icm=(sev_rc%GetProc_icm_mod)-(sev_rc%(GetProc_icm_mod/10))"
          let "ser=(sev_rc%J2EEGetProc_ser0_mod)-(sev_rc%(J2EEGetProc_ser0_mod/10))"
          let "dw=(sev_rc%GetProc_dw_mod)-(sev_rc%(GetProc_dw_mod/10))"
          let "rest=sev_rc-dw-ser-icm-ig-jc"
      	  KLIB_SAP_SA_logmsg DEBUG 151 52 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Evaluate health of a DUAL stack Application server. The accumulated returncode is: \"%4\$s\".\n"  $script_name ${inst_names[$count]} $SID ${sev_rc}
			#eval severity of core processes jc and server.
      	  [[ $jc == $GetProc_jc_GREEN ]] && [[ $dw == $GetProc_dw_GREEN ]] && {
      	    KLIB_SAP_SA_logmsg DEBUG 151 53 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The jcontrol/jstart and disp+work are running. This processees will be able to recover all remaining issues.\n"  $script_name ${inst_names[$count]} $SID	
          } || {
      	    [[ $jc == $GetProc_jc_YELLOW ]] && [[ $ser < $J2EEGetProc_ser0_GRAY ]] && {
      	  	  [[ $notification_level > 0 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - Functionalty is degraded. The jc and/od ser0 is effected.\n")
      	  	  KLIB_SAP_SA_logmsg WARNING 151 54 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Functionalty is degraded. The jc and/or ser0 is effected.\n"  $script_name ${inst_names[$count]} $SID
      	    }
            [[ $jc > $GetProc_jc_YELLOW ]] ||  [[ $dw > $GetProc_dw_YELLOW ]] && {
      	      KLIB_SAP_SA_logmsg ERROR 151 55 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The jcontrol/jstart and/or disp+work are broken. Monitor will exit with 1.\n"  $script_name ${inst_names[$count]} $SID
          	  exit 1
      	    } 
      	  }
		    #eval severity of intermediate processes: ig and icm
          [[ $ig == $GetProc_ig_YELLOW ]] || [[ $icm == $GetProc_icm_YELLOW ]] && {
      	  	[[ $notification_level > 1 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - Functionalty is degraded. The ig and icm are running in state YELLOW.\n")
      	  	KLIB_SAP_SA_logmsg WARNING 151 56 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Functionalty is degraded. The ig and icm are running in state YELLOW.\n"  $script_name ${inst_names[$count]} $SID      	     	  
          }
          [[ $ig > $GetProc_ig_YELLOW ]] || [[ $icm > $GetProc_icm_YELLOW ]] && {
      	  	[[ $notification_level > 1 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - Functionalty is degraded. The ig and icm are not operational.\n")  
      	  	KLIB_SAP_SA_logmsg ERROR 151 57 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Functionalty is degraded. The ig and icm are not operational.\n"  $script_name ${inst_names[$count]} $SID      	     	  
          }          
            #log for low priority processes if they are not GREEN   
          [[ $rest > 0 ]] && {
          	KLIB_SAP_SA_logmsg WARNING 151 58 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Functionalty of rslgcoll, and/or rslsend, and/or debugproxy have a non green state.\n"  $script_name ${inst_names[$count]} $SID
          	[[ $notification_level > 1 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - Functionalty of rslgcoll, and/or rslsend, and/or debugproxy have a non green state.\n")
          }   
      	;;
      	2) #ABAP
      	  typeset -i dw ig icm gw
          let "ig=(sev_rc%GetProc_ig_mod)-(sev_rc%(GetProc_ig_mod/10))"
          let "icm=(sev_rc%GetProc_icm_mod)-(sev_rc%(GetProc_icm_mod/10))"
          let "dw=(sev_rc%GetProc_dw_mod)-(sev_rc%(GetProc_dw_mod/10))"
          let "gw=(sev_rc%GetProc_gw_mod)-(sev_rc%(GetProc_gw_mod/10))"
      	  KLIB_SAP_SA_logmsg DEBUG 151 59 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Evaluate health of an ABAP Application server. The accumulated returncode is: \"%4\$s\".\n"  $script_name ${inst_names[$count]} $SID ${sev_rc}
      	  [[ $dw == $GetProc_dw_GREEN ]] && KLIB_SAP_SA_logmsg DEBUG 151 60 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The disp+work is running. This process will be able to recover all remaining issues.\n"  $script_name ${inst_names[$count]} $SID
      	  [[ $dw > $GetProc_dw_YELLOW ]] && {
      	  	KLIB_SAP_SA_logmsg ERROR 151 61 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The disp+work is not operational. Monitor will exit with 1.\n"  $script_name ${inst_names[$count]} $SID
      	  	exit 1
      	  }
      	  [[ $dw == $GetProc_dw_YELLOW ]] && {
      	  	KLIB_SAP_SA_logmsg WARNING 151 62 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The disp+work is not fully operational. Please investigate.\n"  $script_name ${inst_names[$count]} $SID
      	  	[[ $notification_level > 0 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - The disp+work is not fully operational. Please investigate.\n") 
      	  } 
      	    #eval severity of intermediate processes: ig and icm
          [[ $ig == $GetProc_ig_YELLOW ]] || [[ $icm == $GetProc_icm_YELLOW ]] && {
      	  	[[ $notification_level > 1 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - Functionalty is degraded. The ig and/or icm are running in state YELLOW.\n")
      	  	KLIB_SAP_SA_logmsg WARNING 151 63 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Functionalty is degraded. The ig and/or icm are running in state YELLOW.\n"  $script_name ${inst_names[$count]} $SID      	     	  
          }
          [[ $ig > $GetProc_ig_YELLOW ]] || [[ $icm > $GetProc_icm_YELLOW ]] && {
      	  	[[ $notification_level > 1 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - The ig and icm are not operational.\n")    
      	  	KLIB_SAP_SA_logmsg ERROR 151 64 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - The ig and icm are not operational.\n"  $script_name ${inst_names[$count]} $SID      	     	  
          }             
          [[ $gw > $GetProc_gw_YELLOW ]] && {
          	KLIB_SAP_SA_logmsg ERROR 151 65 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Functionality of gw not available.\n"  $script_name ${inst_names[$count]} $SID
          	[[ $notification_level > 1 ]] && [[ -n $notification_script ]] && $(${notification_script} "Instance ${inst_names[$count]} of ${SID} - Functionality of gw not available.\n")
          	[[ $exit_code_monitor_failover_on_gw_outage == 1 ]] && exit 1
          }
      	;;
      	3 )
      	  KLIB_SAP_SA_logmsg ERROR 151 66 sapsa.cat "\"%1\$s\" Instance \"%2\$s\" of \"%3\$s\" - Stack type could not be determined. No health check will be performed.\n"  $script_name ${inst_names[$count]} $SID
      	;;
      esac
    }
  }  
((count=count+1))
done
exit 0

