Spaces:
Sleeping
Sleeping
Download server/renderer.py from brand0nwalls/NetOps: direct link, hf CLI and curl.
- Browser
- Download file 30 kB
-
https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/renderer.py
- Command line
-
hf download hf://spaces/brand0nwalls/NetOps/server/renderer.py
-
curl -L -o renderer.py https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/renderer.py
30 kB
| """Render raw telemetry dicts as human-readable NOC-style text observations. | |
| Pure formatting β no data reading, no network access. | |
| """ | |
| from server.constants import AS_CONFIGS, INTRA_AS_LINKS, EBGP_PEERS, router_to_asn | |
| def _router_as(router_name: str) -> int | None: | |
| return router_to_asn(router_name) | |
| def _format_bytes(n: int) -> str: | |
| if n >= 1_000_000_000: | |
| return f"{n / 1_000_000_000:.1f} GB" | |
| if n >= 1_000_000: | |
| return f"{n / 1_000_000:.1f} MB" | |
| if n >= 1_000: | |
| return f"{n / 1_000:.1f} KB" | |
| return f"{n} B" | |
| def _format_packets(n: int) -> str: | |
| if n >= 1_000_000: | |
| return f"{n / 1_000_000:.1f}M" | |
| if n >= 1_000: | |
| return f"{n / 1_000:.1f}K" | |
| return str(n) | |
| def _format_uptime(raw: str) -> str: | |
| if not raw or raw == "never": | |
| return "never" | |
| return raw | |
| def _severity(level: str) -> str: | |
| return f"[{level}]" | |
| def _peer_status_line(peer_ip: str, peer_data: dict) -> tuple[str, str]: | |
| state = peer_data.get("state", "Unknown") | |
| pfx_rcvd = peer_data.get("pfxRcd", 0) | |
| pfx_sent = peer_data.get("pfxSnt", 0) | |
| remote_as = peer_data.get("remoteAs", "?") | |
| uptime = _format_uptime(str(peer_data.get("peerUptimeMsec", peer_data.get("peerUptime", "")))) | |
| if isinstance(uptime, str) and uptime.isdigit(): | |
| ms = int(uptime) | |
| secs = ms // 1000 | |
| if secs >= 3600: | |
| uptime = f"{secs // 3600}h{(secs % 3600) // 60}m" | |
| elif secs >= 60: | |
| uptime = f"{secs // 60}m{secs % 60}s" | |
| else: | |
| uptime = f"{secs}s" | |
| if state != "Established": | |
| severity = "CRITICAL" | |
| line = f" {_severity(severity):12s} {peer_ip} (AS {remote_as}): {state} β session DOWN, uptime {uptime}" | |
| elif pfx_rcvd == 0: | |
| severity = "WARNING" | |
| line = f" {_severity(severity):12s} {peer_ip} (AS {remote_as}): Established but receiving 0 prefixes (pfxRcd=0), uptime {uptime}" | |
| else: | |
| severity = "OK" | |
| line = f" {_severity(severity):12s} {peer_ip} (AS {remote_as}): Established, {pfx_rcvd} prefixes received, {pfx_sent} sent, uptime {uptime}" | |
| return severity, line | |
| def render_routing_view(router_name: str, bgp_summary: dict, ospf_neighbors: dict, | |
| ospf_interface: dict, routes: dict, | |
| bgp_detail: dict = None) -> str: | |
| lines = [f"=== ROUTING STATE: {router_name} ==="] | |
| asn = _router_as(router_name) | |
| if asn: | |
| lines[0] += f" (AS {asn})" | |
| # --- BGP --- | |
| bgp_issues = [] | |
| bgp_ok = 0 | |
| ipv4 = bgp_summary.get("ipv4Unicast", {}) | |
| peers = ipv4.get("peers", {}) | |
| router_id = ipv4.get("routerId", "?") | |
| lines.append(f"\nBGP Summary (router-id {router_id}):") | |
| if not peers: | |
| lines.append(" No BGP peers configured") | |
| else: | |
| peer_lines = [] | |
| for pip, pdata in peers.items(): | |
| sev, line = _peer_status_line(pip, pdata) | |
| peer_lines.append((0 if sev == "CRITICAL" else 1 if sev == "WARNING" else 2, line)) | |
| if sev != "OK": | |
| bgp_issues.append(line.strip()) | |
| else: | |
| bgp_ok += 1 | |
| peer_lines.sort(key=lambda x: x[0]) | |
| total = len(peer_lines) | |
| lines.append(f" Peers: {bgp_ok}/{total} Established") | |
| for _, pl in peer_lines: | |
| lines.append(pl) | |
| # --- OSPF --- | |
| ospf_issues = [] | |
| ospf_ok = 0 | |
| lines.append("\nOSPF Neighbors:") | |
| neighbors = ospf_neighbors.get("neighbors", ospf_neighbors) | |
| if not neighbors: | |
| lines.append(" No OSPF neighbors") | |
| else: | |
| nbr_lines = [] | |
| for nid, ndata_wrap in neighbors.items(): | |
| if isinstance(ndata_wrap, list): | |
| for ndata in ndata_wrap: | |
| nbr_lines.append(_ospf_neighbor_line(nid, ndata)) | |
| elif isinstance(ndata_wrap, dict): | |
| nbr_lines.append(_ospf_neighbor_line(nid, ndata_wrap)) | |
| nbr_lines.sort(key=lambda x: x[0]) | |
| total_nbr = len(nbr_lines) | |
| full_count = sum(1 for s, _ in nbr_lines if s == 2) | |
| lines.append(f" Adjacencies: {full_count}/{total_nbr} Full") | |
| for _, nl in nbr_lines: | |
| lines.append(nl) | |
| if "CRITICAL" in nl or "WARNING" in nl: | |
| ospf_issues.append(nl.strip()) | |
| # --- OSPF Interfaces --- | |
| ifaces = ospf_interface.get("interfaces", ospf_interface) | |
| suspicious_costs = [] | |
| if ifaces: | |
| for ifname, idata in ifaces.items(): | |
| cost = idata.get("cost", 10) | |
| if cost > 100: | |
| suspicious_costs.append(f" [WARNING] {ifname}: OSPF cost {cost} (expected ~10)") | |
| if suspicious_costs: | |
| lines.append("\nOSPF Interface Anomalies:") | |
| lines.extend(suspicious_costs) | |
| # --- Routing table --- | |
| lines.append("\nRouting Table:") | |
| if isinstance(routes, dict): | |
| route_entries = [] | |
| for prefix, route_list in routes.items(): | |
| if not isinstance(route_list, list): | |
| continue | |
| for r in route_list: | |
| if not r.get("selected", False) and not r.get("installed", False): | |
| continue | |
| proto = r.get("protocol", "?") | |
| nhs = r.get("nexthops", []) | |
| nh_str = "" | |
| if nhs: | |
| nh = nhs[0] | |
| nh_ip = nh.get("ip", "") | |
| nh_intf = nh.get("interfaceName", nh.get("interface", "")) | |
| if nh_ip: | |
| nh_str = f"via {nh_ip}" | |
| elif nh_intf: | |
| nh_str = f"via {nh_intf}" | |
| else: | |
| nh_str = "directly connected" | |
| route_entries.append((proto, prefix, nh_str)) | |
| by_proto = {} | |
| for proto, prefix, nh in route_entries: | |
| by_proto.setdefault(proto, []).append((prefix, nh)) | |
| total_routes = len(route_entries) | |
| proto_counts = ", ".join(f"{len(v)} {k}" for k, v in sorted(by_proto.items())) | |
| lines.append(f" {total_routes} active routes ({proto_counts})") | |
| for proto in ["connected", "ospf", "bgp", "static", "kernel"]: | |
| if proto not in by_proto: | |
| continue | |
| for prefix, nh in sorted(by_proto[proto]): | |
| lines.append(f" {prefix:22s} {proto:10s} {nh}") | |
| for proto, entries in sorted(by_proto.items()): | |
| if proto in ["connected", "ospf", "bgp", "static", "kernel"]: | |
| continue | |
| for prefix, nh in sorted(entries): | |
| lines.append(f" {prefix:22s} {proto:10s} {nh}") | |
| # --- BGP Path Details --- | |
| if bgp_detail: | |
| bgp_routes = bgp_detail.get("ipv4Unicast", bgp_detail).get("routes", {}) | |
| if bgp_routes: | |
| bgp_path_issues = [] | |
| lines.append("\nBGP Path Details:") | |
| for prefix, rdata in sorted(bgp_routes.items()): | |
| paths = rdata.get("paths", []) | |
| for p in paths: | |
| as_path = p.get("aspath", {}).get("string", "") | |
| loc_pref = p.get("locPrf", 100) | |
| comms = p.get("community", {}).get("list", []) | |
| nh_list = p.get("nexthops", []) | |
| nh_ip = nh_list[0].get("ip", "?") if nh_list else "?" | |
| flags = [] | |
| if loc_pref != 100: | |
| flags.append(f"local-pref={loc_pref}") | |
| bgp_path_issues.append( | |
| f"{router_name}: {prefix} has non-default local-pref {loc_pref}") | |
| if as_path and len(as_path.split()) > 3: | |
| flags.append("long AS-path") | |
| bgp_path_issues.append( | |
| f"{router_name}: {prefix} has unusually long AS-path: {as_path}") | |
| if comms: | |
| flags.append(f"communities={','.join(str(c) for c in comms)}") | |
| flag_str = f" [{', '.join(flags)}]" if flags else "" | |
| lines.append(f" {prefix:22s} via {nh_ip:15s} AS-path: {as_path or 'local'}{flag_str}") | |
| if bgp_path_issues: | |
| bgp_issues.extend(bgp_path_issues) | |
| # --- Issues summary --- | |
| all_issues = bgp_issues + ospf_issues + [c.strip() for c in suspicious_costs] | |
| lines.append("\nIssues Detected:") | |
| if all_issues: | |
| for issue in all_issues: | |
| lines.append(f" - {issue}") | |
| else: | |
| lines.append(" None β routing state healthy") | |
| return "\n".join(lines) | |
| def _ospf_neighbor_line(neighbor_id: str, ndata: dict) -> tuple[int, str]: | |
| state = ndata.get("nbrState", ndata.get("state", "Unknown")) | |
| if "/" in state: | |
| state = state.split("/")[0] | |
| dead_ms = ndata.get("routerDeadIntervalTimerDueMsec", ndata.get("deadTimeMsecs", 0)) | |
| dead_s = dead_ms / 1000 if dead_ms else 0 | |
| address = ndata.get("ifaceAddress", ndata.get("address", "")) | |
| if state == "Full": | |
| sev = 2 | |
| tag = _severity("OK") | |
| detail = f"Full (dead timer {dead_s:.0f}s)" | |
| elif state in ("2-Way", "ExStart", "Exchange", "Loading", "Init"): | |
| sev = 1 | |
| tag = _severity("WARNING") | |
| detail = f"stuck in {state} (dead timer {dead_s:.0f}s)" | |
| else: | |
| sev = 0 | |
| tag = _severity("CRITICAL") | |
| detail = f"{state} β adjacency lost" | |
| line = f" {tag:12s} {neighbor_id} ({address}): {detail}" | |
| return sev, line | |
| def render_traffic_view(all_interfaces: dict[str, list[dict]], | |
| all_queue_stats: dict[str, dict[str, dict]], | |
| as_filter: int | None = None) -> str: | |
| if as_filter: | |
| lines = [f"=== TRAFFIC STATE (AS {as_filter}) ==="] | |
| else: | |
| lines = ["=== TRAFFIC STATE ==="] | |
| issues = [] | |
| as_routers = set(AS_CONFIGS[as_filter]["routers"]) if as_filter else None | |
| lines.append("\nIntra-AS Links (OSPF):") | |
| for asn, link_list in INTRA_AS_LINKS.items(): | |
| if as_filter and asn != as_filter: | |
| continue | |
| for ra, rb in link_list: | |
| line, issue = _link_traffic_line(ra, rb, all_interfaces, all_queue_stats) | |
| lines.append(f" {line}") | |
| if issue: | |
| issues.append(issue) | |
| lines.append("\nInter-AS Links (eBGP):") | |
| for ra, rb in EBGP_PEERS: | |
| if as_filter and ra not in as_routers and rb not in as_routers: | |
| continue | |
| line, issue = _link_traffic_line(ra, rb, all_interfaces, all_queue_stats) | |
| lines.append(f" {line}") | |
| if issue: | |
| issues.append(issue) | |
| qdisc_issues = [] | |
| for rname, iface_qs in all_queue_stats.items(): | |
| if as_filter and rname not in as_routers: | |
| continue | |
| for ifname, qs in iface_qs.items(): | |
| for qd in qs.get("qdiscs", []): | |
| qtype = qd.get("type", "") | |
| dropped = qd.get("dropped", 0) | |
| if qtype in ("netem", "tbf") or dropped > 0: | |
| qdisc_issues.append( | |
| f" [WARNING] {rname} {ifname}: qdisc {qtype}, {dropped} drops, " | |
| f"{qd.get('overlimits', 0)} overlimits" | |
| ) | |
| lines.append("\nQueuing:") | |
| if qdisc_issues: | |
| lines.extend(qdisc_issues) | |
| else: | |
| lines.append(" All queues nominal (default pfifo)") | |
| lines.append("\nIssues Detected:") | |
| all_issues = issues + [q.strip() for q in qdisc_issues] | |
| if all_issues: | |
| for i in all_issues: | |
| lines.append(f" - {i}") | |
| else: | |
| lines.append(" None β traffic flow healthy") | |
| return "\n".join(lines) | |
| def _find_interface_for_peer(router_name: str, peer_name: str, | |
| interfaces: list[dict]) -> dict | None: | |
| for iface in interfaces: | |
| ifname = iface.get("ifname", "") | |
| if peer_name in ifname or f"{router_name}-{peer_name}" in ifname: | |
| return iface | |
| # Heuristic: IPMininet names interfaces like r1-eth0, r1-eth1 | |
| # We can't reliably map without network access, so return stats for all non-lo | |
| return None | |
| def _link_traffic_line(ra: str, rb: str, | |
| all_interfaces: dict[str, list[dict]], | |
| all_queue_stats: dict[str, dict[str, dict]]) -> tuple[str, str | None]: | |
| a_ifaces = all_interfaces.get(ra, []) | |
| b_ifaces = all_interfaces.get(rb, []) | |
| a_if = _find_interface_for_peer(ra, rb, a_ifaces) | |
| b_if = _find_interface_for_peer(rb, ra, b_ifaces) | |
| if a_if: | |
| a_stats = a_if.get("stats64", a_if.get("stats", {})) | |
| a_rx = a_stats.get("rx", {}) | |
| a_tx = a_stats.get("tx", {}) | |
| tx_bytes = a_tx.get("bytes", 0) | |
| rx_bytes = a_rx.get("bytes", 0) | |
| tx_drops = a_tx.get("dropped", 0) | |
| rx_drops = a_rx.get("dropped", 0) | |
| tx_errors = a_tx.get("errors", 0) | |
| rx_errors = a_rx.get("errors", 0) | |
| flags = a_if.get("flags", []) | |
| up = "UP" in flags | |
| drops_total = tx_drops + rx_drops | |
| errors_total = tx_errors + rx_errors | |
| issue = None | |
| if not up: | |
| status = _severity("CRITICAL") + " LINK DOWN" | |
| issue = f"{ra}-{rb}: link DOWN" | |
| elif drops_total > 0 or errors_total > 0: | |
| status = _severity("WARNING") + f" {drops_total} drops, {errors_total} errors" | |
| issue = f"{ra}-{rb}: {drops_total} drops, {errors_total} errors" | |
| else: | |
| status = _severity("OK") | |
| mtu = a_if.get("mtu", 1500) | |
| return (f"{ra}-{rb}: tx {_format_bytes(tx_bytes)}, rx {_format_bytes(rx_bytes)}, " | |
| f"mtu {mtu} {status}"), issue | |
| else: | |
| return f"{ra}-{rb}: (interface stats unavailable)", None | |
| def render_device_view(router_name: str, interfaces: list[dict], | |
| protocol_counters: dict, | |
| queue_stats: dict[str, dict], | |
| iptables: str = "", | |
| cpu: dict = None) -> str: | |
| lines = [f"=== DEVICE STATE: {router_name} ==="] | |
| issues = [] | |
| # System Resources | |
| if cpu: | |
| load = cpu.get("load_1m", 0.0) | |
| lines.append("\nSystem Resources:") | |
| if load > 90: | |
| lines.append(f" [CRITICAL] CPU load {load:.1f}%") | |
| issues.append(f"{router_name}: CPU load {load:.1f}% (exhausted)") | |
| elif load > 50: | |
| lines.append(f" [WARNING] CPU load {load:.1f}%") | |
| issues.append(f"{router_name}: CPU load {load:.1f}% (elevated)") | |
| elif cpu.get("raw", "").startswith("0."): | |
| raw = cpu["raw"] | |
| if "recent_cpu_spike" in cpu: | |
| lines.append(f" [WARNING] Recent OOM killer activity detected") | |
| issues.append(f"{router_name}: Recent OOM killer activity") | |
| else: | |
| lines.append(f" [OK] CPU load {load:.1f}%") | |
| else: | |
| lines.append(f" [OK] CPU load {load:.1f}%") | |
| lines.append("\nInterfaces:") | |
| for iface in interfaces: | |
| ifname = iface.get("ifname", "?") | |
| if ifname == "lo": | |
| continue | |
| flags = iface.get("flags", []) | |
| up = "UP" in flags | |
| lower_up = "LOWER_UP" in flags | |
| mtu = iface.get("mtu", 1500) | |
| stats = iface.get("stats64", iface.get("stats", {})) | |
| rx = stats.get("rx", {}) | |
| tx = stats.get("tx", {}) | |
| state_str = "UP" if up else "DOWN" | |
| if up and not lower_up: | |
| state_str = "UP (no carrier)" | |
| if not up: | |
| tag = _severity("CRITICAL") | |
| issues.append(f"{router_name} {ifname}: interface DOWN") | |
| elif rx.get("errors", 0) > 0 or tx.get("errors", 0) > 0: | |
| tag = _severity("WARNING") | |
| issues.append(f"{router_name} {ifname}: {rx.get('errors', 0)} RX errors, {tx.get('errors', 0)} TX errors") | |
| elif rx.get("dropped", 0) > 0 or tx.get("dropped", 0) > 0: | |
| tag = _severity("WARNING") | |
| issues.append(f"{router_name} {ifname}: {rx.get('dropped', 0)} RX drops, {tx.get('dropped', 0)} TX drops") | |
| else: | |
| tag = _severity("OK") | |
| lines.append(f" {tag:12s} {ifname}: {state_str}, mtu {mtu}") | |
| lines.append(f" RX: {_format_packets(rx.get('packets', 0))} pkts, " | |
| f"{_format_bytes(rx.get('bytes', 0))}, " | |
| f"{rx.get('errors', 0)} errors, {rx.get('dropped', 0)} dropped") | |
| lines.append(f" TX: {_format_packets(tx.get('packets', 0))} pkts, " | |
| f"{_format_bytes(tx.get('bytes', 0))}, " | |
| f"{tx.get('errors', 0)} errors, {tx.get('dropped', 0)} dropped") | |
| qs = queue_stats.get(ifname, {}) | |
| for qd in qs.get("qdiscs", []): | |
| qtype = qd.get("type", "?") | |
| if qtype not in ("pfifo_fast", "noqueue", "fq_codel"): | |
| lines.append(f" Qdisc: {qtype}, " | |
| f"{qd.get('dropped', 0)} drops, " | |
| f"{qd.get('overlimits', 0)} overlimits, " | |
| f"backlog {qd.get('backlog_pkts', 0)}p") | |
| # Protocol counters | |
| snmp = protocol_counters.get("snmp", {}) | |
| netstat = protocol_counters.get("netstat", {}) | |
| tcp = snmp.get("Tcp", {}) | |
| icmp = snmp.get("Icmp", {}) | |
| lines.append("\nProtocol Counters:") | |
| if tcp: | |
| out_segs = tcp.get("OutSegs", 0) | |
| retrans = tcp.get("RetransSegs", 0) | |
| in_errs = tcp.get("InErrs", 0) | |
| retrans_pct = (retrans / out_segs * 100) if out_segs > 0 else 0 | |
| if retrans_pct > 1.0: | |
| tag = _severity("WARNING") | |
| issues.append(f"{router_name}: TCP retransmit rate {retrans_pct:.2f}%") | |
| elif retrans_pct > 5.0: | |
| tag = _severity("CRITICAL") | |
| issues.append(f"{router_name}: TCP retransmit rate {retrans_pct:.2f}% (severe)") | |
| else: | |
| tag = _severity("OK") | |
| lines.append(f" {tag:12s} TCP: {_format_packets(out_segs)} segments out, " | |
| f"{retrans} retransmits ({retrans_pct:.3f}%), {in_errs} checksum errors") | |
| if in_errs > 0: | |
| issues.append(f"{router_name}: {in_errs} TCP checksum errors (possible corruption)") | |
| reorder = netstat.get("TcpExt", {}).get("TCPSACKReorder", 0) | |
| if reorder > 0: | |
| lines.append(f" [WARNING] TCP reordering events: {reorder}") | |
| issues.append(f"{router_name}: {reorder} TCP reordering events") | |
| if icmp: | |
| time_exc = icmp.get("OutTimeExcds", 0) | |
| if time_exc > 0: | |
| lines.append(f" [WARNING] ICMP Time Exceeded sent: {time_exc} (possible routing loop)") | |
| issues.append(f"{router_name}: {time_exc} ICMP TTL exceeded (routing loop?)") | |
| # Firewall rules | |
| if iptables: | |
| lines.append("\nFirewall Rules:") | |
| has_drop = False | |
| for line in iptables.split("\n"): | |
| if "DROP" in line: | |
| has_drop = True | |
| lines.append(f" [WARNING] {line.strip()}") | |
| if has_drop: | |
| issues.append(f"{router_name}: iptables DROP rules active") | |
| else: | |
| lines.append(f" [OK] No active firewall rules") | |
| lines.append("\nIssues Detected:") | |
| if issues: | |
| for i in issues: | |
| lines.append(f" - {i}") | |
| else: | |
| lines.append(" None β device healthy") | |
| return "\n".join(lines) | |
| def render_topology_view(all_bgp: dict[str, dict], | |
| all_ospf: dict[str, dict], | |
| all_interfaces: dict[str, list[dict]], | |
| as_filter: int | None = None) -> str: | |
| if as_filter: | |
| lines = [f"=== NETWORK TOPOLOGY (AS {as_filter}) ==="] | |
| else: | |
| lines = ["=== NETWORK TOPOLOGY ==="] | |
| issues = [] | |
| for asn, cfg in AS_CONFIGS.items(): | |
| if as_filter and asn != as_filter: | |
| continue | |
| lines.append(f"\nAS {asn}:") | |
| routers = cfg["routers"] | |
| for rname in routers: | |
| bgp_data = all_bgp.get(rname, {}) | |
| ospf_data = all_ospf.get(rname, {}) | |
| ifaces = all_interfaces.get(rname, []) | |
| # Router-level health summary | |
| bgp_peers = bgp_data.get("ipv4Unicast", {}).get("peers", {}) | |
| bgp_up = sum(1 for p in bgp_peers.values() if p.get("state") == "Established") | |
| bgp_total = len(bgp_peers) | |
| neighbors = ospf_data.get("neighbors", ospf_data) | |
| ospf_full = 0 | |
| ospf_total = 0 | |
| if isinstance(neighbors, dict): | |
| for nid, ndata in neighbors.items(): | |
| if isinstance(ndata, list): | |
| for nd in ndata: | |
| ospf_total += 1 | |
| s = nd.get("nbrState", nd.get("state", "")) | |
| if s.startswith("Full"): | |
| ospf_full += 1 | |
| elif isinstance(ndata, dict): | |
| ospf_total += 1 | |
| s = ndata.get("nbrState", ndata.get("state", "")) | |
| if s.startswith("Full"): | |
| ospf_full += 1 | |
| iface_down = [] | |
| for iface in ifaces: | |
| ifn = iface.get("ifname", "") | |
| if ifn == "lo": | |
| continue | |
| if "UP" not in iface.get("flags", []): | |
| iface_down.append(ifn) | |
| # Determine router health | |
| if iface_down or (bgp_total > 0 and bgp_up < bgp_total) or ospf_full < ospf_total: | |
| if iface_down or bgp_up == 0 and bgp_total > 0: | |
| tag = _severity("CRITICAL") | |
| else: | |
| tag = _severity("WARNING") | |
| else: | |
| tag = _severity("OK") | |
| parts = [f" {tag:12s} {rname}"] | |
| if bgp_total > 0: | |
| parts.append(f"BGP {bgp_up}/{bgp_total}") | |
| if ospf_total > 0: | |
| parts.append(f"OSPF {ospf_full}/{ospf_total}") | |
| if iface_down: | |
| parts.append(f"DOWN: {', '.join(iface_down)}") | |
| issues.append(f"{rname}: interfaces down β {', '.join(iface_down)}") | |
| lines.append(" | ".join(parts)) | |
| # Hosts | |
| hosts = cfg["hosts"] | |
| host_router = cfg["host_router"] | |
| lines.append(f" Hosts: {', '.join(hosts)} (attached to {host_router})") | |
| # Link status summary | |
| lines.append("\nLink Summary:") | |
| as_routers = set(AS_CONFIGS[as_filter]["routers"]) if as_filter else None | |
| ospf_links_up = 0 | |
| ospf_links_total = 0 | |
| for asn, link_list in INTRA_AS_LINKS.items(): | |
| if as_filter and asn != as_filter: | |
| continue | |
| for ra, rb in link_list: | |
| ospf_links_total += 1 | |
| a_ifaces = all_interfaces.get(ra, []) | |
| all_up = all("UP" in i.get("flags", []) for i in a_ifaces if i.get("ifname", "") != "lo") | |
| if all_up: | |
| ospf_links_up += 1 | |
| ebgp_up = 0 | |
| ebgp_total = 0 | |
| for ra, rb in EBGP_PEERS: | |
| if as_filter and ra not in as_routers and rb not in as_routers: | |
| continue | |
| ebgp_total += 1 | |
| bgp_a = all_bgp.get(ra, {}).get("ipv4Unicast", {}).get("peers", {}) | |
| for pip, pdata in bgp_a.items(): | |
| if pdata.get("state") == "Established": | |
| ebgp_up += 1 | |
| break | |
| lines.append(f" OSPF links: {ospf_links_up}/{ospf_links_total} operational") | |
| lines.append(f" eBGP sessions: {ebgp_up}/{ebgp_total} Established") | |
| lines.append("\nIssues Detected:") | |
| if issues: | |
| for i in issues: | |
| lines.append(f" - {i}") | |
| else: | |
| lines.append(" None β topology healthy") | |
| return "\n".join(lines) | |
| def render_events_view(snapshots: list[dict], | |
| as_filter: int | None = None) -> str: | |
| """Render a timeline of state changes from sequential telemetry snapshots. | |
| Each snapshot: {"timestamp": float, "router": str, "bgp_summary": dict, | |
| "ospf_neighbors": dict, "interfaces": list[dict]} | |
| """ | |
| if as_filter: | |
| lines = [f"=== RECENT EVENTS (AS {as_filter}) ==="] | |
| as_routers = set(AS_CONFIGS[as_filter]["routers"]) if as_filter else None | |
| else: | |
| lines = ["=== RECENT EVENTS ==="] | |
| as_routers = None | |
| if len(snapshots) < 2: | |
| lines.append(" Insufficient history (need at least 2 snapshots)") | |
| return "\n".join(lines) | |
| events = [] | |
| for i in range(1, len(snapshots)): | |
| prev = snapshots[i - 1] | |
| curr = snapshots[i] | |
| ts = curr.get("timestamp", 0) | |
| router = curr.get("router", "?") | |
| if as_routers and router not in as_routers: | |
| continue | |
| # BGP state changes | |
| prev_peers = prev.get("bgp_summary", {}).get("ipv4Unicast", {}).get("peers", {}) | |
| curr_peers = curr.get("bgp_summary", {}).get("ipv4Unicast", {}).get("peers", {}) | |
| for pip in set(list(prev_peers.keys()) + list(curr_peers.keys())): | |
| old_state = prev_peers.get(pip, {}).get("state", "Gone") | |
| new_state = curr_peers.get(pip, {}).get("state", "Gone") | |
| if old_state != new_state: | |
| if new_state == "Established": | |
| events.append((ts, "OK", f"{router}: BGP peer {pip} came UP (was {old_state})")) | |
| elif old_state == "Established": | |
| events.append((ts, "CRITICAL", f"{router}: BGP peer {pip} went DOWN β {new_state}")) | |
| else: | |
| events.append((ts, "WARNING", f"{router}: BGP peer {pip} state changed {old_state} β {new_state}")) | |
| old_pfx = prev_peers.get(pip, {}).get("pfxRcd", 0) | |
| new_pfx = curr_peers.get(pip, {}).get("pfxRcd", 0) | |
| if old_pfx != new_pfx and abs(old_pfx - new_pfx) > 1: | |
| if new_pfx < old_pfx: | |
| events.append((ts, "WARNING", f"{router}: prefixes from {pip} dropped {old_pfx} β {new_pfx}")) | |
| else: | |
| events.append((ts, "INFO", f"{router}: prefixes from {pip} increased {old_pfx} β {new_pfx}")) | |
| # OSPF state changes | |
| prev_nbrs = prev.get("ospf_neighbors", {}).get("neighbors", prev.get("ospf_neighbors", {})) | |
| curr_nbrs = curr.get("ospf_neighbors", {}).get("neighbors", curr.get("ospf_neighbors", {})) | |
| for nid in set(list(_flatten_ospf_ids(prev_nbrs)) + list(_flatten_ospf_ids(curr_nbrs))): | |
| old_s = _get_ospf_state(prev_nbrs, nid) | |
| new_s = _get_ospf_state(curr_nbrs, nid) | |
| if old_s != new_s: | |
| if new_s == "Full": | |
| events.append((ts, "OK", f"{router}: OSPF neighbor {nid} reached Full (was {old_s})")) | |
| elif old_s == "Full": | |
| events.append((ts, "CRITICAL", f"{router}: OSPF neighbor {nid} lost adjacency β {new_s}")) | |
| else: | |
| events.append((ts, "WARNING", f"{router}: OSPF neighbor {nid} state {old_s} β {new_s}")) | |
| # Interface state changes | |
| prev_ifs = {i.get("ifname"): i for i in prev.get("interfaces", [])} | |
| curr_ifs = {i.get("ifname"): i for i in curr.get("interfaces", [])} | |
| for ifn in set(list(prev_ifs.keys()) + list(curr_ifs.keys())): | |
| if ifn == "lo": | |
| continue | |
| was_up = "UP" in prev_ifs.get(ifn, {}).get("flags", []) | |
| is_up = "UP" in curr_ifs.get(ifn, {}).get("flags", []) | |
| if was_up and not is_up: | |
| events.append((ts, "CRITICAL", f"{router}: interface {ifn} went DOWN")) | |
| elif not was_up and is_up: | |
| events.append((ts, "OK", f"{router}: interface {ifn} came UP")) | |
| events.sort(key=lambda x: x[0]) | |
| if not events: | |
| lines.append(" No state changes detected between snapshots") | |
| else: | |
| t0 = events[0][0] | |
| for ts, sev, msg in events: | |
| elapsed = ts - t0 | |
| lines.append(f" [T+{elapsed:.0f}s] [{sev:8s}] {msg}") | |
| return "\n".join(lines) | |
| def _flatten_ospf_ids(neighbors: dict) -> list[str]: | |
| ids = [] | |
| for nid, ndata in neighbors.items(): | |
| if isinstance(ndata, list): | |
| ids.append(nid) | |
| elif isinstance(ndata, dict): | |
| ids.append(nid) | |
| return ids | |
| def _get_ospf_state(neighbors: dict, nid: str) -> str: | |
| ndata = neighbors.get(nid) | |
| if ndata is None: | |
| return "Gone" | |
| if isinstance(ndata, list): | |
| if ndata: | |
| s = ndata[0].get("nbrState", ndata[0].get("state", "Unknown")) | |
| return s.split("/")[0] if "/" in s else s | |
| return "Gone" | |
| if isinstance(ndata, dict): | |
| s = ndata.get("nbrState", ndata.get("state", "Unknown")) | |
| return s.split("/")[0] if "/" in s else s | |
| return "Unknown" | |
| def render_ping_result(src: str, dst: str, result: dict) -> str: | |
| loss = result.get("loss_pct", -1) | |
| if loss == 100: | |
| tag = _severity("CRITICAL") | |
| detail = "100% packet loss β destination unreachable" | |
| elif loss > 0: | |
| tag = _severity("WARNING") | |
| detail = f"{loss:.0f}% packet loss" | |
| elif loss == 0: | |
| tag = _severity("OK") | |
| detail = "0% loss" | |
| else: | |
| tag = _severity("WARNING") | |
| detail = "unable to parse result" | |
| rtt_avg = result.get("rtt_avg", 0) | |
| rtt_min = result.get("rtt_min", 0) | |
| rtt_max = result.get("rtt_max", 0) | |
| rtt_str = "" | |
| if rtt_avg > 0: | |
| rtt_str = f", RTT min/avg/max = {rtt_min:.1f}/{rtt_avg:.1f}/{rtt_max:.1f} ms" | |
| if rtt_avg > 100: | |
| rtt_str += " [HIGH LATENCY]" | |
| return f"Ping {src} β {dst}: {tag} {detail}{rtt_str}" | |
| def render_traceroute_result(src: str, dst: str, raw: str) -> str: | |
| lines = [f"Traceroute {src} β {dst}:"] | |
| for line in raw.strip().split("\n"): | |
| line = line.strip() | |
| if line: | |
| if "* * *" in line: | |
| lines.append(f" {line} β no response (possible blackhole or ACL)") | |
| else: | |
| lines.append(f" {line}") | |
| return "\n".join(lines) | |