#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

SCLOGGER=/usr/cluster/lib/sc/scds_syslog
PKG=SUNWscmqi.sib
METHOD=`basename $0`
RESOURCE_DEPENDENCIES=`/usr/cluster/bin/scha_resource_get -O RESOURCE_DEPENDENCIES -R $RESOURCE -G $RESOURCEGROUP`
STOP_TIMEOUT=`/usr/cluster/bin/scha_resource_get -O Stop_timeout -R $RESOURCE -G $RESOURCEGROUP`
status=`/usr/cluster/bin/scha_resource_get -O STATUS -R $RESOURCE -G $RESOURCEGROUP`
STATUS=`echo $status | awk '{print $1}'`
DS_TMPDIR=/var/tmp
DS_BASEDIR=`dirname $0`
LOGFILE=${DS_TMPDIR}/${RESOURCE}_logfile
TASK_COMMAND=""
RESOURCE_PROJECT_NAME=""
SCHA_RESOURCEGROUP_GET=/usr/cluster/bin/scha_resourcegroup_get
SCHA_RESOURCE_GET=/usr/cluster/bin/scha_resource_get
SCHA_CLUSTER_GET=/usr/cluster/bin/scha_cluster_get

syslog_tag()
{
        #
        # Data Service message format
        #

        $SET_DEBUG

        print "SC[${PKG:-??}.${METHOD:-??}]:${RESOURCEGROUP:-??}:${RESOURCE:-??}"
}

scds_syslog()
{

        #
        # Log a message
        #

        $SET_DEBUG

        $SCLOGGER "$@" &
}

debug_message()
{
        #
        # Output debug test to syslog if required
        #

        if [ "$DEBUG" = "$RESOURCE" -o "$DEBUG" = "ALL" ]
        then
                SET_DEBUG="set -x"

                DEBUG_TEXT=$1

                scds_syslog -p daemon.debug -t $(syslog_tag) -m \
                        "%s" "$DEBUG_TEXT"
        else
                SET_DEBUG=
        fi
}

log_message()
{
        #
        # Output a message to syslog as required
        #

        debug_message "Function: log_message - Begin"
        $SET_DEBUG

        if [ -s "${LOGFILE}" ]
        then
                PRIORITY=$1
                HEADER=$2

                if [ "${PRIORITY}" = "error" ]
                then
                        echo "The following command was executed - pgrep -fl -u $MQSIUSER" >> $LOGFILE
                        pgrep -fl -u $MQSIUSER >> $LOGFILE
                fi

                #
                # Ensure the while loop only reads a closed file
                #

                strings ${LOGFILE} > ${LOGFILE}.copy

                while read MSG_TXT
                do
                        scds_syslog -p daemon.${PRIORITY} -t $(syslog_tag) -m \
                                "%s - %s" \
                                "${HEADER}" "${MSG_TXT}"
                done < ${LOGFILE}.copy

                su ${MQSIUSER} -c "cat /dev/null > $LOGFILE" > /dev/null
		cat /dev/null > ${LOGFILE}.copy
        fi

        debug_message "Function: log_message - End"
}

validate_options()
{
        #
        # Ensure all options are set
        #

        for i in RESOURCE RESOURCEGROUP QMGR IN_QUEUE OUT_QUEUE MQSIUSER BROKER DBUSER
        do
                case $i in
                        RESOURCE)
                        if [ -z $RESOURCE ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -R not set"
                                exit 1
                        fi;;

                        RESOURCEGROUP)
                        if [ -z $RESOURCEGROUP ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -G not set"
                                exit 1
                        fi;;

                        QMGR)
                        if [ -z $QMGR ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -Q not set"
                                exit 1
                        fi;;

                        IN_QUEUE)
                        if [ -z $IN_QUEUE ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -I not set"
                                exit 1
                        fi;;

                        OUT_QUEUE)
                        if [ -z $OUT_QUEUE ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -O not set"
                                exit 1
                        fi;;

                        MQSIUSER)
                        if [ -z $MQSIUSER ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -U not set"
                                exit 1
                        fi;;

                        BROKER)
                        if [ -z $BROKER ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -B not set"
                                exit 1
                        fi;;

                        DBUSER)
                        if [ -z $DBUSER ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -D not set"
                                exit 1
                        fi;;
                esac
        done
}

