This script was written by me a while back (still has an old version of the CLI)
I found a minor bug, when using non integer values (decimal values) causes a problem,
bash does not handle it very well.
Anyone who feels like updating and fixing please do so.
It works for anything that is on Cloud Watch!
Exemple (debug mode):
./check_aws2.sh –d “LoadBalancerName=lbinfvarnish” –n “AWS/ELB” –t Latency –p 60 –s Average –w 1 –c 2
Processing Args –d ‘LoadBalancerName=lbinfvarnish’ –n ‘AWS/ELB’ –t ‘Latency’ –p ’60’ –s ‘Average’ –w ‘1’ –c ‘2’ —
Option –d is LoadBalancerName=lbinfvarnish
Option –n is AWS/ELB
Option –t is Latency
Option –p is 60
Option –s is Average
Option –w is 1
Option –c is 2
Executing:
mon-get-stats Latency –namespace ‘AWS/ELB’ –statistics Average –period 60 –dimensions ‘LoadBalancerName=lbinfvarnish’
Result of cmd is: 0
OK – LoadBalancerName=lbinfvarnish Latency=0 |Latency=0
Here is the code:

#!/bin/bash
#This script uses AWS command line tools to get information from Amazon CloudWatch metrics
#Should be used with nagios
#by Felipe Ferreira May 2013
#Requirements: AWS tools installed and working without asking for password (how to setup)
#Requirements-link = http://felipeferreira.net/?p=1341
#SET EC2 CMDS
export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk.x86_64/
export AWS_AUTO_SCALING_HOME=/opt/AutoScaling
export AWS_CLOUDWATCH_HOME=/opt/cloudwatch
export PATH=$PATH:$AWS_AUTO_SCALING_HOME/bin:$EC2_HOME/bin:$AWS_CLOUDWATCH_HOME/bin
export AWS_AUTO_SCALING_URL=https://autoscaling.us-east-1.amazonaws.com
export AWS_CREDENTIAL_FILE=/opt/AutoScaling/key.txt
VERBOSE=0
outputDebug() {
    if [ $VERBOSE -gt 0 ] ; then
        echo -e "$1"
    fi
}
if [ $# -eq 0 ] ; then
    TEMP="-h"
else
    TEMP=`getopt -o vh -l 'versbose,help,d:,n:,t:,p:,s:,w:,c:' -- "$@"`
fi
outputDebug "Processing Args $TEMP"
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$TEMP"
while true ; do
    case "$1" in
        -v|--verbose) VERBOSE=1 ; outputDebug "Verbose Mode ON" ; shift ;;
        -h|--help) echo "Usage: $0 [--d DIMENSION] [--n NAMESPACE] [--t TYPE] [--p PERIOD] [--s STATISTC(Average|Sum)]  [--w WARN] [--c CRITICAL]  [-v|--verbose] " ; exit 0;;
        --d) outputDebug "Option $1 is $2" ; DIMENSION=$2 ; shift 2;;
        --n) outputDebug "Option $1 is $2" ; NAMESPACE=$2 ; shift 2;;
        --t) outputDebug "Option $1 is $2" ; TYPE=$2 ; shift 2;;
        --p) outputDebug "Option $1 is $2" ; PERIOD=$2 ; shift 2;;
        --s) outputDebug "Option $1 is $2" ; STATS=$2 ; shift 2;;
        --w) outputDebug "Option $1 is $2" ; WARN=$2 ; shift 2;;
        --c) outputDebug "Option $1 is $2" ; CRIT=$2 ; shift 2;;
        --) shift ; break ;;
        *) echo "Internal error! - found $1 and $2" ; exit 3 ;;
    esac
done
CMD="mon-get-stats  $TYPE --namespace '${NAMESPACE}' --statistics ${STATS} --period ${PERIOD} --dimensions '${DIMENSION}'"
outputDebug "Executing: \n $CMD"
#RESULT=`eval ${CMD}|tail -n1|awk '{ printf "%.0f\n",$3}'|cut -b0-4`
RESULT=`eval ${CMD}|tail -n1|awk '{ printf "%.0f\n",$3}'`
outputDebug "Result of cmd is: $RESULT"
if [ "$RESULT" -ge "$CRIT" ]; then
   echo "CRITICAL - $DIMENSION ${TYPE}=${RESULT} |$TYPE=$RESULT"
   exit 2
elif [ "$RESULT" -gt "$WARN" ]; then
   echo "WARNING - $DIMENSION ${TYPE}=${RESULT} |$TYPE=$RESULT"
   exit 1
else
   echo "OK - $DIMENSION ${TYPE}=${RESULT} |$TYPE=$RESULT"
   exit 0
fi
Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *