#!/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() { 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 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 MSG="${MSG}%0A%0A %2B ${site}%0A DNS record not found" FAILURE=true ;; 200) 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 MSG="${MSG}%0A%0A %2B ${site}%0A responding ${respcode}" FAILURE=true ;; esac 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