validate()
{
        debug_message "Function: validate - Begin"
        $SET_DEBUG

        rc_validate=0

	#
        # Validate Group mqbrkrs and user root exists within group mqbrkrs
        #

	if cat /etc/group | awk -F: '{print $1}' | grep mqbrkrs > /dev/null
        then
                debug_message "Validate - Group mqbrkrs exists"

                if cat /etc/group | awk -F: '{ if ($1 == "mqbrkrs") print $4}' | grep root > /dev/null
                then
                        debug_message "Validate - Group mqbrkrs exists with user root"
                else
                        rc_validate=1

                	scds_syslog -p daemon.error -t $(syslog_tag) -m \
			"Validate - User root is not a member of group mqbrkrs" 
                fi
        else
                rc_validate=1

                scds_syslog -p daemon.error -t $(syslog_tag) -m \
		"Validate - Group mqbrkrs does not exist"
        fi

	# 
	# Validate ${MQSIUSER}
	#
	
	pgroup=`grep -w $MQSIUSER /etc/passwd | awk -F: '{print $4}'`
	
	if [ ! -z $pgroup ]
	then
		if cat /etc/group | awk -F: '{ if ($1 == "mqm") print $3}' | grep $pgroup > /dev/null
		then
			debug_message "Validate - Primary group for userid $MQSIUSER is mqm"
		else
			if cat /etc/group | awk -F: '{ if ($1 == "mqm") print $4}' | grep $MQSIUSER > /dev/null
			then
				debug_message "Validate - Secondary group for userid $MQSIUSER is mqm"
			else
				rc_validate=1

				scds_syslog -p daemon.error -t $(syslog_tag) -m \
				"Validate - Userid $MQSIUSER is not a member of group mqm" 
			fi
		fi

		if cat /etc/group | awk -F: '{ if ($1 == "mqbrkrs") print $3}' | grep $pgroup > /dev/null
		then
			debug_message "Validate - Primary group for userid $MQSIUSER is mqbrkrs"
		else
			if cat /etc/group | awk -F: '{ if ($1 == "mqbrkrs") print $4}' | grep $MQSIUSER > /dev/null
			then
				debug_message "Validate - Secondary group for userid $MQSIUSER is mqbrkrs"
			else
				rc_validate=1

				scds_syslog -p daemon.error -t $(syslog_tag) -m \
				"Validate - Userid $MQSIUSER is not a member of group mqm" 
			fi
		fi
	else
		rc_validate=1

		scds_syslog -p daemon.error -t $(syslog_tag) -m \
		"Validate - Userid $MQSIUSER is not a valid userid"
	fi

	#
        # Validate that /opt/mqsi and /var/mqsi exist
        #

        if [ -d /opt/mqsi -a -d /var/mqsi ]
        then
                debug_message "Validate - WebSphere MQ Broker file systems ok"
        else
                rc_validate=1

                scds_syslog -p daemon.error -t $(syslog_tag) -m \
		"Validate - WebSphere MQ Broker file systems not defined"
        fi

	#
        # Validate that /var/mqsi/locks is a symbolic link
        #

	if [ -h /var/mqsi/locks ]
	then
		debug_message "Validate - WebSphere MQ Broker /var/mqsi/locks is a symbolic link"
	else
		rc_validate=1

                scds_syslog -p daemon.error -t $(syslog_tag) -m \
		"Validate - WebSphere MQ Broker /var/mqsi/locks is not a symbolic link"
	fi

        debug_message "Function: validate - End"
        $SET_DEBUG
}

standard_resource_get()
{
        debug_message "Function: standard_resource_get - Begin"
        $SET_DEBUG

        PROPERTY=$1

        ${SCHA_RESOURCE_GET} -R ${RESOURCE} -G ${RESOURCEGROUP} -O ${PROPERTY}

        s1=$?

        if [ "${s1}" -ne 0 ]; then
                debug_message "standard_resource_get - Get property ${PROPERTY} rc<${s1}>, ignore if SC3.0"
        fi

        debug_message "Function: standard_resource_get - End"
        return ${s1}
}

standard_resourcegroup_get()
{
        debug_message "Function: standard_resourcegroup_get - Begin"
        $SET_DEBUG

        PROPERTY=$1

        ${SCHA_RESOURCEGROUP_GET} -R ${RESOURCE} -G ${RESOURCEGROUP} -O ${PROPERTY}

        s1=$?

        if [ "${s1}" -ne 0 ]; then
                debug_message "standard_resource_get - Get property ${PROPERTY} rc<${s1}>, ignore if SC3.0"
        fi

        debug_message "Function: standard_resourcegroup_get - End"
        return ${s1}
}

srm_function()
{
        debug_message "Function: srm_function - Begin"
        $SET_DEBUG

        USER=$1

        #
        # If Solaris 8 just return
        #

        if [ `/usr/bin/uname -r` = "5.8" ];
        then
                return 0
        fi

        #
        # Retrieve RESOURCE_PROJECT_NAME
        #

        RESOURCE_PROJECT_NAME=`standard_resource_get RESOURCE_PROJECT_NAME`

        #
        # Retrieve RG_PROJECT_NAME if RESOURCE_PROJECT_NAME is not set
        #

        if [ -z "${RESOURCE_PROJECT_NAME}" ]; then

                RESOURCE_PROJECT_NAME=`standard_resourcegroup_get RG_PROJECT_NAME`
        fi

        #
        # Return if no projects are defined
        #

        if [ -z "${RESOURCE_PROJECT_NAME}" ]; then
                return 0
        fi

        #
        # Validate that $USER belongs to the project defined by
        # ${RESOURCE_PROJECT_NAME}
        #

        PROJ_MEMBER=`/usr/bin/projects ${USER} | /usr/bin/grep -w ${RESOURCE_PROJECT_NAME}`

        if [ -z "${PROJ_MEMBER}" ];
        then
             scds_syslog -p daemon.error -t $(syslog_tag) -m \
                        "Validate - The user %s does not belongs to project %s" \
                        "${USER}" "${RESOURCE_PROJECT_NAME}"
                return 1
        else
                debug_message "srm_function - User ${USER} belongs to project ${RESOURCE_PROJECT_NAME}"
        fi

        #
        # Set TASK_COMMAND
        #

        TASK_COMMAND="/usr/bin/newtask -p ${RESOURCE_PROJECT_NAME}"

        debug_message "Function: srm_function - End"
        return 0
}

create_dummy_process()
{
        debug_message "Function: create_dummy_process - Begin"
        $SET_DEBUG

        DUMMY_PROCESS="/tmp/${RESOURCE}_${RESOURCEGROUP}_dummy.csh"

        cat > ${DUMMY_PROCESS} <<-EOF
	#!/bin/csh

	while (1)
		sleep 3600
	end
	EOF

        /bin/chmod 755 ${DUMMY_PROCESS}

        ${DUMMY_PROCESS} &

        scds_syslog -p daemon.notice -t $(syslog_tag) -m \
                "Create_dummy_process - WebSphere MQ broker %s was manually started" \
                "${BROKER}"

        debug_message "Function: create_dummy_process - End"
}

get_resource_dependencies()
{
	#
	# Check and get correct Resource_dependencies
	#

        debug_message "Function: get_resource_dependencies - Begin"
	$SET_DEBUG

	QM_RES=
	DB_RES=
	ORA_SID=
	ORA_HOME=

        # Here we ensure (via Resource Type) that the resource has been defined with the correct
        # Resource_dependencies. We expect to find at least a resource for the Broker Queue Manager
        # and a Broker RDBMS (Currently only DB2 and Oracle)

	for i in $RESOURCE_DEPENDENCIES
	do
		RESOURCE_TYPE=`scha_resource_get -O TYPE -R $i -G $RESOURCEGROUP`

		case $RESOURCE_TYPE in
			SUNW.gds*)

				#
				# Check for GDS Resource Type and Start_command for SUNWscmgr 
				#
				
				type=`scha_resource_get -O extension -R $i -G $RESOURCEGROUP Start_command`
				if echo $type | awk '{print $2}' | grep SUNWscmqs | grep start-qmgr > /dev/null
				then
					if [ "$QM_RES" ]
					then
					        scds_syslog -p daemon.error -t $(syslog_tag) -m \
						"get_resource_dependencies - WebSphere MQ Broker Queue Manager resource %s already set" \
						"${QM_RES}"

					        scds_syslog -p daemon.error -t $(syslog_tag) -m \
						"get_resource_dependencies - Only one WebSphere MQ Broker Queue Manager resource dependency can be set"

						exit 1
					else
						QM_RES=$i
					fi
				fi
				;;
			
			IBM.db2udb*)

				#
				# Check for DB2 Resource Type
				#
				
				if [ "$DB_RES" ]
				then
					scds_syslog -p daemon.error -t $(syslog_tag) -m \
					"get_resource_dependencies - WebSphere MQ Broker RDBMS resource %s already set" \
					"${DB_RES}"
	
					scds_syslog -p daemon.error -t $(syslog_tag) -m \
					"get_resource_dependencies - Only one WebSphere MQ Broker RDBMS resource dependency can be set"

					exit 1
				fi

				DB_RES=$i

				for brk in `ls /var/mqsi/brokers`
				do
					if grep -w "$QMGR" /var/mqsi/registry/${brk}/QueueManagerName > /dev/null
					then
						DB2_DB=`cat /var/mqsi/registry/${brk}/DataSourceName`
					fi
				done

				if [ -z "$DB2_DB" ]
				then
					scds_syslog -p daemon.error -t $(syslog_tag) -m \
					"get_resource_dependencies - DB2 database not found in /var/mqsi/registry"

					exit 1
				fi
			
				;;

			SUNW.oracle_server*)

				# 
				# Check for Oracle Resource Type
				#

				if [ "$DB_RES" ]
				then
					scds_syslog -p daemon.error -t $(syslog_tag) -m \
					"get_resource_dependencies - WebSphere MQ Broker RDBMS resource %s already set" \
					"${DB_RES}"
					
					scds_syslog -p daemon.error -t $(syslog_tag) -m \
					"get_resource_dependencies - Only one WebSphere MQ Broker RDBMS resource dependency can be set"

					exit 1
				fi

				DB_RES=$i
				ora_sid=`scha_resource_get -O extension -R $i -G $RESOURCEGROUP ORACLE_SID`
				ORA_SID=`echo $ora_sid | awk '{print $2}'`
				ora_home=`scha_resource_get -O extension -R $i -G $RESOURCEGROUP ORACLE_HOME`
				ORA_HOME=`echo $ora_home | awk '{print $2}'`
				;;

			*)		
				# Try again, perhaps it's not the correct resource_dependency
				continue
				;;
		esac
	done

	if [ -z "$QM_RES" ]
	then
		scds_syslog -p daemon.error -t $(syslog_tag) -m \
		"get_resource_dependencies - Resource_dependecies does not have a Queue Manager resource"
		exit 1
	fi

	if [ -z "$DB_RES" ]
	then
		scds_syslog -p daemon.error -t $(syslog_tag) -m \
		"get_resource_dependencies - Resource_dependecies does not have a RDBMS resource"
		exit 1
	fi

        debug_message "Function: get_resource_dependencies - End"
}

