I wrote this script to monitor my Amazon EC2 servers and LoadBalancers, this script is able to get any metrics
avaiable at the cloud-watch. For it to work you will need the amazon api commands working, here is how to setup it.
Usage: ./check_aws.sh [–d DIMENSION] [–n NAMESPACE] [–t TYPE] [–p PERIOD] [–s STATS (Average|Sum)] [–w WARN] [–c CRITICAL] [-v|–verbose]
Example:
# sudo -u nagios ./check_aws.sh –d LoadBalancerName=master –n AWS/ELB –t RequestCount –p 300 –s Sum –w 1200 –c 1300 OK – LoadBalancerName=master RequestCount=412 |RequestCount=412

#!/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}'`
outputDebug "Result of cmd is: $RESULT"
if [ $RESULT -gt $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

Leave a Reply

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