Skip to content

Instantly share code, notes, and snippets.

@mscansian
Last active December 12, 2018 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mscansian/bf9944e8b9c948cdb1a2e3a9f5f0bdf7 to your computer and use it in GitHub Desktop.
Save mscansian/bf9944e8b9c948cdb1a2e3a9f5f0bdf7 to your computer and use it in GitHub Desktop.
Push RabbitMQ statistics to CloudWatch. First argument is the vhost.
#! /bin/bash
STATS2READ=( messages consumers )
# Namespace
NS=RabbitMQ
REGION=us-east-2
# Instance ID
EC2ID=$(ec2metadata --instance-id)
# Endpoint (using HTTP instead of HTTPS to save CPU credits)
ENDPOINT="http://monitoring.$REGION.amazonaws.com"
# Collecting statistics for RabbitMQ queues
UNIT=Count
# RabbitMQ Virtual Host
VHOST="$1"
echo "Fetching metrics for vhost $VHOST"
for STATISTIC in ${STATS2READ[@]}; do
QUEUE_STATS=$(rabbitmqctl list_queues -p $VHOST $STATISTIC name)
# Metric data is stored in $MDATA[12]. It is populated while parsing output of rabbitmqctl
MDATA1=''
MDATA2=''
# Parsing output of rabbitmqctl
while read -r line ; do
#echo "Processing line: $line"
if [[ $line == "Listing queues for vhost $VHOST ..." ]]; then
continue
elif [[ $line == 'Timeout: 60.0 seconds ...' ]]; then
continue
fi
if [[ $line == '...done.' ]]; then break; fi
read VALUE QUEUE <<< $line
echo "$VHOST $QUEUE"
MDATA1+="MetricName=$STATISTIC,Value=$VALUE,Unit=Count,Dimensions=[{Name=Queue,Value=$QUEUE},{Name=Vhost,Value=$VHOST}] "
MDATA2+="MetricName=$STATISTIC,Value=$VALUE,Unit=Count,Dimensions=[{Name=Queue,Value=$QUEUE},{Name=Vhost,Value=$VHOST},{Name=InstanceId,Value=$EC2ID}] "
done <<< "$QUEUE_STATS"
aws cloudwatch put-metric-data --endpoint-url $ENDPOINT --namespace $NS --region $REGION --metric-data $MDATA1
aws cloudwatch put-metric-data --endpoint-url $ENDPOINT --namespace $NS --region $REGION --metric-data $MDATA2
done
@mscansian
Copy link
Author

This is a fork from a script that I found online made to suit my needs at the time :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment