go.d/smartctl: fix exit status check in scan (#18635)
Ilya Mashchenko committed
Sep 29, 2024 at 00:03 UTC
c00f2e0758f53f8bc63992e5a20c6043e61fdcc5
2 files changed
+15
-8
src/go/plugin/go.d/modules/smartctl/collect.go
+12
-5
@@ -181,7 +181,7 @@ func isSmartAttrValid(a *smartAttribute) bool {
181
}
182
183
func isDeviceInLowerPowerMode(r *gjson.Result) bool {
184
- if !isExitStatusHasBit(r, 1) {
184
+ if !isExitStatusHasAnyBit(r, 1) {
185
return false
186
}
187
@@ -194,7 +194,7 @@ func isDeviceInLowerPowerMode(r *gjson.Result) bool {
194
}
195
196
func isDeviceOpenFailedNoSuchDevice(r *gjson.Result) bool {
197
- if !isExitStatusHasBit(r, 1) {
197
+ if !isExitStatusHasAnyBit(r, 1) {
198
return false
199
}
200
@@ -206,9 +206,16 @@ func isDeviceOpenFailedNoSuchDevice(r *gjson.Result) bool {
206
})
207
}
208
209
-func isExitStatusHasBit(r *gjson.Result, bit int) bool {
209
+func isExitStatusHasAnyBit(r *gjson.Result, bit int, bits ...int) bool {
210
// https://manpages.debian.org/bullseye/smartmontools/smartctl.8.en.html#EXIT_STATUS
211
status := int(r.Get("smartctl.exit_status").Int())
212
- mask := 1 << bit
213
- return (status & mask) != 0
212
+
213
+ for _, b := range append([]int{bit}, bits...) {
214
+ mask := 1 << b
215
+ if (status & mask) != 0 {
216
+ return true
217
+ }
218
+ }
219
+
220
+ return false
221
}
src/go/plugin/go.d/modules/smartctl/scan.go
+3
-3
@@ -100,12 +100,12 @@ func (s *Smartctl) handleGuessedScsiScannedDevice(dev *scanDevice) {
100
}
101
102
resp, _ := s.exec.deviceInfo(dev.name, "sat", s.NoCheckPowerMode)
103
- if resp == nil || resp.Get("smartctl.exit_status").Int() != 0 {
103
+ if resp == nil || isExitStatusHasAnyBit(resp, 0, 1, 2) {
104
return
105
}
106
107
- atts, ok := newSmartDevice(resp).ataSmartAttributeTable()
108
- if !ok || len(atts) == 0 {
107
+ attrs, ok := newSmartDevice(resp).ataSmartAttributeTable()
108
+ if !ok || len(attrs) == 0 {
109
return
110
}
111