Skip to content

Commit

Permalink
unhealthy-hosts-to-slack-alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
Varun Chandak committed Feb 2, 2018
1 parent b27c92c commit 8527a90
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* aws-audit
* iam-mfa-alert
* remove-iam-user-completely
* unhealthy-hosts-to-slack-alerts
---

#### More to come..
43 changes: 43 additions & 0 deletions unhealthy-hosts-to-slack-alerts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Alert unhealthy instances present in the ALB/ELB to slack

The script is used to report unhealthy hosts present in ALB/ELB on AWS to a specific slack channel.

Prerequisites:

* Slack Channel
* Slack Webhook URL
* AWS Role on EC2 (or IAM credentials in profile)

---
## check-unhealthy-hosts-alb.sh

This script will check unhealthy hosts in a particular `ALB`.

**Requirements**:
- Target Group ARN of the ALB to check for unhealthy hosts.

**Usage**:
```
./check-unhealthy-hosts-alb.sh <TARGET_GROUP_ARN> <AWS_PROFILE> <AWS_REGION>
```
**Example**:
```
./check-unhealthy-hosts-alb.sh arn:aws:elasticloadbalancing:ap-south-1:123456789012:targetgroup/test-targetgroup/abcd1234 default ap-south-1
```
---
## check-unhealthy-hosts.sh

This script will check unhealthy hosts in a particular `ELB`.

**Requirements**:
- ELB Name to check for unhealthy hosts.

**Usage**:
```
./check-unhealthy-hosts.sh <ELB_NAME> <AWS PROFILE> <AWS_REGION>
```

**Example**:
```
./check-unhealthy-hosts.sh test-elb default ap-south-1
```
59 changes: 59 additions & 0 deletions unhealthy-hosts-to-slack-alerts/check-unhealthy-hosts-alb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

################################################################################
################################### ALB ONLY ###################################
################################################################################
# This script will check the count (and instance ids) unhealthy hosts of particular load balancer.
# If there are no instances which are OutOfService, no notification will be sent.
# If there are > 2 instances which are OutOfService, notification will be sent to the slack channel.
################################################################################

# Set AWS Alias
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH
alias aws=''`which aws`' --profile $2 --output text --region $3'
shopt -s expand_aliases

usage() {
echo '################################################################################
# This script will check the instance ids for unhealthy hosts of particular load balancer.
# If the instances which are not OutOfService/unhealthy, no notification will be sent.
# If the instances which are OutOfService/unhealthy, notification will be sent to the slack channel.
# Recommended to set cron for every 5/10 minutes and check for instances which are repeating.
################################################################################
Prerequisites:
* Slack Channel
* Slack Webhook URL
* AWS Role on EC2 (or credentials in profile)
Usage:
./check-unhealthy-hosts-alb.sh <TARGET_GROUP_ARN> <AWS PROFILE> <AWS_REGION>
Example:
./check-unhealthy-hosts-alb.sh arn:aws:elasticloadbalancing:ap-south-1:123456789012:targetgroup/test-targetgroup/abcd1234 default ap-south-1
'
}

# Slack Settings ###############################################################
slackChannel(){ # subject message
SLACK_WEBHOOK_URL="<Slack Webhook URL>"
SLACK_CHANNEL="#<Slack Channel Name>"
SLACK_BOTNAME="<Slack Bot Name>"
PAYLOAD="payload={\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_BOTNAME}\", \"text\": \"Unhealthy Target Groups (every 5 mins): \`\`\`"$1"\`\`\`\"}"
curl --connect-timeout 30 --max-time 60 -s -S -X POST --data-urlencode "${PAYLOAD}" "${SLACK_WEBHOOK_URL}"
}

if [ "$#" -ne 3 ]; then
usage
else
TG_ARN="$1"
if [ ! -z "$(aws elbv2 describe-target-health --target-group-arn "$TG_ARN" --query 'TargetHealthDescriptions[?TargetHealth.State!=`healthy`].Target.Id')" ]; then
echo "$TG_ARN" | cut -d/ -f2
aws elbv2 describe-target-health \
--target-group-arn "$TG_ARN" \
--query 'TargetHealthDescriptions[?TargetHealth.State!=`healthy`].Target.Id' | tr -s '\t' ','
fi | paste -d, - - | while read TGDETAILS; do
slackChannel "Target Group: $(echo $TGDETAILS | cut -d, -f1)\nInstanceIds: $(echo $TGDETAILS | cut -d, -f2-)"
done

fi
57 changes: 57 additions & 0 deletions unhealthy-hosts-to-slack-alerts/check-unhealthy-hosts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

################################################################################
################################### ELB ONLY ###################################
################################################################################
# This script will check the count (and instance ids) unhealthy hosts of particular load balancer.
# If there are no instances which are OutOfService, no notification will be sent.
# If there are > 2 instances which are OutOfService, notification will be sent to the slack channel.
################################################################################

# Set AWS Alias
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH
alias aws=''`which aws`' --profile $2 --output text --region $3'
shopt -s expand_aliases

usage() {
echo '################################################################################
# This script will check the instance ids for unhealthy hosts of particular load balancer.
# If the instances which are not OutOfService/unhealthy, no notification will be sent.
# If the instances which are OutOfService/unhealthy, notification will be sent to the slack channel.
# Recommended to set cron for every 5/10 minutes and check for instances which are repeating.
################################################################################
Prerequisites:
* Slack Channel
* Slack Webhook URL
* AWS Role on EC2 (or credentials in profile)
Usage:
./check-unhealthy-hosts.sh <ELB_NAME> <AWS PROFILE> <AWS_REGION>
Example:
./check-unhealthy-hosts.sh test-elb default ap-south-1
'
}

# Slack Settings ###############################################################
slackChannel(){ # subject message
SLACK_WEBHOOK_URL="<Slack Webhook URL>"
SLACK_CHANNEL="#<Slack Channel Name>"
SLACK_BOTNAME="<Slack Bot Name>"
PAYLOAD="payload={\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_BOTNAME}\", \"text\": \"Unhealthy hosts checks (every 5 mins): \`\`\`"$1"\`\`\`\"}"
curl --connect-timeout 30 --max-time 60 -s -S -X POST --data-urlencode "${PAYLOAD}" "${SLACK_WEBHOOK_URL}"
}

if [ "$#" -ne 3 ]; then
usage
else
LB_NAME="$1"
if [ ! -z "$(aws elb describe-instance-health --load-balancer-name "$LB_NAME" --output text --query 'InstanceStates[?State==`OutOfService`].InstanceId')" ]; then
echo "$LB_NAME"
aws elb describe-instance-health \
--load-balancer-name "$LB_NAME" \
--query 'InstanceStates[?State==`OutOfService`].InstanceId' | tr -s '\t' ','
fi | paste -d, - - | while read LBDETAILS; do slackChannel "LoadBalancer: $(echo $LBDETAILS | cut -d, -f1)\nInstanceIds: $(echo $LBDETAILS | cut -d, -f2-)"; done

fi

0 comments on commit 8527a90

Please sign in to comment.