Skip to content

Commit

Permalink
Condense code
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan committed Nov 1, 2023
1 parent bb365d3 commit f6898d4
Showing 1 changed file with 5 additions and 25 deletions.
30 changes: 5 additions & 25 deletions host/host_linux.go
Expand Up @@ -490,44 +490,24 @@ 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,
Optional: optionalProperties(filepath.Join(directory, basename)),
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
}

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

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

return value
}

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

// Collect the information from all files that are not already handled
Expand Down

0 comments on commit f6898d4

Please sign in to comment.