Added awk-fu to check for DNS record of any complete URL; switched to a single message format

master
Aaron Johnon 4 years ago
parent 45fc0cbca9
commit b3ceb9be9d

@ -14,26 +14,42 @@ send_message() {
} }
get_http_code() { get_http_code() {
curl -sILk --max-time $TIME $1 | grep HTTP | tail -n 1 | grep -Eo '[0-9]{3}' || printf '%s\n' 000 if host $(awk '{gsub("https?://|/.*","")}1' <<< $1) &>/dev/null; then
curl -sILk --max-time $TIME $1 | grep HTTP | tail -n 1 | grep -Eo '[0-9]{3}' || printf '%s\n' 999
else
printf '%s\n' 000
fi
} }
# Prepare message header
MSG='HTTP/S Problems Found:'
FAILURE=false
# Check HTTP codes # Check HTTP codes
for site in $(< $CHKLIST); do for site in $(< $CHKLIST); do
respcode=$(get_http_code $site) respcode=$(get_http_code $site)
case $respcode in case $respcode in
000) 000)
printf '%s\n' "[$(date '+%Y%m%d %H:%M:%S')]: PROBLEM -- $site: DNS record not found" >&2 printf '%s\n' "[$(date '+%Y%m%d %H:%M:%S')]: PROBLEM -- $site: DNS record not found" >&2
send_message "HTTP/S Alert:%0A $site DNS record not found." && printf '%s\n' "Message sent via Telegram bot" >&2 MSG="${MSG}%0A%0A %2B ${site}%0A DNS record not found"
FAILURE=true
;; ;;
200) 200)
printf '%s\n' "[$(date '+%Y%m%d %H:%M:%S')]: OK -- $site returns $respcode" printf '%s\n' "[$(date '+%Y%m%d %H:%M:%S')]: OK -- $site returns $respcode"
;; ;;
999)
printf '%s\n' "[$(date '+%Y%m%d %H:%M:%S')]: PROBLEM -- $site caused general cURL failure" >&2
;;
*) *)
printf '%s\n' "[$(date '+%Y%m%d %H:%M:%S')]: PROBLEM -- $site returns $respcode" >&2 printf '%s\n' "[$(date '+%Y%m%d %H:%M:%S')]: PROBLEM -- $site returns $respcode" >&2
send_message "HTTP/S Alert:%0A $site responding with $respcode." && printf '%s\n' "Message sent via Telegram bot" >&2 MSG="${MSG}%0A%0A %2B ${site}%0A responding ${respcode}"
FAILURE=true
;; ;;
esac esac
done || exit 1 done || exit 2
# Send compiled message if any problems were found
send_message "$MSG" && printf '%s\n' "Problems found. Message sent via Telegram bot" >&2 || printf '%s\n' "Problems found. Message sending has failed"
exit 0 exit 0
Loading…
Cancel
Save