adaptai / platform /dbops /tools /connectivity_report.py
ADAPT-Chase's picture
Add files using upload-large-folder tool
503b0e9 verified
#!/usr/bin/env python3
import os, socket, json, time
from datetime import datetime
# Prefer DATAOP_ROOT; fall back to DBOPS_ROOT for backward compatibility
DBOPS_ROOT = os.environ.get("DBOPS_ROOT", "/data/adaptai/platform/dbops")
SECRETS = "/data/adaptai/secrets/dataops"
RUN_DIR = os.path.join(DBOPS_ROOT, "run", "reports")
def load_yaml(path):
# Minimal YAML loader for simple key: value and lists
data = {}
try:
import yaml # if available
with open(path) as f:
return yaml.safe_load(f)
except Exception:
pass
# Very simple fallback (not full YAML)
try:
with open(path) as f:
content = f.read()
return json.loads(json.dumps({"raw": content}))
except Exception:
return {}
def dns_check(host):
try:
ip = socket.gethostbyname(host)
return {"ok": True, "ip": ip}
except Exception as e:
return {"ok": False, "error": str(e)}
def tcp_check(host, port, timeout=2.0):
try:
with socket.create_connection((host, port), timeout=timeout):
return {"ok": True}
except Exception as e:
return {"ok": False, "error": str(e)}
def http_check(url, timeout=2.0):
try:
import requests
r = requests.get(url, timeout=timeout)
return {"ok": r.status_code == 200, "status": r.status_code}
except Exception as e:
return {"ok": False, "error": str(e)}
def main():
os.makedirs(RUN_DIR, exist_ok=True)
ports = load_yaml(os.path.join(SECRETS, "ports.yaml"))
# Prefer effective connections if present
eff_path = os.path.join(SECRETS, "effective_connections.yaml")
base_path = os.path.join(SECRETS, "connections.yaml")
use_path = eff_path if os.path.exists(eff_path) else base_path
conns = load_yaml(use_path)
report = {
"ts": time.time(),
"human_ts": datetime.utcnow().isoformat() + "Z",
"dns": {},
"tcp": {},
"http": {},
}
# Qdrant
try:
qhost = conns.get("qdrant", {}).get("host", "qdrant.dbops.local")
except Exception:
qhost = "qdrant.dbops.local"
qhttp = 17000
qgrpc = 17001
report["dns"][qhost] = dns_check(qhost)
report["tcp"][f"{qhost}:17000"] = tcp_check(qhost, qhttp)
report["tcp"][f"{qhost}:17001"] = tcp_check(qhost, qgrpc)
report["http"][f"http://{qhost}:17000/collections"] = http_check(f"http://{qhost}:17000/collections")
# Gremlin
try:
ghost = conns.get("janusgraph", {}).get("gremlin_host", "gremlin.dbops.local")
except Exception:
ghost = "gremlin.dbops.local"
report["dns"][ghost] = dns_check(ghost)
report["tcp"][f"{ghost}:17002"] = tcp_check(ghost, 17002)
# DragonFly
for i, port in enumerate([18000, 18001, 18002], start=1):
host = f"dragonfly-{i}.dbops.local"
report["dns"][host] = dns_check(host)
report["tcp"][f"{host}:{port}"] = tcp_check(host, port)
# Redis cluster
for i, port in enumerate([18010, 18011, 18012], start=1):
host = f"redis-{i}.dbops.local"
report["dns"][host] = dns_check(host)
report["tcp"][f"{host}:{port}"] = tcp_check(host, port)
# Write outputs
ts = int(report["ts"])
jpath = os.path.join(RUN_DIR, f"connectivity_{ts}.json")
tpath = os.path.join(RUN_DIR, f"connectivity_{ts}.txt")
with open(jpath, "w") as f:
json.dump(report, f, indent=2)
# Text
lines = []
lines.append("DNS")
for host, res in report["dns"].items():
lines.append(f"- {host}: {'OK' if res.get('ok') else 'FAIL'}" + (f" ({res.get('ip')})" if res.get('ip') else ""))
lines.append("")
lines.append("TCP")
for key, res in report["tcp"].items():
lines.append(f"- {key}: {'OK' if res.get('ok') else 'FAIL'}")
lines.append("")
lines.append("HTTP")
for key, res in report["http"].items():
if res.get("status"):
lines.append(f"- {key}: {res['status']}")
else:
lines.append(f"- {key}: {'OK' if res.get('ok') else 'FAIL'}")
with open(tpath, "w") as f:
f.write("\n".join(lines) + "\n")
print(f"Wrote {jpath}\nWrote {tpath}")
if __name__ == "__main__":
main()