# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/usr/sbin/cluster/sa/sap/etc/SAPInstanceGlobals.sh 1.8 
#  
# 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 

# @(#)60  1.8  src/43haes/usr/sbin/cluster/sa/sap/etc/SAPInstanceGlobals.sh, hacmp.assist.sap, 61haes_r720, 1539B_hacmp720 9/15/15 11:44:48 

typeset version="1.8"

typeset my_inst_numbers
typeset -i mycnt=0
typeset -A inst_number
typeset -A sap_exe_dir
typeset -A instance_prof
typeset -A virt_ips

typeset my_inst_numbers=$(clodmget -n -q "application_id=$APPLICATION_ID and name=INSTANCE_NUMBERS" -f value HACMPsa_metadata 2>/dev/null)
[[ -z $my_inst_numbers ]] && {
  INSTANCE_TYPE=${my_inst_names%[0-9][0-9]}
  my_inst_numbers=${my_inst_names#${INSTANCE_TYPE}}
}

my_inst_numbers=$(echo $my_inst_numbers | awk '{gsub("\n"," ");print $0}')
# For PHA 712 Style ODM entries like --> "01_02"
my_inst_numbers=$(echo $my_inst_numbers | awk '{gsub("_"," ");print $0}')

mycnt=0
for  i  in $my_inst_numbers
do
   inst_numbers[$mycnt]=$i
   ((mycnt=mycnt+1))
done


typeset SID=${SAPSYSTEMNAME}

typeset my_sap_exe_dir=$(clodmget -n -q "application_id=$APPLICATION_ID and name=EXECUTABLE" -f value HACMPsa_metadata 2>/dev/null)
[[ -z $my_sap_exe_dir ]] && my_sap_exe_dir="/usr/sap/${SID}/SYS/exe/run"

my_sap_exe_dir=$(echo $my_sap_exe_dir | awk '{gsub("\n"," ");print $0}')

mycnt=0
for  i  in $my_sap_exe_dir
do
   sap_exe_dir[$mycnt]=$i
   ((mycnt=mycnt+1))
done


typeset wait_for_started_delay=$(clodmget -n -q "application_id=$APPLICATION_ID and name=START_DELAY" -f value HACMPsa_metadata 2>/dev/null)
[[ -z $wait_for_started_delay ]] && wait_for_started_delay=0

typeset wait_for_started_timeout=$(clodmget -n -q "application_id=$APPLICATION_ID and name=START_TIMEOUT" -f value HACMPsa_metadata 2>/dev/null)
[[ -z $wait_for_started_timeout ]] && wait_for_started_timeout=0

typeset WaitforStopped_delay=$(clodmget -n -q "application_id=$APPLICATION_ID and name=STOP_DELAY" -f value HACMPsa_metadata 2>/dev/null)
[[ -z $WaitforStopped_delay ]] && WaitforStopped_delay=0

typeset wait_for_stopped_timeout=$(clodmget -n -q "application_id=$APPLICATION_ID and name=STOP_TIMEOUT" -f value HACMPsa_metadata 2>/dev/null)
[[ -z $wait_for_stopped_timeout ]] && wait_for_stopped_timeout=0

#this profile can only be used to start a SAP command. It is not relyable when grep for that string as there 
#are multible paths considering sapcpe and the links to SYS.
typeset my_instance_prof=$(clodmget -n -q "application_id=$APPLICATION_ID and name=PROFILE" -f value HACMPsa_metadata 2>/dev/null)
my_instance_prof=$(echo $my_instance_prof | awk '{gsub("\n"," ");print $0}')

mycnt=0
for  i  in $my_instance_prof
do
   instance_prof[$mycnt]=$i
   ((mycnt=mycnt+1))
done

typeset -i is_local_vg=$(clodmget -n -q "application_id=$APPLICATION_ID and name=VOLUME_GROUP" -f value HACMPsa_metadata | grep -c LOCAL 2>/dev/null) #value is 0 or 1 as we do grep -c

typeset -i is_nfs=$(clodmget -n -q "application_id=SAP_GLOBALS_${SID} and name=IS_NFS" -f value HACMPsa_metadata)
                 
typeset sapmnt_nfs=$(clodmget -n -q "application_id=SAP_GLOBALS_${SID} and name=EXPORTS" -f value HACMPsa_metadata)


typeset nfs_ip=$(clodmget -n -q "application_id=SAP_GLOBALS_${SID} and name=NFS_IP" -f value HACMPsa_metadata)

typeset -i is_ers_enabled=$(clodmget -n -q "application_id=$APPLICATION_ID and name=IS_ERS_ENABLED" -f value HACMPsa_metadata 2>/dev/null)

typeset -i max_ensmon_retries=$(clodmget -n -q "application_id=$APPLICATION_ID and name=ENS_SYNC" -f value HACMPsa_metadata 2>/dev/null) # default = 10 what is 10 * 5 seconds

typeset logger_logfile=$(clodmget -n -q "application_id=SAP_GLOBALS_${SID} and name=LOGGER_LOGFILE" -f value HACMPsa_metadata)

typeset -i notification_level=$(clodmget -n -q "application_id=$APPLICATION_ID and name=NOTIFICATION_LEVEL" -f value HACMPsa_metadata)
                     #0= no additional notification will be send == default
                     #1= monitor will send notification for YELLOW core processes
                     #2= monitor will send notification for all Yellow and worse processes where operation is continuing in addition.
                     #4= startscript will send notification, not monitors
                     #>4 = satartscripts will send notification for status Yellow and monitors as well
                     #combinations by adding values allowed ==> 8 levels
                     #the level is the same for all instance types for one SID

notification_script=$(clodmget -n -q "application_id=$APPLICATION_ID and name=NOTIFY" -f value HACMPsa_metadata)
      
typeset -l sapadmuser=$(clodmget -n -q "application_id=SAP_GLOBALS_${SID} and name=SAPADMUSER" -f value HACMPsa_metadata 2>/dev/null)               
typeset su_sapadm="/usr/bin/su - ${sapadmuser} -c"
typeset SAPenv=$(clodmget -n -q "application_id=SAP_GLOBALS_${SID} and name=SAPENV" -f value HACMPsa_metadata)

typeset my_virt_ips=$(clodmget -n -q "application_id=$APPLICATION_ID and name=VIRTUAL_IPS" -f value HACMPsa_metadata 2>/dev/null)
[[ $my_virt_ips == "LOCAL" ]] || [[ -z $my_virt_ips ]] && {
  ${su_sapadm} ${sap_exe_dir}/sapcontrol -nr ${inst_numbers} -function GetVersionInfo  > /dev/null
  [[ $? == 0 ]] || {
  	${su_sapadm} ${sap_exe_dir}/sapcontrol -nr ${inst_numbers} -function StartService ${SID} > /dev/null
  	[[ $? == 0 ]] || {
      KLIB_SAP_SA_logmsg ERROR 151 74 sapsa.cat "clodmget failed to get value of %1$s.\n" "VIRTUAL_IPS"
      exit 1
  	}
  }
  my_virt_ips=$(${su_sapadm} ${sap_exe_dir}/sapcontrol -nr ${inst_numbers} -format script -function ParameterValue SAPLOCALHOST  | grep "0 : " | cut -f2 -d : | tr -d " ")
  [[ -z $my_virt_ips ]] && {
    KLIB_SAP_SA_logmsg ERROR 151 74 sapsa.cat "clodmget failed to get value of %1$s.\n" "VIRTUAL_IPS"
    exit 1
  }  	
}


# For PHA 712 Style ODM entries like --> "VIP1_VIP2"
my_virt_ips=$(echo $my_virt_ips | awk '{gsub("_"," ");print $0}')

mycnt=0
for  i  in $my_virt_ips
do
   virt_ips[$mycnt]=$i
   ((mycnt=mycnt+1))
done

sa_sap_xplatform_logging=$(clodmget -n -q "application_id=$APPLICATION_ID and name=SA_SAP_XPLATFORM_LOGGING" -f value HACMPsa_metadata 2>/dev/null)

exit_code_start_sapcontrol_start_failed=$(clodmget -n -q "application_id=$APPLICATION_ID and name=EC_START_FAILED" -f value HACMPsa_metadata 2>/dev/null)
exit_code_start_sapcontrol_startservice_failed=$(clodmget -n -q "application_id=$APPLICATION_ID and name=EC_START_SERVICE_FAILED" -f value HACMPsa_metadata 2>/dev/null)
exit_code_start_nfs_failed=$(clodmget -n -q "application_id=$APPLICATION_ID and name=EC_START_NFS_FAILED" -f value HACMPsa_metadata 2>/dev/null) 
exit_code_monitor_sapstartsrv_unavailable=$(clodmget -n -q "application_id=$APPLICATION_ID and name=EC_MONITOR_SAPSTARTSRV_UNAVAILABLE" -f value HACMPsa_metadata 2>/dev/null) 
exit_code_monitor_failover_on_gw_outage=$(clodmget -n -q "application_id=$APPLICATION_ID and name=EC_MONITOR_FAILOVER_ON_GW_OUTAGE" -f value HACMPsa_metadata 2>/dev/null) 
# End of GLOBALS

