Last active
August 13, 2019 08:34
-
-
Save boly38/8dd3f9efae25136d8ae0c5974531fd9b to your computer and use it in GitHub Desktop.
shell script to state and purge RabbitMQ queues using RabbitMQ Management HTTP API (https://pulse.mozilla.org/api/) and Jq JSON Processor (https://stedolan.github.io/jq/) #rabbitmq #purge #bash #script #jq #json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# This Gist is under MIT License | |
# | |
# Copyright 2018 Orange - Brice Vandeputte | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# | |
## | |
# State and purge RabbitMq using shell script | |
## | |
# Inspired from JavaScript Gist: https://gist.github.com/flopezluis/2172067 (by flopezluis) | |
# Rely on RabbitMQ Admin API - https://pulse.mozilla.org/api/ | |
# JQ Json Parser - https://github.com/stedolan/jq | |
usage() { | |
echo "state and purge rabbit queues" | |
echo "Usage:" | |
echo " (dry) $ ./rabbitStateAndPurge.sh <host> <vhost> <rabbituser> <rabbitpwd>" | |
echo " (PURGE) $ ./rabbitStateAndPurge.sh <host> <vhost> <rabbituser> <rabbitpwd> YES_PURGE" | |
echo | |
echo "Example:" | |
echo | |
echo " [localhost] " | |
echo " $ ./rabbitStateAndPurge.sh 192.168.99.100 myVhost guest guest" | |
echo | |
echo " [remotely] " | |
echo " (setup) $ scp rabbitStateAndPurge.sh myRemoteRabbitVm:~" | |
echo " (dry) $ ssh myRemoteRabbitVm ./rabbitStateAndPurge.sh localhost myVhost rabbitUser rabbitPwd" | |
echo " (PURGE) $ ssh myRemoteRabbitVm ./rabbitStateAndPurge.sh localhost myVhost rabbitUser rabbitPwd YES_PURGE" | |
} | |
if [ "$1" == "--help" ] || [ "$1" == "" ] || [ "$2" == "" ] || [ "$3" == "" ] || [ "$4" == "" ]; then | |
usage; exit 0; | |
fi | |
RABBIT_HOST="$1" | |
RABBIT_PORT="15672" | |
RABBIT_VHOST="$2" | |
RABBIT_AUTH="$3:$4" | |
CONFIRM="$5" | |
ACTION_RESULT="(dry)" | |
if [ "$CONFIRM" == "YES_PURGE" ]; then | |
ACTION_RESULT="purge" | |
fi | |
RABBIT_API="http://${RABBIT_HOST}:${RABBIT_PORT}/api" | |
RABBIT_GET_VHOST="curl -s -u ${RABBIT_AUTH} ${RABBIT_API}/vhosts/${RABBIT_VHOST}" | |
RABBIT_GET_QUEUES="curl -s -u ${RABBIT_AUTH} ${RABBIT_API}/queues/${RABBIT_VHOST}" | |
bold() { echo "\e[1m${*}\e[0m"; } | |
## JQ Json Parser - https://github.com/stedolan/jq | |
installJq() { | |
JQ_CMD=./jq-linux64 | |
JQ_URL=https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 | |
if [ "$OS" == "Windows_NT" ]; then | |
JQ_CMD=./jq-win64.exe | |
JQ_URL=https://github.com/stedolan/jq/releases/download/jq-1.6/jq-win64.exe | |
fi | |
if [ ! -f ${JQ_CMD} ]; then | |
echo " * download jq" | |
curl -s -L ${JQ_URL} -o ${JQ_CMD} | |
fi | |
} | |
rabbitState() { | |
echo "${RABBIT_GET_VHOST}" | |
RABBIT_GET_RESULT=$(${RABBIT_GET_VHOST}) | |
if [[ ${RABBIT_GET_RESULT} =~ "Not Found" ]]; then | |
echo " X vhost ${RABBIT_VHOST} not found" | |
exit 1; | |
fi | |
MSG_COUNT=$(echo "${RABBIT_GET_RESULT}" | ${JQ_CMD} -r .messages) | |
MSG_READY_COUNT=$(echo "${RABBIT_GET_RESULT}" | ${JQ_CMD} -r .messages_ready) | |
echo -e " Rabbit ${RABBIT_HOST} [vhost=${RABBIT_VHOST}] has $(bold ${MSG_COUNT} messages) (${MSG_READY_COUNT} ready)" | |
if [ "${MSG_READY_COUNT}" == "0" ]; then | |
echo " no message, bye." | |
exit 0; | |
fi | |
} | |
rabbitPurgeOneQueue() { | |
queueName=$1 | |
echo " * purge queue \"${queueName}\"" | |
if [ "${ACTION_RESULT}" == "purge" ]; then | |
DELETE_OUTPUT=$(curl -s -X "DELETE" -u ${RABBIT_AUTH} ${RABBIT_API}/queues/${RABBIT_VHOST}/${queueName}/contents/) | |
fi | |
if [ "${DELETE_OUTPUT}" == "" ]; then | |
echo "${ACTION_RESULT}" | |
else | |
echo "${DELETE_OUTPUT}" # new line because we already get error with payload | |
fi | |
} | |
# jq helper | |
# ========= | |
# how to reduce to not empty queues | |
# $ echo '[{"nb":0,"name":"zero"},{"nb":1,"name":"un"},{"nb":2,"name":"deux"}]' | ${JQ_CMD} 'foreach .[] as $item ([];if $item.nb == 0 then empty else $item.name end;.)' | |
# "un" | |
# "deux" | |
#DEV# ALL_QUEUES=$(echo "sampleA" && echo "sambleB" && echo "sampleDontExist") | |
# purge all not empty rabbit queues via admin API | |
rabbitPurgeAllQueues() { | |
#~ query rabbit admin one time only | |
GET_ALL_QUEUES_RESULT=$(${RABBIT_GET_QUEUES}) | |
#~ extract stats from result | |
ALL_QUEUES=$(echo "${GET_ALL_QUEUES_RESULT}"| ${JQ_CMD} -r .[].name| sed 's/.$//') # sed drop last char (cr). | |
NOT_EMPTY_QUEUES=$(echo "${GET_ALL_QUEUES_RESULT}"| ${JQ_CMD} 'foreach .[] as $item ([];if $item.messages == 0 then empty else $item.name end;.)'| sed 's/"//g') # sed extract string. | |
ALL_QUEUES_COUNT=$(echo "$ALL_QUEUES" |wc -l) | |
NOT_EMPTY_QUEUES_COUNT=$(echo "$NOT_EMPTY_QUEUES" |wc -l) | |
echo " ${ALL_QUEUES_COUNT} queues including ${NOT_EMPTY_QUEUES_COUNT} not empty" | |
echo -e " * you will purge $(bold ${NOT_EMPTY_QUEUES_COUNT} queues) :" | |
# show details per not empty queues | |
echo "${GET_ALL_QUEUES_RESULT}"| ${JQ_CMD} 'foreach .[] as $item ([];if $item.messages == 0 then empty else {name:$item.name,messages:$item.messages} end;.)' | |
while read -r oneQueue; do | |
rabbitPurgeOneQueue ${oneQueue} | |
done <<< "${NOT_EMPTY_QUEUES}" | |
} | |
# main | |
installJq | |
rabbitState | |
rabbitPurgeAllQueues | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment