#!/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin CONFIG=$1 # BEGIN CONFIG CHECK ### Provides KEY, CHATID, CHKLIST if [ -n "$CONFIG" ]; then if [ -f "$CONFIG" ]; then . $CONFIG #Source config file else printf '%s\n' "$CONFIG: File does not exist." >&2 exit 10 fi else if [ -f ".httpcheck_telegram" ]; then . .httpcheck_telegram #Source config file else printf '%s\n' "No configuration file found." >&2 exit 10 fi fi # Check if config was loaded by examining all configuration variables, exit if not if [ -z "$KEY" ]; then printf '%s\n' "Configuration not set correctly: KEY not set" >&2 ERROR11=true fi if [ -z "$CHATID" ]; then printf '%s\n' "Configuration not set correctly: CHATID not set" >&2 ERROR11=true fi if [ -z "$CHKLIST" ]; then printf '%s\n' "Configuration not set correctly: CHKLIST not set" >&2 ERROR11=true fi [ "$ERROR11" = "true" ] && exit 11 # END CONFIG CHECK 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 $(printf '%s\n' $1 | awk '{gsub("https?://|/.*","")}1') 2>&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 } print_time() { date '+%Y-%m-%d %H:%M:%S' } # Prepare message header MSG="${HEADER}%0AProblems Found:" FAILURE=false # Check HTTP codes for site in $(grep -v "^#\|^$" $CHKLIST | sort | uniq); do respcode=$(get_http_code $site) case $respcode in 000) printf '%s\n' "[$(print_time)]: PROBLEM -- $site: DNS record not found" >&2 MSG="${MSG}%0A%0A %2B ${site}%0A DNS record not found" FAILURE=true ;; 200) printf '%s\n' "[$(print_time)]: OK -- $site returns $respcode" ;; 999) printf '%s\n' "[$(print_time)]: PROBLEM -- $site caused general cURL failure" >&2 ;; *) printf '%s\n' "[$(print_time)]: 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 if [ "$FAILURE" = "true" ]; then send_message "$MSG" && printf '%s\n' "Problems found. Message sent via Telegram bot" >&2 || printf '%s\n' "Problems found. Message sending has failed" fi exit 0