Compare commits

...
2 Commits
Author SHA1 Message Date
MrBlakeandClaude Opus 5.5 eee60d26ad chore: release 4.12.0
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 10:06:11 +01:00
MrBlakeandClaude Opus 5.5 63fd7b987a feat: LAG members get their LAG in the label and NetBox LAG field
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 10:06:10 +01:00
6 changed files with 139 additions and 9 deletions

No files matched your search

+6
View File
@@ -62,6 +62,12 @@ vSphere and Proxmox VE, HPE/Aruba switches and Windows clients in NetBox.
- VLAN membership per port (untagged and tagged, read from the running
configuration) is written to the interface **label**, e.g. `10` or `1,20-22`;
only VLAN-related lines of the configuration are kept in the raw data
- LAG members (Aruba CX `lag N`, ArubaOS-Switch `trunk … trkN`, Comware
`port link-aggregation group N`) get their LAG in the label, e.g.
`10,20 (LAG 1)`, and are assigned to the LAG interface in NetBox (field
*LAG*; only on the same device). Members without own VLAN configuration take
the VLANs of their LAG. NetBox Utilities 0.25.0 or later ignores the LAG note
when it evaluates VLAN labels
- Automatic port mapping when NetBox names differ from the switch (e.g. `5` ↔
`1/1/5`): equal name, then equal port number, then natural order
- **Mapping review** before the preview: non-obvious port mappings and every
+1 -1
View File
@@ -3,7 +3,7 @@
#define AppName "NetBox Synchronizer"
#ifndef AppVersion
#define AppVersion "4.11.0"
#define AppVersion "4.12.0"
#endif
#define AppPublisher "Louis-Alexander Kerst"
#define AppExeName "NetBoxSynchronizer.exe"
+1 -1
View File
@@ -1,7 +1,7 @@
"""NetBox Synchronizer – VMs, switches and clients from the field into NetBox."""
APP_NAME = "NetBox Synchronizer"
APP_VERSION = "4.11.0"
APP_VERSION = "4.12.0"
APP_PUBLISHER = "Louis-Alexander Kerst"
APP_USER_MODEL_ID = "Louis-AlexanderKerst.NetBoxSynchronizer"
+64 -4
View File
@@ -608,8 +608,8 @@ def _parse_neighbor_table(output):
# VLAN membership from the running configuration
# --------------------------------------------------------------------------
_PORT_RANGE = re.compile(r"^(\D*(?:\d+/)*)(\d+)-(?:\1)?(\d+)$")
_VLAN_CONFIG_LINE = re.compile(r"(?i)^\s*(?:interface\s|vlan\s|#|exit|\s*(?:no\s+)?routing\b)|vlan|tagged|link-type|link-mode")
_PORT_RANGE = re.compile(r"^(\D*(?:\d+/)*[A-Za-z]?)(\d+)-(?:\1)?(\d+)$")
_VLAN_CONFIG_LINE = re.compile(r"(?i)^\s*(?:interface\s|vlan\s|#|exit|\s*(?:no\s+)?routing\b)|vlan|tagged|link-type|link-mode|^\s*lag \d|^trunk\s|link-aggregation")
def parse_vlan_list(text):
@@ -776,6 +776,35 @@ def vlan_label(vlans, limit=64):
return text
def port_label(vlans, lag=None, limit=64):
"""NetBox label of a port: its VLANs plus its LAG, e.g. ``10,20 (LAG 1)``."""
number = re.search(r"(\d+)$", lag or "")
suffix = f"(LAG {number.group(1)})" if number else ""
text = vlan_label(vlans, limit - len(suffix) - 1 if suffix else limit)
return f"{text} {suffix}".strip()
def parse_lags(config, platform):
"""LAG of every member port as ``{interface_key(port): LAG name}``."""
lags = {}
if platform == "aruba_aos":
# "trunk 49-50 trk1 lacp" is a global line.
for match in re.finditer(r"(?im)^trunk\s+(\S+)\s+trk(\d+)\b", clean_output(config or "")):
for port in _expand_ports(match.group(1)):
lags[interface_key(port)] = f"Trk{match.group(2)}"
return lags
member = {"aruba_cx": (re.compile(r"(?i)^lag (\d+)$"), "lag{}"),
"hpe_comware": (re.compile(r"(?i)^port link-aggregation group (\d+)$"), "Bridge-Aggregation{}")}
if platform not in member:
return lags
pattern, name = member[platform]
for match, body in _config_blocks(config or "", re.compile(r"^interface\s+(\S+)\s*$", re.I)):
group = next((found.group(1) for found in map(pattern.match, body) if found), None)
if group:
lags[interface_key(match.group(1))] = name.format(group)
return lags
def _vlan_config_excerpt(config):
"""Keep only VLAN-related lines – the full configuration may contain secrets."""
return "\n".join(line for line in clean_output(config).splitlines() if _VLAN_CONFIG_LINE.search(line))
@@ -826,8 +855,13 @@ def parse_stack(config, platform, interfaces=()):
def build_snapshot(outputs, platform):
interfaces = parse_interfaces(outputs["interfaces"], platform)
vlans = parse_vlans(outputs.get("config", ""), platform)
lags = parse_lags(outputs.get("config", ""), platform)
for interface in interfaces:
interface["vlans"] = vlans.get(interface_key(interface["name"]))
interface["lag"] = lags.get(interface_key(interface["name"]))
if interface["lag"] and not interface["vlans"]:
# Aruba CX/Comware: the VLANs are configured on the LAG and apply to its member ports.
interface["vlans"] = vlans.get(interface_key(interface["lag"]))
interface_ips = parse_interface_ips(outputs.get("ipv4_interfaces", ""), outputs.get("ipv6_interfaces", ""), platform)
lldp_neighbors = parse_neighbors(outputs["neighbors"])
cdp_neighbors = parse_cdp_neighbors(outputs.get("cdp_neighbors", ""))
@@ -1357,7 +1391,7 @@ def plan_switch_sync(nb, config, snapshot):
"interfaces": [
{"name": item["name"], "netbox_name": record.name if record else "", "method": method,
"physical": _physical_interface(item), "owner_id": owners[item["name"]].id,
"vlan_label": vlan_label(without_vlans(item.get("vlans"), ignored_vlans(vlan_map)))}
"vlan_label": port_label(without_vlans(item.get("vlans"), ignored_vlans(vlan_map)), item.get("lag"))}
for item in snapshot["interfaces"] if owners[item["name"]] is not None
for record, method in [matches[item["name"]]]
],
@@ -1620,6 +1654,30 @@ def _sync_switch_stack(nb, device, members, dry_run, report):
report["errors"].append(f"Switch-Stack (NetBox Utilities): {error}")
def _sync_lag_members(snapshot, owners, records, dry_run, report):
"""Assign member ports to their LAG interface (NetBox field ``lag``)."""
names = {interface_key(item["name"]): item["name"] for item in snapshot["interfaces"]}
for interface in snapshot["interfaces"]:
key, lag_key = interface_key(interface["name"]), interface_key(interface.get("lag") or "")
if not interface.get("lag") or key not in records or lag_key not in records:
continue
record, lag_record = records[key], records[lag_key]
owner, lag_owner = owners[interface["name"]], owners[names[lag_key]]
if owner.id != lag_owner.id:
# NetBox only allows a LAG of the same device (or virtual chassis).
report["notes"].append(f"Interface {interface['name']}: LAG {names[lag_key]} liegt auf {lag_owner.name} "
f"– nur im Label vermerkt")
continue
if record and lag_record and related_id(record, "lag") == lag_record.id:
continue
report["changes"].append(f"Interface {record.name if record else interface['name']}: LAG {names[lag_key]}")
if not dry_run and record and lag_record:
try:
record.update({"lag": lag_record.id})
except Exception as error: # noqa: BLE001
report["errors"].append(f"Interface {interface['name']}: LAG {names[lag_key]}: {error}")
def sync_snapshot_to_netbox(nb, config, snapshot, create_cables=True, dry_run=False, log=_noop):
"""Write a discovery snapshot to NetBox.
@@ -1659,7 +1717,7 @@ def sync_snapshot_to_netbox(nb, config, snapshot, create_cables=True, dry_run=Fa
record, method = matches[interface["name"]]
where = f" auf {owner.name}" if owner.id != device_id else ""
port = dict(interface, vlans=without_vlans(interface.get("vlans"), ignored))
label = vlan_label(port["vlans"])
label = port_label(port["vlans"], interface.get("lag"))
try:
if record:
changes = {}
@@ -1708,6 +1766,8 @@ def sync_snapshot_to_netbox(nb, config, snapshot, create_cables=True, dry_run=Fa
except Exception as error: # noqa: BLE001
report["errors"].append(f"Interface {interface['name']}: {error}")
_sync_lag_members(snapshot, owners, records, dry_run, report)
for interface in snapshot["interfaces"]:
key = interface_key(interface["name"])
if key not in records:
+3 -3
View File
@@ -4,7 +4,7 @@ import uuid
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QComboBox, QHBoxLayout, QMenu, QSplitter, QStackedWidget
from netbox_sync.core.switches import PLATFORMS, ignored_vlans, vlan_label, without_vlans
from netbox_sync.core.switches import PLATFORMS, ignored_vlans, port_label, without_vlans
from netbox_sync.ui import widgets
from netbox_sync.ui.dialogs import AddSwitchesDialog, RawDataDialog, SwitchDialog
from netbox_sync.ui.icons import icon
@@ -108,8 +108,8 @@ class SwitchesPage(Page):
dot=lambda row: "success" if row["connected"] else ("text_faint" if row["enabled"] else "danger")),
Column("speed", "Geschwindigkeit", 115, value=lambda row: _speed(row.get("speed_mbps")),
sort=lambda row: row.get("speed_mbps") or 0),
Column("vlans", "VLANs", 110, value=lambda row: vlan_label(without_vlans(
row.get("vlans"), ignored_vlans((self.state.profile or {}).get("vlan_map"))))),
Column("vlans", "VLANs", 110, value=lambda row: port_label(without_vlans(
row.get("vlans"), ignored_vlans((self.state.profile or {}).get("vlan_map"))), row.get("lag"))),
Column("ips", "IP-Adressen", 160, value=lambda row: ", ".join(row.get("ip_addresses", []))),
Column("neighbor", "Nachbar", 170, value=lambda row: (row.get("neighbor") or {}).get("system_name", "")),
Column("remote_port", "Nachbar-Port", 110, value=lambda row: (row.get("neighbor") or {}).get("remote_port", "")),
+64
View File
@@ -5,8 +5,10 @@ from fake_netbox import Endpoint, FakeNetBox, Record
from netbox_sync.core.switches import (
build_snapshot,
match_interfaces,
parse_lags,
parse_stack,
plan_switch_sync,
port_label,
stack_member,
sync_snapshot_to_netbox,
)
@@ -401,3 +403,65 @@ def test_virtual_chassis_positions_are_suggested():
virtual_chassis=SimpleNamespace(id=9), vc_position=2))
plan = plan_switch_sync(nb, switch, snapshot)
assert [entry["device_id"] for entry in plan["stack"]] == [1, 4]
CX_LAG_BRIEF = """
Port Native Mode Type Enabled Status Reason Speed Description
1/1/1 1 trunk 10G-SR yes up 10000 SOEM-E1
1/1/2 1 trunk 10G-SR yes up 10000 SOEM-E1
1/1/23 120 access 1G-BT yes up 1000 FritzBox
lag1 1 trunk -- yes up -- 20000 Etage 1 Uplink
"""
CX_LAG_CONFIG = """vlan 1
vlan 100
interface lag 1
no routing
vlan trunk native 1
vlan trunk allowed 100,110,120,201,209-213
interface 1/1/1
no shutdown
lag 1
interface 1/1/2
no shutdown
lag 1
interface 1/1/23
no routing
vlan access 120
"""
def test_lags_are_parsed_for_all_platforms():
assert parse_lags(CX_LAG_CONFIG, "aruba_cx") == {"1/1/1": "lag1", "1/1/2": "lag1"}
assert parse_lags("trunk 49-50 trk1 lacp\ntrunk 1/A1-1/A2 Trk2 trunk\n", "aruba_aos") == {
"49": "Trk1", "50": "Trk1", "1/a1": "Trk2", "1/a2": "Trk2"}
comware = "interface GigabitEthernet1/0/49\n port link-aggregation group 3\n#\n"
assert parse_lags(comware, "hpe_comware") == {"ge1/0/49": "Bridge-Aggregation3"}
def test_lag_members_take_the_lag_vlans_and_name_the_lag_in_the_label():
snapshot = build_snapshot({"interfaces": CX_LAG_BRIEF, "neighbors": "", "config": CX_LAG_CONFIG}, "aruba_cx")
ports = {item["name"]: item for item in snapshot["interfaces"]}
assert ports["1/1/1"]["lag"] == "lag1" and ports["1/1/23"]["lag"] is None
assert port_label(ports["1/1/1"]["vlans"], ports["1/1/1"]["lag"]) == "1,100,110,120,201,209-213 (LAG 1)"
assert port_label(ports["1/1/23"]["vlans"], ports["1/1/23"]["lag"]) == "120"
assert port_label(None, "Trk2") == "(LAG 2)"
assert len(port_label({"untagged": [], "tagged": list(range(2, 400, 2)), "tagged_all": False}, "lag1")) <= 64
assert " lag 1" in snapshot["raw"]["vlan_config"]
def test_lag_members_are_assigned_to_their_lag_in_netbox():
nb, switch, snapshot = _numbered_setup()
nb.dcim.interfaces.records.append(Record(nb.dcim.interfaces, id=30, name="lag1", device=SimpleNamespace(id=1),
type=SimpleNamespace(value="lag"), enabled=True, cable=None, label=""))
snapshot["interfaces"].append({"name": "lag1", "enabled": True, "connected": True, "ip_addresses": [],
"vlans": None})
for item in snapshot["interfaces"][:2]:
item["lag"] = "lag1"
report = sync_snapshot_to_netbox(nb, switch, snapshot, create_cables=False, dry_run=True)
assert "Interface 1: LAG lag1" in report["changes"] and nb.dcim.interfaces.updates == []
sync_snapshot_to_netbox(nb, switch, snapshot, create_cables=False)
assert nb.dcim.interfaces.get(1).lag == 30 and nb.dcim.interfaces.get(1).label == "10 (LAG 1)"
assert not getattr(nb.dcim.interfaces.get(3), "lag", None)
report = sync_snapshot_to_netbox(nb, switch, snapshot, create_cables=False, dry_run=True)
assert not any("LAG lag1" in change for change in report["changes"]), "already assigned"