Gets a list of domains with curl, sending an email if the curl exit code is non-zero
This file contains 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/bash | |
ERROR_EMAIL="billg@microsoft.com" | |
DOMAINS=( | |
"https://github.com" | |
) | |
ERRORED_DOMAINS=() | |
# joins the rest of the input args with the first arg | |
function join_by { local IFS="$1"; shift; echo "$*"; } | |
for i in "${DOMAINS[@]}" | |
do | |
DOMAIN="$i" | |
curl --silent --output /dev/null $DOMAIN | |
STATUS="$?" | |
if [ "$STATUS" -ne 0 ]; then | |
echo "`date +\"%Y-%m-%d %H:%M:%S\"` $DOMAIN curl-exit-code=$STATUS" | |
ERRORED_DOMAINS+=("$DOMAIN curl-exit-code=$STATUS") | |
else | |
echo "`date +\"%Y-%m-%d %H:%M:%S\"` $DOMAIN curl-exit-code=$STATUS" | |
fi | |
done | |
if [ ${#ERRORED_DOMAINS[@]} -eq 0 ]; then | |
echo "`date +\"%Y-%m-%d %H:%M:%S\"` No errors found" | |
else | |
echo "`date +\"%Y-%m-%d %H:%M:%S\"` Errors found, emailing $ERROR_EMAIL" | |
BODY=$(join_by $'\n' "${ERRORED_DOMAINS[@]/#/}" "$1") | |
echo "$BODY" | mail -E -s "check_domains error" $ERROR_EMAIL | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment