From 0bf68b5a0fac86b5edc3da89deaca5281438c6cd Mon Sep 17 00:00:00 2001 From: Tim_THW Date: Mon, 10 Nov 2025 13:11:54 +0000 Subject: [PATCH] First --- Mon.sh | 72 +++++++++++++++++++++++++++++++++++++++++++++ hosts.txt | 2 ++ logs/2025-11-10.csv | 5 ++++ 3 files changed, 79 insertions(+) create mode 100755 Mon.sh create mode 100644 hosts.txt create mode 100644 logs/2025-11-10.csv diff --git a/Mon.sh b/Mon.sh new file mode 100755 index 0000000..0aae426 --- /dev/null +++ b/Mon.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash +# Webhost Availability Monitor - Single File - Loop +# user only. keine root. + +set -o errexit +set -o nounset +set -o pipefail + +INTERVAL=60 + +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +LOG_DIR="$ROOT_DIR/logs" +HOSTS_FILE="$ROOT_DIR/hosts.txt" +SEP=';' + +timestamp_iso() { + date -Iseconds +} + +ensure_dirs() { + mkdir -p "$LOG_DIR" +} + +current_logfile() { + printf "%s/%s.csv" "$LOG_DIR" "$(date +%F)" +} + +write_header_if_needed() { + local file="$1" + if [ ! -f "$file" ] || [ ! -s "$file" ]; then + printf "timestamp${SEP}label${SEP}http_code\n" >>"$file" + fi +} + +stop_requested=0 +_on_exit() { + stop_requested=1 + printf "\nstop received. exit after current batch\n" >&2 +} +trap _on_exit INT TERM + +ensure_dirs + +printf "loop start. 60s. logs dir: %s\n" "$LOG_DIR" + +while [ "$stop_requested" -eq 0 ]; do + logfile="$(current_logfile)" + write_header_if_needed "$logfile" + + while IFS= read -r line; do + [ -z "$line" ] && continue + case "$line" in \#*) continue ;; esac + + IFS='|' read -r label url expected <<<"$line" + + ts="$(timestamp_iso)" + set +o errexit + code="$(curl -sS -o /dev/null -w "%{http_code}" --max-time 10 -L "$url" 2>/dev/null)" || code="000" + set -o errexit + + printf "%s%s%s%s%s\n" "$ts" "$SEP" "$label" "$SEP" "$code" >>"$logfile" + done <"$HOSTS_FILE" + + # interruptable sleep + waited=0 + while [ "$waited" -lt "$INTERVAL" ] && [ "$stop_requested" -eq 0 ]; do + sleep 1 + waited=$((waited+1)) + done +done + +printf "exit\n" \ No newline at end of file diff --git a/hosts.txt b/hosts.txt new file mode 100644 index 0000000..049ce80 --- /dev/null +++ b/hosts.txt @@ -0,0 +1,2 @@ +google|https://www.google.com|200 +Authentik-Tim|https://auth.st-server.org|200 diff --git a/logs/2025-11-10.csv b/logs/2025-11-10.csv new file mode 100644 index 0000000..b523ce6 --- /dev/null +++ b/logs/2025-11-10.csv @@ -0,0 +1,5 @@ +timestamp;label;http_code +2025-11-10T12:23:06+00:00;google;200 +2025-11-10T12:23:07+00:00;Authentik-Tim;503 +2025-11-10T12:24:08+00:00;google;200 +2025-11-10T12:24:08+00:00;Authentik-Tim;503