diff --git a/ops/hermes/jobs.yaml b/ops/hermes/jobs.yaml index 15d89e5..ad9b270 100644 --- a/ops/hermes/jobs.yaml +++ b/ops/hermes/jobs.yaml @@ -53,3 +53,8 @@ swarm-watchdog: every_seconds: 60 timeout: 120 retries: 0 +audit-targets: + command: /usr/bin/python3 /opt/buryan-hub/scripts/audit-targets.py + daily_at: "07:15" + timeout: 300 + retries: 0 diff --git a/ops/scripts/audit-targets.py b/ops/scripts/audit-targets.py new file mode 100755 index 0000000..c9ee299 --- /dev/null +++ b/ops/scripts/audit-targets.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +"""Ищет МЁРТВЫЕ цели мониторинга. + +Мёртвая цель = адрес упомянут в проверках/реестрах, но не отвечает вовсе (код 000). +Именно так выглядит «порт съехал»: служба жива, а проверка смотрит на старый порт +(реальный случай: n8n 5678 в копии check-services, при живом 5680). + +Любой ответ (200/401/404/500) считается живым: важно, что хост и порт на месте. +""" +import concurrent.futures as cf +import os +import re +import subprocess +import sys + +SRC = [ + "/opt/buryan-hub/scripts/check-services.sh", + "/opt/hub-core/reconcile.py", + "/opt/hub-core/selfheal.py", + "/opt/hub-core/services.yaml", +] + +# Известные и намеренно отключённые адреса — впишите сюда с датой и причиной +ALLOW_DEAD = set() + + +def collect(): + urls = {} + for p in SRC: + if not os.path.exists(p): + continue + t = open(p, encoding="utf-8", errors="ignore").read() + for m in re.finditer(r"https?://[A-Za-z0-9_.\-]+(?::\d+)?[^\s\"')<>]*", t): + urls.setdefault(m.group(0).rstrip(".,;"), os.path.basename(p)) + return urls + + +def probe(u): + r = subprocess.run(["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}", + "-k", "--max-time", "6", u], capture_output=True, text=True) + return u, (r.stdout.strip() or "000") + + +def main(): + urls = collect() + dead = [] + with cf.ThreadPoolExecutor(max_workers=24) as ex: + for u, code in ex.map(probe, sorted(urls)): + if code == "000" and u not in ALLOW_DEAD: + dead.append(u) + print("проверено адресов: %d, мёртвых: %d" % (len(urls), len(dead))) + for u in dead: + print(" мёртв:", u) + return 1 if dead else 0 + + +if __name__ == "__main__": + sys.exit(main())