Skip to content

Commit 0d49742

Browse files
authored
Merge pull request #10 from rokernel/fix/lldp-remote-port-display
fix: improve LLDP remote port labels for mixed neighbors
2 parents 87aae99 + 8f9f1c1 commit 0d49742

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

fixtures/test/appl_db_data.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@
4040
"LLDP_ENTRY_TABLE:Ethernet88": {
4141
"lldp_rem_chassis_id": "74:86:e2:6d:df:a5",
4242
"lldp_rem_man_addr": "192.168.240.123",
43+
"lldp_rem_port_desc": "Ethernet88",
4344
"lldp_rem_port_id": "hundredGigE1/23",
45+
"lldp_rem_port_id_subtype": "7",
4446
"lldp_rem_sys_name": "net-tor-lab001.lau1"
4547
},
4648
"LLDP_ENTRY_TABLE:eth0": {
4749
"lldp_rem_chassis_id": "00:11:22:33:44:55",
4850
"lldp_rem_man_addr": "192.168.240.1",
49-
"lldp_rem_port_id": "mgmt0",
51+
"lldp_rem_port_desc": "ge-0/0/15.0",
52+
"lldp_rem_port_id": "535",
53+
"lldp_rem_port_id_subtype": "7",
5054
"lldp_rem_sys_name": "oob-switch01"
5155
},
5256
"LLDP_ENTRY_TABLE:Ethernet0": {

internal/collector/collector_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ func TestLldpCollector(t *testing.T) {
275275
`
276276

277277
neighborExpected := `
278-
sonic_lldp_neighbor_info{local_interface="Ethernet88",local_role="frontpanel",remote_chassis_id="74:86:e2:6d:df:a5",remote_mgmt_ip="192.168.240.123",remote_port_id="hundredGigE1/23",remote_system_name="net-tor-lab001.lau1"} 1
279-
sonic_lldp_neighbor_info{local_interface="eth0",local_role="management",remote_chassis_id="00:11:22:33:44:55",remote_mgmt_ip="192.168.240.1",remote_port_id="mgmt0",remote_system_name="oob-switch01"} 1
278+
sonic_lldp_neighbor_info{local_interface="Ethernet88",local_role="frontpanel",remote_chassis_id="74:86:e2:6d:df:a5",remote_mgmt_ip="192.168.240.123",remote_port_desc="Ethernet88",remote_port_display="Ethernet88",remote_port_id="hundredGigE1/23",remote_port_id_subtype="7",remote_system_name="net-tor-lab001.lau1"} 1
279+
sonic_lldp_neighbor_info{local_interface="eth0",local_role="management",remote_chassis_id="00:11:22:33:44:55",remote_mgmt_ip="192.168.240.1",remote_port_desc="ge-0/0/15.0",remote_port_display="ge-0/0/15.0",remote_port_id="535",remote_port_id_subtype="7",remote_system_name="oob-switch01"} 1
280280
`
281281

282282
if err := testutil.CollectAndCompare(lldpCollector, strings.NewReader(neighborMetadata+neighborExpected), "sonic_lldp_neighbor_info"); err != nil {

internal/collector/lldp_collector.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func NewLldpCollector(logger *slog.Logger) *lldpCollector {
5252

5353
collector := &lldpCollector{
5454
lldpNeighborInfo: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "neighbor_info"),
55-
"Non-numeric data about LLDP neighbor, value is always 1", []string{"local_interface", "local_role", "remote_system_name", "remote_port_id", "remote_chassis_id", "remote_mgmt_ip"}, nil),
55+
"Non-numeric data about LLDP neighbor, value is always 1", []string{"local_interface", "local_role", "remote_system_name", "remote_port_id", "remote_port_desc", "remote_port_id_subtype", "remote_port_display", "remote_chassis_id", "remote_mgmt_ip"}, nil),
5656
lldpNeighbors: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "neighbors"),
5757
"Number of LLDP neighbors exported", nil, nil),
5858
scrapeDuration: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "scrape_duration_seconds"),
@@ -202,10 +202,13 @@ func (collector *lldpCollector) scrapeMetrics(ctx context.Context) ([]prometheus
202202

203203
remoteSystemName := lldpData["lldp_rem_sys_name"]
204204
remotePortID := lldpData["lldp_rem_port_id"]
205+
remotePortDesc := lldpData["lldp_rem_port_desc"]
206+
remotePortIDSubtype := lldpData["lldp_rem_port_id_subtype"]
207+
remotePortDisplay := resolvedRemotePortDisplay(remotePortID, remotePortDesc, remotePortIDSubtype)
205208
remoteChassisID := lldpData["lldp_rem_chassis_id"]
206209
remoteMgmtIP := lldpData["lldp_rem_man_addr"]
207210

208-
if remoteSystemName == "" && remotePortID == "" && remoteChassisID == "" && remoteMgmtIP == "" {
211+
if remoteSystemName == "" && remotePortID == "" && remotePortDesc == "" && remoteChassisID == "" && remoteMgmtIP == "" {
209212
skippedEntries++
210213
continue
211214
}
@@ -223,6 +226,9 @@ func (collector *lldpCollector) scrapeMetrics(ctx context.Context) ([]prometheus
223226
localRole,
224227
remoteSystemName,
225228
remotePortID,
229+
remotePortDesc,
230+
remotePortIDSubtype,
231+
remotePortDisplay,
226232
remoteChassisID,
227233
remoteMgmtIP,
228234
))
@@ -297,3 +303,17 @@ func parseIntEnv(logger *slog.Logger, key string, defaultValue int) int {
297303

298304
return parsedValue
299305
}
306+
307+
func resolvedRemotePortDisplay(remotePortID, remotePortDesc, remotePortIDSubtype string) string {
308+
subtype := strings.ToLower(strings.TrimSpace(remotePortIDSubtype))
309+
310+
if remotePortDesc != "" && (subtype == "7" || subtype == "local" || subtype == "3" || subtype == "mac") {
311+
return remotePortDesc
312+
}
313+
314+
if remotePortID != "" {
315+
return remotePortID
316+
}
317+
318+
return remotePortDesc
319+
}

0 commit comments

Comments
 (0)