diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6537d0c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.httpcheck_telegram diff --git a/example_configs/httpcheck_telegram b/example_configs/httpcheck_telegram new file mode 100644 index 0000000..c851025 --- /dev/null +++ b/example_configs/httpcheck_telegram @@ -0,0 +1,7 @@ +# Bot Info +CHATID="5555555" +KEY="THISISAUNIQUEANDPRIVATEKEY" + +# Site list (file location) +CHKLIST="${HOME}/.site_check" + diff --git a/http_check b/http_check new file mode 100755 index 0000000..0693852 --- /dev/null +++ b/http_check @@ -0,0 +1,39 @@ +#!/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 +