#!/bin/sh

# This script was written as a courtesy for customers to run "ipcs" in order to
# quickly filter out NetBackup (NBU) related message queue keys.
# Valid NBU keys are:
#	0x412a4250
#	0x4f564e42
#	0x4c544952
#	0x412a4250
#	0x4f564e42
#	0x52455245
#	0x4d50584d
#
# It will also check running bpps to see if there are any processes.  If there are not any,
# There should not be any NBU keys listed in the message queues.  This is likely a hung key.
# The script will see if this key exist, then check if NBU is running or not, if it is not,
# It will prompt you if you like to clear this key with "ipcrm", then it will attempt to clear
# this key.
#
# This script is provided AS-IS, and you may freely distribute, and modify for your own use.
# Please note, this is a USE AT YOUR OWN RISK, deal.  
#

M_ADD=`ipcs -q | grep 0x412a4250`
M_COMM=`ipcs -q | grep 0x4f564e42`
M_LTID=`ipcs -q | grep 0x4c544952`
M_BPSCHED_REQ=`ipcs -q | grep 0x412a4250`
M_BPSCHED_RUN=`ipcs -q | grep 0x4f564e42`
M_STU=`ipcs -q | grep 0x52455245`
M_MPX=`ipcs -q | grep 0x4d50584d`
NOACTIVE_NBU=`/usr/openv/netbackup/bin/bpps`

echo ""
echo "-------------------------"
echo " Checking NetBackup mesQ"
echo "-------------------------"
echo "	This script is provided AS-IS, thank you."
echo ""
echo " This script should be running as root."
echo ""
echo " There are 7 mesgQ keys associated with NetBackup processes."
echo " There should be none when NBU is not running.  This utility"
echo " will check for these keys and if there are any while"
echo " NetBackup is not running, it will give you a chance to"
echo " attempt to clear them."
echo ""

if [ "$NOACTIVE_NBU" ]; then
	ACTIVE_NBU=1
else
	ACTIVE_NBU=0
fi

echo ""

if [ $ACTIVE_NBU -eq 1 ]; then
	echo "* NetBackup is Running *"
else
	echo "* NetBackup is not Running *"
fi


echo ""
echo " Checking MsgQueue used to add work to the empty-main bpsched worklist (0x412a4250):"
if [ "$M_ADD" ]; then
#		ipcs -a | grep 0x412a4250
		echo " $M_ADD"
		nbu_msgq=1
		if [ $ACTIVE_NBU -eq 0 ]; then
				echo "	NBU is not running!"
				echo "	Attempt to clear this key? [Y/N]"
				read answ_add
				if [ "$answ_add" = "Y" ]; then
					/usr/bin/ipcrm -Q 0x412a4250
				fi
		fi
else
	echo "	NONE"
fi


echo ""
echo " Checking MsgQueue used to communicate between parent & child bpscheds (0x4f564e42):"
if [ "$M_COMM" ]
	then
#		ipcs -a | grep 0x4f564e42
		echo " $M_COMM"
		nbu_msgq=1
		if [ $ACTIVE_NBU = 0 ]
			then
				echo "	NBU is not running!"
				echo "	Attempt to clear this key? [Y/N]"
				read answ_comm
				if [ "$answ_comm" = "Y" ]; 
					then
						/usr/bin/ipcrm -Q 0x4f564e42
				fi
		fi
else
	echo "	NONE"
fi


echo ""
echo " Checking ltid MsgQueue (0x4c544952):"
if [ "$M_LTID" ]; then
#		ipcs -a | grep 0x4c544952
		echo " $M_LTID"
		nbu_msgq=1
		if [ $ACTIVE_NBU = 0 ]
			then
				echo "	NBU is not running!"
				echo "	Attempt to clear this key? [Y/N]"
				read answ_ltid
				if [ "$answ_ltid" ="Y" ]; then
					/usr/bin/ipcrm -Q 0x4c544952
				fi
		fi

else 
	echo "	NONE"
fi


echo ""
echo " Checking BPSCHED_REQ_KEY used for requesting schedules (0x412a4250):"
if [ "$M_BPSCHED_REQ" ]
	then 
#		ipcs -a | grep 0x412a4250
		echo " $M_BPSCHED_REQ"
		nbu_msgq=1
		if [ $ACTIVE_NBU = 0 ]
			then
				echo "	NBU is not running!"
				echo "	Attempt to clear this key? [Y/N]"
				read answ_req
				if [ "$answ_req" ="Y" ] 
					then
						/usr/bin/ipcrm -Q 0x412a4250
				fi
		fi
else 
	echo "	NONE"
fi


echo ""
echo " Checking BPSCHED_RUN_KEY used for start backups (0x4f564e42):"
if [ "$M_BPSCHED_RUN" ]
	then
#		ipcs -a | grep 0x4f564e42
		echo " $M_BPSCHED_RUN"
		nbu_msgq=1
		if [ $ACTIVE_NBU = 0 ]
			then
				echo "	NBU is not running!"
				echo "	Attempt to clear this key? [Y/N]"
				read answ_run
				if [ "$answ_run" ="Y" ] 
					then
						/usr/bin/ipcrm -Q 0x4f564e42
				fi
		fi
else
	echo "	NONE"
fi


echo ""
echo " Checking REREAD_KEY used for rereading the storage unit (0x52455245):"
if [ "$M_STU" ]
	then
#		ipcs -a | grep 0x52455245
		echo " $M_STU"
		nbu_msgq=1
		if [ $ACTIVE_NBU = 0 ]
			then
				echo "	NBU is not running!"
				echo "	Attempt to clear this key? [Y/N]"
				read answ_stu
				if [ "$answ_stu" = "Y" ] 
					then
						/usr/bin/ipcrm -Q 0x52455245
				fi
		fi
else
	echo "	NONE"
fi


echo ""
echo " Checking BPRD_REQ_KEY used for MPX main bprd (0x4d50584d):"
if [ "$M_MPX" ]
	then
#		ipcs -a | grep 0x4d50584d
		echo " $M_MPX"
		nbu_msgq=1
		if [ $ACTIVE_NBU = 0 ]
			then
				echo "	NBU is not running!"
				echo "	Attempt to clear this key? [Y/N]"
				read answ_mpx
				if [ "$answ_mpx" = "Y" ] 
					then
						/usr/bin/ipcrm -Q 0x4d50584d
				fi
		fi
else
		echo "	NONE"
fi


if [ $nbu_msgq = 1 ]
	then
		echo ""
		echo ""
		echo "-------------------------"
		echo ""
		echo ""
		echo " When CBYTES approaches very close to QBYTES, that's the hint that more"
		echo " message queues are needed. Also note that the maximum length of a message" 
		echo " que should be set to atleast 65536 bytes. Your kernel settings for" 
		echo " *msgmnb* should meet or exceed the value of 65536."
		echo ""
		echo "NetBackup Related Message Queues:"
		echo "T         ID      KEY        MODE        OWNER    GROUP  CREATOR   CGROUP CBYTES  QNUM QBYTES LSPID LRPID   STIME RTIME    CTIME"
		ipcs -a | grep 0x412a4250
		ipcs -a | grep 0x4f564e42
		ipcs -a | grep 0x4c544952
		ipcs -a | grep 0x412a4250
		ipcs -a | grep 0x4f564e42
		ipcs -a | grep 0x52455245
		ipcs -a | grep 0x4d50584d
fi

echo ""
echo ""
ipcs -qa

