Skip to content

Instantly share code, notes, and snippets.

@elocnatsirt
Last active February 9, 2017 17:24
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 elocnatsirt/3006c59f2082b98fe71770275eb691f7 to your computer and use it in GitHub Desktop.
Save elocnatsirt/3006c59f2082b98fe71770275eb691f7 to your computer and use it in GitHub Desktop.
Checks the health of an AWS Elastic Beanstalk environment and reports based on color.
#!/bin/bash
# Written By: https://github.com/elocnatsirt
# Options:
# -r = Region of the beanstalk environment
# -n = Name of the beanstalk environment
# extract options and their arguments into variables.
while getopts 'r:n:' OPT; do
case $OPT in
r) region=$OPTARG;;
n) env_name=$OPTARG;;
esac
done
region=${region:=unknown}
env_name=${env_name:=unknown}
if [ "$region" == "unknown" ]; then
echo "You need to specify an AWS region with -r."
exit 3
elif [ "$env_name" == "unknown" ]; then
echo "You need to specify a beanstalk environment name with -n."
exit 3
fi
env_health_color=`aws --region ${region} elasticbeanstalk describe-environment-health --environment-name ${env_name} --attribute-names All | jq -r '.Color'`
env_instance_health=`aws --region ${region} elasticbeanstalk describe-environment-health --environment-name ${env_name} --attribute-names All | jq -r '.InstancesHealth'`
if [ "$env_health_color" == "Green" ]; then
echo "The beanstalk environment is healthy."
exit 0
elif [ "$env_health_color" == "Yellow" ]; then
echo "The beanstalk environment has potential issues.\n"
echo $env_instance_health
exit 1
elif [ "$env_health_color" == "Red" ]; then
echo "The beanstalk environment is unhealthy."
echo $env_instance_health
exit 2
else
echo "The beanstalk environment is in an unknown state."
exit 3
fi
@elocnatsirt
Copy link
Author

elocnatsirt commented Sep 17, 2016

This was written to use as a check with Sensu, but could be modified for anything. The "check-beanstalk-health.rb" available in the sensu-plugins-aws package would hang up randomly and cause issues on our Sensu master, so I wrote a quick bash script instead. This assumes that you are running this from an AWS instance that has an IAM profile applied with proper permissions or is using the default profile you have setup on whatever box you are running from. It also assumes you have JQ installed.

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