82 lines
3.1 KiB
Python
Executable File
82 lines
3.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
||
"""Монитор пути до VDS: трассировка по TCP/443 + коннекты на порты.
|
||
|
||
Пишет JSONL: /var/lib/path-monitor/<дата>.jsonl
|
||
Каждые 30 с: полный список хопов с RTT и состояние портов VDS.
|
||
"""
|
||
import json
|
||
import os
|
||
import re
|
||
import subprocess
|
||
import time
|
||
|
||
OUT = "/var/lib/path-monitor"
|
||
VDS = "193.233.210.184"
|
||
INTERVAL = 30
|
||
|
||
|
||
def tcp(host, port=443, timeout=5):
|
||
t0 = time.time()
|
||
r = subprocess.run("timeout %d bash -c 'echo > /dev/tcp/%s/%d'" % (timeout, host, port),
|
||
shell=True, capture_output=True, text=True, timeout=timeout + 5)
|
||
return (r.returncode == 0, round((time.time() - t0) * 1000, 1))
|
||
|
||
|
||
def traceroute():
|
||
"""Список (хоп, ip, rtt) через TCP-трассировку."""
|
||
try:
|
||
r = subprocess.run(["traceroute", "-n", "-T", "-p", "443", "-m", "14", "-w", "2", VDS],
|
||
capture_output=True, text=True, timeout=110)
|
||
hops = []
|
||
for line in (r.stdout or "").splitlines():
|
||
m = re.match(r"^\s*(\d+)\s+(.*)$", line)
|
||
if not m:
|
||
continue
|
||
num = int(m.group(1))
|
||
rest = m.group(2)
|
||
ip = re.search(r"(\d+\.\d+\.\d+\.\d+)", rest)
|
||
rtt = re.search(r"([\d.]+)\s*ms", rest)
|
||
hops.append({"n": num,
|
||
"ip": ip.group(1) if ip else None,
|
||
"rtt": float(rtt.group(1)) if rtt else None})
|
||
return hops
|
||
except Exception:
|
||
return []
|
||
|
||
|
||
def main():
|
||
os.makedirs(OUT, exist_ok=True)
|
||
while True:
|
||
rec = {"ts": time.strftime("%Y-%m-%dT%H:%M:%S")}
|
||
rec["vds"] = {}
|
||
for p in (443, 2053, 2087, 8444):
|
||
ok, ms = tcp(VDS, p)
|
||
rec["vds"][str(p)] = {"ok": ok, "ms": ms}
|
||
# туннель (UDP) — живёт ли, когда TCP рвётся
|
||
t0 = time.time()
|
||
tr = subprocess.run("curl -s -o /dev/null -w '%{http_code}' -m 5 http://10.88.0.1/",
|
||
shell=True, capture_output=True, text=True, timeout=15)
|
||
code = (tr.stdout or "").strip()
|
||
rec["tunnel"] = {"code": code, "ok": code not in ("", "000"),
|
||
"ms": round((time.time() - t0) * 1000, 1)}
|
||
hops = traceroute()
|
||
rec["hops"] = hops
|
||
rec["reached"] = hops[-1]["n"] if hops else None
|
||
rec["last_hop"] = (hops[-1]["ip"] if hops else None)
|
||
# хопы с таймаутом
|
||
rec["timeouts"] = [h["n"] for h in hops if not h["ip"]]
|
||
|
||
path = os.path.join(OUT, time.strftime("%Y-%m-%d") + ".jsonl")
|
||
with open(path, "a", encoding="utf-8") as f:
|
||
f.write(json.dumps(rec, ensure_ascii=False) + "\n")
|
||
|
||
bad = [p for p, v in rec["vds"].items() if not v["ok"]]
|
||
print("%s vds=%s reached=%s last=%s timeouts=%s"
|
||
% (rec["ts"], "ok" if not bad else "СБОЙ:" + ",".join(bad),
|
||
rec["reached"], rec["last_hop"], rec["timeouts"]), flush=True)
|
||
time.sleep(INTERVAL)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|