#!/usr/bin/env python3 """Единая утренняя сводка (пункт 1, 16.11.2026). Заменяет собой разрозненные утренние сообщения: состояние служб, бэкапы, ночной разбор журналов, задачи владельца, заявки на подтверждение. Одно сообщение в ALERT в 08:00. Источники: * /status.json хаба — службы; * systemctl --failed — упавшие юниты; * /var/lib/vz/dump, second_contour.log, ops/audit/snapshot-* — бэкапы; * /var/log/hub/log-digest-YYYY-MM-DD.md — ночной разбор (Фаза 3); * /opt/work-hub/work.db — открытые задачи; * approvals.py list — заявки на подтверждение. Запуск: python3 morning_digest.py # собрать и отправить python3 morning_digest.py --dry-run # только напечатать """ import glob import json import os import re import sqlite3 import subprocess import sys from datetime import date, datetime, timedelta HUB = "http://127.0.0.1:9120" NOTIFY = "/opt/buryan-hub/scripts/notify.py" WORK_DB = "/opt/work-hub/work.db" OUT = "/var/log/hub/morning-digest-%s.md" % date.today().isoformat() APPROVALS = "/opt/music-tools/venv/bin/python" def sh(cmd, timeout=60): try: r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=timeout) return (r.stdout or "").strip() except Exception: return "" def services(): try: import urllib.request as u with u.urlopen(HUB + "/status.json", timeout=15) as r: d = json.loads(r.read()) except Exception: return "• Службы: хаб не отвечает", [] svc = d.get("services") or [] down = [s.get("name") for s in svc if not s.get("up")] if down: return "• Службы: %d из %d, лежат: %s" % (len(svc) - len(down), len(svc), ", ".join(down[:6])), down return "• Службы: %d из %d работают" % (len(svc), len(svc)), down def failed_units(): n = sh("systemctl --failed --no-legend --no-pager | wc -l") if not n or n == "0": return "• Упавших юнитов: нет" names = sh("systemctl --failed --no-legend --no-pager | awk '{print $2}' | head -4") return "• Упавшие юниты: %s (%s)" % (n, names.replace("\n", ", ")) def backups(): lines = [] dump = sh("ls -t /var/lib/vz/dump/*.tar.zst 2>/dev/null | head -1") if dump: age = (datetime.now().timestamp() - os.path.getmtime(dump)) / 3600 lines.append("дамп CT %s (%.0f ч)" % (os.path.basename(dump).split("-")[2], age)) sn = sorted(glob.glob("/root/buryan-infra/ops/audit/snapshot-*.md")) if sn: lines.append("snapshot %s" % os.path.basename(sn[-1]).replace("snapshot-", "").replace(".md", "")) ver = sh("grep -h 'ИТОГ: баз' /var/log/buryan-hub/backups/verify-backups.log 2>/dev/null | tail -1") if ver: lines.append(re.sub(r"^\[[^]]+\]\s*", "", ver)) if not lines: return "• Бэкапы: свежих данных нет" return "• Бэкапы: " + "; ".join(lines) def log_digest(): p = "/var/log/hub/log-digest-%s.md" % date.today().isoformat() if not os.path.exists(p): return "• Ночной разбор: отчёта нет" txt = open(p, encoding="utf-8").read() head = "" m = re.search(r"\*\*(.+?)\*\*", txt) if m: head = m.group(1).strip() counts = "" m2 = re.search(r"новых:\s*(\d+)\s*\|\s*всплесков:\s*(\d+)\s*\|\s*корреляций:\s*(\d+)", txt) if m2: counts = "новых %s, всплесков %s, корреляций %s" % m2.groups() if not head: return "• Ночной разбор: %s" % (counts or "без находок") return "• Ночной разбор: %s%s" % (head, (" (" + counts + ")") if counts else "") def tasks(): try: c = sqlite3.connect("file:%s?mode=ro" % WORK_DB, uri=True) rows = c.execute("select id, text, due from items where status != 'done' " "order by id desc limit 5").fetchall() c.close() except Exception as e: return "• Задачи: база недоступна (%s)" % str(e)[:40] if not rows: return "• Задачи: открытых нет" items = [] for i, text, due in rows[:3]: t = re.sub(r"\s+", " ", (text or ""))[:60] items.append("#%s %s%s" % (i, t, (" (до %s)" % due) if due else "")) return "• Задачи (%d): %s" % (len(rows), " · ".join(items)) def approvals(): out = sh("%s /opt/hub-agents/approvals.py list 2>/dev/null" % APPROVALS) if not out or "заявок нет" in out: return "• Заявки на подтверждение: нет" lines = [ln for ln in out.splitlines() if ln.startswith("#")] return "• Заявки: %d (ждёт «одобри N»): %s" % (len(lines), "; ".join(lines[:2])[:160]) def errors_night(): n = sh("journalctl --since '-24h' -p err --no-pager 2>/dev/null | wc -l") return "• Ошибок в журнале за сутки: %s" % (n or "?") def main(): dry = "--dry-run" in sys.argv sline, _ = services() parts = ["🌅 Утренняя сводка — %s" % date.today().strftime("%d.%m.%Y"), "", sline, failed_units(), backups(), log_digest(), tasks(), approvals(), errors_night()] text = "\n".join(parts) print(text) try: os.makedirs("/var/log/hub", exist_ok=True) open(OUT, "w", encoding="utf-8").write(text + "\n") except Exception: pass if dry: return 0 r = subprocess.run([sys.executable, NOTIFY, "--priority", "high", "--title", "Утренняя сводка", text], capture_output=True, text=True, timeout=180) print("\nотправка:", (r.stdout or r.stderr).strip()[:100]) return 0 if __name__ == "__main__": sys.exit(main())