Skip to content

Commit 61e2809

Browse files
authored
Merge pull request #93 from lahwaacz/master
Fix runtime error: index out of range in mineVersion
2 parents 97947b0 + a35f67b commit 61e2809

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

Diff for: main.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,11 @@ func (i SMARTctlManagerCollector) Describe(ch chan<- *prometheus.Desc) {
4949
func (i SMARTctlManagerCollector) Collect(ch chan<- prometheus.Metric) {
5050
info := NewSMARTctlInfo(ch)
5151
for _, device := range i.Devices {
52-
if json, err := readData(i.logger, device); err == nil {
52+
json := readData(i.logger, device)
53+
if json.Exists() {
5354
info.SetJSON(json)
5455
smart := NewSMARTctl(i.logger, json, ch)
5556
smart.Collect()
56-
} else {
57-
level.Error(i.logger).Log("msg", "Error collecting SMART data", "err", err.Error())
5857
}
5958
}
6059
info.Collect()

Diff for: readjson.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ func readSMARTctlDevices(logger log.Logger) gjson.Result {
8989
}
9090

9191
// Select json source and parse
92-
func readData(logger log.Logger, device string) (gjson.Result, error) {
92+
func readData(logger log.Logger, device string) gjson.Result {
9393
if *smartctlFakeData {
94-
return readFakeSMARTctl(logger, device), nil
94+
return readFakeSMARTctl(logger, device)
9595
}
9696

9797
cacheValue, cacheOk := jsonCache.Load(device)
@@ -103,11 +103,11 @@ func readData(logger log.Logger, device string) (gjson.Result, error) {
103103
if !found {
104104
level.Warn(logger).Log("msg", "device not found", "device", device)
105105
}
106-
return j.(JSONCache).JSON, nil
106+
return j.(JSONCache).JSON
107107
}
108-
return gjson.Parse("{}"), fmt.Errorf("smartctl returned bad data for device %s", device)
108+
return gjson.Result{}
109109
}
110-
return cacheValue.(JSONCache).JSON, nil
110+
return cacheValue.(JSONCache).JSON
111111
}
112112

113113
// Parse smartctl return code

Diff for: smartctlinfo.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ func (smart *SMARTctlInfo) SetJSON(json gjson.Result) {
4545

4646
// Collect metrics
4747
func (smart *SMARTctlInfo) Collect() {
48-
smart.mineVersion()
48+
if smart.Ready {
49+
smart.mineVersion()
50+
}
4951
}
5052

5153
func (smart *SMARTctlInfo) mineVersion() {

0 commit comments

Comments
 (0)