Move the port inspector onto device pages
Lint / JS (eslint) (push) Successful in 1m14s
Lint / Python (flake8) (push) Successful in 2m12s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 1m3s
Test / Python Tests (pytest) (push) Successful in 1m11s
Lint / Python (flake8) (pull_request) Successful in 1m1s
Lint / JS (eslint) (pull_request) Successful in 10s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 56s
Test / Python Tests (pytest) (pull_request) Successful in 1m5s

Replace the all-switches Inspector page with a per-device front panel,
port table and detail panel. /inspector now redirects to the dashboard,
and /device/<name>#port-<n> links directly to a port.

Restore neighbor names after UniFi's LLDP change: read local_port_idx
and resolve neighbors by MAC using Prometheus NIC data, UniFi devices
and the wired client table. This also brings back Run Diagnostics on
server ports, which depends on a named neighbor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-05 10:11:11 -05:00
co-authored by Claude Opus 5.5
parent e32499b7f6
commit bdcfb76c25
13 changed files with 484 additions and 226 deletions
+44 -1
View File
@@ -21,6 +21,7 @@ import requests
from urllib3.exceptions import InsecureRequestWarning
import db
import devices
import modem as modem_api
@@ -150,6 +151,7 @@ class UnifiClient:
'latency_ms': w.get('latency'), 'availability': w.get('availability'),
}
return {
'mac': d.get('mac', ''),
'firmware': d.get('version', ''),
'uptime_s': d.get('uptime'),
'cpu_pct': number(stats.get('cpu')),
@@ -174,6 +176,20 @@ class UnifiClient:
],
}
def get_wired_clients(self) -> List[dict]:
"""Return wired clients as {mac, sw_mac, sw_port}; empty if unavailable."""
try:
url = f'{self.base_url}/proxy/network/api/s/{self.site_id}/stat/sta'
resp = self.session.get(url, headers=self.headers, timeout=15)
resp.raise_for_status()
return [
{'mac': c.get('mac', ''), 'sw_mac': c.get('sw_mac', ''), 'sw_port': c.get('sw_port')}
for c in resp.json().get('data', []) if c.get('is_wired')
]
except Exception as e:
logger.error(f'UniFi client list error: {e}')
return []
def get_system_events(self, since_ms: int, keys: tuple) -> Optional[List[dict]]:
"""Return system log entries with the given keys, or None if the log is unavailable."""
url = f'{self.base_url}/proxy/network/v2/api/site/{self.site_id}/system-log/all'
@@ -251,7 +267,8 @@ class UnifiClient:
# Build LLDP neighbor map (keyed by port_idx)
lldp_map: Dict[int, dict] = {}
for entry in dev.get('lldp_table', []):
pidx = entry.get('lldp_port_idx')
# Current UniFi Network versions use local_port_idx
pidx = entry.get('local_port_idx', entry.get('lldp_port_idx'))
if pidx is not None:
lldp_map[int(pidx)] = {
'chassis_id': entry.get('chassis_id', ''),
@@ -730,6 +747,7 @@ class LinkStatsCollector:
logger.warning(f'UniFi switch port collection failed: {e}')
unifi_devices = (self.unifi.get_device_details() or {}) if self.unifi else {}
self._resolve_neighbors(unifi_switches, unifi_devices, instance_map)
return {
'hosts': result_hosts,
@@ -738,6 +756,31 @@ class LinkStatsCollector:
'updated': datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC'),
}
def _resolve_neighbors(self, switches: dict, unifi_devices: dict, instance_map: Dict[str, str]) -> None:
"""Name port neighbors: UniFi's LLDP data has only MACs, and many servers send none."""
host_macs: Dict[str, tuple] = {}
host_ips: Dict[str, str] = {}
# Bonds share their MAC with their member NICs, and LLDP runs on the NIC.
results = sorted(
self.prom.query('node_network_info'),
key=lambda r: r['metric'].get('device', '').startswith('bond'),
)
for r in results:
metric = r['metric']
device, mac = metric.get('device', ''), metric.get('address', '').lower()
instance = metric.get('instance', '')
if not is_physical_interface(device) or not mac or mac == '00:00:00:00:00:00':
continue
ip = instance.split(':')[0]
host = instance_map.get(instance, ip)
host_macs.setdefault(mac, (host, device))
host_ips[ip] = host
device_macs = {
info['mac'].lower(): name for name, info in unifi_devices.items() if info.get('mac')
}
devices.resolve_lldp_neighbors(switches, host_macs, device_macs, host_ips)
devices.apply_client_locations(switches, self.unifi.get_wired_clients(), host_macs, device_macs)
def _compute_unifi_rates(self, raw: Dict[str, dict], now: float) -> Dict[str, dict]:
"""Compute per-port byte/error rates from delta against previous snapshot."""
dt = now - self._prev_unifi_time if self._prev_unifi_time > 0 else 0