[AUDIT] Глубокий read-only аудит после LLM: скрипт deep_audit.py (12 разделов) + after-llm-audit.sh, запуск только когда очередь LLM пуста (Hermes каждые 30 мин, раз в сутки). Первый прогон: hub-db-dumps, hub-rag, hub-cert-sync в failed; память 12/15 ГБ + swap 17 ГБ (kswapd, task blocked); NVMe Critical Warning Temperature; 99 записей write access unavailable. Плюс гигиена: 49 заданий Hermes, старые .bak, порты NFS наружу. Миграция прогресса LLM: 25778 записей слиты, остаток 344 вместо 26055
This commit is contained in:
1 parent
ca9f1e55af
commit
201f403797
5 files changed
+640
No files matched your search
Executable
+70
@@ -0,0 +1,70 @@
|
||||
#!/bin/bash
|
||||
# Аудит запускается ТОЛЬКО когда LLM-сбор закончил очередь.
|
||||
# Ничего не меняет: только читает и пишет отчёт.
|
||||
set -u
|
||||
LOG=/var/log/deep-audit.log
|
||||
DONE=/var/lib/deep-audit.last
|
||||
TODAY=$(date +%F)
|
||||
|
||||
exec >>"$LOG" 2>&1
|
||||
|
||||
# уже делали сегодня?
|
||||
if [ -f "$DONE" ] && [ "$(cat "$DONE")" = "$TODAY" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# очередь LLM пуста? считаем незавершённые адреса по всем очередям
|
||||
python3 - <<'EOF'
|
||||
import glob, json, os, sys
|
||||
|
||||
OUT = "/opt/work-hub/research"
|
||||
pending = 0
|
||||
detail = []
|
||||
for q in glob.glob(os.path.join(OUT, "queue*.jsonl")):
|
||||
try:
|
||||
items = [json.loads(l) for l in open(q, encoding="utf-8") if l.strip()]
|
||||
except Exception:
|
||||
continue
|
||||
base = os.path.basename(q).replace("queue-", "").replace("queue", "yealink").replace(".jsonl", "")
|
||||
# файл прогресса: progress-<vendor>[-tag]-0.json
|
||||
prog_files = glob.glob(os.path.join(OUT, "progress-%s*-0.json" % base))
|
||||
done = set()
|
||||
for pf in prog_files:
|
||||
try:
|
||||
done |= {k for k, v in json.load(open(pf, encoding="utf-8")).items()
|
||||
if v in ("ok", "empty", "skip")}
|
||||
except Exception:
|
||||
pass
|
||||
left = [i for i in items if i["url"] not in done]
|
||||
if left:
|
||||
pending += len(left)
|
||||
detail.append("%s:%d" % (base, len(left)))
|
||||
print("PENDING=%d %s" % (pending, " ".join(detail)))
|
||||
sys.exit(0 if pending == 0 else 1)
|
||||
EOF
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "[$(date '+%F %T')] LLM ещё работает — аудит отложен"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "[$(date '+%F %T')] LLM закончил — запускаю глубокий аудит"
|
||||
/opt/work-hub/venv/bin/python /root/buryan-infra/ops/scripts/deep_audit.py
|
||||
RC=$?
|
||||
if [ $RC -eq 0 ]; then
|
||||
echo "$TODAY" > "$DONE"
|
||||
/opt/work-hub/venv/bin/python - <<'EOF'
|
||||
import json, urllib.request as u
|
||||
msg = ("🧾 Глубокий аудит готов (LLM закончил сбор).\n"
|
||||
"Отчёт: /root/buryan-infra/ops/audits/ — только чтение, ничего не менялось.")
|
||||
body = json.dumps({"title": "Аудит", "message": msg, "priority": "normal"}).encode()
|
||||
req = u.Request("http://127.0.0.1:9120/notify", data=body,
|
||||
headers={"Content-Type": "application/json"})
|
||||
try:
|
||||
u.urlopen(req, timeout=10).read()
|
||||
except Exception:
|
||||
pass
|
||||
EOF
|
||||
fi
|
||||
echo "[$(date '+%F %T')] аудит завершён с кодом $RC"
|
||||
exit $RC
|
||||
Executable
+164
@@ -0,0 +1,164 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Полный read-only аудит Buryans Hub. Ничего не меняет — только читает.
|
||||
|
||||
Запускается после того, как LLM-сбор закончит очередь (см. after-llm-audit.sh).
|
||||
Отчёт: /root/buryan-infra/ops/audits/<дата>-deep-audit.md
|
||||
"""
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from collections import Counter
|
||||
|
||||
OUTDIR = "/root/buryan-infra/ops/audits"
|
||||
DATE = time.strftime("%Y-%m-%d")
|
||||
OUT = os.path.join(OUTDIR, DATE + "-deep-audit.md")
|
||||
|
||||
SECTIONS = []
|
||||
|
||||
|
||||
def sh(cmd, timeout=90):
|
||||
try:
|
||||
r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=timeout)
|
||||
return (r.stdout or "") + (r.stderr or "")
|
||||
except Exception as e:
|
||||
return "ERR: %s" % str(e)[:80]
|
||||
|
||||
|
||||
def add(title, body):
|
||||
SECTIONS.append((title, body.rstrip()))
|
||||
|
||||
|
||||
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 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())
|
||||
|
||||
|
||||
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 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))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user