Merge pull request #93 from lahwaacz/master

Fix runtime error: index out of range in mineVersion
This commit is contained in:
Ben Kochie 2022-11-06 12:20:27 +01:00 committed by GitHub
commit 61e28093f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 9 deletions

View file

@ -49,12 +49,11 @@ func (i SMARTctlManagerCollector) Describe(ch chan<- *prometheus.Desc) {
func (i SMARTctlManagerCollector) Collect(ch chan<- prometheus.Metric) { func (i SMARTctlManagerCollector) Collect(ch chan<- prometheus.Metric) {
info := NewSMARTctlInfo(ch) info := NewSMARTctlInfo(ch)
for _, device := range i.Devices { for _, device := range i.Devices {
if json, err := readData(i.logger, device); err == nil { json := readData(i.logger, device)
if json.Exists() {
info.SetJSON(json) info.SetJSON(json)
smart := NewSMARTctl(i.logger, json, ch) smart := NewSMARTctl(i.logger, json, ch)
smart.Collect() smart.Collect()
} else {
level.Error(i.logger).Log("msg", "Error collecting SMART data", "err", err.Error())
} }
} }
info.Collect() info.Collect()

View file

@ -89,9 +89,9 @@ func readSMARTctlDevices(logger log.Logger) gjson.Result {
} }
// Select json source and parse // Select json source and parse
func readData(logger log.Logger, device string) (gjson.Result, error) { func readData(logger log.Logger, device string) gjson.Result {
if *smartctlFakeData { if *smartctlFakeData {
return readFakeSMARTctl(logger, device), nil return readFakeSMARTctl(logger, device)
} }
cacheValue, cacheOk := jsonCache.Load(device) cacheValue, cacheOk := jsonCache.Load(device)
@ -103,11 +103,11 @@ func readData(logger log.Logger, device string) (gjson.Result, error) {
if !found { if !found {
level.Warn(logger).Log("msg", "device not found", "device", device) level.Warn(logger).Log("msg", "device not found", "device", device)
} }
return j.(JSONCache).JSON, nil return j.(JSONCache).JSON
} }
return gjson.Parse("{}"), fmt.Errorf("smartctl returned bad data for device %s", device) return gjson.Result{}
} }
return cacheValue.(JSONCache).JSON, nil return cacheValue.(JSONCache).JSON
} }
// Parse smartctl return code // Parse smartctl return code

View file

@ -45,8 +45,10 @@ func (smart *SMARTctlInfo) SetJSON(json gjson.Result) {
// Collect metrics // Collect metrics
func (smart *SMARTctlInfo) Collect() { func (smart *SMARTctlInfo) Collect() {
if smart.Ready {
smart.mineVersion() smart.mineVersion()
} }
}
func (smart *SMARTctlInfo) mineVersion() { func (smart *SMARTctlInfo) mineVersion() {
smartctlJSON := smart.json.Get("smartctl") smartctlJSON := smart.json.Get("smartctl")