Memory: Report-Stats, Ablauf-Warnung, Cleanup-Skript

This commit is contained in:
root 2026-03-15 16:04:05 +07:00
parent 2c362b1009
commit a1618b37fe
2 changed files with 63 additions and 0 deletions

View file

@ -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()

View file

@ -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:")