From 0ad2364b95df1f70b22b5a3fb710f0e2b3e5b9fc Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Tue, 31 Oct 2023 15:40:03 +0100 Subject: [PATCH] Restore optional temperatures properties on Linux --- host/host.go | 9 +++++---- host/host_linux.go | 50 +++++++++++++++++++++++++++++----------------- host/host_test.go | 30 ++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 22 deletions(-) diff --git a/host/host.go b/host/host.go index c7e84e3a5..51fb10e6e 100644 --- a/host/host.go +++ b/host/host.go @@ -41,10 +41,11 @@ type UserStat struct { } type TemperatureStat struct { - SensorKey string `json:"sensorKey"` - Temperature float64 `json:"temperature"` - High float64 `json:"sensorHigh"` - Critical float64 `json:"sensorCritical"` + SensorKey string `json:"sensorKey"` + Temperature float64 `json:"temperature"` + High float64 `json:"sensorHigh"` + Critical float64 `json:"sensorCritical"` + Optional map[string]float64 `json:"optional,omitempty"` } func (h InfoStat) String() string { diff --git a/host/host_linux.go b/host/host_linux.go index f9d7995e7..6ae816137 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -490,36 +490,50 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err } // Add discovered temperature sensor to the list + optional := optionalProperties(filepath.Join(directory, basename)) temperatures = append(temperatures, TemperatureStat{ SensorKey: name, Temperature: temperature / hostTemperatureScale, - High: optionalValueReadFromFile(basepath+"_max") / hostTemperatureScale, - Critical: optionalValueReadFromFile(basepath+"_crit") / hostTemperatureScale, + High: optional["max"], + Critical: optional["crit"], + Optional: optional, }) } return temperatures, warns.Reference() } -func optionalValueReadFromFile(filename string) float64 { - var raw []byte - - var err error - - var value float64 - - // Check if file exists - if _, err := os.Stat(filename); os.IsNotExist(err) { - return 0 +func optionalProperties(basename string) map[string]float64 { + // Determine all files with the base-prefix + matches, err := filepath.Glob(basename + "_*") + if err != nil { + return map[string]float64{} } - if raw, err = os.ReadFile(filename); err != nil { - return 0 - } + // Collect the information from all files that are not already handled + // with the exception of "max" and "crit" to keep an indicator if those + // actually exist + values := make(map[string]float64) + for _, fn := range matches { + // Skip already handles files + suffix := strings.Split(fn, "_") + property := suffix[len(suffix)-1] + switch property { + case "label", "input": + continue + } - if value, err = strconv.ParseFloat(strings.TrimSpace(string(raw)), 64); err != nil { - return 0 + // Read and parse the file and keep the property on success + raw, err := os.ReadFile(fn) + if err != nil { + continue + } + value, err := strconv.ParseFloat(strings.TrimSpace(string(raw)), 64) + if err != nil { + continue + } + values[property] = value / hostTemperatureScale } - return value + return values } diff --git a/host/host_test.go b/host/host_test.go index 0aa092598..008f2c3d4 100644 --- a/host/host_test.go +++ b/host/host_test.go @@ -1,12 +1,15 @@ package host import ( + "encoding/json" "errors" "fmt" "os" "sync" "testing" + "github.com/stretchr/testify/require" + "github.com/shirou/gopsutil/v3/internal/common" ) @@ -149,6 +152,33 @@ func TestTemperatureStat_String(t *testing.T) { } } +func TestTemperatureStat_StringNotSet(t *testing.T) { + v := TemperatureStat{ + SensorKey: "CPU", + Temperature: 1.1, + } + expected := `{"sensorKey":"CPU","temperature":1.1,"sensorHigh":0,"sensorCritical":0}` + require.Equalf(t, expected, v.String(), "TemperatureStat string is invalid, %s", v) +} + +func TestTemperatureStat_StringOptional(t *testing.T) { + v := TemperatureStat{ + SensorKey: "CPU", + Temperature: 1.1, + High: 30.1, + Critical: 0.1, + Optional: map[string]float64{ + "min": -273.1, + "max": 30.1, + "crit": 0.1, + "alarm": 80.3, + }, + } + var actual TemperatureStat + require.NoError(t, json.Unmarshal([]byte(v.String()), &actual)) + require.EqualValues(t, v, actual) +} + func TestVirtualization(t *testing.T) { wg := sync.WaitGroup{} testCount := 10