restart_resource_group()
{
	# 
	# As the result of the Broker RDBMS failing or restarting we need to
	# restart the Resource Group to effectively restart the Broker Queue Manager
	#

        debug_message "Function: restart_resource_group - Begin"
	$SET_DEBUG

	scds_syslog -p daemon.notice -t $(syslog_tag) -m \
	"Resource Group %s will be restarted" \
	"${RESOURCEGROUP}"

        scha_control -O RESTART -G ${RESOURCEGROUP} -R ${RESOURCE}

        rc_scha_control=$?

        if [ "$rc_scha_control" != 0 ]
        then
		scds_syslog -p daemon.error -t $(syslog_tag) -m \
		"restart_resource_group - Resource Group restart failed rc<%s>" \
		"${rc_scha_control}"
        fi

        debug_message "Function: restart_resource_group - End"
}

set_status()
{
        #
        # Set appropriate resource status
        #

        debug_message "Function: set_status - Begin"
	$SET_DEBUG

        STATUS_TYPE=$1

        scha_resource_setstatus -R $RESOURCE -G $RESOURCEGROUP -s $STATUS_TYPE

        rc_scha_resource_setstatus=$?

        if [ "$rc_scha_resource_setstatus" != 0 ]
        then
                scds_syslog -p daemon.error -t $(syslog_tag) -m \
                "set_status - rc<%s> type<%s>" \
                "${rc_scha_resource_setstatus}" "${STATUS_TYPE}"
        fi

        debug_message "Function: set_status - End"
}

check_qmgr_pids()
{
	# 
	# Check that the main Queue Manager pids are running
	#

        debug_message "Function: check_qmgr_pids - Begin"
	$SET_DEBUG

	rc_check_qmgr_pids=

	while [ "$rc_check_qmgr_pids" != 000 ]
	do
		rc_check_qmgr_pids=
		sleep 1

		for pid in amqzxma0 amqhasmx amqzllp0 
		do
			if ps -ef | grep $pid | grep $QMGR | \
				grep -v grep > /dev/null
			then
				rc=0
			else
				rc=1
			fi
			rc_check_qmgr_pids=$rc_check_qmgr_pids$rc
		done
	done

        debug_message "Function: check_qmgr_pids - End"
}

delete_pid_files()
{
	#
	# Delete Broker Queue Manager and RDBMS primary pid files
	#

        debug_message "Function: delete_pid_files - Begin"
	$SET_DEBUG

	rm $DS_TMPDIR/${RESOURCEGROUP},${QM_RES},pid 2> /dev/null
	rm $DS_TMPDIR/${RESOURCEGROUP},${DB_RES},pid 2> /dev/null
	rm $DS_TMPDIR/${RESOURCEGROUP},${DB_RES},pmon_pid 2> /dev/null
	rm $DS_TMPDIR/${RESOURCEGROUP},${DB_RES},lgwr_pid 2> /dev/null
	rm $DS_TMPDIR/${RESOURCEGROUP},${DB_RES},ckpt_pid 2> /dev/null
	rm $DS_TMPDIR/${RESOURCEGROUP},${DB_RES},smon_pid 2> /dev/null

        debug_message "Function: delete_pid_files - End"
}

check_oracle_pid()
{
	# 
	# Wait for correct Broker RDBMS (Oracle) and store pid(s)
	#

        debug_message "Function: check_oracle_pid - Begin"
	$SET_DEBUG

	# 
	# Here we check for some key Oracle processes and record their pid
	
	rc_check_oracle_pids=
	oracle_pid=
	flag=
	rc=

	while [ "$rc_check_oracle_pids" != 0000 ]
	do
		rc_check_oracle_pids=
		sleep 1
		
		# Just check the key processes

		for pid in pmon lgwr ckpt smon
		do
			if oracle_pid=`pgrep -f ora_${pid}_${ORA_SID}`
			then
				echo $oracle_pid > $DS_TMPDIR/${RESOURCEGROUP},${DB_RES},${pid}_pid
				rc=0
			else
				if [ "$flag" ]
				then
					rc=1
				else
					flag=set

					scds_syslog -p daemon.notice -t $(syslog_tag) -m \
					"Waiting for WebSphere MQ Broker RDBMS"

					rc=1
				fi
			fi

			rc_check_oracle_pids=$rc_check_oracle_pids$rc
		done
		
	done

	rc_tnsping=
	
	while [ "${rc_tnsping}" != 0 ]
	do
		sleep 2
		su - $DBUSER -c "tnsping $ORA_SID" > /dev/null
		rc_tnsping=$?
	done
	
	scds_syslog -p daemon.notice -t $(syslog_tag) -m \
	"WebSphere MQ Broker RDBMS available"

        debug_message "Function: check_oracle_pid - End"
}

check_db2_pid()
{
	#
	# Wait for correct Broker RDBMS (DB2) and store pid
	#

        debug_message "Function: check_db2_pid - Begin"
	$SET_DEBUG

	rc_check_db2_pids=
	db2_pid=
	flag=

	while [ "$rc_check_db2_pids" != 0 ]
	do
		rc_check_db2_pids=
		sleep 1

		# Here we check for the first pid started by root which
		# subsequently becomes the parent pid for the primary pid
		# started by $DBUSER

		db2_pid=`pgrep -f -u root db2sysc -P 1`

		if [ "$db2_pid" ]
		then
                        for i in $db2_pid    
                        do                  
                        	pid=`pgrep -u $DBUSER -P $i`     

				if [ "$pid" ]
				then
					echo $pid > $DS_TMPDIR/${RESOURCEGROUP},${DB_RES},pid
					rc_check_db2_pids=0
					break
				else
					if [ "$flag" ]
					then
						rc_check_db2_pids=1
					else
						flag=set
					
						scds_syslog -p daemon.notice -t $(syslog_tag) -m \
						"Waiting for WebSphere MQ Broker RDBMS"

						rc_check_db2_pids=1
					fi
				fi
			done
		else
			if [ "$flag" ]
			then
				rc_check_db2_pids=1
			else
				flag=set

				scds_syslog -p daemon.notice -t $(syslog_tag) -m \
				"Waiting for WebSphere MQ Broker RDBMS"

				rc_check_db2_pids=1
			fi
		fi
	done

	rc_connect=
	
	while [ "${rc_connect}" != 0 ]
	do
		sleep 2
		su - $DBUSER -c "db2 connect to $DB2_DB" > /dev/null
		rc_connect=$?
	done
	
	scds_syslog -p daemon.notice -t $(syslog_tag) -m \
	"WebSphere MQ Broker RDBMS available"

        debug_message "Function: check_db2_pid - End"
}

check_qmgr_pid()
{
	#
	# Wait for correct Broker Queue Manager and store pid
	#

        debug_message "Function: check_qmgr_pid - Begin"
	$SET_DEBUG

	rc_check_qmgr_pid=
	qmgr_pid=
	flag=

	while [ "$rc_check_qmgr_pid" != 0 ]
	do
		rc_check_qmgr_pid=
		sleep 1

		# Here we check for the Execution Controller pid, however only
		# if the pmftag exists for the resource.

		if pmfadm -l ${RESOURCEGROUP},${QM_RES},0.svc > /dev/null
		then
			qmgr_pid=`pmfadm -l ${RESOURCEGROUP},${QM_RES},0.svc |\
				 grep pid | awk '{print $2}'`

			# We only accept the Execution Controller - amqzxma0 as the pid

			if ps -p $qmgr_pid | awk '{print $4}'| grep amqzxma0
			then
				echo $qmgr_pid > $DS_TMPDIR/${RESOURCEGROUP},${QM_RES},pid
				rc_check_qmgr_pid=0
			else
				if [ "$flag" ]
				then
					rc_check_qmgr_pid=1
				else
					flag=set
		
					scds_syslog -p daemon.notice -t $(syslog_tag) -m \
					"Waiting for WebSphere MQ Broker Queue Manager"

					rc_check_qmgr_pid=1
				fi
			fi
		else
			if [ "$flag" ]
			then
				rc_check_qmgr_pid=1
			else
				flag=set
		
				scds_syslog -p daemon.notice -t $(syslog_tag) -m \
				"Waiting for WebSphere MQ Broker Queue Manager"

				rc_check_qmgr_pid=1
			fi
		fi
	done

        debug_message "Function: check_qmgr_pid - End"
}

test_oracle_pid()
{
	#
	# Test if the Broker RDBMS (Oracle) pid has changed
	#

        debug_message "Function: test_oracle_pid - Begin"
	$SET_DEBUG

	rdbms_pid=
	rdbms_pid_previous=
	rdbms_pid_current=
	rc_test_oracle_pids=
	rc=

	for pid in pmon lgwr ckpt smon
	do
		if [ -r $DS_TMPDIR/${RESOURCEGROUP},${DB_RES},${pid}_pid ]
		then
			rdbms_pid_previous=`cat $DS_TMPDIR/${RESOURCEGROUP},${DB_RES},${pid}_pid`
			rdbms_pid_current=`pgrep -f ora_${pid}_${ORA_SID}`

			if [ "$rdbms_pid_previous" = "$rdbms_pid_current" ]
			then 
				rc=0
			else
				rc=1
			fi
		else	
			rc_test_oracle_pids=
		fi

		rc_test_oracle_pids=$rc_test_oracle_pids$rc
	done

	if [ -z "${rc_test_oracle_pids}" ] 
	then
		rdbms_pid=lost
	elif [ "${rc_test_oracle_pids}" = 0000 ] 
	then
		rdbms_pid=not_changed
	else
		rdbms_pid=changed
	fi

        debug_message "Function: test_oracle_pid - End"
}

test_db2_pid()
{
	#
	# Test if the Broker RDBMS (DB2) pid has changed
	#

        debug_message "Function: test_db2_pid - Begin"
	$SET_DEBUG

	rdbms_pid=
	rdbms_pid_previous=
	rdbms_pid_current=

	if [ -r $DS_TMPDIR/${RESOURCEGROUP},${DB_RES},pid ]
	then
		rdbms_pid_previous=`cat $DS_TMPDIR/${RESOURCEGROUP},${DB_RES},pid`

		db2_pid=`pgrep -f -u root db2sysc -P 1`

		if [ "$db2_pid" ]
		then
                        for i in $db2_pid 
                        do               
                        	rdbms_pid_current=`pgrep -u $DBUSER -P $i`    

				if [ "$rdbms_pid_previous" = "$rdbms_pid_current" ]
				then 
					rdbms_pid=not_changed
					break
				else
					rdbms_pid=changed
				fi
			done
		else	
			rdbms_pid=lost
		fi
	else
		rdbms_pid=lost
	fi

        debug_message "Function: test_db2_pid - End"
}

test_qmgr_pid()
{
	#
	# Test if the Broker Queue Manager pid has changed
	#

        debug_message "Function: test_qmgr_pid - Begin"
	$SET_DEBUG

	qmgr_pid=
	qmgr_pid_previous=
	qmgr_pid_current=

	if [ -r $DS_TMPDIR/${RESOURCEGROUP},${QM_RES},pid ]
	then
		qmgr_pid_previous=`cat $DS_TMPDIR/${RESOURCEGROUP},${QM_RES},pid`

		if pmfadm -l ${RESOURCEGROUP},${QM_RES},0.svc > /dev/null
		then
			qmgr_pid_current=`pmfadm -l ${RESOURCEGROUP},${QM_RES},0.svc |\
				 grep pid | awk '{print $2}'`

			if [ "$qmgr_pid_previous" = "$qmgr_pid_current" ]
			then 
				qmgr_pid=not_changed
			else
				qmgr_pid=changed
			fi
		else	
			qmgr_pid=lost
		fi
	else
		qmgr_pid=lost
	fi

        debug_message "Function: test_qmgr_pid - End"
}

start_broker()
{
        #
        # Start the MQSI Broker
	#
	# Here, we test for the existence of some semaphores
	#
	#  (a) MQSeriesIntegrator2BrokerResourceTableLockSempahore
	#  (b) MQSeriesIntegrator2RetainedPubsTableLockSemaphore
	# 
	# If either (a) or (b) exists and their respective 
	# semaphore id is not present on that node then we remove 
	# that lock file entry. This prevents 
	#  
	#  (a) Execution Group termination on startup with BIP2123
	#  (b) bipbroker termination on startup with BIP2088 
	#
	# Please note that /var/mqsi is installed as a GFS and as
	# part of the install of SUNWscmqi, a symlink for locks 
	# should be made to local disk, 	
	#
	#	ie, ln -s /var/mqsi_locks /var/mqsi/locks
	#
	# mqsistart will start the Queue Manager if it is
	# not running. Unfortunately, we can't have that 
	# behaviour, as the Queue Manager needs to be 
	# managed by Sun Cluster and it's respective
	# PMF tag.
	#
	# So, we delay issuing a mqsistart until we can 
	# at least see the main queue manager processes.
	# This loop continues until either the Queue
	# Manager becomes available or Start_timeout
	# expires for this resource. 
	#
	# Additionally, we also delay issuing a mqsistart until
	# we can ensure that the database is running.
        #

        debug_message "Function: start_broker - Begin"
	$SET_DEBUG

	# Ensure that $LOGFILE is created by ${MQSIUSER}
	srm_function ${MQSIUSER}
	su - ${MQSIUSER} -c "$TASK_COMMAND touch $LOGFILE"

	check_qmgr_pids

	rc_ping_qmgr=

        while [ "${rc_ping_qmgr}" != 0 ]
        do
		echo "ping qmgr" | runmqsc $QMGR > /dev/null
		rc_ping_qmgr=$?

		if [ "${rc_ping_qmgr}" = 0 ]
		then
			scds_syslog -p daemon.notice -t $(syslog_tag) -m \
			"WebSphere MQ Broker Queue Manager available" 

			# Test the MQSI Semaphores

			for semaphore in BrokerResourceTableLockSempahore RetainedPubsTableLockSemaphore
			do
				if [ -r /var/mqsi/locks/MQSeriesIntegrator2${semaphore} ]
				then
					semid_with_zeros=`cat /var/mqsi/locks/MQSeriesIntegrator2${semaphore}`
					semid=`expr $semid_with_zeros / 1`

					if ipcs -s | grep $semid > /dev/null
					then
						continue
					else
						scds_syslog -p daemon.notice -t $(syslog_tag) -m \
						"MQSeriesIntegrator2%s exists without an IPC semaphore entry" \
						"${semaphore}"
					
						scds_syslog -p daemon.notice -t $(syslog_tag) -m \
						"MQSeriesIntegrator2%s file deleted" \
						"${semaphore}"

						/usr/bin/rm /var/mqsi/locks/MQSeriesIntegrator2${semaphore}
					fi
				fi
			done
		
			if getent passwd ${MQSIUSER} | awk -F: '{print $7}' | grep csh > /dev/null
			then
				su - ${MQSIUSER} -c "$TASK_COMMAND env SC3_COMMAND=${START_COMMAND} mqsistart ${BROKER} >& $LOGFILE" > /dev/null
				rc_mqsistart=$?
			else
				su - ${MQSIUSER} -c "$TASK_COMMAND env SC3_COMMAND=${START_COMMAND} mqsistart ${BROKER} > $LOGFILE 2>&1" > /dev/null
				rc_mqsistart=$?
			fi

			break
		else
			scds_syslog -p daemon.notice -t $(syslog_tag) -m \
			"start_broker - Waiting for WebSphere MQ Broker Queue Manager"
			sleep 2
		fi
	done

        debug_message "Function: start_broker - End"
}

orderly_stop_broker()
{
	# 
	# Orderly stop of services
	#

        debug_message "Function: orderly_stop_broker - Begin"
	$SET_DEBUG

        for func in stop_broker_immediate \
                    cleanup_broker
        do
		case $func in
			stop_broker_immediate)	((FORCE_TIME=$STOP_TIMEOUT *30/100));;
			cleanup_broker)		((FORCE_TIME=$STOP_TIMEOUT *5/100));;
                esac

		LOOP_TIME=$FORCE_TIME

                $func

                while (( $LOOP_TIME > 0 ))
                do
			# Match only broker and effective id and exclude all mqsi<commands>

			if pgrep -fl ${BROKER} -u ${MQSIUSER} | grep -v mqsi > /dev/null
                        then
                                sleep 1
                                LOOP_TIME=`expr $LOOP_TIME - 1`
                        else
                                break
                        fi
                done

                # Test if we broke out of the loop
		# Match only broker and effective id and exclude all mqsi<commands>

		if pgrep -fl ${BROKER} -u ${MQSIUSER} | grep -v mqsi > /dev/null 
                then
			# As we process stop_broker_immediate output
			# any messages that have been collected

			log_message notice mqsistop
			continue
		else
                        # Break out of running all the functions
			log_message notice mqsistop
                        break
                fi
	done

        debug_message "Function: orderly_stop_broker - End"
}

stop_broker_immediate()
{
	#
	# Immediately stop the MQSI Broker
	#

        debug_message "Function: stop_broker_immediate - Begin"
	$SET_DEBUG

	srm_function ${MQSIUSER}

	if getent passwd ${MQSIUSER} | awk -F: '{print $7}' | grep csh > /dev/null
	then
		su - ${MQSIUSER} -c "$TASK_COMMAND env SC3_COMMAND=${STOP_COMMAND} mqsistop -i ${BROKER} >& $LOGFILE &" > /dev/null
	else
		su - ${MQSIUSER} -c "$TASK_COMMAND env SC3_COMMAND=${STOP_COMMAND} mqsistop -i ${BROKER} > $LOGFILE 2>&1 &" > /dev/null
	fi

        debug_message "Function: stop_broker_immediate - End"
}

cleanup_broker()
{
	# 
	# Cleanup the MQSI Broker
	#

        debug_message "Function: cleanup_broker - Begin"
	$SET_DEBUG
	
	if pgrep -fl -u ${MQSIUSER} ${BROKER} | grep -v mqsi > /dev/null
	then
		for pid in bipservice bipbroker DataFlowEngine
		do
			for i in `ps -ef | grep $pid | grep $BROKER | \
				grep -v grep | awk '{print $2}'`
			do
				kill -9 $i
			done
		done

		scds_syslog -p daemon.notice -t $(syslog_tag) -m \
		"All WebSphere MQ Broker processes stopped"
	fi

        debug_message "Function: cleanup_broker - End"
}

