MQ Basic scripting snippets

We will see some basic’s to see how we can start writing shell scripts from a simple script to a complex one .

Que ) How to test the QMGR is running or not using shell script .

Ans ) We can directly test using dspmq command and with the script if i want to see QM1 status it can use below

[mqm@ip-172-31-14-154 ~]$ cat test.sh 
runmqsc QM1 > /dev/null  <<!!!
!!!
if [ $? -eq 0 ] ; then
  echo "QM1 QMGR is running "
else
  echo "QM1 QMGR is not running "
fi
[mqm@ip-172-31-14-154 ~]$ 

Here we are redirecting console output to /dev/null terminal . We can route to a file if we need for later processing as well.

We can see dspmq output like below from the server

mqm@ip-172-31-14-154 ~]$ dspmq
QMNAME(QM1)                                               STATUS(Running)
QMNAME(QM2)                                               STATUS(Running)
QMNAME(PR1)                                               STATUS(Ended normally)
QMNAME(PR2)                                               STATUS(Ended normally)
QMNAME(FR1)                                               STATUS(Ended normally)
QMNAME(FR2)                                               STATUS(Ended normally)
QMNAME(GWQM)                                              STATUS(Ended normally)
QMNAME(APPQM1)                                            STATUS(Ended normally)
QMNAME(TEST.QM1)                                          STATUS(Ended unexpectedly)
QMNAME(TEST.QM2)                                          STATUS(Ended unexpectedly)
QMNAME(TEST.QM3)                                          STATUS(Ended unexpectedly)
QMNAME(TEST.QM4)                                          STATUS(Ended unexpectedly)
[mqm@ip-172-31-14-154 ~]$ 

Sample script is given below . Where we get the list of QMGR name using dspmq and awk command combination then used runmqsc . Here one thing to note is any commands that we want to run on QMGR can be passed between <<!!! and !!!. If there are no commands then it just connect to QMGR runmqsc and exit . with return code we will identify the QMGR status .$? store the return value from the previous command if it is 0 then success otherwise failure

[mqm@ip-172-31-14-154 ~]$ cat test.sh 
for QM in `dspmq |awk -F"(" '{ print $2 }' |awk -F")" '{ print $1 }'`; do
runmqsc $QM > /dev/null  <<!!!

!!!
if [ $? -eq 0 ] ; then
  echo "$QM QMGR is running "
else
  echo "$QM QMGR is not running "
fi

done
[mqm@ip-172-31-14-154 ~]$ 

Let us run the above test.sh and see the status .

mqm@ip-172-31-14-154 ~]$ sh test.sh 
QM1 QMGR is running 
QM2 QMGR is running 
PR1 QMGR is not running 
PR2 QMGR is not running 
FR1 QMGR is not running 
FR2 QMGR is not running 
GWQM QMGR is not running 
APPQM1 QMGR is not running 
TEST.QM1 QMGR is not running 
TEST.QM2 QMGR is not running 
TEST.QM3 QMGR is not running 
TEST.QM4 QMGR is not running 
[mqm@ip-172-31-14-154 ~]$ 

Perfect .It gives us the correct QMGR which is up and running .

Let’s imagine a situation where we want to run the script and see specific SDR Channel is not running then we need to reset and start it.

[mqm@ip-172-31-14-154 errors]$ vi AMQERR01.LOG 

[1]+  Stopped                 vi AMQERR01.LOG
[mqm@ip-172-31-14-154 errors]$ cd 
[mqm@ip-172-31-14-154 ~]$ cat chstest.sh 
QMGR=$1
CHL=$2

echo " dis chs($CHL) status" |runmqsc $QMGR | grep "(RUNNING)"
if [ $? -eq 0 ] ; then 
 echo " $CHL is running "
 exit 0
 fi

runmqsc $QMGR > /dev/null  <<!!!
reset chl($CHL)
start chl($CHL)
!!!
if [ $? -eq 0 ] ; then
  echo "$QMGR $CHL  is started "
else
  echo "$QMGR $CHL is not able to strart .Pls check "
fi

[mqm@ip-172-31-14-154 ~]$ 

Channel looks not running and the result looks like below

[mqm@ip-172-31-14-154 ~]$ sh chstest.sh QM1 TO.QM2
QM1 TO.QM2  is started 
[mqm@ip-172-31-14-154 ~]$ 

let see the logs to see if the channel triggered to start

