This scripts counts how many active instances are inService of a AWS ElasticLoadBalancer (ELB).
The code is based on a simple API call via bash
elb-describe-instance-health
Here is the code:
#!/bin/bash
#This script uses AWS command line tools to get information from AWS ELB
#Should be used with nagios
#by Felipe Ferreira Set 2013
#Requirements: AWS tools installed and working without asking for password (how to setup)
#Requirements-link = http://felipeferreira.net:81/?p=1341
#SET EC2 CMDS (CONFIGURE HERE)
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 JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk.x86_64/
export AWS_CREDENTIAL_FILE=/opt/AutoScaling/key.txt
export AWS_ELB_HOME=/opt/elbtools
export PATH=$PATH:$AWS_AUTO_SCALING_HOME/bin:$EC2_HOME/bin:$AWS_CLOUDWATCH_HOME/bin:AWS_ELB_HOME:AWS_ELB_HOME/bin
VERBOSE=1
outputDebug() {
if [ $VERBOSE -gt 0 ] ; then
echo -e "$1"
fi
}
if [ $# -eq 0 ] ; then
TEMP="-h"
else
TEMP=`getopt -o vh -l 'versbose,help,l:,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 [--l ELASTICLAOADBANCE [--w WARN] [--c CRITICAL] [-v|--verbose] " ; exit 0;;
--l) outputDebug "Option $1 is $2" ; ELB=$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="/opt/elbtools/bin/elb-describe-instance-health $ELB |grep -c InService"
outputDebug "Executing: \n $CMD"
RESULT=`eval ${CMD}`
outputDebug "Result of cmd is: $RESULT"
if [ $RESULT -gt $CRIT ] || [ $RESULT -eq 0 ] || [ -z $RESULT ]; then
echo "CRITICAL - ELB $ELB has ${RESULT} instances InService |ELB=$RESULT"
exit 2
elif [ $RESULT -gt $WARN ]; then
echo "WARNING - ELB $ELB has ${RESULT} instances InService |ELB=$RESULT"
exit 1
else
echo "OK - ELB $ELB has ${RESULT} instances InService |ELB=$RESULT"
exit 0
fi

Hi,
Thanks for this. Made a quick change though as it did not seem to be giving me the desired result.
if [ $CRIT -gt $RESULT ] || [ $RESULT -eq 0 ] || [ -z $RESULT ]; then
echo “CRITICAL – ELB $ELB has ${RESULT} instances InService |ELB=$RESULT”
exit 2
elif [ $WARN -gt $RESULT ]; then
echo “WARNING – ELB $ELB has ${RESULT} instances InService |ELB=$RESULT”
exit 1
else
echo “OK – ELB $ELB has ${RESULT} instances InService |ELB=$RESULT”
exit 0
fi
Now when running
bash -x ./check_elb.sh –l ELB-NAME –w 1 –c 2
Returns
+ RESULT=2
+ outputDebug ‘Result of cmd is: 2’
+ ‘[‘ 1 -gt 0 ‘]’
+ echo -e ‘Result of cmd is: 2’
Result of cmd is: 2
+ ‘[‘ 2 -gt 2 ‘]’
+ ‘[‘ 2 -eq 0 ‘]’
+ ‘[‘ -z 2 ‘]’
+ ‘[‘ 1 -gt 2 ‘]’
+ echo ‘OK – ELB ELB-ADMIN-INT has 2 instances InService |ELB=2’
OK – ELB ELB-ADMIN-INT has 2 instances InService |ELB=2
+ exit 0
Which is what I’m looking for.
Thanks again!
Joe.