Lightweight server monitoring with historical data, docker stats, and alerts.
feat(agent): add FAN_SENSORS to whitelist or blacklist fans (#2713)
Mario Mohar5aaa7aee9236
diff --git a/agent/fans.go b/agent/fans.goindex 52398619d..5923a0fb9 100644--- a/agent/fans.go+++ b/agent/fans.go@@ -29,7 +29,7 @@ func newFanSensorCache(root string) func() ([]fanSensor, error) {// updateFans populates systemStats.Fans from the host's hwmon sysfs tree.// No-op on platforms where hwmon isn't available (see fans_other.go).func (a *Agent) updateFans(systemStats *system.Stats) {- if hwmonRoot == "" {+ if hwmonRoot == "" || (a.sensorConfig != nil && a.sensorConfig.skipFans) {return}fans, err := a.readFansWithTimeout(getFanSensors)@@ -72,8 +72,8 @@ func (a *Agent) readFansWithTimeout(getSensors func() ([]fanSensor, error)) (mapreturn nil, err}// Filter before reading fan*_input: each read can wake an idle GPU.- if a.sensorConfig != nil && a.sensorConfig.skipGPU {- sensors = filterGpuFans(sensors)+ if a.sensorConfig != nil {+ sensors = a.sensorConfig.filterFans(sensors)}return readFanSensors(sensors), nil})@@ -143,11 +143,15 @@ func readFanSensors(sensors []fanSensor) map[string]uint16 {return fans}-// filterGpuFans drops GPU chips without touching the shared cache backing array.-func filterGpuFans(sensors []fanSensor) []fanSensor {+// filterFans drops GPU chips (SKIP_GPU) and fans excluded by FAN_SENSORS in a+// single pass, without touching the shared cache backing array.+func (config *SensorConfig) filterFans(sensors []fanSensor) []fanSensor {kept := make([]fanSensor, 0, len(sensors))for _, sensor := range sensors {- if isGpuChipName(sensor.chip) {+ if config.skipGPU && isGpuChipName(sensor.chip) {+ continue+ }+ if !config.fanSensors.allows(sensor.key) {continue}kept = append(kept, sensor)diff --git a/agent/fans_test.go b/agent/fans_test.goindex 2f1d28884..58e753a40 100644--- a/agent/fans_test.go+++ b/agent/fans_test.go@@ -8,6 +8,7 @@ import ("testing""time"+ "github.com/henrygd/beszel/internal/entities/system""github.com/stretchr/testify/assert""github.com/stretchr/testify/require")@@ -116,7 +117,7 @@ func TestFilterGpuFans(t *testing.T) {require.NoError(t, err)require.Len(t, discovered, 2)- filtered := filterGpuFans(discovered)+ filtered := (&SensorConfig{skipGPU: true}).filterFans(discovered)require.Len(t, filtered, 1)assert.Equal(t, "nct6798_fan1", filtered[0].key)assert.Len(t, discovered, 2)@@ -155,3 +156,94 @@ func TestReadFansWithTimeout(t *testing.T) {require.NoError(t, err)assert.Equal(t, map[string]uint16{"chip_fan1": 1000}, fans)}++func TestFilterFanSensors(t *testing.T) {+ root := t.TempDir()+ writeFile(t, filepath.Join(root, "hwmon0", "name"), "nct6791\n")+ writeFile(t, filepath.Join(root, "hwmon0", "fan1_input"), "0\n")+ writeFile(t, filepath.Join(root, "hwmon0", "fan2_input"), "900\n")+ writeFile(t, filepath.Join(root, "hwmon0", "fan3_input"), "0\n")+ writeFile(t, filepath.Join(root, "hwmon0", "fan4_input"), "1100\n")+ writeFile(t, filepath.Join(root, "hwmon0", "fan4_label"), "chassis\n")+ writeFile(t, filepath.Join(root, "hwmon1", "name"), "gpio_fan\n")+ writeFile(t, filepath.Join(root, "hwmon1", "fan1_input"), "0\n")++ discovered, err := discoverHwmonFans(root)+ require.NoError(t, err)+ require.Len(t, discovered, 5)++ tests := []struct {+ name string+ fanSensors string+ expected []string+ }{+ {"empty list keeps all", "", []string{"nct6791_fan1", "nct6791_fan2", "nct6791_fan3", "nct6791_chassis", "gpio_fan_fan1"}},+ {"whitelist", "nct6791_fan2,nct6791_chassis", []string{"nct6791_fan2", "nct6791_chassis"}},+ {"whitelist matches label not fan index", "nct6791_fan4", []string{}},+ {"blacklist", "-gpio_fan_fan1", []string{"nct6791_fan1", "nct6791_fan2", "nct6791_fan3", "nct6791_chassis"}},+ {"whitelist wildcard", "nct6791_*", []string{"nct6791_fan1", "nct6791_fan2", "nct6791_fan3", "nct6791_chassis"}},+ {"blacklist wildcard and exact", "-gpio_*, nct6791_fan1 ,nct6791_fan3", []string{"nct6791_fan2", "nct6791_chassis"}},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ filtered := (&SensorConfig{fanSensors: parseSensorFilter(tt.fanSensors)}).filterFans(discovered)+ keys := make([]string, 0, len(filtered))+ for _, sensor := range filtered {+ keys = append(keys, sensor.key)+ }+ assert.ElementsMatch(t, tt.expected, keys)+ assert.Len(t, discovered, 5)+ })+ }+}++func TestReadFansWithTimeoutFanSensors(t *testing.T) {+ root := t.TempDir()+ writeFile(t, filepath.Join(root, "hwmon0", "name"), "nct6791\n")+ writeFile(t, filepath.Join(root, "hwmon0", "fan1_input"), "0\n")+ writeFile(t, filepath.Join(root, "hwmon0", "fan2_input"), "900\n")++ agent := &Agent{sensorConfig: &SensorConfig{timeout: time.Second, fanSensors: parseSensorFilter("-nct6791_fan1")}}+ getSensors := newFanSensorCache(root)+ fans, err := agent.readFansWithTimeout(getSensors)+ require.NoError(t, err)+ assert.Equal(t, map[string]uint16{"nct6791_fan2": 900}, fans)++ // the cached discovery still holds every fan+ sensors, err := getSensors()+ require.NoError(t, err)+ assert.Len(t, sensors, 2)+}++func TestFilterFansSkipGpuAndFanSensors(t *testing.T) {+ root := t.TempDir()+ writeFile(t, filepath.Join(root, "hwmon0", "name"), "xe\n")+ writeFile(t, filepath.Join(root, "hwmon0", "fan1_input"), "1200\n")+ writeFile(t, filepath.Join(root, "hwmon1", "name"), "nct6798\n")+ writeFile(t, filepath.Join(root, "hwmon1", "fan1_input"), "800\n")+ writeFile(t, filepath.Join(root, "hwmon1", "fan2_input"), "900\n")++ discovered, err := discoverHwmonFans(root)+ require.NoError(t, err)++ config := &SensorConfig{skipGPU: true, fanSensors: parseSensorFilter("-nct6798_fan1")}+ filtered := config.filterFans(discovered)+ require.Len(t, filtered, 1)+ assert.Equal(t, "nct6798_fan2", filtered[0].key)+ assert.Len(t, discovered, 3)+}++func TestUpdateFansSkipFans(t *testing.T) {+ originalGetFanSensors := getFanSensors+ t.Cleanup(func() { getFanSensors = originalGetFanSensors })+ getFanSensors = func() ([]fanSensor, error) {+ t.Fatal("fan discovery should not run when FAN_SENSORS is empty")+ return nil, nil+ }++ a := &Agent{sensorConfig: &SensorConfig{skipFans: true}}+ var stats system.Stats+ a.updateFans(&stats)+ assert.Nil(t, stats.Fans)+}diff --git a/agent/sensors.go b/agent/sensors.goindex a50e99d4d..5a1c0f88a 100644--- a/agent/sensors.go+++ b/agent/sensors.go@@ -34,13 +34,13 @@ type getTempsFn func(ctx context.Context) ([]sensors.TemperatureStat, error)type SensorConfig struct {context context.Context- sensors map[string]struct{}+ sensors sensorFilterprimarySensor stringtimeout time.Duration- isBlacklist bool- hasWildcards boolskipCollection boolskipGPU bool+ skipFans bool+ fanSensors sensorFiltersensorShadow stringfirstRun bool}@@ -53,7 +53,14 @@ func (a *Agent) newSensorConfig() *SensorConfig {sensorsTimeout, _ := utils.GetEnv("SENSORS_TIMEOUT")skipGPU, _ := utils.GetEnv("SKIP_GPU")- return a.newSensorConfigWithEnv(primarySensor, sysSensors, sensorsEnvVal, sensorsTimeout, skipCollection, skipGPU == "true")+ config := a.newSensorConfigWithEnv(primarySensor, sysSensors, sensorsEnvVal, sensorsTimeout, skipCollection, skipGPU == "true")++ // FAN_SENSORS uses the same syntax as SENSORS, matched against fan keys+ fanSensorsEnvVal, fanSensorsSet := utils.GetEnv("FAN_SENSORS")+ config.skipFans = fanSensorsSet && fanSensorsEnvVal == ""+ config.fanSensors = parseSensorFilter(fanSensorsEnvVal)++ return config}// newSensorConfigWithEnv creates a SensorConfig with the provided environment variables@@ -75,7 +82,7 @@ func (a *Agent) newSensorConfigWithEnv(primarySensor, sysSensors, sensorsEnvVal,skipCollection: skipCollection,skipGPU: skipGPU,firstRun: true,- sensors: make(map[string]struct{}),+ sensors: parseSensorFilter(sensorsEnvVal),}// Set sensors context (allows overriding sys location for sensors)@@ -99,23 +106,38 @@ func (a *Agent) newSensorConfigWithEnv(primarySensor, sysSensors, sensorsEnvVal,}}+ return config+}++// sensorFilter is a parsed SENSORS-style list of sensor names+type sensorFilter struct {+ sensors map[string]struct{}+ isBlacklist bool+ hasWildcards bool+}++// parseSensorFilter parses a comma separated list of sensor names, which may+// contain wildcards. A leading "-" turns the list into a blacklist.+func parseSensorFilter(val string) sensorFilter {+ filter := sensorFilter{sensors: make(map[string]struct{})}+// handle blacklist- if strings.HasPrefix(sensorsEnvVal, "-") {- config.isBlacklist = true- sensorsEnvVal = sensorsEnvVal[1:]+ if strings.HasPrefix(val, "-") {+ filter.isBlacklist = true+ val = val[1:]}- for sensor := range strings.SplitSeq(sensorsEnvVal, ",") {+ for sensor := range strings.SplitSeq(val, ",") {sensor = strings.TrimSpace(sensor)if sensor != "" {- config.sensors[sensor] = struct{}{}+ filter.sensors[sensor] = struct{}{}if strings.Contains(sensor, "*") {- config.hasWildcards = true+ filter.hasWildcards = true}}}- return config+ return filter}// updateTemperatures updates the agent with the latest sensor temperatures@@ -252,32 +274,37 @@ func readWithTimeout[T any](inflight *atomic.Bool, timeout time.Duration, timeou// isValidSensor checks if a sensor is valid based on the sensor name and the sensor configfunc isValidSensor(sensorName string, config *SensorConfig) bool {+ return config.sensors.allows(sensorName)+}++// allows checks if a sensor name passes the filter+func (f sensorFilter) allows(sensorName string) bool {// if no sensors configured, everything is valid- if len(config.sensors) == 0 {+ if len(f.sensors) == 0 {return true}// Exact match - return true if whitelist, false if blacklist- if _, exactMatch := config.sensors[sensorName]; exactMatch {- return !config.isBlacklist+ if _, exactMatch := f.sensors[sensorName]; exactMatch {+ return !f.isBlacklist}// If no wildcards, return true if blacklist, false if whitelist- if !config.hasWildcards {- return config.isBlacklist+ if !f.hasWildcards {+ return f.isBlacklist}// Check for wildcard patterns- for pattern := range config.sensors {+ for pattern := range f.sensors {if !strings.Contains(pattern, "*") {continue}if match, _ := path.Match(pattern, sensorName); match {- return !config.isBlacklist+ return !f.isBlacklist}}- return config.isBlacklist+ return f.isBlacklist}// scaleTemperature scales temperatures in fractional values to reasonable Celsius valuesdiff --git a/agent/sensors_test.go b/agent/sensors_test.goindex 6229761f8..e3315bc7e 100644--- a/agent/sensors_test.go+++ b/agent/sensors_test.go@@ -30,8 +30,10 @@ func TestIsValidSensor(t *testing.T) {name: "Whitelist - sensor in list",sensorName: "cpu_temp",config: &SensorConfig{- sensors: map[string]struct{}{"cpu_temp": {}},- isBlacklist: false,+ sensors: sensorFilter{+ sensors: map[string]struct{}{"cpu_temp": {}},+ isBlacklist: false,+ },},expectedValid: true,},@@ -39,8 +41,10 @@ func TestIsValidSensor(t *testing.T) {name: "Whitelist - sensor not in list",sensorName: "gpu_temp",config: &SensorConfig{- sensors: map[string]struct{}{"cpu_temp": {}},- isBlacklist: false,+ sensors: sensorFilter{+ sensors: map[string]struct{}{"cpu_temp": {}},+ isBlacklist: false,+ },},expectedValid: false,},@@ -48,8 +52,10 @@ func TestIsValidSensor(t *testing.T) {name: "Blacklist - sensor in list",sensorName: "cpu_temp",config: &SensorConfig{- sensors: map[string]struct{}{"cpu_temp": {}},- isBlacklist: true,+ sensors: sensorFilter{+ sensors: map[string]struct{}{"cpu_temp": {}},+ isBlacklist: true,+ },},expectedValid: false,},@@ -57,8 +63,10 @@ func TestIsValidSensor(t *testing.T) {name: "Blacklist - sensor not in list",sensorName: "gpu_temp",config: &SensorConfig{- sensors: map[string]struct{}{"cpu_temp": {}},- isBlacklist: true,+ sensors: sensorFilter{+ sensors: map[string]struct{}{"cpu_temp": {}},+ isBlacklist: true,+ },},expectedValid: true,},@@ -66,9 +74,11 @@ func TestIsValidSensor(t *testing.T) {name: "Whitelist with wildcard - matching pattern",sensorName: "core_0_temp",config: &SensorConfig{- sensors: map[string]struct{}{"core_*_temp": {}},- isBlacklist: false,- hasWildcards: true,+ sensors: sensorFilter{+ sensors: map[string]struct{}{"core_*_temp": {}},+ isBlacklist: false,+ hasWildcards: true,+ },},expectedValid: true,},@@ -76,9 +86,11 @@ func TestIsValidSensor(t *testing.T) {name: "Whitelist with wildcard - non-matching pattern",sensorName: "gpu_temp",config: &SensorConfig{- sensors: map[string]struct{}{"core_*_temp": {}},- isBlacklist: false,- hasWildcards: true,+ sensors: sensorFilter{+ sensors: map[string]struct{}{"core_*_temp": {}},+ isBlacklist: false,+ hasWildcards: true,+ },},expectedValid: false,},@@ -86,9 +98,11 @@ func TestIsValidSensor(t *testing.T) {name: "Blacklist with wildcard - matching pattern",sensorName: "core_0_temp",config: &SensorConfig{- sensors: map[string]struct{}{"core_*_temp": {}},- isBlacklist: true,- hasWildcards: true,+ sensors: sensorFilter{+ sensors: map[string]struct{}{"core_*_temp": {}},+ isBlacklist: true,+ hasWildcards: true,+ },},expectedValid: false,},@@ -96,9 +110,11 @@ func TestIsValidSensor(t *testing.T) {name: "Blacklist with wildcard - non-matching pattern",sensorName: "gpu_temp",config: &SensorConfig{- sensors: map[string]struct{}{"core_*_temp": {}},- isBlacklist: true,- hasWildcards: true,+ sensors: sensorFilter{+ sensors: map[string]struct{}{"core_*_temp": {}},+ isBlacklist: true,+ hasWildcards: true,+ },},expectedValid: true,},@@ -106,9 +122,11 @@ func TestIsValidSensor(t *testing.T) {name: "No sensors configured",sensorName: "any_temp",config: &SensorConfig{- sensors: map[string]struct{}{},- isBlacklist: false,- hasWildcards: false,+ sensors: sensorFilter{+ sensors: map[string]struct{}{},+ isBlacklist: false,+ hasWildcards: false,+ },skipCollection: false,},expectedValid: true,@@ -117,9 +135,11 @@ func TestIsValidSensor(t *testing.T) {name: "Mixed patterns in whitelist - exact match",sensorName: "cpu_temp",config: &SensorConfig{- sensors: map[string]struct{}{"cpu_temp": {}, "core_*_temp": {}},- isBlacklist: false,- hasWildcards: true,+ sensors: sensorFilter{+ sensors: map[string]struct{}{"cpu_temp": {}, "core_*_temp": {}},+ isBlacklist: false,+ hasWildcards: true,+ },},expectedValid: true,},@@ -127,9 +147,11 @@ func TestIsValidSensor(t *testing.T) {name: "Mixed patterns in whitelist - wildcard match",sensorName: "core_1_temp",config: &SensorConfig{- sensors: map[string]struct{}{"cpu_temp": {}, "core_*_temp": {}},- isBlacklist: false,- hasWildcards: true,+ sensors: sensorFilter{+ sensors: map[string]struct{}{"cpu_temp": {}, "core_*_temp": {}},+ isBlacklist: false,+ hasWildcards: true,+ },},expectedValid: true,},@@ -137,9 +159,11 @@ func TestIsValidSensor(t *testing.T) {name: "Mixed patterns in blacklist - exact match",sensorName: "cpu_temp",config: &SensorConfig{- sensors: map[string]struct{}{"cpu_temp": {}, "core_*_temp": {}},- isBlacklist: true,- hasWildcards: true,+ sensors: sensorFilter{+ sensors: map[string]struct{}{"cpu_temp": {}, "core_*_temp": {}},+ isBlacklist: true,+ hasWildcards: true,+ },},expectedValid: false,},@@ -147,9 +171,11 @@ func TestIsValidSensor(t *testing.T) {name: "Mixed patterns in blacklist - wildcard match",sensorName: "core_1_temp",config: &SensorConfig{- sensors: map[string]struct{}{"cpu_temp": {}, "core_*_temp": {}},- isBlacklist: true,- hasWildcards: true,+ sensors: sensorFilter{+ sensors: map[string]struct{}{"cpu_temp": {}, "core_*_temp": {}},+ isBlacklist: true,+ hasWildcards: true,+ },},expectedValid: false,},@@ -181,12 +207,14 @@ func TestNewSensorConfigWithEnv(t *testing.T) {sysSensors: "",sensors: "",expectedConfig: &SensorConfig{- context: context.Background(),- primarySensor: "",- timeout: 2 * time.Second,- sensors: map[string]struct{}{},- isBlacklist: false,- hasWildcards: false,+ context: context.Background(),+ primarySensor: "",+ timeout: 2 * time.Second,+ sensors: sensorFilter{+ sensors: map[string]struct{}{},+ isBlacklist: false,+ hasWildcards: false,+ },skipCollection: false,},},@@ -199,7 +227,9 @@ func TestNewSensorConfigWithEnv(t *testing.T) {expectedConfig: &SensorConfig{context: context.Background(),timeout: 5 * time.Second,- sensors: map[string]struct{}{},+ sensors: sensorFilter{+ sensors: map[string]struct{}{},+ },},},{@@ -211,7 +241,9 @@ func TestNewSensorConfigWithEnv(t *testing.T) {expectedConfig: &SensorConfig{context: context.Background(),timeout: 2 * time.Second,- sensors: map[string]struct{}{},+ sensors: sensorFilter{+ sensors: map[string]struct{}{},+ },},},{@@ -221,12 +253,14 @@ func TestNewSensorConfigWithEnv(t *testing.T) {sensors: "",skipCollection: true,expectedConfig: &SensorConfig{- context: context.Background(),- primarySensor: "",- timeout: 2 * time.Second,- sensors: map[string]struct{}{},- isBlacklist: false,- hasWildcards: false,+ context: context.Background(),+ primarySensor: "",+ timeout: 2 * time.Second,+ sensors: sensorFilter{+ sensors: map[string]struct{}{},+ isBlacklist: false,+ hasWildcards: false,+ },skipCollection: true,},},@@ -239,9 +273,11 @@ func TestNewSensorConfigWithEnv(t *testing.T) {context: context.Background(),primarySensor: "cpu_temp",timeout: 2 * time.Second,- sensors: map[string]struct{}{},- isBlacklist: false,- hasWildcards: false,+ sensors: sensorFilter{+ sensors: map[string]struct{}{},+ isBlacklist: false,+ hasWildcards: false,+ },},},{@@ -253,12 +289,14 @@ func TestNewSensorConfigWithEnv(t *testing.T) {context: context.Background(),primarySensor: "cpu_temp",timeout: 2 * time.Second,- sensors: map[string]struct{}{- "cpu_temp": {},- "gpu_temp": {},+ sensors: sensorFilter{+ sensors: map[string]struct{}{+ "cpu_temp": {},+ "gpu_temp": {},+ },+ isBlacklist: false,+ hasWildcards: false,},- isBlacklist: false,- hasWildcards: false,},},{@@ -270,12 +308,14 @@ func TestNewSensorConfigWithEnv(t *testing.T) {context: context.Background(),primarySensor: "cpu_temp",timeout: 2 * time.Second,- sensors: map[string]struct{}{- "cpu_temp": {},- "gpu_temp": {},+ sensors: sensorFilter{+ sensors: map[string]struct{}{+ "cpu_temp": {},+ "gpu_temp": {},+ },+ isBlacklist: true,+ hasWildcards: false,},- isBlacklist: true,- hasWildcards: false,},},{@@ -287,12 +327,14 @@ func TestNewSensorConfigWithEnv(t *testing.T) {context: context.Background(),primarySensor: "cpu_temp",timeout: 2 * time.Second,- sensors: map[string]struct{}{- "cpu_*": {},- "gpu_temp": {},+ sensors: sensorFilter{+ sensors: map[string]struct{}{+ "cpu_*": {},+ "gpu_temp": {},+ },+ isBlacklist: false,+ hasWildcards: true,},- isBlacklist: false,- hasWildcards: true,},},{@@ -304,12 +346,14 @@ func TestNewSensorConfigWithEnv(t *testing.T) {context: context.Background(),primarySensor: "cpu_temp",timeout: 2 * time.Second,- sensors: map[string]struct{}{- "cpu_*": {},- "gpu_temp": {},+ sensors: sensorFilter{+ sensors: map[string]struct{}{+ "cpu_*": {},+ "gpu_temp": {},+ },+ isBlacklist: false,+ hasWildcards: true,},- isBlacklist: false,- hasWildcards: true,},},{@@ -320,11 +364,13 @@ func TestNewSensorConfigWithEnv(t *testing.T) {expectedConfig: &SensorConfig{primarySensor: "cpu_temp",timeout: 2 * time.Second,- sensors: map[string]struct{}{- "cpu_temp": {},+ sensors: sensorFilter{+ sensors: map[string]struct{}{+ "cpu_temp": {},+ },+ isBlacklist: false,+ hasWildcards: false,},- isBlacklist: false,- hasWildcards: false,},},}@@ -337,19 +383,19 @@ func TestNewSensorConfigWithEnv(t *testing.T) {assert.Equal(t, tt.expectedConfig.primarySensor, result.primarySensor)// Check sensor map- if tt.expectedConfig.sensors == nil {- assert.Nil(t, result.sensors)+ if tt.expectedConfig.sensors.sensors == nil {+ assert.Nil(t, result.sensors.sensors)} else {- assert.Equal(t, len(tt.expectedConfig.sensors), len(result.sensors))- for sensor := range tt.expectedConfig.sensors {- _, exists := result.sensors[sensor]+ assert.Equal(t, len(tt.expectedConfig.sensors.sensors), len(result.sensors.sensors))+ for sensor := range tt.expectedConfig.sensors.sensors {+ _, exists := result.sensors.sensors[sensor]assert.True(t, exists, "Sensor %s should exist in the result", sensor)}}// Check flags- assert.Equal(t, tt.expectedConfig.isBlacklist, result.isBlacklist)- assert.Equal(t, tt.expectedConfig.hasWildcards, result.hasWildcards)+ assert.Equal(t, tt.expectedConfig.sensors.isBlacklist, result.sensors.isBlacklist)+ assert.Equal(t, tt.expectedConfig.sensors.hasWildcards, result.sensors.hasWildcards)assert.Equal(t, tt.expectedConfig.timeout, result.timeout)// Check context@@ -378,10 +424,10 @@ func TestNewSensorConfig(t *testing.T) {// Verify resultsassert.Equal(t, "test_primary", result.primarySensor)assert.Equal(t, 7*time.Second, result.timeout)- assert.NotNil(t, result.sensors)- assert.Equal(t, 3, len(result.sensors))- assert.True(t, result.hasWildcards)- assert.False(t, result.isBlacklist)+ assert.NotNil(t, result.sensors.sensors)+ assert.Equal(t, 3, len(result.sensors.sensors))+ assert.True(t, result.sensors.hasWildcards)+ assert.False(t, result.sensors.isBlacklist)// Check that sys sensors path is in contextenvMap, ok := result.context.Value(common.EnvKey).(common.EnvMap)@@ -391,6 +437,70 @@ func TestNewSensorConfig(t *testing.T) {assert.Equal(t, "/test/path", sysPath)}+func TestParseSensorFilter(t *testing.T) {+ tests := []struct {+ name string+ val string+ expected sensorFilter+ }{+ {"empty", "", sensorFilter{sensors: map[string]struct{}{}}},+ {"blacklist prefix only", "-", sensorFilter{sensors: map[string]struct{}{}, isBlacklist: true}},+ {"whitelist", "nct6791_fan2, nct6791_fan4,", sensorFilter{sensors: map[string]struct{}{"nct6791_fan2": {}, "nct6791_fan4": {}}}},+ {"blacklist with wildcard", "-gpio_fan_*,nct6791_fan1", sensorFilter{sensors: map[string]struct{}{"gpio_fan_*": {}, "nct6791_fan1": {}}, isBlacklist: true, hasWildcards: true}},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ assert.Equal(t, tt.expected, parseSensorFilter(tt.val))+ })+ }+}++func TestSensorFilterAllows(t *testing.T) {+ tests := []struct {+ name string+ filter string+ sensorName string+ expected bool+ }{+ {"empty list allows all", "", "nct6791_fan1", true},+ {"blacklist prefix only allows all", "-", "nct6791_fan1", true},+ {"whitelist exact match", "nct6791_fan2", "nct6791_fan2", true},+ {"whitelist no match", "nct6791_fan2", "nct6791_fan1", false},+ {"blacklist exact match", "-gpio_fan_fan1", "gpio_fan_fan1", false},+ {"blacklist no match", "-gpio_fan_fan1", "nct6791_fan1", true},+ {"whitelist wildcard match", "nct6791_*", "nct6791_chassis", true},+ {"whitelist wildcard no match", "nct6791_*", "gpio_fan_fan1", false},+ {"blacklist wildcard match", "-*_fan1", "nct6791_fan1", false},+ {"blacklist wildcard no match", "-*_fan1", "nct6791_fan2", true},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ assert.Equal(t, tt.expected, parseSensorFilter(tt.filter).allows(tt.sensorName))+ })+ }+}++func TestNewSensorConfigFanSensors(t *testing.T) {+ t.Setenv("SENSORS", "")+ t.Setenv("BESZEL_AGENT_SENSORS", "")++ t.Setenv("BESZEL_AGENT_FAN_SENSORS", "-gpio_fan_*")+ config := (&Agent{}).newSensorConfig()+ assert.False(t, config.skipFans)+ assert.True(t, config.fanSensors.isBlacklist)+ assert.True(t, config.fanSensors.hasWildcards)+ assert.Contains(t, config.fanSensors.sensors, "gpio_fan_*")+ // SENSORS is unaffected+ assert.Empty(t, config.sensors.sensors)++ // set but empty skips fan collection, like SENSORS+ t.Setenv("BESZEL_AGENT_FAN_SENSORS", "")+ config = (&Agent{}).newSensorConfig()+ assert.True(t, config.skipFans)+}+func TestScaleTemperature(t *testing.T) {tests := []struct {name string@@ -783,7 +893,9 @@ func TestUpdateTemperaturesSkipGpu(t *testing.T) {sensorConfig: &SensorConfig{context: context.Background(),timeout: 2 * time.Second,- sensors: map[string]struct{}{},+ sensors: sensorFilter{+ sensors: map[string]struct{}{},+ },skipGPU: skipGPU,},}@@ -821,10 +933,12 @@ func TestUpdateTemperaturesExcludedSensorKeepsNames(t *testing.T) {return &Agent{systemInfo: system.Info{},sensorConfig: &SensorConfig{- context: context.Background(),- timeout: 2 * time.Second,- sensors: config,- isBlacklist: exclude != "",+ context: context.Background(),+ timeout: 2 * time.Second,+ sensors: sensorFilter{+ sensors: config,+ isBlacklist: exclude != "",+ },},}}