Cutsheet: port names in UniFi that no longer fit, document a cable from topology
Lint / JS (eslint) (push) Successful in 23s
Lint / Python (flake8) (push) Successful in 1m10s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Lint / Python (flake8) (pull_request) Successful in 53s
Lint / JS (eslint) (pull_request) Successful in 11s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (push) Successful in 1m26s
Test / Python Tests (pytest) (push) Successful in 1m49s
Security / Python Security (bandit) (pull_request) Successful in 2m17s
Test / Python Tests (pytest) (pull_request) Successful in 2m12s
Lint / JS (eslint) (push) Successful in 23s
Lint / Python (flake8) (push) Successful in 1m10s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Lint / Python (flake8) (pull_request) Successful in 53s
Lint / JS (eslint) (pull_request) Successful in 11s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (push) Successful in 1m26s
Test / Python Tests (pytest) (push) Successful in 1m49s
Security / Python Security (bandit) (pull_request) Successful in 2m17s
Test / Python Tests (pytest) (pull_request) Successful in 2m12s
- topology.port_name_report: switch port names that no longer fit what is plugged in, as a checklist with suggested names: named after the wrong device, named after a device that moved to another port, named with nothing plugged in (and who was last seen there), and still "Port N" with a known device on it. Ports named after a device that is only off are left alone. Gandalf only reports; it never renames anything in UniFi. - /api/cutsheet returns it as port_names; the cutsheet page shows it as "Port names in UniFi", grouped, with a summary chip. - The topology cable panel's "not documented" gets a "document it" link that opens the cutsheet's new-cable form with both ends, the NIC and DAC filled in; /cutsheet?... now takes b_device, b_port and cable_type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+93
@@ -135,6 +135,99 @@ def _named_device(text: str, known: Dict[str, str], skip=()) -> Optional[str]:
|
||||
return None
|
||||
|
||||
|
||||
# What each port-name finding means, most urgent first
|
||||
PORT_NAME_KINDS = ('wrong', 'moved', 'empty', 'unnamed')
|
||||
|
||||
|
||||
def _port_name_suggestion(neighbour: str, kind: str, port: dict, links: int) -> str:
|
||||
if kind in ('router', 'switch'):
|
||||
return 'Uplink' if port.get('is_uplink') else f'Downlink to {neighbour}'
|
||||
iface = str((port.get('lldp') or {}).get('port_id') or '')
|
||||
# A server with two cables: say which NIC, so the two ports read differently
|
||||
if kind == 'host' and links > 1 and iface and not _MAC_IN_TEXT.fullmatch(iface):
|
||||
return f'{neighbour} {iface}'
|
||||
return neighbour
|
||||
|
||||
|
||||
def port_name_report(snapshot: dict, link_stats: dict, modem: Optional[dict],
|
||||
known_uplinks: Optional[dict] = None) -> List[dict]:
|
||||
"""UniFi port names that no longer fit what is plugged in, as a checklist to fix in UniFi.
|
||||
|
||||
Each row is {switch, port_idx, port_name, kind, neighbour, suggestion, detail}, kind one of:
|
||||
wrong: named after one device, with a different known device plugged in
|
||||
moved: named after a device that is now linked on another port, with nothing known here
|
||||
empty: a custom name on a port with nothing plugged in (with who was last seen there)
|
||||
unnamed: still "Port N" / "SFP+ N" with a known device plugged in
|
||||
Gandalf only reports these; it never renames anything in UniFi.
|
||||
"""
|
||||
snapshot = snapshot or {}
|
||||
link_stats = link_stats or {}
|
||||
switches = link_stats.get('unifi_switches') or {}
|
||||
configured = set(snapshot.get('hosts') or {})
|
||||
kinds = {e['name']: e['kind'] for e in devices.build_index(snapshot, link_stats, modem)
|
||||
if e['kind'] != 'host' or e['name'] in configured}
|
||||
known = {_norm(n): n for n in kinds}
|
||||
|
||||
# Where each known device is linked right now, and who was last seen on each port
|
||||
live_at: Dict[str, List[str]] = {}
|
||||
for sw_name, sw in sorted(switches.items()):
|
||||
for p in (sw.get('ports') or {}).values():
|
||||
nb = (p.get('lldp') or {}).get('system_name') or ''
|
||||
if p.get('up') and nb in kinds and p.get('port_idx') is not None:
|
||||
live_at.setdefault(nb, []).append(f'{sw_name} port {p["port_idx"]}')
|
||||
last_seen: Dict[Tuple[str, int], Tuple[str, str]] = {}
|
||||
for name, entry in (known_uplinks or {}).items():
|
||||
for up in (entry or {}).get('uplinks') or []:
|
||||
if up.get('switch') and up.get('port_idx') is not None:
|
||||
last_seen[(up['switch'], up['port_idx'])] = (name, (entry or {}).get('seen') or '')
|
||||
|
||||
rows: List[dict] = []
|
||||
for sw_name in sorted(switches):
|
||||
if kinds.get(sw_name) in POWER_KINDS or _POWER_MODELS.get((switches[sw_name] or {}).get('model', '')):
|
||||
continue # a PDU's one management port
|
||||
for pname, port in (switches[sw_name].get('ports') or {}).items():
|
||||
idx = port.get('port_idx')
|
||||
if idx is None:
|
||||
continue
|
||||
nb = (port.get('lldp') or {}).get('system_name') or ''
|
||||
nb = nb if nb in kinds and nb != sw_name else ''
|
||||
generic = _is_generic_port_name(pname)
|
||||
row = {'switch': sw_name, 'port_idx': idx, 'port_name': pname, 'neighbour': nb,
|
||||
'suggestion': '', 'detail': ''}
|
||||
named = None if generic else _named_device(pname, known, {_norm(sw_name)})
|
||||
if nb and port.get('up'):
|
||||
suggestion = _port_name_suggestion(nb, kinds[nb], port, len(live_at.get(nb, [])))
|
||||
if named and named != nb and _norm(named) != _norm(nb):
|
||||
elsewhere = [w for w in live_at.get(named, []) if w != f'{sw_name} port {idx}']
|
||||
row.update(kind='wrong', suggestion=suggestion,
|
||||
detail=f'Named after {named}, but {nb} is plugged in'
|
||||
+ (f'; {named} is on {", ".join(elsewhere)}' if elsewhere else ''))
|
||||
elif generic and not pname.lower().startswith(('uplink', 'downlink')):
|
||||
row.update(kind='unnamed', suggestion=suggestion,
|
||||
detail=f'{nb} is plugged in')
|
||||
else:
|
||||
continue
|
||||
elif named and named in live_at:
|
||||
row.update(kind='moved', suggestion=f'Port {idx}' if not port.get('up') else '',
|
||||
detail=f'Named after {named}, which is now on {", ".join(live_at[named])}')
|
||||
elif named:
|
||||
continue # named after a known device that is just not linked now: the name is right
|
||||
elif not generic and not port.get('up') and not port.get('lldp'):
|
||||
seen = last_seen.get((sw_name, idx))
|
||||
detail = 'Nothing plugged in'
|
||||
if seen:
|
||||
detail += f'; last seen here: {seen[0]}' + (f' ({seen[1]})' if seen[1] else '')
|
||||
if seen[0] in live_at:
|
||||
detail += f', now on {", ".join(live_at[seen[0]])}'
|
||||
row.update(kind='empty', detail=detail)
|
||||
else:
|
||||
continue
|
||||
rows.append(row)
|
||||
order = {k: i for i, k in enumerate(PORT_NAME_KINDS)}
|
||||
rows.sort(key=lambda r: (order[r['kind']], r['switch'].lower(), r['port_idx']))
|
||||
return rows
|
||||
|
||||
|
||||
_MAC_IN_TEXT = re.compile(r'\b([0-9a-f]{2}(?:[:-][0-9a-f]{2}){5})\b', re.IGNORECASE)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user