60 lines
2.0 KiB
Python
Executable File
60 lines
2.0 KiB
Python
Executable File
#!/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())
|