[NIGHT] Остальные задачи: alert digest+analysis, memory/USER.md, onboarding, redaction watcher, fallback notice, task backlog, deep audit v2
This commit is contained in:
1 parent
4804dc6a75
commit
eed9381b2e
7 files changed
+859
-137
No files matched your search
Executable
+68
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Alert digest: новые алерты Alertmanager → Matrix (Секретарь-Бета)."""
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
AM = "http://192.168.1.134:9093/api/v2/alerts"
|
||||
ROOM = "!puEdcZCc87kKoCm3mX5bbjrTS-Skv2XCZg7JJBKOMw0"
|
||||
HS = "http://192.168.1.155:8008"
|
||||
STATE = "/var/lib/alert-digest/seen.json"
|
||||
|
||||
|
||||
def send(text):
|
||||
tok = open("/root/.matrix-admin-token").read().strip()
|
||||
txn = "alert-%d" % int(time.time() * 1000)
|
||||
body = json.dumps({"msgtype": "m.text", "body": text}).encode()
|
||||
url = "%s/_matrix/client/v3/rooms/%s/send/m.room.message/%s?access_token=%s" % (HS, ROOM, txn, tok)
|
||||
req = urllib.request.Request(url, data=body, headers={"Content-Type": "application/json"}, method="PUT")
|
||||
with urllib.request.urlopen(req, timeout=20) as r:
|
||||
return r.status
|
||||
|
||||
|
||||
def main():
|
||||
os.makedirs(os.path.dirname(STATE), exist_ok=True)
|
||||
try:
|
||||
seen = json.load(open(STATE))
|
||||
except Exception:
|
||||
seen = {}
|
||||
try:
|
||||
with urllib.request.urlopen(AM, timeout=15) as r:
|
||||
alerts = json.loads(r.read().decode())
|
||||
except Exception as e:
|
||||
print("alertmanager:", e)
|
||||
return 0
|
||||
new = []
|
||||
for a in alerts:
|
||||
fp = a.get("fingerprint") or (a.get("labels") or {}).get("alertname", "")
|
||||
if fp and fp not in seen:
|
||||
new.append(a)
|
||||
seen[fp] = int(time.time())
|
||||
# prune > 7d
|
||||
now = int(time.time())
|
||||
seen = {k: v for k, v in seen.items() if now - v < 7 * 86400}
|
||||
json.dump(seen, open(STATE, "w"))
|
||||
if not new:
|
||||
print("no new alerts")
|
||||
return 0
|
||||
lines = ["🚨 **Новые алерты** — " + time.strftime("%d.%m %H:%M")]
|
||||
for a in new[:10]:
|
||||
lb = a.get("labels") or {}
|
||||
an = a.get("annotations") or {}
|
||||
lines.append("• **%s** (%s): %s" % (lb.get("alertname"), lb.get("severity", "?"),
|
||||
an.get("summary") or an.get("description") or ""))
|
||||
if lb.get("instance"):
|
||||
lines.append(" instance: %s" % lb.get("instance"))
|
||||
if len(new) > 10:
|
||||
lines.append("… ещё %d" % (len(new) - 10))
|
||||
lines.append("\nСекретарь, проанализируй: причина, что проверить, риск и следующий шаг.")
|
||||
try:
|
||||
print("sent", send("\n".join(lines)))
|
||||
except Exception as e:
|
||||
print("matrix:", e)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user