----- amqrccca.c : 1121 -------------------------------------------------------
04/03/2021 01:43:27 AM - Process(1463.18) User(mqm) Program(amqzlaa0)
                    Host(ip-172-31-14-154.us-east-2.compute.internal) Installation(Installation1)
                    VRMF(9.2.0.1) QMgr(QM1)
                    Time(2021-04-03T01:43:27.142Z)
                    ArithInsert1(1)
                    CommentInsert1(TO.QM2)

AMQ8023I: IBM MQ channel reset.

EXPLANATION:
Channel 'TO.QM2' has been reset, the new sequence number of the channel is 1.
ACTION:
None.
----- amqkchla.c : 2929 -------------------------------------------------------
04/03/2021 01:43:27 AM - Process(15224.1) User(mqm) Program(runmqchl)
                    Host(ip-172-31-14-154.us-east-2.compute.internal) Installation(Installation1)
                    VRMF(9.2.0.1) QMgr(QM1)
                    Time(2021-04-03T01:43:27.174Z)
                    CommentInsert1(TO.QM2)

AMQ9002I: Channel 'TO.QM2' is starting.

EXPLANATION:
Channel 'TO.QM2' is starting.
ACTION:
None.
----- amqrccca.c : 469 --------------------------------------------------------
04/03/2021 01:43:27 AM - Process(15224.1) User(mqm) Program(runmqchl)
                    Host(ip-172-31-14-154.us-east-2.compute.internal) Installation(Installation1)
                    VRMF(9.2.0.1) QMgr(QM1)
                    Time(2021-04-03T01:43:27.347Z)
                    CommentInsert1(TO.QM2)

AMQ9299I: Channel 'TO.QM2' has started.

EXPLANATION:
The channel 'TO.QM2' has finished starting.
ACTION:
No action required.
----- amqrccca.c : 969 --------------------------------------------------------

Looks fine . Let us try to stop the channel and run the script once again .

dis chs(TO.QM2)
     4 : dis chs(TO.QM2)
AMQ8417I: Display Channel Status details.
   CHANNEL(TO.QM2)                         CHLTYPE(SDR)
   CONNAME(172.31.14.154(1415))            CURRENT
   RQMNAME(QM2)                            STATUS(RUNNING)
   SUBSTATE(MQGET)                         XMITQ(QM1.XMITQ)
stop channel(TO.QM2) status(stopped) mode(force)
     5 : stop channel(TO.QM2) status(stopped) mode(force)
AMQ8019I: Stop IBM MQ channel accepted.
dis chs(TO.QM2)
     6 : dis chs(TO.QM2)
AMQ8417I: Display Channel Status details.
   CHANNEL(TO.QM2)                         CHLTYPE(SDR)
   CONNAME(172.31.14.154(1415))            CURRENT
   RQMNAME(QM2)                            STATUS(STOPPED)
   SUBSTATE( )                             XMITQ(QM1.XMITQ)

Now run the script again and see

[mqm@ip-172-31-14-154 ~]$ sh chstest.sh QM1 TO.QM2
QM1 TO.QM2  is started 
[mqm@ip-172-31-14-154 ~]$ 
[mqm@ip-172-31-14-154 ~]$ 
[mqm@ip-172-31-14-154 ~]$ runmqsc QM1
5724-H72 (C) Copyright IBM Corp. 1994, 2020.
Starting MQSC for queue manager QM1.


dis chs(TO.QM2)
     1 : dis chs(TO.QM2)
AMQ8417I: Display Channel Status details.
   CHANNEL(TO.QM2)                         CHLTYPE(SDR)
   CONNAME(172.31.14.154(1415))            CURRENT
   RQMNAME(QM2)                            STATUS(RUNNING)
   SUBSTATE(MQGET)                         XMITQ(QM1.XMITQ)
end
     2 : end
One MQSC command read.
No commands have a syntax error.
All valid MQSC commands were processed.
[mqm@ip-172-31-14-154 ~]$ 

Run the script again to see the result

[mqm@ip-172-31-14-154 ~]$ sh chstest.sh QM1 TO.QM2
   RQMNAME(QM2)                            STATUS(RUNNING)
 TO.QM2 is running 
[mqm@ip-172-31-14-154 ~]$ 

As the QMGR channel TO.QM2 is up and running fine . script just displayed information .

Now this script is required to configure in cron to run every 2 min to check the status and start the channel if it is down for some reason .

Hopefully this helps !!!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *