Created
June 15, 2017 20:40
-
-
Save gkspranger/f9bb1d180d5665d02806684d34c37102 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# checks for the number of AWS AMIs and allows alerts when threshold is met | |
# example usage: | |
# ./check_aws_amis.sh -w <integer> -c <integer> | |
### | |
### USES ANSIBLE to put AWS KEYs as VARS | |
### USES ANSIBLE to define NAGIOS REGION | |
### | |
### REQUIRES AWS CLI : https://aws.amazon.com/cli/ | |
### REQUIRES JQ : https://stedolan.github.io/jq/ | |
export AWS_ACCESS_KEY_ID={{ aws_access_key_id }} | |
export AWS_SECRET_ACCESS_KEY={{ aws_secret_access_key }} | |
warn=NULL | |
critical=NULL | |
help () { | |
cat << EOF | |
Check number of AMIs in AWS. | |
Usage: | |
check_aws_amis.sh -w <warning number> -c <critical number> | |
Options: | |
-h, | |
Print detailed help screen | |
-w INTEGER | |
Exit with WARNING status if greater than INTEGER | |
-c INTEGER | |
Exit with CRITICAL status if greater than INTEGER | |
EOF | |
exit 3 | |
} | |
while getopts "w:c:h" opt; do | |
case $opt in | |
w) | |
warn="$OPTARG" | |
;; | |
c) | |
critical="$OPTARG" | |
;; | |
h) | |
help | |
;; | |
esac | |
done | |
if [[ "$warn" == "NULL" ]] || [[ "$critical" == "NULL" ]]; then | |
help | |
fi | |
amis=`aws ec2 describe-images --owners self --region {{ ansible_ec2_placement_region }} | jq -r '.Images | length'` | |
if [ $amis -ge $critical ]; then | |
dostatus="CRITICAL" | |
doexit=2 | |
elif [ $amis -ge $warn ]; then | |
dostatus="WARNING" | |
doexit=1 | |
elif [ $amis -lt $warn ]; then | |
dostatus="OK" | |
doexit=0 | |
else | |
dostatus="UNKOWN" | |
doexit=3 | |
fi | |
echo "AWS AMIs ${dostatus} - ${amis} AWS AMIs | amis=${amis};0;0;0;0" | |
exit $doexit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment