Skip to content

Instantly share code, notes, and snippets.

@Porrapat
Created January 9, 2020 06:15
Show Gist options
  • Save Porrapat/a219d663fe1e1e2529038f7e2a40e0da to your computer and use it in GitHub Desktop.
Save Porrapat/a219d663fe1e1e2529038f7e2a40e0da to your computer and use it in GitHub Desktop.
# Simple Check Website (With Send Slack Message to Channel)
# How to use
# ======================================
# chmod -R 777 checkwebsite.sh
# ./checkwebsite.sh
# Can install as crontab and output to log file. Something like
# * * * * * bash ./checkwebsite.sh >> /www/checkwebsite/html/checkwebsite.log
# To Do More
# Add Line Channel Feature
# Add Facebook Personal Channel
# Add Google E-mail Channel
now=$(date)
website_type="My Check Website"
# (Optional) In the case, you need to send the result to your team slack channel
# Please provide your slack token, otherwise leave it blank
slack_token=""
slack_channel=""
echo "Current date: $now"
URLs=(
"google.com"
"facebook.com"
"youtube.com"
)
OK=true
for URL in "${URLs[@]}"
do
echo -n "Testing $URL : Result "
status_code=$(curl -sL -w "%{http_code}\\n" "$URL" -o /dev/null)
echo -n $status_code
if [[ "$status_code" -ne 200 ]] ; then
echo " *** Site $URL Not working"
OK=false
else
echo
fi
done
if [ "$OK" = true ] ; then
MESSAGE="Every Website $website_type is OK at $(date)"
else
MESSAGE="Some $website_type is down. Please fix at $(date)"
fi
if [ "$OK" = true ] ; then
# Echo Result to Output
echo "$MESSAGE"
if [ ! -z "$slack_token" -a "$slack_token" != " " ]; then
curl --silent -X POST -d "token=$slack_token&channel=$slack_channel&text=$MESSAGE" "https://slack.com/api/chat.postMessage" -o /dev/null
fi
else
# Echo Result to Output
echo "$MESSAGE"
# Send Result to Slack
if [ ! -z "$slack_token" -a "$slack_token" != " " ]; then
curl --silent -X POST -d "token=$slack_token&channel=$slack_channel&text=$MESSAGE" "https://slack.com/api/chat.postMessage" -o /dev/null
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment