From d3a4d73a35774d97ade60ed17ef58b475c632a22 Mon Sep 17 00:00:00 2001 From: Ahmet Ozturk Date: Mon, 16 Nov 2026 16:28:48 +0100 Subject: [PATCH 1/7] check_temperature: report implausible readings as unknown MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some sensors report values that cannot be a computer temperature. A common case is an NVMe sensor that reports 0 Kelvin. The kernel converts this value to -273.15 °C, and gopsutil passes it through. The old filter only removed 0 °C and 1 °C. The default thresholds only used the min and crit values from the driver. If both are zero, the check returned OK for an impossible reading. Add a plausible range of -50 °C to 150 °C and a default unknown condition for readings outside that range. The reading stays visible in the output and performance data, but the check state becomes UNKNOWN. Update the hardware-based test so it accepts negative values and checks the state against the readings. Add a deterministic test that mocks sysfs with HOST_SYS. The mock test covers a normal sensor, a 0 Kelvin sensor, and a reading above the plausible maximum. --- docs/checks/commands/check_temperature.md | 3 + pkg/snclient/check_temperature.go | 9 ++ pkg/snclient/check_temperature_linux_test.go | 105 +++++++++++++++++-- 3 files changed, 109 insertions(+), 8 deletions(-) diff --git a/docs/checks/commands/check_temperature.md b/docs/checks/commands/check_temperature.md index 471159ba1..e02326932 100644 --- a/docs/checks/commands/check_temperature.md +++ b/docs/checks/commands/check_temperature.md @@ -22,6 +22,7 @@ Check temperature sensors. | filter | temperature != 0 and temperature != 1 | | warning | (min != 0 && temperature < %(min)) \|\| (crit != 0 && temperature > %(crit)) | | critical | (min != 0 && temperature < %(min)) \|\| (crit != 0 && temperature > %(crit)) | +| unknown | temperature < -50 \|\| temperature > 150 | | empty-state | 3 (UNKNOWN) | | empty-syntax | check_temperature failed to find any sensors. | | top-syntax | %(status) - %(list) | @@ -62,6 +63,8 @@ Show all temperature sensors and apply custom thresholds: check_temperature filter=none warn="temperature > 85" crit="temperature > 90" OK - Package id 0: 65.0 °C, Core 0: 62.0 °C, Core 1: 61.0 °C, Core 2: 65.0 °C |... +Sensors outside the plausible temperature range are reported as UNKNOWN. + ### Example using check_snclient and Naemon Naemon Config diff --git a/pkg/snclient/check_temperature.go b/pkg/snclient/check_temperature.go index 5e3e15c6e..8d3466f27 100644 --- a/pkg/snclient/check_temperature.go +++ b/pkg/snclient/check_temperature.go @@ -26,6 +26,12 @@ type temperatureStat struct { Min float64 // Temperature min value. } +// Plausible temperature range for computer hardware in Celsius. +const ( + minPlausibleTemperature = -50.0 + maxPlausibleTemperature = 150.0 +) + func NewCheckTemperature() CheckHandler { return &CheckTemperature{} } @@ -45,6 +51,7 @@ func (l *CheckTemperature) Build() *CheckData { defaultFilter: "temperature != 0 and temperature != 1", // seems like disabled sensors return 1.0000 or 0.0000 defaultWarning: "(min != 0 && temperature < %(min)) || (crit != 0 && temperature > %(crit))", defaultCritical: "(min != 0 && temperature < %(min)) || (crit != 0 && temperature > %(crit))", + defaultUnknown: fmt.Sprintf("temperature < %g || temperature > %g", minPlausibleTemperature, maxPlausibleTemperature), topSyntax: "%(status) - %(list)", detailSyntax: "%(sensor): %(temperature:fmt=%.1f) °C", emptyState: 3, @@ -67,6 +74,8 @@ Show all temperature sensors and apply custom thresholds: check_temperature filter=none warn="temperature > 85" crit="temperature > 90" OK - Package id 0: 65.0 °C, Core 0: 62.0 °C, Core 1: 61.0 °C, Core 2: 65.0 °C |... + +Sensors outside the plausible temperature range are reported as UNKNOWN. `, } } diff --git a/pkg/snclient/check_temperature_linux_test.go b/pkg/snclient/check_temperature_linux_test.go index eddb0c331..622b69aa4 100644 --- a/pkg/snclient/check_temperature_linux_test.go +++ b/pkg/snclient/check_temperature_linux_test.go @@ -1,10 +1,15 @@ package snclient import ( + "context" + "os" + "path/filepath" + "strconv" "strings" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestTemperature(t *testing.T) { @@ -12,15 +17,99 @@ func TestTemperature(t *testing.T) { defer StopTestAgent(t, snc) res := snc.RunCheck("check_temperature", []string{}) - if res.State == CheckExitUnknown && strings.Contains(string(res.BuildPluginOutput()), "failed to find any sensors") { + out := string(res.BuildPluginOutput()) + if res.State == CheckExitUnknown && strings.Contains(out, "failed to find any sensors") { // no sensors found, cannot test return } - assert.Equalf(t, CheckExitOK, res.State, "state ok") - assert.Regexpf( - t, - `^OK -.*: [\d.]+ °C`, - string(res.BuildPluginOutput()), - "output matches", - ) + assert.Regexpf(t, `^(OK|UNKNOWN) - .*: -?[\d.]+ °C`, out, "output matches") + require.NotNil(t, res.Raw) + + hasImplausible := false + for _, entry := range res.Raw.listData { + temp, err := strconv.ParseFloat(entry["temperature"], 64) + require.NoError(t, err) + if temp < minPlausibleTemperature || temp > maxPlausibleTemperature { + hasImplausible = true + } + } + if hasImplausible { + assert.Equalf(t, CheckExitUnknown, res.State, "implausible sensor must make the check unknown") + } else { + assert.NotEqualf(t, CheckExitUnknown, res.State, "plausible sensors must not make the check unknown") + } +} + +func TestTemperatureMocked(t *testing.T) { + tests := []struct { + name string + files map[string]string + expectState int64 + expectOut string + }{ + { + name: "plausible sensor", + files: map[string]string{ + "class/hwmon/hwmon0/name": "nvme", + "class/hwmon/hwmon0/temp1_label": "Composite", + "class/hwmon/hwmon0/temp1_input": "65000", + "class/hwmon/hwmon0/temp1_max": "69850", + }, + expectState: CheckExitOK, + expectOut: "nvme_composite: 65.0 °C", + }, + { + name: "kelvin sentinel", + files: map[string]string{ + "class/hwmon/hwmon0/name": "nvme", + "class/hwmon/hwmon0/temp1_label": "Composite", + "class/hwmon/hwmon0/temp1_input": "-273150", + "class/hwmon/hwmon0/temp1_max": "69850", + }, + expectState: CheckExitUnknown, + expectOut: "nvme_composite: -273.1 °C", + }, + { + name: "temperature too high", + files: map[string]string{ + "class/hwmon/hwmon0/name": "nvme", + "class/hwmon/hwmon0/temp1_label": "Composite", + "class/hwmon/hwmon0/temp1_input": "200000", + }, + expectState: CheckExitUnknown, + expectOut: "nvme_composite: 200.0 °C", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + mockSysFS(t, test.files) + res := runTemperatureCheck(t) + assert.Equal(t, test.expectState, res.State) + assert.Contains(t, string(res.BuildPluginOutput()), test.expectOut) + }) + } +} + +func mockSysFS(t *testing.T, files map[string]string) { + t.Helper() + sysDir := t.TempDir() + for name, content := range files { + path := filepath.Join(sysDir, filepath.FromSlash(name)) + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + require.NoError(t, os.WriteFile(path, []byte(content+"\n"), 0o644)) + } + t.Setenv("HOST_SYS", sysDir) +} + +func runTemperatureCheck(t *testing.T) *CheckResult { + t.Helper() + check := NewCheckTemperature().(*CheckTemperature) + data := check.Build() + _, err := data.parseArgs(nil) + require.NoError(t, err) + res, err := check.Check(context.Background(), nil, data, nil) + require.NoError(t, err) + + return res } From 1788790b6e56498adebd5aebe53d1a9772d52a42 Mon Sep 17 00:00:00 2001 From: Ahmet Ozturk Date: Mon, 16 Nov 2026 16:29:45 +0100 Subject: [PATCH 2/7] check_temperature: sanitize implausible threshold values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Linux hwmon interface and some drivers return sentinel values for thresholds that are not set. The old code only removed the 0xFFFF Kelvin sentinel from the high value. Use the plausible range from -50 °C to 150 °C for the min, critical, and high values. Values outside the range are treated as disabled thresholds and set to zero. The current reading is not clamped, so the output still shows the value that caused the unknown state. Add a unit test with sentinel min, critical, and high values. --- pkg/snclient/check_temperature.go | 14 ++++++++++-- pkg/snclient/check_temperature_linux_test.go | 24 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pkg/snclient/check_temperature.go b/pkg/snclient/check_temperature.go index 8d3466f27..fe421723c 100644 --- a/pkg/snclient/check_temperature.go +++ b/pkg/snclient/check_temperature.go @@ -32,6 +32,10 @@ const ( maxPlausibleTemperature = 150.0 ) +func isPlausibleTemperature(value float64) bool { + return value >= minPlausibleTemperature && value <= maxPlausibleTemperature +} + func NewCheckTemperature() CheckHandler { return &CheckTemperature{} } @@ -114,10 +118,16 @@ func (l *CheckTemperature) addSensor(check *CheckData, sensor *temperatureStat, duplicates[label] = 1 } - //nolint:mnd // sanitizing: this value is 0xFFFF kelvin converted to celsius and means the high value is not used - if sensor.High >= 65261.850000 { + // Sanitizing: values outside the plausible range are sentinel or disabled thresholds. + if !isPlausibleTemperature(sensor.High) { sensor.High = 0 } + if !isPlausibleTemperature(sensor.Min) { + sensor.Min = 0 + } + if !isPlausibleTemperature(sensor.Critical) { + sensor.Critical = 0 + } entry := map[string]string{ "sensor": sensorKey, diff --git a/pkg/snclient/check_temperature_linux_test.go b/pkg/snclient/check_temperature_linux_test.go index 622b69aa4..fe8396828 100644 --- a/pkg/snclient/check_temperature_linux_test.go +++ b/pkg/snclient/check_temperature_linux_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + "github.com/shirou/gopsutil/v4/sensors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -91,6 +92,29 @@ func TestTemperatureMocked(t *testing.T) { } } +func TestTemperatureSanitizeThresholds(t *testing.T) { + check := NewCheckTemperature().(*CheckTemperature) + data := check.Build() + _, err := data.parseArgs(nil) + require.NoError(t, err) + + check.addSensor(data, &temperatureStat{ + TemperatureStat: sensors.TemperatureStat{ + SensorKey: "nvme_composite", + Temperature: 65, + Critical: -273.15, + High: 65261.85, + }, + Min: -273.15, + }, map[string]int{}) + + require.Len(t, data.listData, 1) + entry := data.listData[0] + assert.Equal(t, "0.000000", entry["min"]) + assert.Equal(t, "0.000000", entry["crit"]) + assert.Equal(t, "0.000000", entry["max"]) +} + func mockSysFS(t *testing.T, files map[string]string) { t.Helper() sysDir := t.TempDir() From fe9f4a85fc829d8e799e0f5b081fd95b55aed288 Mon Sep 17 00:00:00 2001 From: Ahmet Ozturk Date: Mon, 16 Nov 2026 16:30:58 +0100 Subject: [PATCH 3/7] check_temperature: fix Linux sensor min merge The length check in mergeExclusiveSensors was inverted. The min value from the extended Linux sensor data was never merged into the regular sensor list. The function also indexed both lists by position. If the regular sensor call skipped an unreadable file, the positions could differ. If the system only provides thermal zones, the extended list is empty and the index can be out of range. A machine with two thermal zones without hwmon could panic. Group the extended values by sensor key and consume them in order. This keeps duplicate sensor keys in the same order and avoids the out-of-range index. Add tests for the min value, duplicate keys, and thermal zones without hwmon. --- pkg/snclient/check_temperature_linux.go | 15 +++-- pkg/snclient/check_temperature_linux_test.go | 68 ++++++++++++++++++++ 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/pkg/snclient/check_temperature_linux.go b/pkg/snclient/check_temperature_linux.go index bd47801f4..827f8b639 100644 --- a/pkg/snclient/check_temperature_linux.go +++ b/pkg/snclient/check_temperature_linux.go @@ -13,17 +13,24 @@ func (l *CheckTemperature) mergeExclusiveSensors(ctx context.Context, src []sens log.Debugf("(linux)sensors.TemperaturesWithContext: %s", err.Error()) } + // Keep the values in order. Some systems expose the same sensor key more than once. + minByKey := make(map[string][]float64, len(exSensors)) + for idx := range exSensors { + key := exSensors[idx].SensorKey + minByKey[key] = append(minByKey[key], exSensors[idx].Min) + } + res := make([]temperatureStat, len(src)) for idx := range src { minVal := float64(0) - if len(exSensors) < idx { - minVal = exSensors[idx].Min + if values := minByKey[src[idx].SensorKey]; len(values) > 0 { + minVal = values[0] + minByKey[src[idx].SensorKey] = values[1:] } - sens := temperatureStat{ + res[idx] = temperatureStat{ TemperatureStat: src[idx], Min: minVal, } - res[idx] = sens } return res, nil diff --git a/pkg/snclient/check_temperature_linux_test.go b/pkg/snclient/check_temperature_linux_test.go index fe8396828..d4afb4bad 100644 --- a/pkg/snclient/check_temperature_linux_test.go +++ b/pkg/snclient/check_temperature_linux_test.go @@ -80,6 +80,17 @@ func TestTemperatureMocked(t *testing.T) { expectState: CheckExitUnknown, expectOut: "nvme_composite: 200.0 °C", }, + { + name: "thermal zones without hwmon", + files: map[string]string{ + "class/thermal/thermal_zone0/type": "x86_pkg_temp", + "class/thermal/thermal_zone0/temp": "65000", + "class/thermal/thermal_zone1/type": "acpitz", + "class/thermal/thermal_zone1/temp": "40000", + }, + expectState: CheckExitOK, + expectOut: "x86_pkg_temp: 65.0 °C", + }, } for _, test := range tests { @@ -115,6 +126,63 @@ func TestTemperatureSanitizeThresholds(t *testing.T) { assert.Equal(t, "0.000000", entry["max"]) } +func TestMergeExclusiveSensors(t *testing.T) { + t.Run("min values", func(t *testing.T) { + mockSysFS(t, map[string]string{ + "class/hwmon/hwmon0/name": "nvme", + "class/hwmon/hwmon0/temp1_label": "Composite", + "class/hwmon/hwmon0/temp1_input": "65000", + "class/hwmon/hwmon0/temp1_min": "10000", + }) + + src, err := sensors.TemperaturesWithContext(context.Background()) + require.NoError(t, err) + merged, err := (&CheckTemperature{}).mergeExclusiveSensors(context.Background(), src) + require.NoError(t, err) + require.Len(t, merged, 1) + assert.InDelta(t, 10.0, merged[0].Min, 0.0001) + }) + + t.Run("duplicate keys", func(t *testing.T) { + mockSysFS(t, map[string]string{ + "class/hwmon/hwmon0/name": "nvme", + "class/hwmon/hwmon0/temp1_label": "Composite", + "class/hwmon/hwmon0/temp1_input": "65000", + "class/hwmon/hwmon0/temp1_min": "10000", + "class/hwmon/hwmon1/name": "nvme", + "class/hwmon/hwmon1/temp1_label": "Composite", + "class/hwmon/hwmon1/temp1_input": "66000", + "class/hwmon/hwmon1/temp1_min": "20000", + }) + + src, err := sensors.TemperaturesWithContext(context.Background()) + require.NoError(t, err) + merged, err := (&CheckTemperature{}).mergeExclusiveSensors(context.Background(), src) + require.NoError(t, err) + require.Len(t, merged, 2) + assert.InDelta(t, 10.0, merged[0].Min, 0.0001) + assert.InDelta(t, 20.0, merged[1].Min, 0.0001) + }) + + t.Run("thermal zones without hwmon", func(t *testing.T) { + mockSysFS(t, map[string]string{ + "class/thermal/thermal_zone0/type": "x86_pkg_temp", + "class/thermal/thermal_zone0/temp": "65000", + "class/thermal/thermal_zone1/type": "acpitz", + "class/thermal/thermal_zone1/temp": "40000", + }) + + src, err := sensors.TemperaturesWithContext(context.Background()) + require.NoError(t, err) + require.Len(t, src, 2) + merged, err := (&CheckTemperature{}).mergeExclusiveSensors(context.Background(), src) + require.NoError(t, err) + require.Len(t, merged, 2) + assert.Zero(t, merged[0].Min) + assert.Zero(t, merged[1].Min) + }) +} + func mockSysFS(t *testing.T, files map[string]string) { t.Helper() sysDir := t.TempDir() From 867ca698c7a0324595a89b9ee463c222e8520130 Mon Sep 17 00:00:00 2001 From: Ahmet Ozturk Date: Mon, 16 Nov 2026 16:42:00 +0100 Subject: [PATCH 4/7] tests: cover negative plausible temperature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The mocked temperature test should also verify that a negative reading inside the plausible range stays OK. The CI runner reported -0.15 °C for a second NVMe sensor. --- pkg/snclient/check_temperature_linux_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/snclient/check_temperature_linux_test.go b/pkg/snclient/check_temperature_linux_test.go index d4afb4bad..75273f9cb 100644 --- a/pkg/snclient/check_temperature_linux_test.go +++ b/pkg/snclient/check_temperature_linux_test.go @@ -59,6 +59,16 @@ func TestTemperatureMocked(t *testing.T) { expectState: CheckExitOK, expectOut: "nvme_composite: 65.0 °C", }, + { + name: "negative plausible sensor", + files: map[string]string{ + "class/hwmon/hwmon0/name": "nvme", + "class/hwmon/hwmon0/temp1_label": "Composite", + "class/hwmon/hwmon0/temp1_input": "-150", + }, + expectState: CheckExitOK, + expectOut: "nvme_composite: -0.1 °C", + }, { name: "kelvin sentinel", files: map[string]string{ From ab74307469eec521c8b5b4403f4f7baae0f5301e Mon Sep 17 00:00:00 2001 From: Ahmet Ozturk Date: Mon, 16 Nov 2026 17:16:00 +0100 Subject: [PATCH 5/7] check_temperature: filter invalid readings instead of reporting unknown MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The default filter already removes the disabled values 0 °C and 1 °C. Add the minimum plausible temperature of -270 °C so invalid readings are filtered out as well. Remove the upper plausibility limit. A sensor can theoretically report any value above the minimum. Remove the default unknown threshold. The check no longer reports these sensors as UNKNOWN; they do not appear in the result. Keep the 0xFFFF Kelvin sentinel handling for the max threshold. Update the tests for the filter, the -0.1 °C reading, the missing upper limit, and the case with one invalid and one valid sensor. --- docs/checks/commands/check_temperature.md | 5 +- pkg/snclient/check_temperature.go | 20 +++++--- pkg/snclient/check_temperature_linux_test.go | 54 +++++++++++++++----- 3 files changed, 54 insertions(+), 25 deletions(-) diff --git a/docs/checks/commands/check_temperature.md b/docs/checks/commands/check_temperature.md index e02326932..f1c111a57 100644 --- a/docs/checks/commands/check_temperature.md +++ b/docs/checks/commands/check_temperature.md @@ -19,10 +19,9 @@ Check temperature sensors. | Argument | Default Value | | ------------- | ---------------------------------------------------------------------------- | -| filter | temperature != 0 and temperature != 1 | +| filter | temperature != 0 and temperature != 1 and temperature >= -270 | | warning | (min != 0 && temperature < %(min)) \|\| (crit != 0 && temperature > %(crit)) | | critical | (min != 0 && temperature < %(min)) \|\| (crit != 0 && temperature > %(crit)) | -| unknown | temperature < -50 \|\| temperature > 150 | | empty-state | 3 (UNKNOWN) | | empty-syntax | check_temperature failed to find any sensors. | | top-syntax | %(status) - %(list) | @@ -63,7 +62,7 @@ Show all temperature sensors and apply custom thresholds: check_temperature filter=none warn="temperature > 85" crit="temperature > 90" OK - Package id 0: 65.0 °C, Core 0: 62.0 °C, Core 1: 61.0 °C, Core 2: 65.0 °C |... -Sensors outside the plausible temperature range are reported as UNKNOWN. +Sensors with invalid values are filtered out. ### Example using check_snclient and Naemon diff --git a/pkg/snclient/check_temperature.go b/pkg/snclient/check_temperature.go index fe421723c..a72e1ec67 100644 --- a/pkg/snclient/check_temperature.go +++ b/pkg/snclient/check_temperature.go @@ -26,14 +26,14 @@ type temperatureStat struct { Min float64 // Temperature min value. } -// Plausible temperature range for computer hardware in Celsius. const ( - minPlausibleTemperature = -50.0 - maxPlausibleTemperature = 150.0 + // Minimum plausible temperature for computer hardware in Celsius. + // There is no upper limit. A sensor can theoretically report any higher value. + minPlausibleTemperature = -270.0 ) func isPlausibleTemperature(value float64) bool { - return value >= minPlausibleTemperature && value <= maxPlausibleTemperature + return value >= minPlausibleTemperature } func NewCheckTemperature() CheckHandler { @@ -52,10 +52,9 @@ func (l *CheckTemperature) Build() *CheckData { result: &CheckResult{ State: CheckExitOK, }, - defaultFilter: "temperature != 0 and temperature != 1", // seems like disabled sensors return 1.0000 or 0.0000 + defaultFilter: fmt.Sprintf("temperature != 0 and temperature != 1 and temperature >= %g", minPlausibleTemperature), // 0 and 1 mean disabled, below -270 is invalid defaultWarning: "(min != 0 && temperature < %(min)) || (crit != 0 && temperature > %(crit))", defaultCritical: "(min != 0 && temperature < %(min)) || (crit != 0 && temperature > %(crit))", - defaultUnknown: fmt.Sprintf("temperature < %g || temperature > %g", minPlausibleTemperature, maxPlausibleTemperature), topSyntax: "%(status) - %(list)", detailSyntax: "%(sensor): %(temperature:fmt=%.1f) °C", emptyState: 3, @@ -79,7 +78,7 @@ Show all temperature sensors and apply custom thresholds: check_temperature filter=none warn="temperature > 85" crit="temperature > 90" OK - Package id 0: 65.0 °C, Core 0: 62.0 °C, Core 1: 61.0 °C, Core 2: 65.0 °C |... -Sensors outside the plausible temperature range are reported as UNKNOWN. +Sensors with invalid values are filtered out. `, } } @@ -118,7 +117,7 @@ func (l *CheckTemperature) addSensor(check *CheckData, sensor *temperatureStat, duplicates[label] = 1 } - // Sanitizing: values outside the plausible range are sentinel or disabled thresholds. + // Sanitizing: values below the plausible range are sentinel or disabled thresholds. if !isPlausibleTemperature(sensor.High) { sensor.High = 0 } @@ -129,6 +128,11 @@ func (l *CheckTemperature) addSensor(check *CheckData, sensor *temperatureStat, sensor.Critical = 0 } + //nolint:mnd // 0xFFFF Kelvin converted to Celsius means the high value is not used + if sensor.High >= 65261.850000 { + sensor.High = 0 + } + entry := map[string]string{ "sensor": sensorKey, "name": name, diff --git a/pkg/snclient/check_temperature_linux_test.go b/pkg/snclient/check_temperature_linux_test.go index 75273f9cb..a630d19bd 100644 --- a/pkg/snclient/check_temperature_linux_test.go +++ b/pkg/snclient/check_temperature_linux_test.go @@ -23,21 +23,14 @@ func TestTemperature(t *testing.T) { // no sensors found, cannot test return } - assert.Regexpf(t, `^(OK|UNKNOWN) - .*: -?[\d.]+ °C`, out, "output matches") + assert.Regexpf(t, `^(OK|WARNING|CRITICAL|UNKNOWN) - .*: -?[\d.]+ °C`, out, "output matches") require.NotNil(t, res.Raw) + assert.NotEqualf(t, CheckExitUnknown, res.State, "valid sensors must not make the check unknown") - hasImplausible := false for _, entry := range res.Raw.listData { temp, err := strconv.ParseFloat(entry["temperature"], 64) require.NoError(t, err) - if temp < minPlausibleTemperature || temp > maxPlausibleTemperature { - hasImplausible = true - } - } - if hasImplausible { - assert.Equalf(t, CheckExitUnknown, res.State, "implausible sensor must make the check unknown") - } else { - assert.NotEqualf(t, CheckExitUnknown, res.State, "plausible sensors must not make the check unknown") + assert.GreaterOrEqual(t, temp, minPlausibleTemperature, "invalid sensor must be filtered out") } } @@ -47,6 +40,7 @@ func TestTemperatureMocked(t *testing.T) { files map[string]string expectState int64 expectOut string + rejectOut string }{ { name: "plausible sensor", @@ -78,7 +72,8 @@ func TestTemperatureMocked(t *testing.T) { "class/hwmon/hwmon0/temp1_max": "69850", }, expectState: CheckExitUnknown, - expectOut: "nvme_composite: -273.1 °C", + expectOut: "failed to find any sensors", + rejectOut: "-273.1", }, { name: "temperature too high", @@ -87,9 +82,21 @@ func TestTemperatureMocked(t *testing.T) { "class/hwmon/hwmon0/temp1_label": "Composite", "class/hwmon/hwmon0/temp1_input": "200000", }, - expectState: CheckExitUnknown, + expectState: CheckExitOK, expectOut: "nvme_composite: 200.0 °C", }, + { + name: "invalid and valid sensor", + files: map[string]string{ + "class/hwmon/hwmon0/name": "nvme", + "class/hwmon/hwmon0/temp1_input": "-273150", + "class/hwmon/hwmon1/name": "coretemp", + "class/hwmon/hwmon1/temp1_input": "65000", + }, + expectState: CheckExitOK, + expectOut: "coretemp: 65.0 °C", + rejectOut: "-273.1", + }, { name: "thermal zones without hwmon", files: map[string]string{ @@ -107,8 +114,12 @@ func TestTemperatureMocked(t *testing.T) { t.Run(test.name, func(t *testing.T) { mockSysFS(t, test.files) res := runTemperatureCheck(t) + out := string(res.BuildPluginOutput()) assert.Equal(t, test.expectState, res.State) - assert.Contains(t, string(res.BuildPluginOutput()), test.expectOut) + assert.Contains(t, out, test.expectOut) + if test.rejectOut != "" { + assert.NotContains(t, out, test.rejectOut) + } }) } } @@ -128,12 +139,27 @@ func TestTemperatureSanitizeThresholds(t *testing.T) { }, Min: -273.15, }, map[string]int{}) + check.addSensor(data, &temperatureStat{ + TemperatureStat: sensors.TemperatureStat{ + SensorKey: "hot_sensor", + Temperature: 65, + Critical: 200, + High: 200, + }, + Min: 200, + }, map[string]int{}) - require.Len(t, data.listData, 1) + require.Len(t, data.listData, 2) entry := data.listData[0] assert.Equal(t, "0.000000", entry["min"]) assert.Equal(t, "0.000000", entry["crit"]) assert.Equal(t, "0.000000", entry["max"]) + + // There is no upper limit, so high threshold values stay. + entry = data.listData[1] + assert.Equal(t, "200.000000", entry["min"]) + assert.Equal(t, "200.000000", entry["crit"]) + assert.Equal(t, "200.000000", entry["max"]) } func TestMergeExclusiveSensors(t *testing.T) { From 1a5d71c363cc65a05e8597fba852ca643ec12f93 Mon Sep 17 00:00:00 2001 From: Ahmet Ozturk Date: Tue, 17 Nov 2026 01:27:49 +0100 Subject: [PATCH 6/7] cleanup pr --- pkg/snclient/check_temperature_linux.go | 10 +++++----- pkg/snclient/check_temperature_linux_test.go | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/snclient/check_temperature_linux.go b/pkg/snclient/check_temperature_linux.go index 827f8b639..326c644e8 100644 --- a/pkg/snclient/check_temperature_linux.go +++ b/pkg/snclient/check_temperature_linux.go @@ -7,25 +7,25 @@ import ( ) func (l *CheckTemperature) mergeExclusiveSensors(ctx context.Context, src []sensors.TemperatureStat) ([]temperatureStat, error) { + // reports min, lowest and highest, exclusive to linux. ex := sensors.NewExLinux() exSensors, err := ex.TemperatureWithContext(ctx) if err != nil { log.Debugf("(linux)sensors.TemperaturesWithContext: %s", err.Error()) } - // Keep the values in order. Some systems expose the same sensor key more than once. - minByKey := make(map[string][]float64, len(exSensors)) + minValueBySensorKey := make(map[string][]float64, len(exSensors)) for idx := range exSensors { key := exSensors[idx].SensorKey - minByKey[key] = append(minByKey[key], exSensors[idx].Min) + minValueBySensorKey[key] = append(minValueBySensorKey[key], exSensors[idx].Min) } res := make([]temperatureStat, len(src)) for idx := range src { minVal := float64(0) - if values := minByKey[src[idx].SensorKey]; len(values) > 0 { + if values := minValueBySensorKey[src[idx].SensorKey]; len(values) > 0 { minVal = values[0] - minByKey[src[idx].SensorKey] = values[1:] + minValueBySensorKey[src[idx].SensorKey] = values[1:] } res[idx] = temperatureStat{ TemperatureStat: src[idx], diff --git a/pkg/snclient/check_temperature_linux_test.go b/pkg/snclient/check_temperature_linux_test.go index a630d19bd..a65d342a4 100644 --- a/pkg/snclient/check_temperature_linux_test.go +++ b/pkg/snclient/check_temperature_linux_test.go @@ -23,7 +23,7 @@ func TestTemperature(t *testing.T) { // no sensors found, cannot test return } - assert.Regexpf(t, `^(OK|WARNING|CRITICAL|UNKNOWN) - .*: -?[\d.]+ °C`, out, "output matches") + assert.Regexpf(t, `^(OK) - .*: -?[\d.]+ °C`, out, "output matches") require.NotNil(t, res.Raw) assert.NotEqualf(t, CheckExitUnknown, res.State, "valid sensors must not make the check unknown") From 8964ad566176ff32f0cfee431d3412d35e4656c3 Mon Sep 17 00:00:00 2001 From: Ahmet Oeztuerk Date: Tue, 17 Nov 2026 01:59:47 +0100 Subject: [PATCH 7/7] citest fixes --- pkg/snclient/check_temperature_linux_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/snclient/check_temperature_linux_test.go b/pkg/snclient/check_temperature_linux_test.go index a65d342a4..8d2323cc1 100644 --- a/pkg/snclient/check_temperature_linux_test.go +++ b/pkg/snclient/check_temperature_linux_test.go @@ -125,28 +125,28 @@ func TestTemperatureMocked(t *testing.T) { } func TestTemperatureSanitizeThresholds(t *testing.T) { - check := NewCheckTemperature().(*CheckTemperature) + check, _ := NewCheckTemperature().(*CheckTemperature) data := check.Build() _, err := data.parseArgs(nil) require.NoError(t, err) check.addSensor(data, &temperatureStat{ - TemperatureStat: sensors.TemperatureStat{ + sensors.TemperatureStat{ SensorKey: "nvme_composite", Temperature: 65, Critical: -273.15, High: 65261.85, }, - Min: -273.15, + -273.15, }, map[string]int{}) check.addSensor(data, &temperatureStat{ - TemperatureStat: sensors.TemperatureStat{ + sensors.TemperatureStat{ SensorKey: "hot_sensor", Temperature: 65, Critical: 200, High: 200, }, - Min: 200, + 200, }, map[string]int{}) require.Len(t, data.listData, 2) @@ -225,14 +225,14 @@ func mockSysFS(t *testing.T, files map[string]string) { for name, content := range files { path := filepath.Join(sysDir, filepath.FromSlash(name)) require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) - require.NoError(t, os.WriteFile(path, []byte(content+"\n"), 0o644)) + require.NoError(t, os.WriteFile(path, []byte(content+"\n"), 0o600)) } t.Setenv("HOST_SYS", sysDir) } func runTemperatureCheck(t *testing.T) *CheckResult { t.Helper() - check := NewCheckTemperature().(*CheckTemperature) + check, _ := NewCheckTemperature().(*CheckTemperature) data := check.Build() _, err := data.parseArgs(nil) require.NoError(t, err)