-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_sensors.go
91 lines (68 loc) · 2.14 KB
/
parse_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
// Go bindings for libsensors.so from the lm-sensors project via cgo: https://github.com/md14454/gosensors
//
// Note: This program does not have a C library dependency and instead calls the 'sensors' command and
// extracts the CPU temperature ('screen scraping').
package main
import "fmt"
import "regexp"
import "log"
import "bufio"
import "strings"
import "os/exec"
// Detect/discover the version of the sensors command and machine type (uname -m: x86_64 or armv6l or armv7l)
func init() {
sensorsCmd := exec.Command("/usr/bin/sensors", "-v")
// read output into a byte slice
sensorsOut, err := sensorsCmd.Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("sensors version: %s\n", sensorsOut)
// set the string constants to match based on the version of the sensors command
}
func main() {
// each argument is represented as a separate string, assign to Cmd sensorsCmd Cmd object
sensorsCmd := exec.Command("/usr/bin/sensors", "-u", "-A")
stdoutSensorsCmd, err := sensorsCmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
if err := sensorsCmd.Start(); err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(stdoutSensorsCmd)
for scanner.Scan() {
str := scanner.Text()
//
matched, _ := regexp.Match("CPUTIN:", []byte(str))
if matched {
// fmt.Println("found CPUTIN")
// read next line containing "temp2_input: 46.500" name value pair
scanner.Scan()
str = scanner.Text()
// fmt.Println("next line = ", str)
matched, _ := regexp.Match("temp2_input:", []byte(str))
if matched {
fmt.Println("found temp2_input", str)
// Example: temp2_input: 46.500
result := strings.Fields(str)
// Display all elements.
for i := range result {
fmt.Println(result[i])
}
// the second element contains the temperature
fmt.Println("temp = ", result[1])
// format: 42.000
temp := strings.Split(result[1], ".")
leftSide := temp[0]
// rightSide := temp[1]
// slice expression to extract the first character after the '.'
// format: 42.0
fmt.Printf("%s.%s\n", leftSide, temp[1][0:1])
}
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}