Sitechecks/sitechecks.sh
hax 245dda9aab fixed some garbage translations ^^ No.2 xD
Signed-off-by: hax <hax@lainlounge.xyz>
2025-01-05 17:36:25 +00:00

133 lines
3.5 KiB
Bash

#!/usr/bin/env bash
# Files
INPUT_FILE="sites.json"
LOG_FILE="sites.log"
# Curl-Options
CURL_OPTS=(
--silent
--max-time 10
--location
--max-redirs 10
--fail
--output /dev/null
)
# Telegram-Bot-Configuration
TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN:-<Your-Bot-Token>}"
TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID:-<Your-Chat-ID>}"
# Function: Send Telegram-Messages
send_telegram_message() {
local chat_id="$1"
local message="$2"
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-d chat_id="${chat_id}" \
-d text="$message" \
> /dev/null
}
# Function: Describe HTTP-Statuscode
http_status_description() {
local status="$1"
case "$status" in
200) echo "OK" ;;
301) echo "Moved Permanently" ;;
302) echo "Found" ;;
400) echo "Bad Request" ;;
401) echo "Unauthorized" ;;
403) echo "Forbidden" ;;
404) echo "Not Found" ;;
408) echo "Request Timeout" ;;
429) echo "Too Many Requests" ;;
500) echo "Internal Server Error" ;;
502) echo "Bad Gateway" ;;
503) echo "Service Unavailable" ;;
504) echo "Gateway Timeout" ;;
*) echo "Unknown Status" ;;
esac
}
# Function: Check Site Status
check_site() {
local site="$1"
curl "${CURL_OPTS[@]}" --write-out "%{http_code}" "$site" 2>/dev/null
}
# Function: Telegram-Command `/check URL` execution
process_telegram_updates() {
local update_url="https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/getUpdates"
local last_update_id=0
while true; do
# Check for Updates
response=$(curl -s "${update_url}?offset=${last_update_id}")
messages=$(echo "$response" | jq -c '.result[]')
for message in $messages; do
# Extract Message Details
update_id=$(echo "$message" | jq -r '.update_id')
chat_id=$(echo "$message" | jq -r '.message.chat.id')
text=$(echo "$message" | jq -r '.message.text')
# Command `/check URL` run
if [[ "$text" == /check* ]]; then
url=$(echo "$text" | awk '{print $2}')
if [[ -z "$url" ]]; then
send_telegram_message "$chat_id" "❗ Error: Please provide a valid URL after /check."
else
status=$(check_site "$url")
description=$(http_status_description "$status")
send_telegram_message "$chat_id" "🌐 URL: $url
HTTP-Status: $status ($description)"
fi
fi
# Update-ID Increasing, to avoid double messages
last_update_id=$((update_id + 1))
done
# Check Intervall
sleep 5
done
}
# Function: Automatic Monitoring
monitor_sites() {
local urls=$(jq -r '.urls[]' "$INPUT_FILE")
if [[ -z "$urls" ]]; then
echo "Error: No URL set."
exit 1
fi
for site in $urls; do
status=$(check_site "$site")
description=$(http_status_description "$status")
# Show Only The Error
if [[ "$status" != "200" ]]; then
send_telegram_message "$TELEGRAM_CHAT_ID" "❗ Website-Monitoring Error:
🌐 URL: $site
HTTP-Status: $status ($description)"
fi
done
}
# Main
case "$1" in
listen)
echo "Start Telegram-Bot-Listeners..."
process_telegram_updates
;;
monitor)
echo "Start Auto Monitoring..."
monitor_sites
;;
*)
echo "Usage: $0 {listen|monitor}"
exit 1
;;
esac