Skip to content

Commit 430c6dd

Browse files
authored
Merge pull request #932 from mackerelio/check-disk-error-handling
Fix error handling in check-disk to avoid unnecessary failures when using --path option
2 parents f5a7ee6 + c16a3d8 commit 430c6dd

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

check-disk/lib/check-disk.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,12 @@ func run(args []string) *checkers.Checker {
116116
}
117117

118118
partitions, err := listPartitions()
119-
if err != nil {
120-
return checkers.Unknown(fmt.Sprintf("Failed to fetch partitions: %s", err))
121-
}
122119

123-
if !opts.All {
120+
if opts.All {
121+
if err != nil {
122+
return checkers.Unknown(fmt.Sprintf("Failed to fetch partitions: %s", err))
123+
}
124+
} else {
124125
// Filtering partitions by Fstype
125126
if opts.IncludeType != nil {
126127
partitions = filterPartitionsByInclusion(partitions, *opts.IncludeType, fstypeOfPartition)
@@ -140,9 +141,13 @@ func run(args []string) *checkers.Checker {
140141
}
141142

142143
partitions = filterPartitionsByInclusion(partitions, *opts.Path, mountpointOfPartition)
143-
if len(partitions) == 0 {
144+
if len(partitions) < len(*opts.Path) {
144145
return checkers.Unknown(fmt.Sprintf("Failed to fetch partitions: %s", errors.New("No device found for the specified *Mountpoint*")))
145146
}
147+
} else {
148+
if err != nil {
149+
return checkers.Unknown(fmt.Sprintf("Failed to fetch partitions: %s", err))
150+
}
146151
}
147152

148153
if opts.Path == nil && opts.Exclude != nil {
@@ -253,9 +258,6 @@ func run(args []string) *checkers.Checker {
253258
// https://github.com/coreutils/gnulib/blob/df336dc/lib/mountlist.c#L164
254259
func listPartitions() ([]gpud.PartitionStat, error) {
255260
allPartitions, err := gpud.Partitions(true)
256-
if err != nil {
257-
return nil, err
258-
}
259261
partitions := make([]gpud.PartitionStat, 0, len(allPartitions))
260262
for _, p := range allPartitions {
261263
switch p.Fstype {
@@ -283,7 +285,7 @@ func listPartitions() ([]gpud.PartitionStat, error) {
283285
}
284286
}
285287

286-
return partitions, nil
288+
return partitions, err
287289
}
288290

289291
func isBindMount(mountOpts []string) bool {

0 commit comments

Comments
 (0)