#!/usr/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,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/wmq/sbin/cl_wmq_monitor.sh 1.4 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2011 
# 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/wmq/sbin/cl_wmq_monitor.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
###############################################################################
# 
# NAME: cl_wmq_monitor
#
# PURPOSE:
#			This script tests the operation of the queue manager.
#
# ARGUMENTS: Queue manager
#
# FLAGS:
#			m) QM name
# RETURNS:
#			An exit code is generated by the runmqsc command:
# 			0  => Either the queue manager is starting or 
#				  the queue manager is running and responds.Either is OK.
# 			>0 => The queue manager is not responding and not starting.
#
# ###########################################################################

function monitor_mqm {
	if [[ $VERBOSE_LOGGING == 'high' ]]
	then
	    PS_FUNC=monitor_mqm
	    set -x
	fi
	# Test the operation of the queue manager.
	#	Result is 0 on success, non-zero on error.
	KLIB_SA_logmsg INFO 2 5 wmq_sa.cat "Monitoring WebSphere MQ \
server configuration for Queue Manager %1\$s" $QM

	echo "ping qmgr" | /usr/bin/su $MQU -c "/usr/mqm/bin/runmqsc $QM > /dev/null 2>&1"
	pingresult=$?

	if (( $pingresult == 0 ))
	then # ping succeeded
		KLIB_SA_logmsg INFO 2 6 wmq_sa.cat "Queue Manager %1\$s is active" $QM
   		result=0

	else # ping failed
 		 # Don't condemn the queue manager immediately, it might be starting.
		 srchstr='( |-m)'${QM}' *.*$'
		 cnt=$(ps -ef | tr "\t" " " | grep strmqm | egrep "$srchstr" | grep -v grep | \
awk '{print $2}' | wc -l)
		if (( $cnt > 0 ))
		then
			# It appears that the queue manager is still starting up, tolerate
			KLIB_SA_logmsg WARN 2 7 wmq_sa.cat "Queue manager %1\$s is still \
starting up, it is active" $QM
			result=0
		else
			# There is no sign of the queue manager starting
			KLIB_SA_logmsg ERROR 2 8 wmq_sa.cat "Queue manager %1\$s is not active" $QM
			result=$pingresult
		fi

	fi

exit $result
}

##################
#MAIN
##################
eval export $(/usr/es/sbin/cluster/utilities/cllsparam -n $(/usr/es/sbin/cluster/utilities/clodmget -f nodename HACMPcluster))
if [[ $VERBOSE_LOGGING == 'high' ]] && set -x
then
    PS4_TIMER=true
    set -x
    version='1.4'
fi
. /usr/es/sbin/cluster/sa/wmq/sbin/cl_wmq_Globals

typeset opt
typeset MQU
typeset QM

while getopts u:m: opt
do
	case $opt in
		
			u) # mqm user name.
				MQU=$OPTARG
				;;
				
			m) # qm name.
				QM=$OPTARG
				;;
	esac
done

[[ -z $MQU ]] && [[ -z $QM ]] && exit 1
monitor_mqm
