From a1618b37fe30217dbf4e4e395ad3cada01537fbb Mon Sep 17 00:00:00 2001 From: root Date: Sun, 15 Mar 2026 16:04:05 +0700 Subject: [PATCH] Memory: Report-Stats, Ablauf-Warnung, Cleanup-Skript --- homelab-ai-bot/memory_cleanup.py | 31 +++++++++++++++++++++++++++++++ homelab-ai-bot/monitor.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 homelab-ai-bot/memory_cleanup.py diff --git a/homelab-ai-bot/memory_cleanup.py b/homelab-ai-bot/memory_cleanup.py new file mode 100644 index 00000000..1ada4717 --- /dev/null +++ b/homelab-ai-bot/memory_cleanup.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +"""Bereinigt abgelaufene temporaere Memory-Items. Laeuft als Cronjob.""" + +import sys +import os +import time +import logging + +sys.path.insert(0, os.path.dirname(__file__)) + +logging.basicConfig(format="%(asctime)s [memory-cleanup] %(message)s", level=logging.INFO) +log = logging.getLogger("memory-cleanup") + +import memory_client + + +def cleanup(): + items = memory_client.get_active_memory() + now_ts = int(time.time()) + archived = 0 + for item in items: + exp = item.get("expires_at") + if exp and exp < now_ts: + memory_client._patch(f"/memory/{item['id']}", {"status": "archived"}) + log.info("Archiviert: [%s] %s (abgelaufen)", item["id"], item["content"][:60]) + archived += 1 + log.info("Cleanup fertig: %d archiviert, %d aktiv", archived, len(items) - archived) + + +if __name__ == "__main__": + cleanup() diff --git a/homelab-ai-bot/monitor.py b/homelab-ai-bot/monitor.py index 7bdb08fd..2d898643 100644 --- a/homelab-ai-bot/monitor.py +++ b/homelab-ai-bot/monitor.py @@ -130,6 +130,20 @@ def check_all() -> list[str]: for r in restarts: alerts.append(f"🔄 Service-Neustart: {r['service']} auf {r['host']} ({r['count']}x in 35 Min)") + try: + import memory_client + import time as _time + now_ts = int(_time.time()) + mem_items = memory_client.get_active_memory() + for item in mem_items: + exp = item.get("expires_at") + if exp and 0 < exp - now_ts < 86400: + from datetime import datetime as _dt + exp_str = _dt.fromtimestamp(exp).strftime("%d.%m. %H:%M") + alerts.append(f"⏰ Memory läuft ab ({exp_str}): {item['content'][:80]}") + except Exception: + pass + try: mail_client.init(cfg) important = mail_client.get_important_mails(hours=1) @@ -187,6 +201,24 @@ def format_report() -> str: else: lines.append("Stille Hosts: keine") + try: + import memory_client + mem_items = memory_client.get_active_memory() + perm = [i for i in mem_items if i.get("memory_type") != "temporary"] + temp = [i for i in mem_items if i.get("memory_type") == "temporary"] + candidates = memory_client.get_candidates() + mem_line = f"Memory: {len(perm)} dauerhaft, {len(temp)} temporär" + import time as _time + now_ts = int(_time.time()) + soon = [i for i in temp if i.get("expires_at") and i["expires_at"] - now_ts < 86400] + if soon: + mem_line += f", {len(soon)} laufen in 24h ab" + if candidates: + mem_line += f", {len(candidates)} Kandidaten offen" + lines.append(mem_line) + except Exception: + pass + alerts = check_all() if alerts: lines.append(f"\n⚠️ {len(alerts)} aktive Alarme:")