Skip to content

Instantly share code, notes, and snippets.

@toniher
Created August 9, 2021 12:52
Show Gist options
  • Save toniher/1a5be39efca6600aa586ef825a02cfdb to your computer and use it in GitHub Desktop.
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
#!/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