kitestacks-homelab/apps/forgejo/uptime-kuma/scripts/kitestacks_uptimecheck.py

32 lines
1.2 KiB
Python
Executable file

#!/usr/bin/env python3
import json
import requests
import subprocess
# Load the monitors JSON
with open("/scripts/kitestacks-monitors.json", "r") as f:
monitors = json.load(f)
for monitor in monitors:
name = monitor.get("name")
mtype = monitor.get("type")
try:
if mtype == "http":
url = monitor.get("url")
method = monitor.get("method", "GET").upper()
resp = requests.request(method, url, timeout=10, verify=False)
print(f"[{name}] HTTP {method} {url} -> Status {resp.status_code}")
elif mtype == "tcp":
host = monitor.get("host")
port = monitor.get("port")
result = subprocess.run(["nc", "-zvw3", host, str(port)], capture_output=True)
print(f"[{name}] TCP {host}:{port} -> Returncode {result.returncode}")
elif mtype == "ping":
host = monitor.get("host")
result = subprocess.run(["ping", "-c", "1", host], capture_output=True)
print(f"[{name}] Ping {host} -> Returncode {result.returncode}")
else:
print(f"[{name}] Unknown type: {mtype}")
except Exception as e:
print(f"[{name}] Error: {e}")