Skip to content

Instantly share code, notes, and snippets.

@shokoe
Last active September 28, 2021 12:17
Show Gist options
  • Save shokoe/3281d17c5966fa46b3937dc0f67224d8 to your computer and use it in GitHub Desktop.
Save shokoe/3281d17c5966fa46b3937dc0f67224d8 to your computer and use it in GitHub Desktop.
AWS ELB/ALB Nagios check script
#!/bin/bash
region="--region us-east-1"
profile=""
elb_name=""
alb=false
while [[ $# > 0 ]]; do
key="$1"
case $key in
--elb) elb_name="${2/*lb./}";;
--region) region="--region $2";;
--profile) [ "$2" != "default" ] && profile="--profile $2";;
--type) [ "$2" == "alb" ] && alb=true;;
--warn) warn=$2;;
--crit) crit=$2;;
*) echo "Unknow option used"; exit 2;;
esac
shift; shift;
done
ins_id2name(){
res_sed=`aws $profile $region ec2 describe-instances --output json |\
jq -r '.Reservations[].Instances[] | "\(.InstanceId) \(if .Tags and ([.Tags[] | select ( .Key == "Name" )] != []) then .Tags[] | select ( .Key == "Name" ) | .Value else "%" end)"' |\
grep -v % | sed 1d | sort -r |\
awk '$2!="None"{printf "s#\\\b"$1"\\\b#"$2":"$1"#g;"};'`
cat | sed "$res_sed"
}
if [ "$elb_name" == "" ]; then
echo "missing --elb parameter!"
exit 1
elif [[ "$elb_name" =~ io-geo ]]; then
elb_name="io-geo"
fi
if ! $alb ; then
ins=`aws $profile $region elb describe-instance-health --load-balancer-name $elb_name --output text |\
grep -v registration | sed -r 's#.*(i-.*)#\1#' | awk '{print $1, $3}'`
ins_out="<pre>`( echo "Name:ID:Status"; echo \"$ins\" | sort | ins_id2name ) | sed 's#:# #g' | column -t`</pre>"
else
lbArn=`aws $profile $region elbv2 describe-load-balancers --names ${elb_name} \
--query 'LoadBalancers[].[LoadBalancerArn]' --output text`
targetGroups=`aws $profile $region elbv2 describe-target-groups --load-balancer-arn $lbArn \
--query 'TargetGroups[].[TargetGroupArn,Port]' --output text`
unset ins
while read garn port; do
tgname=$(echo $garn | sed -r 's#.*/([^/]*)/.*#\1#')
i=`aws $profile $region elbv2 describe-target-health --target-group-arn $garn \
--query 'TargetHealthDescriptions[].[Target.Id,TargetHealth.State]' --output text |\
sed "s#\t#:$tgname:$port #"`
ins="$ins
$i"
done < <(echo "$targetGroups")
ins=`echo "$ins" | sed '/^$/d'`
ins_out="<pre>`( echo "Name:ID:Target:Port:Status"; echo \"$ins\" | sort | ins_id2name ) | sed 's#:# #g' | column -t`</pre>"
fi
if [ -z "$ins" ]; then
echo "Unable to get instance list"
exit 2
fi
if [[ ! `echo "$ins" | wc -l` = `echo "$ins" | grep -c '^i-'` ]]; then
echo "Bad api output"
echo "$ins"
exit 2
fi
now=`date +%s`
while read name i launch; do
if (( now - `date +%s -d $launch` < 900 )); then
ins=$(echo "$ins" | sed "/$i/s#\$# (NewAS $(($now - `date +%s -d $launch`))sec)#")
fi
done< <(aws ec2 describe-instances $profile $region --instance-ids $(echo "$ins" | sed 's#:.*##' | awk '{print $1}') --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value|[0],InstanceId,LaunchTime]' --output text);
ins_check=`echo "$ins" | grep -v '\<draining\>|\<initial\>|\<(NewAS)\>'`
ins_cnt=`echo "$ins_check" | wc -l`
ins_act=`echo "$ins_check" | egrep -c '\<InService\>|\<healthy\>|\<draining\>|\<initial\>|\<NewAS\>'`
perf="registered=$ins_cnt;0;0; InService=$ins_act;$(($ins_cnt/2));1;"
if [ ! $ins_cnt -eq $ins_act ]; then
echo "Found $(($ins_cnt-$ins_act)) endpoints not active | $perf"
echo "$ins_out"
exit 2
elif [ $ins_act -le $crit ]; then
echo "Found $ins_act/$ins_cnt (<=$crit) active | $perf"
echo "$ins_out"
exit 2
elif [ $ins_act -le $warn ]; then
echo "Found $ins_act/$ins_cnt (<=$warn) active | $perf"
echo "$ins_out"
exit 1
else
echo "All $ins_cnt (>$warn) endpoints are active | $perf"
echo "$ins_out"
exit 0
fi
exit 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment