diff --git a/agent/wifi/README.md b/agent/wifi/README.md index e195e18ce..e04931ce5 100644 --- a/agent/wifi/README.md +++ b/agent/wifi/README.md @@ -3,7 +3,8 @@ Reports connected station interfaces only (no scans). `info.wf` holds the current snapshot keyed by interface (`s` SSID, `r` RSSI in dBm when available); `stats.wf` stores available RSSI as integer dBm. Collected on the default -interval only; real-time requests reuse the last snapshot. +interval only; real-time requests reuse the last snapshot. Set `SKIP_WIFI=true` +on the agent to disable collection. - Linux: nl80211 via `github.com/mdlayher/wifi`. Docker needs `network_mode: host`. Skipped when `/sys/class/ieee80211` lists no wireless devices. diff --git a/agent/wifi/wifi.go b/agent/wifi/wifi.go index 016e9f1a9..7486cc602 100644 --- a/agent/wifi/wifi.go +++ b/agent/wifi/wifi.go @@ -4,10 +4,13 @@ package wifi import ( "context" + "log/slog" "math" + "sync" "time" "unicode/utf8" + "github.com/henrygd/beszel/agent/utils" "github.com/henrygd/beszel/internal/entities/system" ) @@ -20,10 +23,29 @@ func validSSID(ssid string) string { return ssid } +// skipFromEnv reports whether SKIP_WIFI disables collection. +func skipFromEnv() bool { + skip, _ := utils.GetEnv("SKIP_WIFI") + return skip == "true" +} + +// skip caches skipFromEnv, as the environment is fixed for the life of the +// process. +var skip = sync.OnceValue(func() bool { + if !skipFromEnv() { + return false + } + slog.Info("SKIP_WIFI enabled, skipping Wi-Fi signal monitoring") + return true +}) + // Collect uses a single deadline across interface queries where supported. // Unsupported platforms and denied association access produce no readings; -// later polls retry. +// later polls retry. SKIP_WIFI=true disables collection entirely. func Collect() map[string]system.WiFi { + if skip() { + return nil + } ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() return collect(ctx) diff --git a/agent/wifi/wifi_test.go b/agent/wifi/wifi_test.go index 28d4764df..797bf97b6 100644 --- a/agent/wifi/wifi_test.go +++ b/agent/wifi/wifi_test.go @@ -7,6 +7,34 @@ import ( "github.com/henrygd/beszel/internal/entities/system" ) +func TestSkipFromEnv(t *testing.T) { + for _, tc := range []struct { + key, value string + want bool + }{ + {"SKIP_WIFI", "true", true}, + {"BESZEL_AGENT_SKIP_WIFI", "true", true}, + {"SKIP_WIFI", "false", false}, + {"SKIP_WIFI", "", false}, + } { + t.Run(tc.key+"="+tc.value, func(t *testing.T) { + t.Setenv(tc.key, tc.value) + if got := skipFromEnv(); got != tc.want { + t.Fatalf("got %v, want %v", got, tc.want) + } + }) + } +} + +func TestCollectSkipped(t *testing.T) { + original := skip + t.Cleanup(func() { skip = original }) + skip = func() bool { return true } + if got := Collect(); got != nil { + t.Fatalf("collected while skipped: %v", got) + } +} + func TestSSIDWireSafety(t *testing.T) { for _, tc := range []struct{ input, want string }{ {"home", "home"}, {"网络 café", "网络 café"}, {"", ""},