Skip to content

Commit 0ad2364

Browse files
committed
Restore optional temperatures properties on Linux
1 parent a7e2bdf commit 0ad2364

3 files changed

Lines changed: 67 additions & 22 deletions

File tree

host/host.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ type UserStat struct {
4141
}
4242

4343
type TemperatureStat struct {
44-
SensorKey string `json:"sensorKey"`
45-
Temperature float64 `json:"temperature"`
46-
High float64 `json:"sensorHigh"`
47-
Critical float64 `json:"sensorCritical"`
44+
SensorKey string `json:"sensorKey"`
45+
Temperature float64 `json:"temperature"`
46+
High float64 `json:"sensorHigh"`
47+
Critical float64 `json:"sensorCritical"`
48+
Optional map[string]float64 `json:"optional,omitempty"`
4849
}
4950

5051
func (h InfoStat) String() string {

host/host_linux.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -490,36 +490,50 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err
490490
}
491491

492492
// Add discovered temperature sensor to the list
493+
optional := optionalProperties(filepath.Join(directory, basename))
493494
temperatures = append(temperatures, TemperatureStat{
494495
SensorKey: name,
495496
Temperature: temperature / hostTemperatureScale,
496-
High: optionalValueReadFromFile(basepath+"_max") / hostTemperatureScale,
497-
Critical: optionalValueReadFromFile(basepath+"_crit") / hostTemperatureScale,
497+
High: optional["max"],
498+
Critical: optional["crit"],
499+
Optional: optional,
498500
})
499501
}
500502

501503
return temperatures, warns.Reference()
502504
}
503505

504-
func optionalValueReadFromFile(filename string) float64 {
505-
var raw []byte
506-
507-
var err error
508-
509-
var value float64
510-
511-
// Check if file exists
512-
if _, err := os.Stat(filename); os.IsNotExist(err) {
513-
return 0
506+
func optionalProperties(basename string) map[string]float64 {
507+
// Determine all files with the base-prefix
508+
matches, err := filepath.Glob(basename + "_*")
509+
if err != nil {
510+
return map[string]float64{}
514511
}
515512

516-
if raw, err = os.ReadFile(filename); err != nil {
517-
return 0
518-
}
513+
// Collect the information from all files that are not already handled
514+
// with the exception of "max" and "crit" to keep an indicator if those
515+
// actually exist
516+
values := make(map[string]float64)
517+
for _, fn := range matches {
518+
// Skip already handles files
519+
suffix := strings.Split(fn, "_")
520+
property := suffix[len(suffix)-1]
521+
switch property {
522+
case "label", "input":
523+
continue
524+
}
519525

520-
if value, err = strconv.ParseFloat(strings.TrimSpace(string(raw)), 64); err != nil {
521-
return 0
526+
// Read and parse the file and keep the property on success
527+
raw, err := os.ReadFile(fn)
528+
if err != nil {
529+
continue
530+
}
531+
value, err := strconv.ParseFloat(strings.TrimSpace(string(raw)), 64)
532+
if err != nil {
533+
continue
534+
}
535+
values[property] = value / hostTemperatureScale
522536
}
523537

524-
return value
538+
return values
525539
}

host/host_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package host
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"os"
78
"sync"
89
"testing"
910

11+
"github.com/stretchr/testify/require"
12+
1013
"github.com/shirou/gopsutil/v3/internal/common"
1114
)
1215

@@ -149,6 +152,33 @@ func TestTemperatureStat_String(t *testing.T) {
149152
}
150153
}
151154

155+
func TestTemperatureStat_StringNotSet(t *testing.T) {
156+
v := TemperatureStat{
157+
SensorKey: "CPU",
158+
Temperature: 1.1,
159+
}
160+
expected := `{"sensorKey":"CPU","temperature":1.1,"sensorHigh":0,"sensorCritical":0}`
161+
require.Equalf(t, expected, v.String(), "TemperatureStat string is invalid, %s", v)
162+
}
163+
164+
func TestTemperatureStat_StringOptional(t *testing.T) {
165+
v := TemperatureStat{
166+
SensorKey: "CPU",
167+
Temperature: 1.1,
168+
High: 30.1,
169+
Critical: 0.1,
170+
Optional: map[string]float64{
171+
"min": -273.1,
172+
"max": 30.1,
173+
"crit": 0.1,
174+
"alarm": 80.3,
175+
},
176+
}
177+
var actual TemperatureStat
178+
require.NoError(t, json.Unmarshal([]byte(v.String()), &actual))
179+
require.EqualValues(t, v, actual)
180+
}
181+
152182
func TestVirtualization(t *testing.T) {
153183
wg := sync.WaitGroup{}
154184
testCount := 10

0 commit comments

Comments
 (0)