diff --git a/fixtures/test/appl_db_data.json b/fixtures/test/appl_db_data.json index 8ad8e0f..bb7e6e5 100644 --- a/fixtures/test/appl_db_data.json +++ b/fixtures/test/appl_db_data.json @@ -40,13 +40,17 @@ "LLDP_ENTRY_TABLE:Ethernet88": { "lldp_rem_chassis_id": "74:86:e2:6d:df:a5", "lldp_rem_man_addr": "192.168.240.123", + "lldp_rem_port_desc": "Ethernet88", "lldp_rem_port_id": "hundredGigE1/23", + "lldp_rem_port_id_subtype": "7", "lldp_rem_sys_name": "net-tor-lab001.lau1" }, "LLDP_ENTRY_TABLE:eth0": { "lldp_rem_chassis_id": "00:11:22:33:44:55", "lldp_rem_man_addr": "192.168.240.1", - "lldp_rem_port_id": "mgmt0", + "lldp_rem_port_desc": "ge-0/0/15.0", + "lldp_rem_port_id": "535", + "lldp_rem_port_id_subtype": "7", "lldp_rem_sys_name": "oob-switch01" }, "LLDP_ENTRY_TABLE:Ethernet0": { diff --git a/internal/collector/collector_test.go b/internal/collector/collector_test.go index 7a62189..052a389 100644 --- a/internal/collector/collector_test.go +++ b/internal/collector/collector_test.go @@ -275,8 +275,8 @@ func TestLldpCollector(t *testing.T) { ` neighborExpected := ` - 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 - 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 + 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 + 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 ` if err := testutil.CollectAndCompare(lldpCollector, strings.NewReader(neighborMetadata+neighborExpected), "sonic_lldp_neighbor_info"); err != nil { diff --git a/internal/collector/lldp_collector.go b/internal/collector/lldp_collector.go index 344df2f..164a8b2 100644 --- a/internal/collector/lldp_collector.go +++ b/internal/collector/lldp_collector.go @@ -52,7 +52,7 @@ func NewLldpCollector(logger *slog.Logger) *lldpCollector { collector := &lldpCollector{ lldpNeighborInfo: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "neighbor_info"), - "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), + "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), lldpNeighbors: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "neighbors"), "Number of LLDP neighbors exported", nil, nil), scrapeDuration: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "scrape_duration_seconds"), @@ -202,10 +202,13 @@ func (collector *lldpCollector) scrapeMetrics(ctx context.Context) ([]prometheus remoteSystemName := lldpData["lldp_rem_sys_name"] remotePortID := lldpData["lldp_rem_port_id"] + remotePortDesc := lldpData["lldp_rem_port_desc"] + remotePortIDSubtype := lldpData["lldp_rem_port_id_subtype"] + remotePortDisplay := resolvedRemotePortDisplay(remotePortID, remotePortDesc, remotePortIDSubtype) remoteChassisID := lldpData["lldp_rem_chassis_id"] remoteMgmtIP := lldpData["lldp_rem_man_addr"] - if remoteSystemName == "" && remotePortID == "" && remoteChassisID == "" && remoteMgmtIP == "" { + if remoteSystemName == "" && remotePortID == "" && remotePortDesc == "" && remoteChassisID == "" && remoteMgmtIP == "" { skippedEntries++ continue } @@ -223,6 +226,9 @@ func (collector *lldpCollector) scrapeMetrics(ctx context.Context) ([]prometheus localRole, remoteSystemName, remotePortID, + remotePortDesc, + remotePortIDSubtype, + remotePortDisplay, remoteChassisID, remoteMgmtIP, )) @@ -297,3 +303,17 @@ func parseIntEnv(logger *slog.Logger, key string, defaultValue int) int { return parsedValue } + +func resolvedRemotePortDisplay(remotePortID, remotePortDesc, remotePortIDSubtype string) string { + subtype := strings.ToLower(strings.TrimSpace(remotePortIDSubtype)) + + if remotePortDesc != "" && (subtype == "7" || subtype == "local" || subtype == "3" || subtype == "mac") { + return remotePortDesc + } + + if remotePortID != "" { + return remotePortID + } + + return remotePortDesc +}