-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsensors.go
89 lines (74 loc) · 1.88 KB
/
sensors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package gosensors
import (
"encoding/json"
"errors"
"io/ioutil"
"os/exec"
"strings"
)
// Sensors struct represents lm-sensors output.
// Content field contains string output.
// Chips field contains map[string]Entries.
// Example (JSON style):
// "coretemp-isa-0000": {
// "CPU": "+60.0°C",
// "GPU": "+48.0°C",
// }
type Sensors struct {
Content string `json:"-"`
Chips map[string]Entries `json:"chips"`
}
// Entries representing key, value pairs for chips.
// Example (JSON style):
// "GPU": "+56.0°C"
// "CPU": "+68.0°C"
type Entries map[string]string
func construct(content string) *Sensors {
s := &Sensors{}
s.Content = content
s.Chips = map[string]Entries{}
lines := strings.Split(s.Content, "\n")
var chip string
for _, line := range lines {
if len(line) > 0 {
if !strings.Contains(line, ":") {
chip = line
s.Chips[chip] = Entries{}
} else if len(chip) > 0 {
parts := strings.Split(line, ":")
entry := parts[0]
value := strings.TrimRight(strings.TrimLeft(parts[1], " "), " ")
s.Chips[chip][entry] = value
}
}
}
return s
}
// NewFromSystem executes "sensors" system command and returns constructed Sensors struct.
// A successful call returns err == nil.
func NewFromSystem() (*Sensors, error) {
out, err := exec.Command("sensors").Output()
if err != nil {
return &Sensors{}, errors.New("lm-sensors missing")
}
s := construct(string(out))
return s, nil
}
// NewFromFile reads content from log file and returns constructed Sensors struct.
// A successful call returns err == nil.
func NewFromFile(path string) (*Sensors, error) {
out, err := ioutil.ReadFile(path)
if err != nil {
return &Sensors{}, err
}
s := construct(string(out))
return s, nil
}
// JSON returns JSON of Sensors.
func (s *Sensors) JSON() string {
out, _ := json.Marshal(s)
return string(out)
}
func (s *Sensors) String() string {
return s.JSON()
}