Restore optional temperatures properties on Linux

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

@ -45,6 +45,7 @@ type TemperatureStat struct {
Temperature float64 `json:"temperature"`
High float64 `json:"sensorHigh"`
Critical float64 `json:"sensorCritical"`
Optional map[string]float64 `json:"optional,omitempty"`
}
func (h InfoStat) String() string {

@ -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{}
}
// 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 raw, err = os.ReadFile(filename); err != nil {
return 0
// Read and parse the file and keep the property on success
raw, err := os.ReadFile(fn)
if err != nil {
continue
}
if value, err = strconv.ParseFloat(strings.TrimSpace(string(raw)), 64); err != nil {
return 0
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
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

Loading…
Cancel
Save