Skip to content

Commit

Permalink
Restore optional temperatures properties on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan committed Nov 1, 2023
1 parent a7e2bdf commit 0ad2364
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
9 changes: 5 additions & 4 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
50 changes: 32 additions & 18 deletions host/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
30 changes: 30 additions & 0 deletions host/host_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0ad2364

Please sign in to comment.