[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
+112
-137
@@ -1,164 +1,139 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Полный read-only аудит Buryans Hub. Ничего не меняет — только читает.
|
||||
"""Глубокий аудит системы силами LLM (DeepSeek через VDS).
|
||||
|
||||
Запускается после того, как LLM-сбор закончит очередь (см. after-llm-audit.sh).
|
||||
Отчёт: /root/buryan-infra/ops/audits/<дата>-deep-audit.md
|
||||
Собирает досье (без секретов), отправляет в DeepSeek с VDS, сохраняет отчёт
|
||||
в ops/audits и присылает выжимку в Matrix.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from collections import Counter
|
||||
import urllib.request
|
||||
|
||||
OUTDIR = "/root/buryan-infra/ops/audits"
|
||||
DATE = time.strftime("%Y-%m-%d")
|
||||
OUT = os.path.join(OUTDIR, DATE + "-deep-audit.md")
|
||||
|
||||
SECTIONS = []
|
||||
ROOM = "!puEdcZCc87kKoCm3mX5bbjrTS-Skv2XCZg7JJBKOMw0"
|
||||
HS = "http://192.168.1.155:8008"
|
||||
VDS = "root@10.88.0.1"
|
||||
REPO = "/root/buryan-infra"
|
||||
|
||||
|
||||
def sh(cmd, timeout=90):
|
||||
def sh(cmd, timeout=60):
|
||||
r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=timeout)
|
||||
return ((r.stdout or "") + (r.stderr or "")).strip()
|
||||
|
||||
|
||||
def gateway_call(tool, args=None):
|
||||
try:
|
||||
r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=timeout)
|
||||
return (r.stdout or "") + (r.stderr or "")
|
||||
tok = open("/root/.tool-gateway-owner-token").read().strip()
|
||||
body = json.dumps({"tool": tool, "args": args or {}}).encode()
|
||||
req = urllib.request.Request("http://127.0.0.1:9140/call", data=body,
|
||||
headers={"Content-Type": "application/json",
|
||||
"X-Gateway-Token": tok}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=40) as r:
|
||||
d = json.loads(r.read().decode())
|
||||
return d.get("result") if d.get("ok") else d
|
||||
except Exception as e:
|
||||
return "ERR: %s" % str(e)[:80]
|
||||
return {"error": str(e)[:200]}
|
||||
|
||||
|
||||
def add(title, body):
|
||||
SECTIONS.append((title, body.rstrip()))
|
||||
def redact(s):
|
||||
s = re.sub(r"(?i)(token|key|password|passwd|secret)\s*[=:]\s*\S+", r"\1=[REDACTED]", s)
|
||||
s = re.sub(r"sk-[A-Za-z0-9]{10,}", "[REDACTED_KEY]", s)
|
||||
return s
|
||||
|
||||
|
||||
def host_health():
|
||||
up = sh("uptime")
|
||||
mem = sh("free -h | head -2")
|
||||
disk = sh("df -h / /var/lib/vz 2>/dev/null | grep -v Filesystem")
|
||||
load = sh("cat /proc/loadavg")
|
||||
top = sh("ps -eo pcpu,pmem,comm --sort=-pcpu | head -6")
|
||||
add("Хост: ресурсы", "```\n%s\n%s\n%s\nload: %s\n\ntop CPU:\n%s\n```"
|
||||
% (up.strip(), mem.strip(), disk.strip(), load.strip(), top.strip()))
|
||||
def build_context():
|
||||
parts = []
|
||||
parts.append("# Досье системы Buryans Hub (для аудита)\n")
|
||||
parts.append("## Дата\n" + time.strftime("%d.%m.%Y %H:%M") + "\n")
|
||||
parts.append("## Хост и контейнеры\n```\n" + sh("hostname; nproc; free -h | head -2; pct list | head -40") + "\n```\n")
|
||||
parts.append("## Hub status\n```\n" + json.dumps(gateway_call("hub.status"), ensure_ascii=False)[:3000] + "\n```\n")
|
||||
parts.append("## Tool Gateway: инструменты и схемы\n```\n" + sh("grep -E '^ \"[a-z_.]+\": \\{\"fn\"|^TOOLS =|scope' /root/buryan-infra/ops/tools/gateway.py | head -80") + "\n```\n")
|
||||
parts.append("## Аудит Tool Gateway (7 дней)\n```\n" + sh('''SUP=$(cat /root/.pg-superuser-pass); PGPASSWORD="$SUP" psql -w -h 192.168.1.151 -U postgres -d hermes -t -A -F'|' -c "select tool,count(*),count(*) filter(where not ok),coalesce(round(avg(ms)),0) from tool_audit where ts>now()-interval '7 days' group by tool order by count(*) desc limit 25;"''') + "\n```\n")
|
||||
parts.append("## Hermes jobs\n```\n" + sh("sed -n '1,200p' /opt/hermes/jobs.yaml") + "\n```\n")
|
||||
parts.append("## OpenClaw config (redacted)\n```\n" + redact(sh("pct exec 115 -- cat /opt/openclaw/openclaw.json"))[:6000] + "\n```\n")
|
||||
parts.append("## OpenClaw workspace policy\n```\n" + sh("pct exec 115 -- tail -40 /opt/openclaw/workspace/AGENTS.md 2>/dev/null") + "\n```\n")
|
||||
parts.append("## Eval baseline\n```\n" + sh("cat /var/lib/secretary-eval/summary.md 2>/dev/null; echo; tail -5 /root/buryan-infra/ops/audits/EVAL-FINAL-2026-11-17.md 2>/dev/null") + "\n```\n")
|
||||
parts.append("## Известные инциденты/документы\n```\n" + sh("ls /root/buryan-infra/ops/audits/ 2>/dev/null | tail -15") + "\n```\n")
|
||||
parts.append("## Репозиторий (структура)\n```\n" + sh("cd /root/buryan-infra && find ops -maxdepth 2 -type d | sort | head -40") + "\n```\n")
|
||||
return redact("\n".join(parts))[:60000]
|
||||
|
||||
|
||||
def services():
|
||||
failed = sh("systemctl list-units --state=failed --no-pager --no-legend")
|
||||
timers = sh("systemctl list-timers --no-pager --no-legend | head -12")
|
||||
add("Сбои systemd", "```\n%s\n```" % (failed.strip() or "нет сбоев"))
|
||||
add("Таймеры (первые 12)", "```\n%s\n```" % timers.strip())
|
||||
VDS_SCRIPT = r'''#!/usr/bin/env python3
|
||||
import json, sys, urllib.request
|
||||
key = open("/tmp/dskey").read().strip()
|
||||
ctx = open("/tmp/deep-context.md", encoding="utf-8").read()
|
||||
system = ("Ты — независимый аудитор домашней инфраструктуры Buryans Hub. "
|
||||
"Цель: максимально глубокий аудит для дальнейшей работы AI-секретаря (OpenClaw). "
|
||||
"Не выдумывай факты; если данных нет — пиши 'неизвестно'. "
|
||||
"Отвечай на русском, структурированно, без воды.")
|
||||
user = """Проведи максимально глубокий аудит системы по досье ниже.
|
||||
Разделы:
|
||||
1. Архитектура и границы ролей (hub-core, Hermes, OpenClaw, n8n, HA).
|
||||
2. Данные, память, приватность (что можно/нельзя в облако).
|
||||
3. Инструменты/MCP: чего не хватает секретарю, какие опасны, как выдавать права.
|
||||
4. Безопасность: prompt injection, секреты, approvals, sandbox, аудит.
|
||||
5. Надёжность: state-lock OpenClaw, долгие turn'ы, fallback, наблюдаемость.
|
||||
6. UX семьи: онбординг, каналы, проактивность, память о людях.
|
||||
7. LLM/стоимость: локальная vs DeepSeek, router, кэш, качество.
|
||||
8. Конкретная дорожная карта P0/P1/P2 на 2-4 недели.
|
||||
9. Список задач с критериями готовности.
|
||||
В конце — 5 главных рисков и 5 главных возможностей.
|
||||
|
||||
ДОСЬЕ:
|
||||
""" + ctx
|
||||
body = {"model": "deepseek-chat", "temperature": 0.2, "max_tokens": 8000,
|
||||
"messages": [{"role": "system", "content": system}, {"role": "user", "content": user}]}
|
||||
req = urllib.request.Request("https://api.deepseek.com/chat/completions",
|
||||
data=json.dumps(body).encode(),
|
||||
headers={"Content-Type": "application/json", "Authorization": "Bearer " + key},
|
||||
method="POST")
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=300) as r:
|
||||
d = json.loads(r.read().decode())
|
||||
out = d["choices"][0]["message"]["content"]
|
||||
except Exception as e:
|
||||
out = "# Ошибка аудита\n\n" + str(e)[:1000]
|
||||
open("/tmp/deep-report.md", "w", encoding="utf-8").write(out)
|
||||
print("report chars:", len(out))
|
||||
'''
|
||||
|
||||
|
||||
def containers():
|
||||
rows = []
|
||||
for ct in sh("pct list | awk 'NR>1{print $1}'").split():
|
||||
cnt = sh("pct exec %s -- sh -c 'docker ps -q 2>/dev/null | wc -l'" % ct, 30).strip()
|
||||
if cnt and cnt.isdigit() and int(cnt) > 0:
|
||||
names = sh("pct exec %s -- sh -c \"docker ps --format '{{.Names}}' 2>/dev/null | tr '\\n' ','\"" % ct, 30)
|
||||
rows.append("CT%s: %s контейнеров — %s" % (ct, cnt, names.strip()[:120]))
|
||||
add("Контейнеры по CT", "```\n%s\n```" % ("\n".join(rows) or "нет данных"))
|
||||
|
||||
|
||||
def journal_errors():
|
||||
counts = Counter()
|
||||
for unit in sh("systemctl list-units --type=service --state=running --no-legend | awk '{print $1}'").split():
|
||||
n = sh("journalctl -u %s --since '7 days ago' --no-pager -p err 2>/dev/null | wc -l" % unit, 30).strip()
|
||||
if n.isdigit() and int(n) > 0:
|
||||
counts[unit] = int(n)
|
||||
top = "\n".join("%-40s %d" % (u, n) for u, n in counts.most_common(15))
|
||||
add("Ошибки (prio err) за 7 дней по службам", "```\n%s\n```" % (top or "ошибок нет"))
|
||||
|
||||
|
||||
def error_patterns():
|
||||
raw = sh("journalctl --since '7 days ago' --no-pager -p err 2>/dev/null | head -4000")
|
||||
pats = Counter()
|
||||
for line in raw.splitlines():
|
||||
m = re.sub(r"\d+", "N", line.split(" ", 5)[-1] if len(line.split(" ", 5)) > 5 else line)
|
||||
pats[m.strip()[:110]] += 1
|
||||
top = "\n".join("%4d %s" % (n, p) for p, n in pats.most_common(20))
|
||||
add("Топ повторяющихся ошибок (7 дней)", "```\n%s\n```" % (top or "нет"))
|
||||
|
||||
|
||||
def disk_hogs():
|
||||
logs = sh("du -sh /var/log /var/lib/vz /root/.cache 2>/dev/null")
|
||||
journal = sh("journalctl --disk-usage 2>/dev/null")
|
||||
big = sh("du -ahx / 2>/dev/null | sort -rh | head -12")
|
||||
add("Место: где занято", "```\n%s\n%s\n\nкрупнейшее:\n%s\n```"
|
||||
% (logs.strip(), journal.strip(), big.strip()))
|
||||
|
||||
|
||||
def backup_status():
|
||||
b = sh("ls -la /var/backups 2>/dev/null | tail -8")
|
||||
dz = sh("find / -maxdepth 3 -name '*.dump' -o -maxdepth 3 -name '*.sql.gz' 2>/dev/null | head -5")
|
||||
add("Бэкапы: свежесть", "```\n%s\n\nдампы:\n%s\n```" % (b.strip(), dz.strip() or "не найдены"))
|
||||
|
||||
|
||||
def network():
|
||||
wg = sh("wg show 2>/dev/null | head -12")
|
||||
ports = sh("ss -tlnp 2>/dev/null | awk '{print $4}' | sort -u | head -25")
|
||||
dns = sh("cat /etc/resolv.conf | grep -v '^#' | head -5")
|
||||
add("Сеть: VPN, порты, DNS", "```\n%s\n\nпорты:\n%s\n\nDNS:\n%s\n```"
|
||||
% (wg.strip() or "wg нет", ports.strip(), dns.strip()))
|
||||
|
||||
|
||||
def hygiene():
|
||||
dup = sh("systemctl list-unit-files --type=service --no-pager --no-legend | awk '{print $1}'"
|
||||
" | sed 's/@.*//' | sort | uniq -d | head -10")
|
||||
cron = sh("crontab -l 2>/dev/null | grep -c '^[^#]'")
|
||||
jobs = sh("grep -c '^[a-z0-9-]*:' /opt/hermes/jobs.yaml 2>/dev/null")
|
||||
old = sh("find /root /opt -maxdepth 2 -name '*.bak*' -o -maxdepth 2 -name '*.old' 2>/dev/null | head -10")
|
||||
pycache = sh("find /opt -name '__pycache__' -maxdepth 3 2>/dev/null | wc -l")
|
||||
add("Гигиена: дубли, кроны, мусор", "```\nдубли unit-файлов:\n%s\n\n"
|
||||
"crontab строк: %s | заданий hermes: %s\n\nстарые копии:\n%s\n\n__pycache__: %s\n```"
|
||||
% (dup.strip() or "нет", cron.strip(), jobs.strip(),
|
||||
old.strip() or "нет", pycache.strip()))
|
||||
|
||||
|
||||
def security():
|
||||
certs = sh("find /etc/letsencrypt/live -name fullchain.pem -maxdepth 2 2>/dev/null | while read f; do"
|
||||
" e=$(openssl x509 -enddate -noout -in \"$f\" 2>/dev/null | cut -d= -f2); echo \"$f | $e\"; done | head -10")
|
||||
fail = sh("journalctl --since '7 days ago' --no-pager 2>/dev/null | grep -ciE 'failed password|invalid user'")
|
||||
add("Безопасность: сертификаты, попытки входа", "```\n%s\n\nнеудачных входов за 7 дней: %s\n```"
|
||||
% (certs.strip() or "не найдено", (fail or "0").strip()))
|
||||
|
||||
|
||||
def llm_state():
|
||||
disp = sh("systemctl is-active llm-dispatcher")
|
||||
log = sh("tail -5 /var/log/llm-dispatch.log 2>/dev/null")
|
||||
prog = sh("ls /opt/work-hub/research/progress-*.json 2>/dev/null | wc -l")
|
||||
add("LLM-сбор: состояние", "```\nдиспетчер: %s\nфайлов прогресса: %s\n\n%s\n```"
|
||||
% (disp.strip(), prog.strip(), log.strip()))
|
||||
def send_matrix(text):
|
||||
tok = open("/root/.matrix-admin-token").read().strip()
|
||||
txn = "deepaudit-%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(OUTDIR, exist_ok=True)
|
||||
t0 = time.time()
|
||||
for fn in (host_health, services, containers, journal_errors, error_patterns,
|
||||
disk_hogs, backup_status, network, hygiene, security, llm_state):
|
||||
try:
|
||||
fn()
|
||||
except Exception as e:
|
||||
add(fn.__name__, "ОШИБКА аудита: %s" % str(e)[:120])
|
||||
|
||||
body = ["# Глубокий аудит Buryans Hub — %s" % DATE,
|
||||
"",
|
||||
"Только чтение. Ничего не менялось.",
|
||||
"",
|
||||
"Итог: разделов %d, собрано за %.0f с." % (len(SECTIONS), time.time() - t0),
|
||||
""]
|
||||
for title, text in SECTIONS:
|
||||
body += ["## " + title, "", text, ""]
|
||||
open(OUT, "w", encoding="utf-8").write("\n".join(body))
|
||||
print("отчёт:", OUT, "| разделов:", len(SECTIONS), "| %.0f с" % (time.time() - t0))
|
||||
|
||||
# короткая сводка в уведомление
|
||||
summary = []
|
||||
for title, text in SECTIONS:
|
||||
if title.startswith("Сбои systemd") and "нет сбоев" not in text:
|
||||
summary.append("есть сбои служб")
|
||||
if title.startswith("Ошибки") and "ошибок нет" not in text:
|
||||
summary.append("ошибки в журнале")
|
||||
if summary:
|
||||
print("внимание:", ", ".join(summary))
|
||||
ctx = build_context()
|
||||
open("/tmp/deep-context.md", "w", encoding="utf-8").write(ctx)
|
||||
open("/tmp/deep_audit_vds.py", "w", encoding="utf-8").write(VDS_SCRIPT)
|
||||
print("context chars:", len(ctx))
|
||||
# ключ и контекст на VDS (временно)
|
||||
sh("scp -q -o BatchMode=yes /root/.deepseek-key %s:/tmp/dskey" % VDS)
|
||||
sh("scp -q -o BatchMode=yes /tmp/deep-context.md /tmp/deep_audit_vds.py %s:/tmp/" % VDS)
|
||||
sh("ssh -o BatchMode=yes %s 'chmod 600 /tmp/dskey && python3 /tmp/deep_audit_vds.py'" % VDS, timeout=600)
|
||||
sh("scp -q -o BatchMode=yes %s:/tmp/deep-report.md /tmp/deep-report.md" % VDS)
|
||||
sh("ssh -o BatchMode=yes %s 'rm -f /tmp/dskey /tmp/deep-context.md /tmp/deep_audit_vds.py'" % VDS)
|
||||
report = open("/tmp/deep-report.md", encoding="utf-8").read() if os.path.exists("/tmp/deep-report.md") else "# Ошибка\n"
|
||||
ts = time.strftime("%Y%m%d-%H%M")
|
||||
dst = "%s/ops/audits/DEEP-AUDIT-%s.md" % (REPO, ts)
|
||||
header = "# Глубокий аудит Buryans Hub (LLM, %s)\n\n" % time.strftime("%d.%m.%Y %H:%M")
|
||||
open(dst, "w", encoding="utf-8").write(header + report)
|
||||
print("saved:", dst)
|
||||
summary = report[:1800]
|
||||
try:
|
||||
send_matrix("🔍 **Глубокий аудит готов** (%s)\n\n%s\n\n…полный отчёт: `%s`" % (ts, summary, dst))
|
||||
except Exception as e:
|
||||
print("matrix failed:", e)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user