Commit b9916880babcc9b9cd745a28619334c76600398e
Commits[COMMIT BEGIN]commit b9916880babcc9b9cd745a28619334c76600398e Author: 0x4248 <[email protected]> Date: Tue Feb 3 12:05:50 2026 +0000 PulseWatch/server: add get funtions diff --git a/usr/net/PulseWatch/server.py b/usr/net/PulseWatch/server.py index 0653f8d..4a2ce94 100644 --- a/usr/net/PulseWatch/server.py +++ b/usr/net/PulseWatch/server.py @@ -11,6 +11,13 @@ os.makedirs(DATA_DIR, exist_ok=True) lock = threading.Lock() +def load_json(path): + if not os.path.exists(path): + return {} + with open(path, "r") as f: + return json.load(f) + + def store_stats(host, payload): path = os.path.join(DATA_DIR, f"{host}.json") ts = str(int(time.time())) @@ -69,25 +76,67 @@ class Handler(BaseHTTPRequestHandler): self.wfile.write(b"OK") def do_GET(self): - if not self.path.startswith("/ping"): - self.send_error(404) + parsed = urlparse(self.path) + + # --- ping endpoint (unchanged) --- + if parsed.path == "/ping": + qs = parse_qs(parsed.query) + ts = qs.get("ts", [None])[0] + ping_id = qs.get("id", [None])[0] + + if ts is None or ping_id is None: + self.send_error(400) + return + + self.send_response(200) + self.send_header("Content-Type", "application/json") + self.end_headers() + self.wfile.write(json.dumps({ + "ts": ts, + "ping_id": ping_id + }).encode()) return - qs = parse_qs(urlparse(self.path).query) - ts = qs.get("ts", [None])[0] - ping_id = qs.get("id", [None])[0] + # --- list hosts --- + if parsed.path == "/data/hosts": + hosts = [] + for f in os.listdir(DATA_DIR): + if f.endswith(".json") and not f.endswith("_pings.json"): + hosts.append(f.replace(".json", "")) + + self.send_response(200) + self.send_header("Content-Type", "application/json") + self.end_headers() + self.wfile.write(json.dumps(hosts).encode()) + return - if ts is None or ping_id is None: - self.send_error(400) + # --- per-host data --- + if parsed.path.startswith("/data/host/"): + parts = parsed.path.split("/") + if len(parts) < 4: + self.send_error(404) + return + + host = parts[3] + is_ping = len(parts) == 5 and parts[4] == "pings" + + fname = ( + f"{host}_pings.json" + if is_ping + else f"{host}.json" + ) + + path = os.path.join(DATA_DIR, fname) + data = load_json(path) + + self.send_response(200) + self.send_header("Content-Type", "application/json") + self.end_headers() + self.wfile.write(json.dumps(data).encode()) return - self.send_response(200) - self.send_header("Content-Type", "application/json") - self.end_headers() - self.wfile.write(json.dumps({ - "ts": ts, - "ping_id": ping_id - }).encode()) + self.send_error(404) + def log_message(self, *_): pass[COMMIT END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.