-->

Wednesday, March 8, 2017

Custom Cloudwatch Plugins CW_Rabbitmq Queue Message Length Part-6

The following Cloudwatch plugin helps to measure the number of messages in the Rabbitmq unack,Ready,Total Message on which alarms can be configured later using the cloudwatch API.


#!/bin/bash
#
#  About                : Check RabbitMQ Queue Message Length
#
#  Name                 : cw_rabbitmq.sh



DIR=$(dirname $0);
PLUGIN_NAME='cw_rabbitmq';

# Include configuration file
source ${DIR}/../conf/plugin.conf;

#Get Current Instance ID
INSTANCE_ID=(`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`);
#Get Hostname
HOST_ID=(`wget -q -O - http://169.254.169.254/latest/meta-data/hostname`);

# Help
usage() {
        echo "Usage: $0 [-n ] [-d ] [-m ] [-u ] [-p ] [-q ]" 1>&2;
        exit 1;
}

# Logger
logger(){

 SEVERITY=$1;
 MESSAGE=$2;
 DATE=`date +"[%Y-%b-%d %H:%M:%S.%3N]"`;

 echo -e "${DATE} [${SEVERITY}] [${PLUGIN_NAME}] [${INSTANCE_ID}] [${HOST_ID}] ${MESSAGE}" >> ${DIR}/../logs/appcwmon.log;

}

# Process Arguments

if [ $# -eq 0 ]; then
        # When no argument is passed
        logger ERROR "Invalid arguments passed";
        usage;
fi



while getopts ":n:d:m:u:p:q:" o; do
    case "${o}" in
        n)
            NAMESPACE=${OPTARG}
            if [ -z "${NAMESPACE}" ]; then
                logger ERROR "Invalid Namespace passed";
                usage;
            fi
            ;;
        d)
            DIMENSION=${OPTARG};

            DNAME=${DIMENSION%=*};
            DVALUE=${DIMENSION#*=};

            if [ -z "${DIMENSION}" ] || [ -z "${DNAME}" ] || [ "${DNAME}" == "${DVALUE}" ]; then
                logger ERROR "Invalid dimension passed <${DIMENSION}>";
                usage;
            fi

            # If Dimension name is 'InstanceId' then Value is not required to be passed
            if [ "${DNAME}" != 'InstanceId' ] && [ -z "${DVALUE}" ]; then
                logger ERROR "Invalid dimension passed <${DIMENSION}>";
                usage;
            fi
            ;;
        m)
            METRICS=${OPTARG}
            if [ -z "${METRICS}" ]; then
                logger ERROR "Invalid metrices passed <${METRICS}>";
                usage;
            fi
            ;;
        u)
            USERNAME=${OPTARG}
            if [ -z "${USERNAME}" ]; then
                logger ERROR "Invalid username passed <${USERNAME}>";
                usage;
            fi
            ;;
        p)
            PASSWORD=${OPTARG}
            if [ -z "${PASSWORD}" ]; then
                logger ERROR "Invalid password passed <${PASSWORD}>";
                usage;
            fi
            ;;
        q)
            QUEUE=${OPTARG}
            if [ -z "${QUEUE}" ]; then
                logger ERROR "Invalid queue passed <${QUEUE}>";
                usage;
            fi
            ;;


        *)
            usage
            ;;
    esac
done
shift $((OPTIND-1))

# Input Validation
if [ -z "${NAMESPACE}" ] || [ -z "${DNAME}" ] || [ -z "$METRICS" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ] || [ -z "$QUEUE" ]; then
                logger ERROR "Invalid argument passed";
    usage
fi


##########################################################
##########################################################


# If "INSTANCE_ID" is passed as Dimension, then use actual AWS Instanec ID as Dimension
if [ "${DNAME}" == "InstanceId" ]; then
        DVALUE=${INSTANCE_ID};
fi


UNIT="Count";

## Total Message
TOTAL_MESSAGE=$(/usr/local/bin/rabbitmqadmin --username=${USERNAME} --password="${PASSWORD}" list queues name messages | grep -w "${QUEUE} " | awk '{print $4}' 2>&1);

if [ "$?" -ne "0" ]; then
        logger ERROR "${NAMESPACE} ${METRICS} ${DNAME}=${DVALUE} | ${QUEUE} value=NULL unit=${UNIT} | ${TOTAL_MESSAGE}";
        exit 1;
