#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2019,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/tsmserver/sbin/cl_tsmservermonitor.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010 
# 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/tsmserver/sbin/cl_tsmservermonitor.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

###############################################################################################
#
# NAME:
#       cl_tsmservermonitor
#
# PURPOSE:
#       monitor script of Smart Assist for TSM server instance
#
# ARGUMENTS:
#       -i TSM instance name
#
# RETURNS
#  0 on success
#  1 on Failure
#
#################################################################################################

. /usr/es/sbin/cluster/sa/tsmserver/sbin/cl_tsmserverGlobals

eval export $(cllsparam -n $(clodmget -f nodename HACMPcluster))
if [[ $VERBOSE_LOGGING == "high" ]]
then
    PS4_TIMER=true
    set -x
    version='1.2'
fi
KLIB_HACMPLOG_ENTRY=$KLIB_TSM_SERVER_HACMPLOG_ENTRY

typeset inst_name

while getopts i:d: opt
do
        case $opt in

                i) # instance name, which will be highly available
                        inst_name=$OPTARG
                        ;;
        esac
done
dat=$(/usr/bin/date)
KLIB_SA_logmsg INFO 2 30 tsmserversa.cat "Starting monitoring of TSM server instance %1\$s at %2\$s \n" $inst_name "$dat"
app_id=$(clodmget -q "name=INSTANCE_NAME and value=$inst_name" -f application_id -n HACMPsa_metadata)
inst_dir=$(clodmget -q "application_id=$app_id and name=INSTANCE_DIRECTORY" -f value -n HACMPsa_metadata)
PID=""
[[ -f $inst_dir/dsmserv.v6lock ]] && {

        read R1 R2 R3 PID R4 < $inst_dir/dsmserv.v6lock
}
#for IPv4 communication
if [[ -z $PID ]];then
	[[ -f $inst_dir/dsmserv.lock ]] && {

 	       read R1 R2 R3 PID R4 < $inst_dir/dsmserv.lock
	}
fi

# check if the pid exists, if not exit 
ps -ef|grep $PID|grep -v grep  >/dev/null 2>&1
[[ $? != 0 ]] && { 
			KLIB_SA_logmsg ERROR 2 31 tsmserversa.cat "dsmserv deamon is not running with PID %1\$s present in dsmserv.v6lock file, for TSM server instance %2\$s \n" $PID $inst_name
			exit 1 
		 }

user=$(clodmget -q "application_id=$app_id and name=INSTANCE_USER" -f value -n HACMPsa_metadata)
set +x
password=$(clodmget -q "application_id=$app_id and name=INSTANCE_PASSWORD" -f value -n HACMPsa_metadata | /usr/es/sbin/cluster/cspoc/cldecodearg)
set -x

# The q sess will test TSM server connection information
$DSM_DIR/dsmadmc -quiet -se=$inst_name -id=$user -pass=$password q sess > /dev/null 2>&1
[[ $? != 0 ]] && {
			KLIB_SA_logmsg ERROR 2 32 tsmserversa.cat "Monitoring is failed while querying TSM server connection information for TSM server instance %1\$s \n" $inst_name 
        		exit 1 
		 }
dat=$(/usr/bin/date)
KLIB_SA_logmsg INFO 2 33 tsmserversa.cat "TSM server instance %1\$s is monitored sucessfully at %2\$s\n" $inst_name "$dat"
exit 0
