Monday 22 September 2014

Displaying MQ status and Creating Queue manager using Shell Script.



Displaying MQ status and Creating Queue manager using  Shell Script.




Objective:

I want to create a queue manager if it does not already exist. First of all I will need to use dspq to get a list of existing queue managers and based on command line parameters (queue names) I wish the script to create a queue manage only if it does not already exist otherwise exit with a message similar to "queue manager already exists!"



I want to use cut to help me get a list of queue mangers

-bash-3.1$ man cut

SYNOPSIS
cut [OPTION]... [FILE]...

DESCRIPTION
Print selected parts of lines from each FILE to standard output.

Mandatory arguments to long options are mandatory for short options
too.

-b, --bytes=LIST
select only these bytes

-c, --characters=LIST
select only these characters

-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter

-f, --fields=LIST
select only these fields; also print any line that contains no
delimiter character, unless the -s option is specified


-bash-3.1$ dspmq
QMNAME(venus.queue.manager) STATUS(Ended normally)
QMNAME(my.queue.manager) STATUS(Ended normally)

delimit by the '(' symbol and only return column 1

-bash-3.1$ dspmq | grep -v Running | cut -d '(' -f1
QMNAME
QMNAME

remove left bracket
delimit by the '(' symbol and return only columns 2 and 3

-bash-3.1$ dspmq | grep -v Running | cut -d '(' -f2,3
venus.queue.manager) STATUS(Ended normally)
my.queue.manager) STATUS(Ended normally)

remove the right bracket from the line above
delimit by the ')' symbol and return only column 1, what we do is the first cut then pipe to the second cut

-bash-3.1$ dspmq | grep -v Running | cut -d '(' -f2,3 | cut -d ')' -f1
venus.queue.manager
my.queue.manager

we now have a list of queue managers, we can loop through to get status and customise out dspmq listing. There are other ways of using dspmq with sed & awk see following link.

http://www.webspheretools.com/stever/webspheretools.nsf/0/922D84B2DAF731B3802573E00075E22C!opendocument

sample script called qstatus.sh



#!/bin/bash
dspmq | grep -v Running | cut -d '(' -f2,3 | cut -d ')' -f1 | while read qmgr
do
status=`dspmq -m $qmgr | cut -d '(' -f2,3 | cut -d ')' -f2 | cut -d '(' -f2`
echo $qmgr is $status
done

qstatus.sh

-bash-3.1$ ./status.sh
venus.queue.manager is Ended normally
my.queue.manager is Ended normally

We now have figured out a way to list queues, what we want to do now is use these skills to provide our solution.

Final script solution:

createq.sh

#!/bin/bash
# BASH Functions file
# Filename: createq.sh
# Tested on OS: Linux, Fedora 6
# Author: Steve Robinson
# Date Created: February 2008
# Arguments: Queue Manager Name
# Description: This script create a new queue manager if it does not already exist
# if it already exists it displays the queue manager status
# Version: 1.0
# History:-
# 01-February-2008 - version 1.0 - Created.
# 02-February-2008 - version 1.1 - Updated, added -o to grep for unique match, otherwise finds subsets
###################################################################################
## Global Variables
###################################################################################
localHost=$(hostname)
_DEBUG="on"
###################################################################################
## Debug Function
###################################################################################
DEBUG()
{
[ "$_DEBUG" == "on" ] && $@ || :
}

#####################################################################################
## Main Routine
#####################################################################################

main() {

DEBUG echo "Status: Entering Main Routine.. "
DEBUG echo "Status: Checking to see if [$qmname] exists? ..."

qm=`dspmq | awk '{ print $1 }' | sed 's/QMNAME(//g;s/)//g' | grep -o $qmname`
if [ "$qm" = "" ];
then
DEBUG echo "Status: Creating: [$qmname]"
`crtmqm $qmname`
DEBUG echo "Status: Successfully created $q, Exiting with value 0"
else
DEBUG echo "Status: [$qmname], already exists"
status=`dspmq -m $qmname| cut -d '(' -f2,3 | cut -d ')' -f2 | cut -d '(' -f2`
echo status of queue manager [$qmname] is [$status]

fi
DEBUG echo "Status: Exiting Main Routine.. "
}


####################################################################################
## Entry Point
####################################################################################
DEBUG echo There are $# arguments to $0: $*
#Checking environment variables
if [ ! "$1" ];
then
DEBUG echo "Falure: You must pass in a queue manager name"
DEBUG echo "exiting with value 1"
exit 1
else

qmname=$1
DEBUG echo "Status: Queue Manager to create = $qmname"
DEBUG echo "Status: Calling Main Routine ... "

main $*

DEBUG echo "Exit: Exiting Main Routine and Terminating script ... "
exit 0

fi


Sample execution

Creating when doesn't already exist ...

-bash-3.1$ ./createq.sh venus.queue.manager2
There are 1 arguments to ./createq.sh: venus.queue.manager2
Status: Queue Manager to create = venus.queue.manager2
Status: Calling Main Routine ...
Status: Entering Main Routine..
Status: Checking to see if [venus.queue.manager2] exists? ...
Status: Creating: [venus.queue.manager2]
WebSphere MQ queue manager created.
Creating or replacing default objects for venus.queue.manager2.
Default objects statistics : 40 created. 0 replaced. 0 failed.
Completing setup.
Setup completed.
Status: Successfully created , Exiting with value 0
Status: Exiting Main Routine..
Exit: Exiting Main Routine and Terminating script ...
-bash-3.1$ ./qexists.sh venus.queue.manager2

Exiting when already exists ...

-bash-3.1$ ./createq.sh venus.queue.manager2
There are 1 arguments to ./createq.sh: venus.queue.manager2
Status: Queue Manager to create = venus.queue.manager2
Status: Calling Main Routine ...
Status: Entering Main Routine..
Status: Checking to see if [venus.queue.manager2] exists? ...
Status: [venus.queue.manager2], already exists
status of queue manager [venus.queue.manager2] is [Ended immediately]
Status: Exiting Main Routine..
Exit: Exiting Main Routine and Terminating script ...

No comments:

Post a Comment