From 127109ddf65182a96416734dc8540f9dd3c1fd51 Mon Sep 17 00:00:00 2001 From: alf Date: Fri, 27 Nov 2026 10:28:48 +0100 Subject: [PATCH] proxi-watch.py: externer 24/7-Watchdog fuer google-vm (WAN/TS-Checks, Alert via kasserver-Relay); Installationsanleitung in README --- README.md | 13 +++++++++ proxi-watch.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 proxi-watch.py diff --git a/README.md b/README.md index 59b9299..f46f9da 100644 --- a/README.md +++ b/README.md @@ -136,3 +136,16 @@ flowchart TB WAN -.->|"Port-Forward → vmbr0 → Gäste"| BR TSN -.->|"Gäste auch per Tailscale-IP erreichbar"| RUN ``` + +## Externer Watchdog (google-vm, 24/7) + +`proxi-watch.py` läuft alle 5 Min auf der Google-VM (100.87.87.104) und prüft von außen: WAN `https://92.209.221.170:8006` (erkennt Router-Hänger), Tailscale `https://100.87.87.100:8006`, Gitea `100.87.87.6:3000`. Bei Ausfall/Wiederkehr: Alert-Mail an `proxmox@wralto.org` via kasserver-Relay (465, SASL). SMTP-Passwort liegt lokal auf der VM in `/etc/proxi-watch.secret` (nicht im Repo). + +Installation auf der google-vm (GCE-Konsole, root): +``` +sudo curl -sL http://100.87.87.6:3000/mistral/doku/raw/branch/main/proxi-watch.py -o /usr/local/bin/proxi-watch.py +sudo chmod 755 /usr/local/bin/proxi-watch.py +sudo sh -c 'echo "" > /etc/proxi-watch.secret && chmod 600 /etc/proxi-watch.secret' +(crontab -l 2>/dev/null; echo "*/5 * * * * /usr/bin/python3 /usr/local/bin/proxi-watch.py") | sudo crontab - +sudo /usr/bin/python3 /usr/local/bin/proxi-watch.py # Testlauf +``` diff --git a/proxi-watch.py b/proxi-watch.py new file mode 100644 index 0000000..1a1a08a --- /dev/null +++ b/proxi-watch.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +"""Externer Watchdog fuer proxi — laeuft auf der immer online Google-VM (google-vm). +Prueft alle 5 Min von aussen: + - WAN https://92.209.221.170:8006 (oeffentliche IP, erkennt Router-Haenger/Portforward-Ausfall) + - TS https://100.87.87.100:8006 (proxi via Tailscale) + - TS tcp 100.87.87.6:3000 (Gitea-VM) +Alert bei Zustandswechsel per Mail an proxmox@wralto.org via kasserver-Relay (465 SSL, SASL PLAIN). +Cron auf der Google-VM: */5 * * * * /usr/bin/python3 /usr/local/bin/proxi-watch.py +""" +import os, socket, ssl, smtplib, time +from email.message import EmailMessage + +PUBLIC, TS_PROXI, TS_GITEA = "92.209.221.170", "100.87.87.100", "100.87.87.6" +SMTP_HOST, SMTP_PORT = "test90969.kasserver.com", 465 +SMTP_USER = "proxmox@wralto.org" +# Passwort kommt NICHT ins Repo: aus /etc/proxi-watch.secret oder Env PROXI_WATCH_SMTP_PASS +SMTP_PASS = os.environ.get("PROXI_WATCH_SMTP_PASS") or open("/etc/proxi-watch.secret").read().strip() +ALERT_TO = "proxmox@wralto.org" +STATE_FILE = "/var/tmp/proxi-watch.state" +LOG = "/var/log/proxi-watch.log" + +def check_https(host, port, timeout=10): + try: + ctx = ssl.create_default_context() + ctx.check_hostname = False + ctx.verify_mode = ssl.CERT_NONE + with socket.create_connection((host, port), timeout) as s: + with ctx.wrap_socket(s, server_hostname=host) as ss: + ss.sendall(f"GET / HTTP/1.0\r\nHost: {host}\r\n\r\n".encode()) + return ss.recv(200).startswith(b"HTTP/1.") + except Exception: + return False + +def check_tcp(host, port, timeout=8): + try: + socket.create_connection((host, port), timeout).close() + return True + except Exception: + return False + +def send_alert(subject, body): + msg = EmailMessage() + msg["From"] = SMTP_USER + msg["To"] = ALERT_TO + msg["Subject"] = subject + msg.set_content(body) + with smtplib.SMTP_SSL(SMTP_HOST, SMTP_PORT, timeout=30) as s: + s.login(SMTP_USER, SMTP_PASS) + s.send_message(msg) + +def main(): + checks = { + f"WAN https://{PUBLIC}:8006": check_https(PUBLIC, 8006), + f"TS https://{TS_PROXI}:8006": check_https(TS_PROXI, 8006), + f"TS tcp {TS_GITEA}:3000 (Gitea)": check_tcp(TS_GITEA, 3000), + } + failed = sorted(k for k, ok in checks.items() if not ok) + prev = [] + if os.path.exists(STATE_FILE): + for line in open(STATE_FILE): + if line.startswith("failed="): + prev = [x for x in line.strip()[7:].split(",") if x] + now = time.strftime("%F %T") + with open(LOG, "a") as f: + f.write(f"{now} checks={ {k: ok for k, ok in checks.items()} }\n") + if failed and failed != prev: + send_alert(f"PROXMOX WARN {now}", "Nicht erreichbar von google-vm:\n" + "\n".join(failed)) + elif not failed and prev: + send_alert(f"PROXMOX OK {now}", "Wieder erreichbar:\n" + "\n".join(prev)) + with open(STATE_FILE, "w") as f: + f.write("failed=" + ",".join(failed) + "\n") + +if __name__ == "__main__": + main()