Skip to content

Instantly share code, notes, and snippets.

@npotier
Last active October 24, 2017 15:20
Show Gist options
  • Save npotier/39073bb1385889892b66472c787ba008 to your computer and use it in GitHub Desktop.
Save npotier/39073bb1385889892b66472c787ba008 to your computer and use it in GitHub Desktop.
Check URL response code and notify it to slack
#!/bin/bash
# The URL you want to check
url="HTTP_MY_AWESOME_URL"
status_code=$(curl -o /dev/null --silent --head --write-out '%{http_code}\n' $url)
date=`date`
# The channel or user you want to notify
channel="MY_CHANNEL"
# The displayed bot username
username="BOT_USERNAME"
# Your slack Webhook URL
webhook_url="SLACK_WEBHOOK_URL"
if [ $status_code -ne "200" ]
then
text="status check failed for $url at $date : status code $status_code returned"
escapedText=$(echo $text | sed 's/"/\"/g' | sed "s/'/\'/g" )
json="{\"channel\": \"$channel\", \"username\":\"$username\", \"icon_emoji\":\"ghost\", \"attachments\":[{\"color\":\"danger\" , \"text\": \"$escapedText\"}]}"
else
text="status check ok for $url at $date : status code $status_code returned"
escapedText=$(echo $text | sed 's/"/\"/g' | sed "s/'/\'/g" )
json="{\"channel\": \"$channel\", \"username\":\"$username\", \"icon_emoji\":\"ghost\", \"attachments\":[{\"color\":\"good\" , \"text\": \"$escapedText\"}]}"
fi
curl -s -d "payload=$json" "$webhook_url"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment