Просмотр исходного кода

feat(wifi): per-radio noise and BSSID on the unit detail (#1216)

The radio card now shows the band's noise floor and the interface's BSSID, straight from the iwinfo info call the agent already makes. Optional end to end, legacy format still parses. Agent 3.0.11. Closes #1213
Nacho 2 месяцев назад
Родитель
Сommit
a88af99a84

+ 1 - 1
agent/VERSION

@@ -1 +1 @@
-3.0.10
+3.0.11

+ 37 - 6
agent/probe/probe.go

@@ -58,7 +58,8 @@ const (
 	// CmdWirelessCombined (#368): iwinfo en UNA pasada por interfaz (info una
 	// vez + assoclist una vez) emitiendo clientes y resumen de radio juntos.
 	// Sustituye a CmdIwinfoAssoc+CmdRadios cuando ubus no está: la mitad de
-	// spawns. Líneas "C|mac|sig|freq" (clientes) y "R|freq|ch|ht|tx|n" (radio).
+	// spawns. Líneas "C|mac|sig|freq" (clientes) y
+	// "R|freq|ch|ht|tx|bssid|noise|n" (radio; #1213).
 	CmdWirelessCombined = `for i in $(iwinfo 2>/dev/null | awk '/^[a-z]/ {print $1}'); do ` +
 		`info=$(iwinfo "$i" info 2>/dev/null) || continue; ` +
 		`echo "$info" | grep -q ESSID || continue; ` +
@@ -66,11 +67,14 @@ const (
 		`ch=$(echo "$info" | sed -n 's/.*Channel: \([0-9][0-9]*\).*/\1/p' | head -1); ` +
 		`ht=$(echo "$info" | sed -n 's/.*HT [Mm]ode: \([A-Za-z0-9]*\).*/\1/p' | head -1); ` +
 		`tx=$(echo "$info" | sed -n 's/.*Tx-Power: \([0-9]*\).*/\1/p' | head -1); ` +
+		`bssid=$(echo "$info" | sed -n 's/.*Access Point: \([0-9A-Fa-f:]\{17\}\).*/\1/p' | head -1); ` +
+		`noise=$(echo "$info" | sed -n 's/.*Noise: \(-\{0,1\}[0-9][0-9]*\).*/\1/p' | head -1); ` +
 		`al=$(iwinfo "$i" assoclist 2>/dev/null); ` +
 		`n=$(echo "$al" | grep -c '^[0-9A-Fa-f:]'); ` +
-		`echo "R|$freq|$ch|$ht|$tx|$n"; ` +
+		`echo "R|$freq|$ch|$ht|$tx|$bssid|$noise|$n"; ` +
 		`echo "$al" | sed -n "s/^\([0-9A-Fa-f:]\{17\}\) *\(-[0-9]*\).*/C|\1|\2|$freq/p"; done`
-	// CmdRadios: "freq|ch|ht|tx|n" por radio (se agrega por banda al parsear).
+	// CmdRadios: "freq|ch|ht|tx|bssid|noise|n" por radio (se agrega por banda
+	// al parsear). #1213: bssid + noise de iwinfo info.
 	CmdRadios = `for i in $(iwinfo 2>/dev/null | awk '/^[a-z]/ {print $1}'); do ` +
 		`info=$(iwinfo "$i" info 2>/dev/null) || continue; ` +
 		`echo "$info" | grep -q ESSID || continue; ` +
@@ -78,8 +82,10 @@ const (
 		`ch=$(echo "$info" | sed -n 's/.*Channel: \([0-9][0-9]*\).*/\1/p' | head -1); ` +
 		`ht=$(echo "$info" | sed -n 's/.*HT [Mm]ode: \([A-Za-z0-9]*\).*/\1/p' | head -1); ` +
 		`tx=$(echo "$info" | sed -n 's/.*Tx-Power: \([0-9]*\).*/\1/p' | head -1); ` +
+		`bssid=$(echo "$info" | sed -n 's/.*Access Point: \([0-9A-Fa-f:]\{17\}\).*/\1/p' | head -1); ` +
+		`noise=$(echo "$info" | sed -n 's/.*Noise: \(-\{0,1\}[0-9][0-9]*\).*/\1/p' | head -1); ` +
 		`n=$(iwinfo "$i" assoclist 2>/dev/null | grep -c '^[0-9A-Fa-f:]'); ` +
-		`echo "$freq|$ch|$ht|$tx|$n"; done`
+		`echo "$freq|$ch|$ht|$tx|$bssid|$noise|$n"; done`
 	// CmdScan (#452): scan pasivo de vecinos con `iw dev`. Lista interfaces,
 	// emite "==IFACE==<iface>" antes de cada scan y parsea BSS/freq/signal/SSID.
 	// Si `iw` no está disponible, la sección Scans queda vacía (best-effort).
@@ -339,6 +345,12 @@ type Radio struct {
 	// pueda construir cambios de canal (uci set wireless.<section>.channel).
 	// Vacía si no se pudo resolver (#500).
 	Section string `json:"section,omitempty"`
+	// #1213/#1206: suelo de ruido (dBm, de iwinfo info "Noise") y BSSID de la
+	// interfaz AP ("Access Point"). Opcionales: drivers que no los reportan →
+	// nil/vacío y el front no dibuja esas filas. El BSSID es "el sitio donde
+	// viven" las MACs de interfaz que antes asomaban como clientes.
+	NoiseDbm *int   `json:"noiseDbm,omitempty"`
+	BSSID    string `json:"bssid,omitempty"`
 }
 
 // ---------------------------------------------------------------------------
@@ -1455,11 +1467,30 @@ func ParseRadios(out string) []Radio {
 		}
 		ch, _ := strconv.Atoi(p[1])
 		tx, _ := strconv.Atoi(p[3])
-		n, _ := strconv.Atoi(p[4])
+		// #1213: formato nuevo "freq|ch|ht|tx|bssid|noise|n" (7 campos);
+		// el histórico "freq|ch|ht|tx|n" (5) sigue parseando igual.
+		var noise *int
+		var bssid string
+		n := 0
+		if len(p) >= 7 {
+			bssid = strings.ToUpper(strings.TrimSpace(p[4]))
+			if v, err := strconv.Atoi(strings.TrimSpace(p[5])); err == nil && v != 0 {
+				noise = &v
+			}
+			n, _ = strconv.Atoi(p[6])
+		} else {
+			n, _ = strconv.Atoi(p[4])
+		}
 		if cur, ok := byBand[band]; ok {
 			cur.Clients += n
+			if cur.NoiseDbm == nil {
+				cur.NoiseDbm = noise
+			}
+			if cur.BSSID == "" {
+				cur.BSSID = bssid
+			}
 		} else {
-			byBand[band] = &Radio{Name: band, Channel: ch, WidthMhz: width, PowerDbm: float64(tx), Clients: n}
+			byBand[band] = &Radio{Name: band, Channel: ch, WidthMhz: width, PowerDbm: float64(tx), Clients: n, NoiseDbm: noise, BSSID: bssid}
 			order = append(order, band)
 		}
 	}

+ 27 - 0
agent/probe/probe_test.go

@@ -431,6 +431,33 @@ func TestParseFdbYRadios(t *testing.T) {
 	}
 }
 
+// TestParseRadiosNoiseBssid (#1213): el formato nuevo lleva BSSID y ruido de
+// iwinfo; el histórico de 5 campos sigue parseando (sin ruido, sin BSSID).
+func TestParseRadiosNoiseBssid(t *testing.T) {
+	radios := ParseRadios("2.4|6|HT20|20|aa:bb:cc:00:00:01|-96|3\n5|36|HT80|23|aa:bb:cc:00:00:02|-91|7\n")
+	if len(radios) != 2 {
+		t.Fatalf("radios: %+v", radios)
+	}
+	if radios[0].NoiseDbm == nil || *radios[0].NoiseDbm != -96 {
+		t.Fatalf("ruido 2.4: %+v", radios[0])
+	}
+	if radios[0].BSSID != "AA:BB:CC:00:00:01" {
+		t.Fatalf("bssid 2.4: %+v", radios[0])
+	}
+	if radios[1].NoiseDbm == nil || *radios[1].NoiseDbm != -91 || radios[1].Clients != 7 {
+		t.Fatalf("radio 5: %+v", radios[1])
+	}
+	// driver sin ruido ("unknown" → campo vacío) y formato viejo
+	old := ParseRadios("5|36|HT80|23|aa:bb:cc:00:00:02||7\n")
+	if len(old) != 1 || old[0].NoiseDbm != nil || old[0].Clients != 7 {
+		t.Fatalf("radio 5 sin ruido: %+v", old)
+	}
+	legacy := ParseRadios("5|36|HT80|23|7\n")
+	if len(legacy) != 1 || legacy[0].NoiseDbm != nil || legacy[0].BSSID != "" || legacy[0].Clients != 7 {
+		t.Fatalf("formato viejo: %+v", legacy)
+	}
+}
+
 // TestParseFdbBridgeFdb — #253: formato `bridge fdb show` (puerto por nombre,
 // p. ej. eth0/eth1 en GLuON) y puertos ethernet fuera de lanN/wan.
 func TestParseFdbBridgeFdb(t *testing.T) {

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

@@ -906,6 +906,8 @@
       "clear": "Clear channel",
       "congested": "Congested",
       "power": "Power",
+      "noise": "Noise",
+      "bssid": "BSSID",
       "title": "WiFi radios",
       "width": "Width"
     },

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

@@ -906,6 +906,8 @@
       "clear": "Canal despejado",
       "congested": "Congestionado",
       "power": "Potencia",
+      "noise": "Ruido",
+      "bssid": "BSSID",
       "title": "Radios WiFi",
       "width": "Ancho"
     },

+ 4 - 0
app/src/components/routers/RadiosPorts.tsx

@@ -62,6 +62,10 @@ export function RadiosPorts({ router, extras, snmpStats }: {
                   [t('routerDetail.radios.width'), `${radio.widthMhz} MHz`],
                   [t('routerDetail.radios.power'), `${radio.powerDbm} dBm`],
                   [t('routers.colClients'), String(radio.clients)],
+                  // #1213: el ruido solo se muestra si el driver lo reporta
+                  ...(radio.noiseDbm != null ? [[t('routerDetail.radios.noise'), `${radio.noiseDbm} dBm`]] : []),
+                  // #1206: el BSSID de la interfaz (la MAC que antes asomaba como cliente)
+                  ...(radio.bssid ? [[t('routerDetail.radios.bssid'), radio.bssid]] : []),
                 ].map(([k, v]) => (
                   <div key={k}>
                     <dt className="text-[10px] font-medium uppercase tracking-[0.06em] text-text-muted">{k}</dt>

+ 4 - 0
app/src/components/routers/routerExtras.ts

@@ -30,6 +30,10 @@ export interface RadioInfo {
   powerDbm: number
   clients: number
   congested?: boolean
+  /** #1213: suelo de ruido en dBm (iwinfo); ausente si el driver no lo da */
+  noiseDbm?: number
+  /** #1206: BSSID de la interfaz AP - "el sitio donde viven" esas MACs */
+  bssid?: string
 }
 
 export interface PortInfo {

+ 2 - 1
server-go/internal/adapters/openwrt.go

@@ -668,7 +668,8 @@ func radiosToAdapter(in []probe.Radio) []Radio {
 	out := make([]Radio, 0, len(in))
 	for _, r := range in {
 		out = append(out, Radio{Name: r.Name, Channel: r.Channel,
-			WidthMhz: r.WidthMhz, PowerDbm: r.PowerDbm, Clients: r.Clients})
+			WidthMhz: r.WidthMhz, PowerDbm: r.PowerDbm, Clients: r.Clients,
+			NoiseDbm: r.NoiseDbm, BSSID: r.BSSID})
 	}
 	return out
 }

+ 4 - 0
server-go/internal/adapters/types.go

@@ -606,6 +606,10 @@ type Radio struct {
 	PowerDbm  float64 `json:"powerDbm"`
 	Clients   int     `json:"clients"`
 	Congested bool    `json:"congested,omitempty"` // solo demo
+	// #1213/#1206: ruido de la banda y BSSID de la interfaz AP (del agente o
+	// de iwinfo vía SSH). nil/vacío = el driver no los reporta.
+	NoiseDbm *int   `json:"noiseDbm,omitempty"`
+	BSSID    string `json:"bssid,omitempty"`
 }
 
 // Backhaul del AP (null en gateway): {kind:'cable', headline, latencyMs} o el