@@ -5,6 +5,7 @@ package sensors
5
5
6
6
import (
7
7
"context"
8
+ "fmt"
8
9
"os"
9
10
"path/filepath"
10
11
"strconv"
@@ -19,34 +20,20 @@ const (
19
20
)
20
21
21
22
func TemperaturesWithContext (ctx context.Context ) ([]TemperatureStat , error ) {
22
- var err error
23
-
24
- var files []string
25
-
26
- temperatures := make ([]TemperatureStat , 0 )
27
-
28
- // Only the temp*_input file provides current temperature
29
- // value in millidegree Celsius as reported by the temperature to the device:
30
- // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
31
- if files , err = filepath .Glob (common .HostSysWithContext (ctx , "/class/hwmon/hwmon*/temp*_input" )); err != nil {
32
- return temperatures , err
33
- }
23
+ var warns Warnings
34
24
35
- if len (files ) == 0 {
36
- // CentOS has an intermediate /device directory:
37
- // https://github.com/giampaolo/psutil/issues/971
38
- if files , err = filepath .Glob (common .HostSysWithContext (ctx , "/class/hwmon/hwmon*/device/temp*_input" )); err != nil {
39
- return temperatures , err
40
- }
25
+ files , err := getTemperatureFiles (ctx )
26
+ if err != nil {
27
+ return nil , fmt .Errorf ("failed to get tempreteure files, %w" , err )
41
28
}
42
29
43
- var warns Warnings
44
-
45
30
if len (files ) == 0 { // handle distributions without hwmon, like raspbian #391, parse legacy thermal_zone files
46
31
files , err = filepath .Glob (common .HostSysWithContext (ctx , "/class/thermal/thermal_zone*/" ))
47
32
if err != nil {
48
- return temperatures , err
33
+ return nil , err
49
34
}
35
+ temperatures := make ([]TemperatureStat , 0 , len (files ))
36
+
50
37
for _ , file := range files {
51
38
// Get the name of the temperature you are reading
52
39
name , err := os .ReadFile (filepath .Join (file , "type" ))
@@ -74,7 +61,7 @@ func TemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
74
61
return temperatures , warns .Reference ()
75
62
}
76
63
77
- temperatures = make ([]TemperatureStat , 0 , len (files ))
64
+ temperatures : = make ([]TemperatureStat , 0 , len (files ))
78
65
79
66
// example directory
80
67
// device/ temp1_crit_alarm temp2_crit_alarm temp3_crit_alarm temp4_crit_alarm temp5_crit_alarm temp6_crit_alarm temp7_crit_alarm
@@ -139,6 +126,29 @@ func TemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
139
126
return temperatures , warns .Reference ()
140
127
}
141
128
129
+ func getTemperatureFiles (ctx context.Context ) ([]string , error ) {
130
+ var files []string
131
+ var err error
132
+
133
+ // Only the temp*_input file provides current temperature
134
+ // value in millidegree Celsius as reported by the temperature to the device:
135
+ // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
136
+ if files , err = filepath .Glob (common .HostSysWithContext (ctx , "/class/hwmon/hwmon*/temp*_input" )); err != nil {
137
+ return nil , err
138
+ }
139
+
140
+ if len (files ) == 0 {
141
+ // CentOS has an intermediate /device directory:
142
+ // https://github.com/giampaolo/psutil/issues/971
143
+ if files , err = filepath .Glob (common .HostSysWithContext (ctx , "/class/hwmon/hwmon*/device/temp*_input" )); err != nil {
144
+ return nil , err
145
+ }
146
+ }
147
+
148
+ return files , nil
149
+
150
+ }
151
+
142
152
func optionalValueReadFromFile (filename string ) float64 {
143
153
var raw []byte
144
154
0 commit comments