Browse Source

fix(agent): fill the iface of nl80211 events and log them at debug (#2231) (#2232)

Nacho 6 days ago
parent
commit
9e9056e0b5

+ 10 - 6
agent/internal/iwevents/iwevents.go

@@ -80,13 +80,17 @@ func parseLine(line string) (Event, bool) {
 	}
 	mac = mac[:17] // aa:bb:cc:dd:ee:ff
 
-	// Extraer iface: lo que va entre el timestamp y ":" antes de "del/new station"
+	// Extraer iface: lo que va entre el timestamp y ":" antes de "del/new
+	// station". Se corta por el primer separador en vez de por una posición
+	// fija: con el formato de `iw event -t` (1726000000.123456: phy2-ap0: ...)
+	// el resto empieza por ": " y el indice 0 descartaba la interfaz siempre.
 	iface := ""
-	if idx := strings.Index(line, ": "); idx > 17 {
-		rest := line[17:] // saltar timestamp (10 dígitos + punto + microsegundos)
-		if idx2 := strings.Index(rest, ": "); idx2 > 0 {
-			iface = strings.TrimSpace(rest[:idx2])
-		}
+	rest := line
+	if idx := strings.Index(line, ": "); idx >= 0 {
+		rest = line[idx+2:]
+	}
+	if idx2 := strings.Index(rest, ": "); idx2 > 0 {
+		iface = strings.TrimSpace(rest[:idx2])
 	}
 
 	return Event{Connected: connected, MAC: mac, Iface: iface}, true

+ 45 - 0
agent/internal/iwevents/iwevents_test.go

@@ -47,3 +47,48 @@ func TestListenNoPanicOnExit(t *testing.T) {
 		t.Fatal("Listen no terminó a tiempo")
 	}
 }
+
+// #2231: la interfaz se descartaba siempre con el formato de `iw event -t`,
+// porque el resto tras el timestamp empieza por ": " y el índice 0 no pasaba
+// la condición. El campo vacío dejaba el log del agente sin radio.
+func TestParseLineFillsIface(t *testing.T) {
+	cases := []struct {
+		name      string
+		line      string
+		connected bool
+		mac       string
+		iface     string
+	}{
+		{
+			name:      "new station",
+			line:      "1726000000.123456: phy2-ap0: new station 5c:cf:7f:1c:0b:06",
+			connected: true,
+			mac:       "5c:cf:7f:1c:0b:06",
+			iface:     "phy2-ap0",
+		},
+		{
+			name:      "del station",
+			line:      "1726000001.654321: phy3-ap0: del station 88:13:bf:68:06:3c",
+			connected: false,
+			mac:       "88:13:bf:68:06:3c",
+			iface:     "phy3-ap0",
+		},
+		{
+			name:      "no interface in the line",
+			line:      "1726000002.000001: new station aa:bb:cc:dd:ee:ff",
+			connected: true,
+			mac:       "aa:bb:cc:dd:ee:ff",
+			iface:     "",
+		},
+	}
+	for _, c := range cases {
+		ev, ok := parseLine(c.line)
+		if !ok {
+			t.Errorf("%s: line not parsed", c.name)
+			continue
+		}
+		if ev.Connected != c.connected || ev.MAC != c.mac || ev.Iface != c.iface {
+			t.Errorf("%s: got %+v, want connected=%v mac=%s iface=%q", c.name, ev, c.connected, c.mac, c.iface)
+		}
+	}
+}

+ 3 - 1
agent/runtime/runtime.go

@@ -197,7 +197,9 @@ func Run(ctx context.Context, opts Options) error {
 				if !ev.Connected {
 					action = "disassoc"
 				}
-				log.Info("[netpulse-agent] iw evento", "action", action, "mac", ev.MAC, "iface", ev.Iface)
+				// Debug: un cliente que va y viene escribe varias de estas
+				// por minuto, y en el embedidor (NetGrip) llegan a syslog.
+				log.Debug("[netpulse-agent] iw evento", "action", action, "mac", ev.MAC, "iface", ev.Iface)
 				payload := prober.BuildWireless(ctx, opts.Slug, opts.Version)
 				withMeta(payload, opts)
 				payload.PanelPort = opts.PanelPort