Преглед на файлове

fix(snmp): keep the switch CPU interface out of the physical port count (#2022)

Nacho преди 3 седмици
родител
ревизия
7474c2317e

+ 2 - 1
app/public/locales/en/translation.json

@@ -1107,7 +1107,8 @@
       "fam_bridge": "Bridge",
       "fam_tunnel": "Tunnel",
       "fam_virtual": "Virtual",
-      "fam_wifi": "WiFi"
+      "fam_wifi": "WiFi",
+      "fam_cpu": "CPU"
     },
     "radios": {
       "channel": "Channel",

+ 2 - 1
app/public/locales/es/translation.json

@@ -1107,7 +1107,8 @@
       "fam_bridge": "Bridge",
       "fam_tunnel": "Túnel",
       "fam_virtual": "Virtual",
-      "fam_wifi": "WiFi"
+      "fam_wifi": "WiFi",
+      "fam_cpu": "CPU"
     },
     "radios": {
       "channel": "Canal",

+ 3 - 0
app/src/components/routers/PortPanel.tsx

@@ -303,6 +303,9 @@ const FAMILY_LABEL_KEY: Record<string, string> = {
   tunnel: 'routerDetail.ports.fam_tunnel',
   wifi: 'routerDetail.ports.fam_wifi',
   virtual: 'routerDetail.ports.fam_virtual',
+  // #2009: la interfaz de CPU de algunos switches (ifType other(1)) ya no
+  // cuenta como boca, pero sigue listada en el grupo virtual.
+  cpu: 'routerDetail.ports.fam_cpu',
 }
 
 export function PortPanel({ router, extras, className, snmpStats }: {

+ 44 - 1
server-go/internal/snmp/ifTable.go

@@ -100,7 +100,7 @@ func PollIfTable(s *gosnmp.GoSNMP) ([]PortStats, error) {
 	}
 	out := make([]PortStats, 0, len(byIdx))
 	for _, ps := range byIdx {
-		ps.Family = ifFamily(ps.Type)
+		ps.Family = ifFamilyFor(*ps)
 		out = append(out, *ps)
 	}
 	sortByIndex(out)
@@ -130,6 +130,49 @@ func ifFamily(t int) string {
 	return nonPhysicalIfTypeFamilies[t]
 }
 
+// ifTypeOther: IANA other(1). No está en el mapa a propósito (la defensa por
+// exclusión de #1115 lo deja como físico), pero hay un caso en que no lo es.
+const ifTypeOther = 1
+
+// ifFamilyFor clasifica la interfaz con lo que el walk dio de ella: el ifType
+// manda, y para other(1) se mira el nombre (#2009).
+func ifFamilyFor(ps PortStats) string {
+	if fam := ifFamily(ps.Type); fam != "" {
+		return fam
+	}
+	// #2009: los Netgear FastPath exponen la interfaz de CPU como other(1) y
+	// con nombre inconfundible ("CPU Interface for Slot: 3 Port: 1" en
+	// ifDescr, "CPU Interface:  3/1" en ifName). Al no estar other(1) en el
+	// mapa pasaba el filtro de físico y sumaba un puerto fantasma al rack y
+	// al panel de puertos. Se reconoce SOLO other(1) con nombre de CPU:
+	// mapear other(1) en bloque rompería #1115, porque hay vendors con
+	// puertos reales en other.
+	if ps.Type == ifTypeOther && (looksLikeCPUInterface(ps.Name) || looksLikeCPUInterface(ps.Descr)) {
+		return "cpu"
+	}
+	return ""
+}
+
+// looksLikeCPUInterface reconoce el nombre de una interfaz de CPU/gestión
+// ("CPU Interface: 0/5/1", "cpu0", "cpu/0"). No se usa como filtro suelto:
+// solo descarta un puerto cuando además su ifType es other(1).
+func looksLikeCPUInterface(s string) bool {
+	s = strings.ToLower(strings.TrimSpace(s))
+	if !strings.HasPrefix(s, "cpu") {
+		return false
+	}
+	if len(s) == 3 {
+		return true
+	}
+	switch c := s[3]; {
+	case c == ' ' || c == '\t' || c == ':' || c == '/' || c == '-' || c == '_':
+		return true
+	case c >= '0' && c <= '9':
+		return true
+	}
+	return false
+}
+
 func applyPortField(ps *PortStats, oid string, pdu gosnmp.SnmpPDU) {
 	switch oid {
 	case OidIfIndex:

+ 60 - 0
server-go/internal/snmp/ifTable_2009_test.go

@@ -0,0 +1,60 @@
+package snmp
+
+// #2009: un switch gestionado expone su interfaz de CPU como ifType other(1),
+// que no está en el mapa de no-físicas por la defensa de #1115. Pasaba el
+// filtro `!p.family` (rack y panel de puertos) y sumaba un puerto fantasma al
+// frontal. Se reconoce por nombre y SOLO cuando el ifType es other(1).
+
+import "testing"
+
+func TestIfFamilyForCPUInterface2009(t *testing.T) {
+	cases := []struct {
+		name  string
+		ps    PortStats
+		want  string
+		notes string
+	}{
+		{
+			name: "XS708T CPU (ifDescr)",
+			ps: PortStats{Type: 1, Index: 17,
+				Descr: "CPU Interface for Slot: 3 Port: 1", Name: "CPU Interface:  3/1"},
+			want: "cpu",
+		},
+		{
+			name: "S3300 CPU (ifDescr de unidad)",
+			ps: PortStats{Type: 1, Index: 313,
+				Descr: "CPU Interface for Unit: 0 Slot: 5 Port: 1", Name: "CPU Interface:  0/5/1"},
+			want: "cpu",
+		},
+		{
+			name: "nombre suelto cpu0",
+			ps:   PortStats{Type: 1, Name: "cpu0"},
+			want: "cpu",
+		},
+		{
+			name: "other(1) con nombre normal sigue siendo físico",
+			ps:   PortStats{Type: 1, Name: "Port 9", Descr: "10G Ethernet"},
+			want: "",
+		},
+		{
+			name: "puerto real que se llama CPU pero es ethernet: físico",
+			ps:   PortStats{Type: 6, Name: "CPU Interface: 1/1"},
+			want: "",
+		},
+		{
+			name: "cpu con ifType propVirtual mantiene virtual",
+			ps:   PortStats{Type: 53, Name: "cpu0"},
+			want: "virtual",
+		},
+		{
+			name: "lag sigue siendo lag",
+			ps:   PortStats{Type: 161, Name: "po1"},
+			want: "lag",
+		},
+	}
+	for _, c := range cases {
+		if got := ifFamilyFor(c.ps); got != c.want {
+			t.Errorf("%s: ifFamilyFor(%+v) = %q, esperaba %q", c.name, c.ps, got, c.want)
+		}
+	}
+}