/
temp_disks_sys.go
82 lines (76 loc) · 2.17 KB
/
temp_disks_sys.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
package datasources
import (
"io/ioutil"
"path/filepath"
"strconv"
"strings"
)
// getFromSys gets temperature data from /sys, requires drivetemp kernel module (Linux 5.6+)
func getFromSys() (deviceList []diskEntry, err error) {
// Model (SCSI): /sys/block/sda/device/model
// Temp (SCSI): /sys/block/sda/device/hwmon/hwmon3/temp1_input
// Model (NVMe): /sys/block/nvme0n1/device/model
// Temp (NVMe): /sys/block/nvme0n1/device/device/hwmon/hwmon1/temp1_input
blockDevices, err := ioutil.ReadDir("/sys/block")
if err != nil {
return
}
var hwmonBaseDir string
var model string
for _, file := range blockDevices {
devicePath := filepath.Join("/sys/block/", file.Name(), "/device")
if strings.HasPrefix(file.Name(), "sd") {
hwmonBaseDir = filepath.Join(devicePath, "/hwmon")
} else if strings.HasPrefix(file.Name(), "nvme") {
hwmonBaseDir = filepath.Join(devicePath, "/device/hwmon")
} else {
continue
}
content, err := ioutil.ReadFile(filepath.Join(devicePath, "/model"))
if err != nil {
model = "N/A"
// Suppress error
err = nil
} else {
model = string(content)
}
hwmonFiles, err := filepath.Glob(hwmonBaseDir + "/hwmon*/temp*_*")
temps := readHwmonFiles(hwmonFiles)
deviceList = append(deviceList, diskEntry{
block: file.Name(),
model: model,
temps: temps,
})
}
return
}
// Adapted from SensorsTemperaturesWithContext in gopsutil
func readHwmonFiles(files []string) (temps []diskTemp) {
for _, file := range files {
filename := strings.Split(filepath.Base(file), "_")
// Only read current temperature
if filename[1] != "input" {
continue
}
// Get the label of the temperature you are reading
var label string
c, _ := ioutil.ReadFile(filepath.Join(filepath.Dir(file), filename[0]+"_label"))
if c != nil {
label = strings.Join(strings.Split(strings.TrimSpace(strings.ToLower(string(c))), " "), "")
}
// Get the temperature reading
current, err := ioutil.ReadFile(file)
if err != nil {
continue
}
temperature, err := strconv.ParseFloat(strings.TrimSpace(string(current)), 64)
if err != nil {
continue
}
temps = append(temps, diskTemp{
name: label,
temp: temperature / 1000.0,
})
}
return
}