|
#!/bin/bash |
|
# Purpose: |
|
# Build and execute an MQ REST admin command. |
|
# This script uses positional parameters deliberately, to make it look a little more like a traditional MQSC command. |
|
# For commands like ALTER or CREATE, the <additional parms> field is read in pairs: an attribute and its value. |
|
# There will certainly be issues here if parameters try to include special characters like '"' or have multiple words such |
|
# as in a DESCR field. This script is not going to deal nicely with escaping them. But it's good enough for what I need. |
|
# Usage: |
|
# mqscJ <qmgr> <command> <object type> <object name> <additional parms> |
|
# For example |
|
# mqscJ QM1 DISPLAY qlocal "app*" monq statq |
|
# mqscJ QM1 ALTER QLOCAL APP.Q.1 MAXDEPTH 3000 MONQ HIGH |
|
|
|
|
|
# Credentials for accessing the web server |
|
user=mqguest |
|
pass=passw0rd |
|
|
|
# Parameter validation |
|
if [ -z "$1" ] |
|
then |
|
echo "Must provide a queue manager name" |
|
exit 1 |
|
fi |
|
|
|
if [ -z "$2" ] |
|
then |
|
echo "Must provide a command" |
|
exit 1 |
|
fi |
|
|
|
# Other parameters are optional |
|
qmgr="$1" |
|
command=`echo $2 | tr '[a-z]' '[A-Z]'` |
|
qualifier=$3 |
|
object=$4 |
|
if [ ! -z "$object" ] |
|
then |
|
shift 4 |
|
parms=$* |
|
else |
|
parms="$5" |
|
fi |
|
|
|
# Build the REST elements into a temporary file |
|
cmd=/tmp/mqsc.json.$$ |
|
rm -f $cmd |
|
|
|
cat << EOF > $cmd |
|
{ |
|
"type" : "runCommandJSON" |
|
,"command" : "$command" |
|
EOF |
|
|
|
if [ ! -z "$qualifier" ] |
|
then |
|
echo ",\"qualifier\" : \"$qualifier\"" >> $cmd |
|
fi |
|
|
|
# Add the object name if supplied |
|
if [ ! -z "$object" ] |
|
then |
|
echo ",\"name\" : \"$object\"" >> $cmd |
|
fi |
|
|
|
|
|
# Add the parameters that are required to be returned from a DISPLAY command |
|
if [ "$command" == "DISPLAY" ] |
|
then |
|
if [ -z "$parms" ] |
|
then |
|
echo ",\"responseParameters\" : [\"ALL\"]" >> $cmd |
|
else |
|
pm="" |
|
for p in $parms |
|
do |
|
if [ -z "$pm" ] |
|
then |
|
comma="" |
|
else |
|
comma="," |
|
fi |
|
|
|
pm="$pm $comma \"$p\"" |
|
|
|
done |
|
echo ",\"responseParameters\" : [$pm]" >> $cmd |
|
fi |
|
else |
|
if [ ! -z "$parms" ] |
|
then |
|
# Use awk to format the request parameters |
|
# This will not work if the value has multiple words (eg in a DESCR field) |
|
echo $parms | |
|
awk ' |
|
BEGIN { |
|
comma = "" |
|
print ",\"parameters\": {" |
|
} |
|
{ |
|
for (i=1;i<=NF/2;i++) { |
|
attr=$(2*i-1) |
|
value=$(2*i) |
|
# Is the value numeric? |
|
if (match(value, "^[0-9]+$")) { |
|
printf ("%s \"%s\" : %s\n",comma,attr,value) |
|
} else { |
|
printf ("%s \"%s\" : \"%s\"\n",comma,attr,value) |
|
} |
|
comma="," |
|
} |
|
} |
|
END { |
|
print "}" |
|
}' >> $cmd |
|
fi |
|
fi |
|
|
|
echo "}" >> $cmd |
|
|
|
# Now run curl to execute the generated file |
|
curl -Ss -X POST \ |
|
--header 'ibm-mq-rest-csrf-token: random' \ |
|
--header 'Content-Type: application/json' \ |
|
--header 'Accept: application/json' \ |
|
-k "https://localhost:9443/ibmmq/rest/v1/admin/action/qmgr/$qmgr/mqsc" \ |
|
-d @$cmd \ |
|
-u $user:$pass |
|
rc=$? |
|
|
|
|
|
# Delete the temp file |
|
rm -f $cmd |
|
|
|
# Don't check the return code from the executed command - make the caller of this |
|
# script do that by looking at the .reasonCode, .completionCode values. The exit code |
|
# from the script is from curl itself. |
|
exit $rc |