Skip to content

Instantly share code, notes, and snippets.

@caleb15
Last active August 8, 2022 23:16
Show Gist options
  • Save caleb15/1a817ef5e58e8a8caf65190cff33806e to your computer and use it in GitHub Desktop.
Save caleb15/1a817ef5e58e8a8caf65190cff33806e to your computer and use it in GitHub Desktop.
#!/bin/sh
GLOBIGNORE="*" # to prevent schedule from becoming wacky
if [ "$1" = "-h" ]; then
echo "Usage: $(basename "$0") check_name \"schedule\" \"command\" alert_channel1,alert_channel2 \"tag1 tag2\" \"description\" grace"
echo "Everything after \"command\" is optional. Grace is a number, if specified"
echo "Example: $(basename "$0") influxdb_is_running \"* * * * *\" \"influx -execute 'SHOW DATABASES'\" VictorOps \"devops\""
echo "Please make sure VPC_NAME and HEALTHCHECKS_API_KEY are set before running the script"
exit 0
fi
check_name="($VPC_NAME) $1"
schedule=$2
command=$3
alert_channels=$4
tags=$5
desc=$6
# grace is optional so we only set json if user specifies it
# in hindsight would've been simpler to use ${1-3600} substitution
# and always pass grace in (3600 is default)
grace=${7+',"grace":'$7}
# we set manual_resume to true to avoid spam if a frequent job we paused keeps on failing
PAYLOAD='{"name": "'$check_name'", "tags": "'$tags'", "desc": "'$desc'"'$grace', "schedule": "'$schedule'", "channels": "'$alert_channels'", "unique": ["name"], "manual_resume": true}'
# Create the check if it does not exist or return existing.
CHECK_DATA=$(curl -s https://healthchecks.io/api/v1/checks/ -H "X-Api-Key: $HEALTHCHECKS_API_KEY" -d "$PAYLOAD" --max-time 10)
ERROR=$(echo "$CHECK_DATA" | jq -r .error)
if [ "$ERROR" != "null" ]; then
echo "Error: $ERROR" >&2;
fi
URL=$(echo "$CHECK_DATA" | jq -r .ping_url)
# Finally, send a ping:
curl -sSf "$URL"/start --max-time 10 1>/dev/null
eval "$command"
status_code=$?
if [ $status_code -ne 0 ]; then URL=$URL/fail; fi
curl -sSf --retry 3 "$URL" --max-time 10 1>/dev/null
exit $status_code
@caleb15
Copy link
Author

caleb15 commented Jun 6, 2020

Approved for open source. This can be considered under MIT license.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment