Last active
August 4, 2020 23:32
-
-
Save ajmas/713b245d80a34870356b64a3273bfaf6 to your computer and use it in GitHub Desktop.
Simple server health check, that sends failure to a discord or slack channel
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/sh | |
| ## simple server health checker. when the service does not respond it calls a | |
| ## Discord or Slack webhook | |
| ## adjust values below according to you needs: | |
| server=www.google.com | |
| service=discord | |
| webhook_url="https://discordapp.com/api/webhooks/...." | |
| ping -c 4 $server | |
| if [[ $? != 0 ]]; then | |
| failure="Possibly down: The server '${server}' did not respond to a ping" | |
| fi | |
| # if [[ -z $failure ]]; then | |
| # port_number=80 | |
| # nc -zvw10 "$server" "${port_number}" > /dev/null | |
| # | |
| # if [[ $? != 0 ]]; then | |
| # failure="Possibly down: Port ${port_number} on ${server} is closed" | |
| # fi | |
| # fi | |
| if [[ -z $failure ]]; then | |
| status_code=$(curl -s -o /dev/null -w "%{http_code}" "https://${server}") | |
| if [[ $? != 0 ]]; then | |
| failure="Possibly down: Webserver on ${server} did not respond" | |
| elif [[ $status_code != "200" ]]; then | |
| failure="Possibly down: Test page on ${server} responded with ${status_code}" | |
| fi | |
| fi | |
| if [[ -n $failure ]]; then | |
| echo "error: ${failure}" | |
| if [[ $service == "discord" ]]; then | |
| json=$(cat <<EOF | |
| { | |
| "username": "Server Health Checker", | |
| "content": "$failure" | |
| } | |
| EOF | |
| ) | |
| elif [[ $service == "slack" ]]; then | |
| json=$(cat <<EOF | |
| { | |
| "text": "$failure" | |
| } | |
| EOF | |
| ) | |
| fi | |
| curl -X POST -H "Content-Type: application/json" -d "$json" "$webhook_url" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment