Repository navigation
check_temperature: filter invalid readings and fix Linux min merge - #501
Merged
Merged
Conversation
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.
inqrphl
commented
Nov 17, 2026
| res := make([]temperatureStat, len(src)) | ||
| for idx := range src { | ||
| minVal := float64(0) | ||
| if len(exSensors) < idx { |
Contributor
Author
There was a problem hiding this comment.
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)) |
Contributor
Author
There was a problem hiding this comment.
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") |
Contributor
Author
There was a problem hiding this comment.
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) { |
Contributor
Author
There was a problem hiding this comment.
this is the case where you have more elements from sensors.NewExLinux() than sensors.TemperaturesWithContext()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this fixes
TestTemperaturefailed 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
Changes
-270 °C, in addition to the existing0 °Cand1 °Cvalues.200 °Cis accepted.-270 °Cas unset. The0xFFFFKelvin sentinel for the max threshold is still removed. High threshold values are no longer rejected.HOST_SYStests cover a normal sensor,-0.1 °C,-273.15 °C,200 °C, one invalid plus one valid sensor, and thermal-zone-only systems.docs/checks/commands/check_temperature.mdregenerated.Verification
golangci-lint run --new-from-rev=origin/main ./pkg/snclient/...-> 0 issuesGOOS=linux go vet ./pkg/snclient-> okGOOS=linux go test -c ./pkg/snclient-> compiledNote on history
An earlier commit (
d3a4d73a) used anUNKNOWNstate for implausible readings.ab743074replaces that approach with filtering, which is the final behavior.