Files
buryan-infra/ops/scripts/hub_briefing.py
T

162 lines
5.0 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""Утренний брифинг: одно сообщение вместо десяти заданий.
Собирает: службы, провалы заданий, события за ночь, диск, медиа, очередь
уведомлений. Отдаёт одним сообщением от секретаря в 08:00.
"""
import json
import os
import subprocess
import sys
import time
import urllib.request
HUB = "http://127.0.0.1:9120"
HERMES = "http://127.0.0.1:9130"
OUT = "/var/lib/hub-core/morning-briefing.txt"
def get(url, timeout=25):
try:
with urllib.request.urlopen(url, timeout=timeout) as r:
return json.loads(r.read() or b"{}")
except Exception:
return {}
def sh(cmd, t=30):
try:
return subprocess.run(cmd, shell=True, capture_output=True, text=True,
timeout=t).stdout.strip()
except Exception:
return ""
def services_block():
d = get(HUB + "/status.json", 30)
svc = d.get("services") or []
down = [s["name"] for s in svc if not s.get("up")]
if not svc:
return "• Службы: нет данных"
if not down:
return "• Службы: %d из %d работают" % (len(svc), len(svc))
return "• Службы: %d из %d, лежат: %s" % (len(svc) - len(down), len(svc),
", ".join(down[:5]))
def jobs_block():
d = get(HERMES + "/health")
jobs_fail = []
try:
import yaml
except Exception:
return "• Задания: нет данных"
out = []
return None # заполнится ниже через metrics
def jobs_failed():
met = sh("curl -s -m 8 %s/metrics" % HERMES)
bad = []
for line in met.splitlines():
if line.startswith("hermes_job_fails{") and not line.startswith("#"):
try:
n = float(line.rsplit(" ", 1)[1])
name = line.split('job="')[1].split('"')[0]
if n > 0:
bad.append((int(n), name))
except Exception:
pass
bad.sort(reverse=True)
if not bad:
return "• Задания: все без провалов"
return "• Задания: провалы — " + ", ".join("%s(%d)" % (n, c) for c, n in bad[:4])
def events_block():
d = get(HERMES + "/events?limit=200")
items = d.get("items") or []
cut = time.time() - 12 * 3600
night = [i for i in items if (i.get("ts") or 0) >= cut]
crit = [i for i in night if i.get("severity") in ("critical", "high")]
if not night:
return "• События: ночь прошла тихо"
shown = "%d" % len(night) if len(night) < 200 else "200+"
line = "• События за 12 ч: " + shown
if crit:
line += ", из них важных %d: %s" % (
len(crit), "; ".join((i.get("title") or "")[:40] for i in crit[:3]))
return line
def disk_block():
out = sh("df -h /mnt/media | tail -1 | awk '{print $5, $4}'")
parts = out.split()
if len(parts) == 2:
return "• Диск: занято %s, свободно %s" % (parts[0], parts[1])
return "• Диск: нет данных"
def media_block():
d = get(HUB + "/catalog/stats", 20)
if not d.get("total"):
return "• Медиа: нет данных"
stats = d.get("stats") or {}
return "• Медиа: %d карточек (Jellyfin %s, Radarr %s, Sonarr %s)" % (
d["total"], stats.get("jellyfin", 0), stats.get("radarr", 0),
stats.get("sonarr", 0))
def queue_block():
try:
q = json.load(open("/var/lib/hub-notify/queue.json", encoding="utf-8"))
except Exception:
q = []
if not q:
return None
groups = {}
for x in q:
t = (x.get("title") or "?")[:30]
groups[t] = groups.get(t, 0) + 1
top = sorted(groups.items(), key=lambda x: -x[1])[:3]
return "• Отложено уведомлений: %d (%s)" % (
len(q), ", ".join("%s×%d" % (t, n) for t, n in top))
def deliver(text):
try:
sys.path.insert(0, "/opt/buryan-hub/scripts")
import send_content
return send_content.send(text, "Утро")
except Exception as e:
print("доставка:", str(e)[:120])
return False
def main():
lines = ["Как дела на сервере:"]
for fn in (services_block, jobs_failed, events_block, disk_block, media_block):
try:
r = fn()
if r:
lines.append(r)
except Exception as e:
lines.append("• %s: ошибка %s" % (fn.__name__, str(e)[:60]))
try:
q = queue_block()
if q:
lines.append(q)
except Exception:
pass
text = "\n".join(lines)
os.makedirs(os.path.dirname(OUT), exist_ok=True)
open(OUT, "w", encoding="utf-8").write(text)
print(text)
ok = deliver(text)
print("доставлено:", ok)
return 0 if ok else 1
if __name__ == "__main__":
raise SystemExit(main())