Skip to content

Commit

Permalink
Improve backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan committed Nov 1, 2023
1 parent 5138aa7 commit 238190f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 32 deletions.
27 changes: 6 additions & 21 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"errors"
"math"
"os"
"runtime"
"time"
Expand Down Expand Up @@ -42,12 +41,11 @@ type UserStat struct {
}

type TemperatureStat struct {
SensorKey string `json:"sensorKey"`
Temperature float64 `json:"temperature"`
Low float64 `json:"sensorLow"`
High float64 `json:"sensorHigh"`
Critical float64 `json:"sensorCritical"`
Alarm float64 `json:"sensorAlarm"`
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 All @@ -61,20 +59,7 @@ func (u UserStat) String() string {
}

func (t TemperatureStat) String() string {
x := t
if math.IsNaN(x.Low) {
x.Low = 0
}
if math.IsNaN(x.High) {
x.High = 0
}
if math.IsNaN(x.Critical) {
x.Critical = 0
}
if math.IsNaN(x.Alarm) {
x.Alarm = 0
}
s, _ := json.Marshal(x)
s, _ := json.Marshal(t)
return string(s)
}

Expand Down
49 changes: 41 additions & 8 deletions host/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/binary"
"fmt"
"io"
"math"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -494,10 +493,9 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err
temperatures = append(temperatures, TemperatureStat{
SensorKey: name,
Temperature: temperature / hostTemperatureScale,
Low: optionalValueReadFromFile(basepath+"_min") / hostTemperatureScale,
High: optionalValueReadFromFile(basepath+"_max") / hostTemperatureScale,
Critical: optionalValueReadFromFile(basepath+"_crit") / hostTemperatureScale,
Alarm: optionalValueReadFromFile(basepath+"_alarm") / hostTemperatureScale,
Optional: optionalProperties(filepath.Join(directory, basename)),
})
}

Expand All @@ -506,23 +504,58 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err

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 math.NaN()
return 0
}

if raw, err = os.ReadFile(filename); err != nil {
return math.NaN()
return 0
}

if value, err = strconv.ParseFloat(strings.TrimSpace(string(raw)), 64); err != nil {
return math.NaN()
return 0
}

return value
}

func optionalProperties(basename string) map[string]float64 {
fmt.Println("base:", basename)
// Determine all files with the base-prefix
matches, err := filepath.Glob(basename + "_*")
if err != nil {
return nil
}
fmt.Println("matches:", matches)

// 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
}

// 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 values
}
36 changes: 33 additions & 3 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 @@ -140,17 +143,44 @@ func TestTemperatureStat_String(t *testing.T) {
v := TemperatureStat{
SensorKey: "CPU",
Temperature: 1.1,
Low: 0.1,
High: 30.1,
Critical: 0.1,
Alarm: 80.1,
}
s := `{"sensorKey":"CPU","temperature":1.1,"sensorLow":0.1,"sensorHigh":30.1,"sensorCritical":0.1,"sensorAlarm":80.1}`
s := `{"sensorKey":"CPU","temperature":1.1,"sensorHigh":30.1,"sensorCritical":0.1}`
if s != fmt.Sprintf("%v", v) {
t.Errorf("TemperatureStat string is invalid, %v", fmt.Sprintf("%v", v))
}
}

func TestTemperatureStat_StringNotSet(t *testing.T) {
v := TemperatureStat{
SensorKey: "CPU",
Temperature: 1.1,
}
s := `{"sensorKey":"CPU","temperature":1.1,"sensorHigh":0,"sensorCritical":0}`
if s != fmt.Sprintf("%v", v) {
t.Errorf("TemperatureStat string is invalid, %v", fmt.Sprintf("%v", 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 expected TemperatureStat
require.NoError(t, json.Unmarshal([]byte(v.String()), &expected))
require.EqualValues(t, expected, v)
}

func TestVirtualization(t *testing.T) {
wg := sync.WaitGroup{}
testCount := 10
Expand Down

0 comments on commit 238190f

Please sign in to comment.