Skip to content

Commit c27ccb5

Browse files
committed
Update error handling
1 parent aa95197 commit c27ccb5

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

blockdevice/stats.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,9 @@ func (fs FS) SysBlockDeviceIOStat(device string) (IODeviceStats, error) {
497497
val, err = util.ReadHexFromFile(fs.sys.Path(sysBlockPath, device, sysDevicePath, file))
498498
if err != nil {
499499
return IODeviceStats{}, err
500+
} else {
501+
*p = val
500502
}
501-
*p = val
502503
}
503504
return ioDeviceStats, nil
504505
}

ext4/ext4.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package ext4
1616

1717
import (
18+
"fmt"
1819
"strings"
1920
"path/filepath"
2021

@@ -23,8 +24,8 @@ import (
2324
)
2425

2526
const (
26-
sysPath = "sys"
2727
sysFSPath = "fs"
28+
sysFSExt4Path = "ext4"
2829
)
2930

3031
// Stats contains statistics for a single Btrfs filesystem.
@@ -79,24 +80,26 @@ func (fs FS) ProcStat() ([]*Stats, error) {
7980

8081
stats := make([]*Stats, 0, len(matches))
8182
for _, m := range matches {
82-
s := &Stats{}
83-
for file, p := range map[string]*uint64{
84-
"errors_count": &s.Errors,
85-
"warning_count": &s.Warnings,
86-
"msg_count": &s.Messages,
87-
} {
88-
var val uint64
89-
val, err = util.ReadUintFromFile(fs.sys.Path(m, file))
90-
if err != nil {
91-
return nil, err
92-
}
83+
s := &Stats{}
84+
85+
// "*" used in glob above indicates the name of the filesystem.
86+
name := filepath.Base(m)
87+
s.Name = name
88+
for file, p := range map[string]*uint64{
89+
"errors_count": &s.Errors,
90+
"warning_count": &s.Warnings,
91+
"msg_count": &s.Messages,
92+
} {
93+
var val uint64
94+
val, err = util.ReadUintFromFile(fs.sys.Path(sysFSPath, sysFSExt4Path, name, file))
95+
if err != nil {
96+
fmt.Errorf("failed to read ext4 stats from %s: %w", file, err)
97+
} else {
9398
*p = val
94-
}
99+
}
100+
}
95101

96-
// "*" used in glob above indicates the name of the filesystem.
97-
name := filepath.Base(filepath.Dir(filepath.Dir(m)))
98-
s.Name = name
99-
stats = append(stats, s)
102+
stats = append(stats, s)
100103
}
101104

102105
return stats, nil

0 commit comments

Comments
 (0)