You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.2 KiB

#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
# Load Telegram bot info
source .httpcheck_telegram #Provides KEY, CHATID, CHKLIST
TIME="10"
URL="https://api.telegram.org/bot$KEY/sendMessage"
#Telegram message function
send_message() {
curl -s --max-time $TIME -d "chat_id=${CHATID}&disable_web_page_preview=1&text=$1" $URL >/dev/null
}
get_http_code() {
curl -sILk --max-time $TIME $1 | grep HTTP | tail -n 1 | grep -Eo '[0-9]{3}' || printf '%s\n' 000
}
# Check HTTP codes
for site in $(< $CHKLIST); do
respcode=$(get_http_code $site)
case $respcode in
000)
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
;;
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')]: PROBLEM -- $site returns $respcode" >&2
send_message "HTTP/S Alert:%0A $site responding with $respcode." && printf '%s\n' "Message sent via Telegram bot" >&2
;;
esac
done || exit 1
exit 0