Restore optional temperatures properties on Linux

pull/1546/head
Sven Rebhan 2 years ago
parent a7e2bdf91c
commit 0ad2364b95

@ -41,10 +41,11 @@ type UserStat struct {
} }
type TemperatureStat struct { type TemperatureStat struct {
SensorKey string `json:"sensorKey"` SensorKey string `json:"sensorKey"`
Temperature float64 `json:"temperature"` Temperature float64 `json:"temperature"`
High float64 `json:"sensorHigh"` High float64 `json:"sensorHigh"`
Critical float64 `json:"sensorCritical"` Critical float64 `json:"sensorCritical"`
Optional map[string]float64 `json:"optional,omitempty"`
} }
func (h InfoStat) String() string { func (h InfoStat) String() string {

@ -490,36 +490,50 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err
} }
// Add discovered temperature sensor to the list // Add discovered temperature sensor to the list
optional := optionalProperties(filepath.Join(directory, basename))
temperatures = append(temperatures, TemperatureStat{ temperatures = append(temperatures, TemperatureStat{
SensorKey: name, SensorKey: name,
Temperature: temperature / hostTemperatureScale, Temperature: temperature / hostTemperatureScale,
High: optionalValueReadFromFile(basepath+"_max") / hostTemperatureScale, High: optional["max"],
Critical: optionalValueReadFromFile(basepath+"_crit") / hostTemperatureScale, Critical: optional["crit"],
Optional: optional,
}) })
} }
return temperatures, warns.Reference() return temperatures, warns.Reference()
} }
func optionalValueReadFromFile(filename string) float64 { func optionalProperties(basename string) map[string]float64 {
var raw []byte // Determine all files with the base-prefix
matches, err := filepath.Glob(basename + "_*")
var err error if err != nil {
return map[string]float64{}
var value float64
// Check if file exists
if _, err := os.Stat(filename); os.IsNotExist(err) {
return 0
} }
if raw, err = os.ReadFile(filename); err != nil { // Collect the information from all files that are not already handled
return 0 // 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 { // Read and parse the file and keep the property on success
return 0 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
} }

@ -1,12 +1,15 @@
package host package host
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"os" "os"
"sync" "sync"
"testing" "testing"
"github.com/stretchr/testify/require"
"github.com/shirou/gopsutil/v3/internal/common" "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) { func TestVirtualization(t *testing.T) {
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
testCount := 10 testCount := 10

Loading…
Cancel
Save