fi;

OUTPUT=$(/usr/local/bin/aws cloudwatch put-metric-data --namespace ${NAMESPACE} --metric-name Total-${METRICS} --dimensions ${DNAME}=${DVALUE} --value ${TOTAL_MESSAGE} --unit ${UNIT} 2>&1);

if [ "$?" -ne "0" ]; then
        logger ERROR "${NAMESPACE} ${METRICS} ${DNAME}=${DVALUE} | ${QUEUE} value=${TOTAL_MESSAGE} unit=${UNIT} | ${OUTPUT}";
        exit 1;
fi;

## Unacknowledge Message
UNACK_MESSAGE=$(/usr/local/bin/rabbitmqadmin --username=${USERNAME} --password="${PASSWORD}" list queues name messages_unacknowledged | grep -w "${QUEUE} " | awk '{print $4}' 2>&1);
if [ "$?" -ne "0" ]; then
        logger ERROR "${NAMESPACE} ${METRICS} ${DNAME}=${DVALUE} | ${QUEUE} value=NULL unit=${UNIT} | ${UNACK_MESSAGE}";
        exit 1;
fi;

OUTPUT=$(/usr/local/bin/aws cloudwatch put-metric-data --namespace ${NAMESPACE} --metric-name UnACK-${METRICS} --dimensions ${DNAME}=${DVALUE} --value ${UNACK_MESSAGE} --unit ${UNIT} 2>&1);

if [ "$?" -ne "0" ]; then
        logger ERROR "${NAMESPACE} ${METRICS} ${DNAME}=${DVALUE} | ${QUEUE} value=${UNACK_MESSAGE} unit=${UNIT} | ${OUTPUT}";
        exit 1;
fi;

## Ready Message
READY_MESSAGE=$(/usr/local/bin/rabbitmqadmin --username=${USERNAME} --password="${PASSWORD}" list queues name messages_ready | grep -w "${QUEUE} " | awk '{print $4}' 2>&1);
if [ "$?" -ne "0" ]; then
        logger ERROR "${NAMESPACE} ${METRICS} ${DNAME}=${DVALUE} | ${QUEUE} value=NULL unit=${UNIT} | ${READY_MESSAGE}";
        exit 1;
fi;

OUTPUT=$(/usr/local/bin/aws cloudwatch put-metric-data --namespace ${NAMESPACE} --metric-name Ready-${METRICS} --dimensions ${DNAME}=${DVALUE} --value ${READY_MESSAGE} --unit ${UNIT} 2>&1);

if [ "$?" -ne "0" ]; then
        logger ERROR "${NAMESPACE} ${METRICS} ${DNAME}=${DVALUE} | ${QUEUE} value=${READY_MESSAGE} unit=${UNIT} | ${OUTPUT}";
        exit 1;
fi;

## Consumer Count
CONSUMER=$(/usr/local/bin/rabbitmqadmin --username=${USERNAME} --password="${PASSWORD}" list queues name consumers | grep -w "${QUEUE} " | awk '{print $4}' 2>&1);
if [ "$?" -ne "0" ]; then
        logger ERROR "${NAMESPACE} ${METRICS} ${DNAME}=${DVALUE} | ${QUEUE} value=NULL unit=${UNIT} | ${CONSUMER}";
        exit 1;
fi;

OUTPUT=$(/usr/local/bin/aws cloudwatch put-metric-data --namespace ${NAMESPACE} --metric-name ConsumerCount-${METRICS} --dimensions ${DNAME}=${DVALUE} --value ${CONSUMER} --unit ${UNIT} 2>&1);

if [ "$?" -ne "0" ]; then
        logger ERROR "${NAMESPACE} ${METRICS} ${DNAME}=${DVALUE} | ${QUEUE} value=${CONSUMER} unit=${UNIT} | ${OUTPUT}";
        exit 1;
fi;

logger INFO "${NAMESPACE} ${METRICS} ${DNAME}=${DVALUE} | ${QUEUE} value=${TOTAL_MESSAGE},${UNACK_MESSAGE},${READY_MESSAGE},${CONSUMER} unit=${UNIT}";
# Success
exit 0;

   

0 comments:

Post a Comment