proxi-watch.py: externer 24/7-Watchdog fuer google-vm (WAN/TS-Checks, Alert via kasserver-Relay); Installationsanleitung in README
This commit is contained in:
74
proxi-watch.py
Normal file
74
proxi-watch.py
Normal file
@ -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()
|
||||
Reference in New Issue
Block a user