File size: 4,226 Bytes
503b0e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/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()