wifi: add SKIP_WIFI env var to disable signal monitoring

This commit is contained in:
henrygd
2026-10-02 12:53:48 -04:00
parent b115e2aa6a
commit e597dd94db
3 changed files with 53 additions and 2 deletions

View File

@@ -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.

View File

@@ -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)

View File

@@ -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é"}, {"", ""},