Skip to content

Instantly share code, notes, and snippets.

@ibmmqmet
Last active January 10, 2021 17:19
Show Gist options
  • Save ibmmqmet/50438e972d968411c549c9c63fb13afb to your computer and use it in GitHub Desktop.
Save ibmmqmet/50438e972d968411c549c9c63fb13afb to your computer and use it in GitHub Desktop.
IBM MQ queue manager configuration as JSON

This program uses IBM MQ's REST interface to get a copy of all object definitions. It's similar to the dmpmqcfg program but uses JSON as the output format which might be more convenient for some uses.

See my blog post for more information.

#!/bin/bash
# Purpose:
# Do something similar to the dmpmqcfg command
# but using MQ's REST Admin API to get the output in JSON
# format instead of MQSC. This output format might be easier
# to work with for some processing activities where parsing is
# required. But it cannot be used directly as a "replay" device
# to recreate the qmgr with the definitions.
#
# Command line parameters say which web server to connect to.
#
# Unlike the real dmpmqcfg, it's an all-or-nothing dump. You don't
# get to select object types or specific object names.
#
# Prereq packages:
# curl, jq
function cleanup() {
if [ -d $tmpDir ]
then
rm -rf $tmpDir
fi
}
function usage() {
cat << EOF
Usage: dmpmqcfg.sh -m qmgr -u user -c credential -s server -p port
EOF
exit 1
}
# Create variables with default values
qmgr=QM1
user=mqguest
pass=passw0rd
serv=localhost
port=9443
# Command line options
while getopts "c:m:p:s:u:?h" arg
do
case $arg in
c) pass=$OPTARG;;
m) qmgr=$OPTARG;;
p) port=$OPTARG;;
s) serv=$OPTARG;;
u) user=$OPTARG;;
*) usage;;
esac
done
# Build the REST elements into a file and create somewhere
# for various output logs to go in case of problems.
tmpDir=/tmp/dmpmqcfg.$$
rm -rf $tmpDir
mkdir $tmpDir
cmd=$tmpDir/curl.in
curlRc=$tmpDir/curl.rc
curlOut=$tmpDir/curl.out
curlErr=$tmpDir/curl.err
trap cleanup EXIT SIGINT
# Work through the different object types including pseudo-objects
# such as AUTHREC definitions. Might want to adjust this list if running
# against a z/OS queue manager
for objType in \
QMGR \
QUEUE \
CHANNEL \
CHANNEL_CLIENT \
PROCESS \
NAMELIST \
LISTENER \
SERVICE \
TOPIC \
AUTHINFO \
AUTHREC \
SUB \
CHLAUTH \
POLICY
do
# Default lines to go in the JSON commands
nameLine=",\"name\" : \"*\""
respLine=",\"responseParameters\" : [\"ALL\"]"
filtLine=""
# But queries for some object types have to be built a little differently
case $objType in
QMGR|AUTHREC)
nameLine=""
;;
CHANNEL_CLIENT)
objType="CHANNEL"
filtLine=",\"parameters\" : { \"chltype\": \"clntconn\" }"
;;
POLICY)
respLine=""
;;
SUB)
filtLine=",\"parameters\" : { \"subtype\": \"admin\" }"
;;
esac
# Build the JSON command with all its parameters as a file
cat << EOF > $cmd
{
"type" : "runCommandJSON"
,"command" : "DISPLAY"
,"qualifier" : "$objType"
$nameLine
$respLine
$filtLine
}
EOF
# And execute it via curl
curl -Ss -X POST \
--header 'ibm-mq-rest-csrf-token: random' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
-k "https://$serv:$port/ibmmq/rest/v2/admin/action/qmgr/$qmgr/mqsc" \
-d @$cmd \
-u $user:$pass >$curlOut 2>$curlErr
curlRc=$?
# If it worked, then use jq to do some simple reformatting that might
# be easier to work with.
#
# Different types of error are lumped together here. It's not surprising to see
# completionCode:null if the request never even made it into the qmgr because of
# an early failure such as incorrect authentication.
#
# Could probably do a lot more with error conditions but this should be sufficient
if [ $curlRc -eq 0 ]
then
cat $curlOut |\
jq --arg qmgr $qmgr \
--arg objType $objType \
-r '
. += {
queueManager: $qmgr ,
objectType: $objType,
objects: .commandResponse
} |
if .overallCompletionCode == 0
then
del(.objects[].completionCode) |
del(.objects[].reasonCode) |
del(.overallCompletionCode) |
del(.overallReasonCode) |
del(.commandResponse) |
.objects[] |= .parameters
else
{
queueManager: $qmgr ,
objectType: $objType,
completionCode: .overallCompletionCode,
reasonCode: .overallReasonCode,
error: .error
}
end
'
jqrc=$?
if [ $jqrc -ne 0 ]
then
echo "JQ error " $jqrc
exit 1
fi
else
# If there was a problem with the curl command itself, bail out quickly. Most likely
# this will fail on the first operation because of incorrect connection parameters.
echo "Curl error: " $curlRc
cat $curlErr
exit 1
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment