Spaces:
Sleeping
Sleeping
Download server/telemetry.py from brand0nwalls/NetOps: direct link, hf CLI and curl.
- Browser
- Download file 7.01 kB
-
https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/telemetry.py
- Command line
-
hf download hf://spaces/brand0nwalls/NetOps/server/telemetry.py
-
curl -L -o telemetry.py https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/telemetry.py
7.01 kB
| """Raw telemetry readers. Each function takes a Mininet node and returns Python dicts/strings. | |
| No formatting, no rendering, no interpretation — just data extraction. | |
| """ | |
| import json | |
| import re | |
| def read_bgp_summary(router) -> dict: | |
| """BGP peer sessions, prefix counts, uptime, message stats.""" | |
| out = router.cmd('vtysh -c "show bgp summary json"') | |
| return json.loads(out) | |
| def read_bgp_neighbors_detail(router) -> dict: | |
| """Detailed BGP neighbor info including received/advertised prefixes.""" | |
| out = router.cmd('vtysh -c "show bgp neighbors json"') | |
| return json.loads(out) | |
| def read_ospf_neighbors(router) -> dict: | |
| """OSPF adjacency state, dead timers, DR election.""" | |
| out = router.cmd('vtysh -c "show ip ospf neighbor json"') | |
| return json.loads(out) | |
| def read_ospf_interface(router) -> dict: | |
| """OSPF interface details including cost, area, state.""" | |
| out = router.cmd('vtysh -c "show ip ospf interface json"') | |
| return json.loads(out) | |
| def read_routes(router) -> dict: | |
| """Active routes with protocol source and nexthops.""" | |
| out = router.cmd('vtysh -c "show ip route json"') | |
| return json.loads(out) | |
| def read_interfaces(router) -> list[dict]: | |
| """Interface RX/TX bytes, packets, errors, drops.""" | |
| out = router.cmd("ip -j -s link") | |
| return json.loads(out) | |
| def read_iptables(router) -> str: | |
| """iptables rules — firewall state.""" | |
| return router.cmd("iptables -L -n") | |
| def read_cpu_status(router) -> dict: | |
| """CPU load status.""" | |
| raw = router.cmd("cat /proc/loadavg") | |
| parts = raw.strip().split() | |
| load_1m = float(parts[0]) if parts else 0.0 | |
| return {"load_1m": load_1m, "raw": raw.strip()} | |
| def read_bgp_detail(router) -> dict: | |
| """BGP path attributes — local-pref, AS-path, communities per prefix.""" | |
| out = router.cmd('vtysh -c "show bgp ipv4 unicast json"') | |
| return json.loads(out) | |
| def read_queue_stats(router, interface: str) -> dict: | |
| """TC qdisc stats: drops, overlimits, backlog — congestion indicators.""" | |
| out = router.cmd(f"tc -s qdisc show dev {interface}") | |
| return _parse_tc_output(out) | |
| def read_protocol_counters(router) -> dict: | |
| """TCP retransmits, ICMP errors, UDP drops from /proc/net/snmp.""" | |
| snmp = router.cmd("cat /proc/net/snmp") | |
| netstat = router.cmd("cat /proc/net/netstat") | |
| return { | |
| "snmp": _parse_proc_table(snmp), | |
| "netstat": _parse_proc_table(netstat), | |
| } | |
| def read_ovs_flows(switch) -> list[dict]: | |
| """Per-flow packet/byte counters and match rules from OVS.""" | |
| out = switch.cmd(f"ovs-ofctl dump-flows {switch.name}") | |
| return _parse_ovs_flows(out) | |
| def read_ovs_ports(switch) -> str: | |
| """Per-port RX/TX, drops, errors from OVS.""" | |
| return switch.cmd(f"ovs-ofctl dump-ports {switch.name}") | |
| def read_ovs_fdb(switch) -> str: | |
| """Learned MACs, port, age from OVS FDB.""" | |
| return switch.cmd(f"ovs-appctl fdb/show {switch.name}") | |
| def read_all_router_telemetry(router) -> dict: | |
| """Read all telemetry streams from a single router.""" | |
| result = {} | |
| readers = { | |
| "bgp_summary": read_bgp_summary, | |
| "ospf_neighbors": read_ospf_neighbors, | |
| "routes": read_routes, | |
| "interfaces": read_interfaces, | |
| "protocol_counters": read_protocol_counters, | |
| } | |
| for key, fn in readers.items(): | |
| try: | |
| result[key] = fn(router) | |
| except (json.JSONDecodeError, Exception) as e: | |
| result[f"{key}_error"] = str(e) | |
| return result | |
| def read_all_switch_telemetry(switch) -> dict: | |
| """Read all telemetry streams from a single OVS switch.""" | |
| result = {} | |
| readers = { | |
| "flows": read_ovs_flows, | |
| "ports": read_ovs_ports, | |
| "fdb": read_ovs_fdb, | |
| } | |
| for key, fn in readers.items(): | |
| try: | |
| result[key] = fn(switch) | |
| except Exception as e: | |
| result[f"{key}_error"] = str(e) | |
| return result | |
| def _parse_tc_output(raw: str) -> dict: | |
| """Parse tc qdisc output into structured data.""" | |
| stripped = raw.strip() | |
| if stripped.startswith("{"): | |
| try: | |
| return json.loads(stripped) | |
| except (json.JSONDecodeError, ValueError): | |
| pass | |
| result = {"raw": stripped, "qdiscs": []} | |
| current = None | |
| for line in raw.split("\n"): | |
| line = line.strip() | |
| if not line: | |
| continue | |
| if line.startswith("qdisc"): | |
| if current: | |
| result["qdiscs"].append(current) | |
| parts = line.split() | |
| current = {"type": parts[1] if len(parts) > 1 else "unknown", "raw": line} | |
| elif current and "Sent" in line: | |
| m = re.search(r"Sent (\d+) bytes (\d+) pkt \(dropped (\d+), overlimits (\d+)", line) | |
| if m: | |
| current["sent_bytes"] = int(m.group(1)) | |
| current["sent_pkts"] = int(m.group(2)) | |
| current["dropped"] = int(m.group(3)) | |
| current["overlimits"] = int(m.group(4)) | |
| elif current and "backlog" in line: | |
| m = re.search(r"backlog (\S+) (\d+)p", line) | |
| if m: | |
| current["backlog_bytes"] = m.group(1) | |
| current["backlog_pkts"] = int(m.group(2)) | |
| if current: | |
| result["qdiscs"].append(current) | |
| return result | |
| def _parse_proc_table(raw: str) -> dict: | |
| """Parse /proc/net/snmp or /proc/net/netstat into {Protocol: {Key: Value}}.""" | |
| result = {} | |
| lines = raw.strip().split("\n") | |
| i = 0 | |
| while i + 1 < len(lines): | |
| header_line = lines[i].strip() | |
| value_line = lines[i + 1].strip() | |
| if not header_line or not value_line: | |
| i += 1 | |
| continue | |
| h_parts = header_line.split() | |
| v_parts = value_line.split() | |
| if len(h_parts) < 2 or len(v_parts) < 2: | |
| i += 1 | |
| continue | |
| proto = h_parts[0].rstrip(":") | |
| if v_parts[0].rstrip(":") == proto: | |
| keys = h_parts[1:] | |
| vals = v_parts[1:] | |
| if proto not in result: | |
| result[proto] = {} | |
| for k, v in zip(keys, vals): | |
| try: | |
| result[proto][k] = int(v) | |
| except ValueError: | |
| result[proto][k] = v | |
| i += 2 | |
| else: | |
| i += 1 | |
| return result | |
| def _parse_ovs_flows(raw: str) -> list[dict]: | |
| """Parse ovs-ofctl dump-flows output into structured list.""" | |
| flows = [] | |
| for line in raw.split("\n"): | |
| line = line.strip() | |
| if not line or line.startswith("NXST") or line.startswith("OFPST"): | |
| continue | |
| flow = {"raw": line} | |
| npkts = re.search(r"n_packets=(\d+)", line) | |
| nbytes = re.search(r"n_bytes=(\d+)", line) | |
| if npkts: | |
| flow["n_packets"] = int(npkts.group(1)) | |
| if nbytes: | |
| flow["n_bytes"] = int(nbytes.group(1)) | |
| actions = re.search(r"actions=(.+)", line) | |
| if actions: | |
| flow["actions"] = actions.group(1) | |
| flows.append(flow) | |
| return flows | |