check_broker()
{
	# 
	# A simple message flow ensures that the MQSI Broker is
	# working properly. Here we simply test the following
	#
	# 	a. Check $IN_QUEUE and $OUT_QUEUE are valid
	# 	b. Empty out $OUT_QUEUE
	# 	c. Put a test message to $IN_QUEUE
	# 	d. Verify the message flow from $IN_QUEUE to $OUT_QUEUE
	#
	# If any of these programs (sc3inq, sc3get, sc3put) fail
	# then it indicates either a Queue Manager problem or an
	# MQSI Broker problem. In either case we return a restart
	# request via rc_check_broker=1. This will cause the MQSI
	# Broker to get restarted.
	#
	# Although this function check_broker is used by the probe
	# it additionally is used by the START method to ensure
	# that the MQSI Broker is truely up and running before
	# control is returned to the RGM. In the latter case a
	# return rc_check_broker=0 while the Resource_State is 
	# not yet Online.
	#
	# The START method will continually try to test, until
	# Start_timeout occurs, that the MQSI broker is truely
	# up and running.
	#

        debug_message "Function: check_broker - Begin"
	$SET_DEBUG

	# To help avoid any AMQ8041 messages on a Queue Manager restart
	# avoid running through this set of tests if the main processes
	# are not running.

        rc_check_qmgr_pids=

	for pid in amqzxma0 amqhasmx amqzllp0
	do
		if ps -ef | grep $pid | grep $QMGR | \
			grep -v grep > /dev/null
		then
			rc=0
		else
			rc=1
		fi

		rc_check_qmgr_pids=$rc_check_qmgr_pids$rc
        done

        if [ "$rc_check_qmgr_pids" != 000 ]
	then
		rc_check_broker=1
		
		scds_syslog -p daemon.error -t $(syslog_tag) -m \
		"check_broker - Main Queue Manager processes not found"

		return
	fi

	#
	# Allow NONE entries for $IN_QUEUE and $OUT_QUEUE 
	#

	if [ "$IN_QUEUE" = "NONE" ] && [ "$OUT_QUEUE" = "NONE" ]
	then
		# Only if bipservice is running

		if pgrep -fx "bipservice ${BROKER}" > /dev/null
		then
			rc_check_broker=0
		else
			rc_check_broker=1
		fi

		return
	fi

	# 
	# Check $IN_QUEUE and $OUT_QUEUE are valid and empty them out
	#
	
	for q in $IN_QUEUE $OUT_QUEUE 
	do
		if ERR_LOGFILE=`${DS_BASEDIR}/sc3get $q $QMGR`
		then
			debug_message "Function: check_broker - sc3get ${q} OK"

			rc_check_broker=0
		else
                	# Just to avoid this message appearing when the
                	# resource is being started.

			if [ "$STATUS" != "OK" ] 
       	        	then
				debug_message "Function: check_broker - sc3get ${q} ${ERR_LOGFILE}"

				rc_check_broker=1
			else
				log_message error "check_broker - sc3get ${q}"

				rc_check_broker=1
				debug_message "Function: check_broker - End"
				return
			fi
		fi
	done
			
	#
	# Put a test message to $IN_QUEUE
	#
				
	if ERR_LOGFILE=`echo "Testing message flow from $IN_QUEUE to $OUT_QUEUE" | \
		${DS_BASEDIR}/sc3put $IN_QUEUE $QMGR`
	then
		debug_message "Function: check_broker - sc3put ${IN_QUEUE} OK"

		rc_check_broker=0
	else
                # Just to avoid this message appearing when the
                # resource is being started.

		if [ "$STATUS" != "OK" ] 
       	        then
			debug_message "Function: check_broker - sc3put ${IN_QUEUE} ${ERR_LOGFILE}"

			rc_check_broker=1
		else
			log_message error "check_broker - sc3put ${IN_QUEUE}"
			
			rc_check_broker=1
			debug_message "Function: check_broker - End"
			return
		fi
	fi
		
	#
	# Verify the message flow from $IN_QUEUE to $OUT_QUEUE
	# 

	# Allow some time for the message flow
	sleep 2

	if ERR_LOGFILE=`${DS_BASEDIR}/sc3inq $OUT_QUEUE $QMGR`
	then
		# Test that Current queue depth = 1

		CURDEPTH=`echo $ERR_LOGFILE | awk -F= '{print $2}'`

		if [ "${CURDEPTH}" -eq 1 ]	
		then
			debug_message "Function: check_broker - sc3inq ${OUT_QUEUE} OK"

			rc_check_broker=0
		else
                	# Just to avoid this message appearing when the
                	# resource is being started.

			if [ "$STATUS" != "OK" ] 
                	then
				debug_message "Function: check_broker - sc3inq ${OUT_QUEUE} CURDEPTH(${CURDEPTH})"
				rc_check_broker=1
			else
				scds_syslog -p daemon.error -t $(syslog_tag) -m \
				"check_broker - sc3inq %s CURDEPTH(%s)" \
				"${OUT_QUEUE}" "${CURDEPTH}"

				rc_check_broker=1
			fi
		fi
	else
                # Just to avoid this message appearing when the
                # resource is being started.

		if [ "$STATUS" != "OK" ] 
                then
			debug_message "Function: check_broker - sc3inq ${OUT_QUEUE} ${ERR_LOGFILE}"
			
			rc_check_broker=1
		else
			log_message error "check_broker - sc3inq ${OUT_QUEUE}"

			rc_check_broker=1
		fi
	fi

        debug_message "Function: check_broker - End"
}
