Skip to content

check_temperature: filter invalid readings and fix Linux min merge - #501

Merged
sni merged 7 commits into
mainfrom
check-temperature-fixes-1
Nov 19, 2026
Merged

sni merged 7 commits into
mainfrom
check-temperature-fixes-1

Conversation

@inqrphl

@inqrphl inqrphl commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

AI GENERATED: This pull request and its description were prepared by an AI assistant. Review all changes carefully.

What this fixes

TestTemperature failed in CI because an NVMe sensor reported -273.15 °C (0 K). The check treated that value as a normal reading, and the test regex rejected the minus sign.

Failing run: 43891227695

OK - nvme_composite: -273.1 °C, nvme_composite.1: -0.1 °C |...

Changes

  • The default filter now drops invalid readings below -270 °C, in addition to the existing 0 °C and 1 °C values.
  • There is no upper temperature limit. A reading of 200 °C is accepted.
  • Threshold sanitization treats values below -270 °C as unset. The 0xFFFF Kelvin sentinel for the max threshold is still removed. High threshold values are no longer rejected.
  • The Linux min merge no longer pairs sensors by index. Values are grouped by sensor key and consumed in order. This also fixes an out-of-range panic on machines with thermal zones but no hwmon.
  • Tests:
    • The existing hardware-based test is kept and updated for negative values.
    • New mocked HOST_SYS tests cover a normal sensor, -0.1 °C, -273.15 °C, 200 °C, one invalid plus one valid sensor, and thermal-zone-only systems.
    • Tests for min merging, duplicate sensor keys, and threshold sanitization.
  • docs/checks/commands/check_temperature.md regenerated.

Verification

  • golangci-lint run --new-from-rev=origin/main ./pkg/snclient/... -> 0 issues
  • GOOS=linux go vet ./pkg/snclient -> ok
  • GOOS=linux go test -c ./pkg/snclient -> compiled
  • Linux test execution is left to CI. The local environment has no Linux runtime or Docker daemon.

Note on history

An earlier commit (d3a4d73a) used an UNKNOWN state for implausible readings. ab743074 replaces that approach with filtering, which is the final behavior.

inqrphl and others added 7 commits November 16, 2026 16:28
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.
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.
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.
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.
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.
res := make([]temperatureStat, len(src))
for idx := range src {
minVal := float64(0)
if len(exSensors) < idx {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking if len(exSensors) < idx, and then accessing exSensors[idx]

the condition has to be reversed

log.Debugf("(linux)sensors.TemperaturesWithContext: %s", err.Error())
}

minValueBySensorKey := make(map[string][]float64, len(exSensors))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sensors.NewExLinux() can report less or more results than sensors.TemperaturesWithContext() ~ the src[] argument

Also the sensors may not be aligned

Thats why a map based on sensor key is built, src might have sensors A, and C while exSensors might have B , C , and D

string(res.BuildPluginOutput()),
"output matches",
)
assert.Regexpf(t, `^(OK) - .*: -?[\d.]+ °C`, out, "output matches")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a "-?" sign to the regex front

assert.InDelta(t, 20.0, merged[1].Min, 0.0001)
})

t.Run("thermal zones without hwmon", func(t *testing.T) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the case where you have more elements from sensors.NewExLinux() than sensors.TemperaturesWithContext()

@inqrphl
inqrphl requested a review from sni November 17, 2026 10:02
@sni
sni merged commit 5bdfb73 into main Nov 19, 2026
151 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants