Skip to content

Commit 091f46c

Browse files
authored
class_cooling_device: ignore EINVAL (etc) when reading files. (#783)
Fixes: #746, fixes prometheus/node_exporter#3420 Some cooling devices return errors when attempting to read state files. When that happens, ignore the error, and skip to the next device rather than failing to return any devices. Signed-off-by: Malcolm Rowe <malcolmr@google.com>
1 parent 5efaf0a commit 091f46c

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

sysfs/class_cooling_device.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,20 @@ func (fs FS) ClassCoolingDeviceStats() ([]ClassCoolingDeviceStats, error) {
4040
}
4141

4242
var coolingDeviceStats = ClassCoolingDeviceStats{}
43-
stats := make([]ClassCoolingDeviceStats, len(cds))
44-
for i, cd := range cds {
43+
stats := make([]ClassCoolingDeviceStats, 0, len(cds))
44+
for _, cd := range cds {
4545
cdName := strings.TrimPrefix(filepath.Base(cd), "cooling_device")
4646

4747
coolingDeviceStats, err = parseCoolingDeviceStats(cd)
4848
if err != nil {
49+
if canIgnoreError(err) {
50+
continue
51+
}
4952
return []ClassCoolingDeviceStats{}, err
5053
}
5154

5255
coolingDeviceStats.Name = cdName
53-
stats[i] = coolingDeviceStats
56+
stats = append(stats, coolingDeviceStats)
5457
}
5558
return stats, nil
5659
}

sysfs/class_cooling_device_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestClassCoolingDeviceStats(t *testing.T) {
4040
CurState: 0,
4141
},
4242
{
43-
Name: "1",
43+
Name: "2",
4444
Type: "intel_powerclamp",
4545
MaxState: 27,
4646
CurState: -1,

testdata/fixtures.ttar

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7057,20 +7057,33 @@ Mode: 644
70577057
Directory: fixtures/sys/class/thermal/cooling_device1
70587058
Mode: 755
70597059
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7060-
Path: fixtures/sys/class/thermal/cooling_device1/cur_state
7060+
Path: fixtures/sys/class/thermal/cooling_device1/max_state
70617061
Lines: 1
7062-
-1
7062+
50
70637063
Mode: 644
70647064
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7065-
Path: fixtures/sys/class/thermal/cooling_device1/max_state
7065+
Path: fixtures/sys/class/thermal/cooling_device1/type
70667066
Lines: 1
7067-
27
7067+
TFN1
70687068
Mode: 644
70697069
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7070-
Path: fixtures/sys/class/thermal/cooling_device1/type
7070+
Directory: fixtures/sys/class/thermal/cooling_device2
7071+
Mode: 750
7072+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7073+
Path: fixtures/sys/class/thermal/cooling_device2/cur_state
7074+
Lines: 1
7075+
-1
7076+
Mode: 640
7077+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7078+
Path: fixtures/sys/class/thermal/cooling_device2/max_state
7079+
Lines: 1
7080+
27
7081+
Mode: 640
7082+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7083+
Path: fixtures/sys/class/thermal/cooling_device2/type
70717084
Lines: 1
70727085
intel_powerclamp
7073-
Mode: 644
7086+
Mode: 640
70747087
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
70757088
Directory: fixtures/sys/class/thermal/thermal_zone0
70767089
Mode: 755

0 commit comments

Comments
 (0)