Skip to content

Instantly share code, notes, and snippets.

@jarshwah
Created June 5, 2019 13:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarshwah/5b6df32e9d3b709deb59321065790519 to your computer and use it in GitHub Desktop.
Save jarshwah/5b6df32e9d3b709deb59321065790519 to your computer and use it in GitHub Desktop.
Send RabbitMQ queue length to Cloudwatch
#! /bin/bash
# This script reads rabbitmq statistics and report them as CloudWatch metrics.
# https://gist.github.com/GoodMirek/2dc39100d18c72eed3f0f3569e221f4f
# RabbitMQ::messages_ready
# RabbitMQ::messages_unacknowledged
# RabbitMQ::consumers
# Dimensions: QueueName, InstanceId, VHost
STATS2READ=( messages_ready messages_unacknowledged consumers )
VHOSTS=$(/usr/sbin/rabbitmqctl list_vhosts -q | grep -v "/" | xargs -n1 | sort -u)
NS=RabbitMQ
EC2ID=$(/usr/bin/ec2metadata --instance-id)
ENDPOINT='https://monitoring.ap-southeast-2.amazonaws.com'
DEBUG=0
DBGFILE=/tmp/rabbitmq2cloudwatch.debug
if [[ $DEBUG == 0 ]]; then DBGFILE=/dev/null; fi
echo "---------" >> $DBGFILE
date -uIns >> $DBGFILE
for VHOST in ${VHOSTS[@]}; do
for STATISTIC in ${STATS2READ[@]}; do
MDATA=''
# Parsing output of rabbitmqctl
while read -r VALUE QUEUE ; do
MDATA+="MetricName=$STATISTIC,Value=$VALUE,Unit=Count,Dimensions=[{Name=Queue,Value=$QUEUE},{Name=InstanceId,Value=$EC2ID},{Name=VHost,Value=$VHOST}] "
done < <(/usr/sbin/rabbitmqctl list_queues -p "$VHOST" "$STATISTIC" name | grep -Ev "pidbox|celeryev|Listing|done")
echo "Submitting metric data: $MDATA" | tee -a $DBGFILE
/usr/local/bin/aws cloudwatch put-metric-data --endpoint-url $ENDPOINT --namespace $NS --region ap-southeast-2 --metric-data $MDATA >>$DBGFILE 2>&1
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment