-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sensors][linux]: add ExLinux on sensors.
This commit references and fixes #1589. Thank you!
- Loading branch information
Showing
2 changed files
with
111 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
//go:build linux | ||
|
||
package sensors | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// ExTemperature represents Linux dependent temperature sensor data | ||
type ExTemperature struct { | ||
SensorKey string `json:"key"` | ||
Min float64 `json:"min"` // Temperature min value. | ||
Lowest float64 `json:"lowest"` // Historical minimum temperature | ||
Highest float64 `json:"highest"` // Historical maximum temperature | ||
} | ||
|
||
type ExLinux struct{} | ||
|
||
func NewExLinux() *ExLinux { | ||
return &ExLinux{} | ||
} | ||
|
||
func (ex *ExLinux) TemperatureWithContext(ctx context.Context) ([]ExTemperature, error) { | ||
var warns Warnings | ||
|
||
files, err := getTemperatureFiles(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get temperature files, %w", err) | ||
} | ||
|
||
temperatures := make([]ExTemperature, 0, len(files)) | ||
for _, file := range files { | ||
var raw []byte | ||
|
||
// Get the base directory location | ||
directory := filepath.Dir(file) | ||
|
||
// Get the base filename prefix like temp1 | ||
basename := strings.Split(filepath.Base(file), "_")[0] | ||
|
||
// Get the base path like <dir>/temp1 | ||
basepath := filepath.Join(directory, basename) | ||
|
||
// Get the label of the temperature you are reading | ||
label := "" | ||
|
||
if raw, _ = os.ReadFile(basepath + "_label"); len(raw) != 0 { | ||
// Format the label from "Core 0" to "core_0" | ||
label = strings.Join(strings.Split(strings.TrimSpace(strings.ToLower(string(raw))), " "), "_") | ||
} | ||
|
||
// Get the name of the temperature you are reading | ||
if raw, err = os.ReadFile(filepath.Join(directory, "name")); err != nil { | ||
warns.Add(err) | ||
continue | ||
} | ||
|
||
name := strings.TrimSpace(string(raw)) | ||
|
||
if label != "" { | ||
name = name + "_" + label | ||
} | ||
|
||
// Add discovered temperature sensor to the list | ||
temperatures = append(temperatures, ExTemperature{ | ||
SensorKey: name, | ||
Min: optionalValueReadFromFile(basepath+"_min") / hostTemperatureScale, | ||
Lowest: optionalValueReadFromFile(basepath+"_lowest") / hostTemperatureScale, | ||
Highest: optionalValueReadFromFile(basepath+"_highest") / hostTemperatureScale, | ||
}) | ||
} | ||
|
||
return temperatures, warns.Reference() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters