diff --git a/docs/checks/commands/check_temperature.md b/docs/checks/commands/check_temperature.md index 471159ba..f1c111a5 100644 --- a/docs/checks/commands/check_temperature.md +++ b/docs/checks/commands/check_temperature.md @@ -19,7 +19,7 @@ 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)) | | empty-state | 3 (UNKNOWN) | @@ -62,6 +62,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 with invalid values are filtered out. + ### Example using check_snclient and Naemon Naemon Config diff --git a/pkg/snclient/check_temperature.go b/pkg/snclient/check_temperature.go index 5e3e15c6..a72e1ec6 100644 --- a/pkg/snclient/check_temperature.go +++ b/pkg/snclient/check_temperature.go @@ -26,6 +26,16 @@ type temperatureStat struct { Min float64 // Temperature min value. } +const ( + // 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 +} + func NewCheckTemperature() CheckHandler { return &CheckTemperature{} } @@ -42,7 +52,7 @@ 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))", topSyntax: "%(status) - %(list)", @@ -67,6 +77,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 with invalid values are filtered out. `, } } @@ -105,7 +117,18 @@ 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 + // Sanitizing: values below 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 + } + + //nolint:mnd // 0xFFFF Kelvin converted to Celsius means the high value is not used if sensor.High >= 65261.850000 { sensor.High = 0 } diff --git a/pkg/snclient/check_temperature_linux.go b/pkg/snclient/check_temperature_linux.go index bd47801f..326c644e 100644 --- a/pkg/snclient/check_temperature_linux.go +++ b/pkg/snclient/check_temperature_linux.go @@ -7,23 +7,30 @@ 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()) } + minValueBySensorKey := make(map[string][]float64, len(exSensors)) + for idx := range exSensors { + key := exSensors[idx].SensorKey + minValueBySensorKey[key] = append(minValueBySensorKey[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 := minValueBySensorKey[src[idx].SensorKey]; len(values) > 0 { + minVal = values[0] + minValueBySensorKey[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 eddb0c33..8d2323cc 100644 --- a/pkg/snclient/check_temperature_linux_test.go +++ b/pkg/snclient/check_temperature_linux_test.go @@ -1,10 +1,16 @@ package snclient import ( + "context" + "os" + "path/filepath" + "strconv" "strings" "testing" + "github.com/shirou/gopsutil/v4/sensors" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestTemperature(t *testing.T) { @@ -12,15 +18,226 @@ 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) - .*: -?[\d.]+ °C`, out, "output matches") + require.NotNil(t, res.Raw) + assert.NotEqualf(t, CheckExitUnknown, res.State, "valid sensors must not make the check unknown") + + for _, entry := range res.Raw.listData { + temp, err := strconv.ParseFloat(entry["temperature"], 64) + require.NoError(t, err) + assert.GreaterOrEqual(t, temp, minPlausibleTemperature, "invalid sensor must be filtered out") + } +} + +func TestTemperatureMocked(t *testing.T) { + tests := []struct { + name string + files map[string]string + expectState int64 + expectOut string + rejectOut 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: "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{ + "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: "failed to find any sensors", + rejectOut: "-273.1", + }, + { + 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: 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{ + "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 { + 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, out, test.expectOut) + if test.rejectOut != "" { + assert.NotContains(t, out, test.rejectOut) + } + }) + } +} + +func TestTemperatureSanitizeThresholds(t *testing.T) { + check, _ := NewCheckTemperature().(*CheckTemperature) + data := check.Build() + _, err := data.parseArgs(nil) + require.NoError(t, err) + + check.addSensor(data, &temperatureStat{ + sensors.TemperatureStat{ + SensorKey: "nvme_composite", + Temperature: 65, + Critical: -273.15, + High: 65261.85, + }, + -273.15, + }, map[string]int{}) + check.addSensor(data, &temperatureStat{ + sensors.TemperatureStat{ + SensorKey: "hot_sensor", + Temperature: 65, + Critical: 200, + High: 200, + }, + 200, + }, map[string]int{}) + + 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) { + 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() + 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"), 0o600)) + } + 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 }