Created
August 9, 2021 12:52
-
-
Save toniher/1a5be39efca6600aa586ef825a02cfdb to your computer and use it in GitHub Desktop.
Detects HTTP code of a server and mail a warning message if none of the expected HTTP codes are found
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
#!/usr/bin/env bash | |
# Placed in a different machine than production with a cron | |
# Server | |
URL="https://example.com" | |
HTTP=$(curl --silent --insecure -m 30 -I GET "${URL}"|perl -lane 'if ($_=~/HTTP/) {$_=~/HTTP\S+\s+(\d+)\s+/; print $1;} ') | |
# Normal situation codes | |
CODES=(200) | |
MSG="Something is wrong with ${URL}. CODE: ${HTTP}" | |
SUBJECT="Server seems down: ${URL}" | |
TO="mail@example.com" | |
FROM="mail@example.com" | |
if [[ ! " ${CODES[@]} " =~ " ${HTTP} " ]]; then | |
echo "${MSG}" | mail -s "${SUBJECT}" ${TO} -aFrom:${FROM}\> | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment