mirror of
https://github.com/prometheus-community/smartctl_exporter.git
synced 2024-11-23 01:43:07 +01:00
Return the cached value if it's not time to scan again yet (#18)
* Return the cached value if it's not time to scan again yet This should ensure that if we have a valid value cached (which ought to be every time after the first scan), we return it as metrics. This fixes the crashes that would happen if queries happened earlier than the re-scan interval allowed. * Address review feedback: Shorten the time-to-scan logic We can express this in a single if statement, so it takes fewer lines to do the "should we check again" check.
This commit is contained in:
parent
1625848c5d
commit
d33d18e535
1 changed files with 2 additions and 9 deletions
11
readjson.go
11
readjson.go
|
@ -76,14 +76,7 @@ func readData(device string) (gjson.Result, error) {
|
|||
|
||||
if _, err := os.Stat(device); err == nil {
|
||||
cacheValue, cacheOk := jsonCache[device]
|
||||
timeToScan := false
|
||||
if cacheOk {
|
||||
timeToScan = time.Now().After(cacheValue.LastCollect.Add(options.SMARTctl.CollectPeriodDuration))
|
||||
} else {
|
||||
timeToScan = true
|
||||
}
|
||||
|
||||
if timeToScan {
|
||||
if !cacheOk || time.Now().After(cacheValue.LastCollect.Add(options.SMARTctl.CollectPeriodDuration)) {
|
||||
json, ok := readSMARTctl(device)
|
||||
if ok {
|
||||
jsonCache[device] = JSONCache{JSON: json, LastCollect: time.Now()}
|
||||
|
@ -91,7 +84,7 @@ func readData(device string) (gjson.Result, error) {
|
|||
}
|
||||
return gjson.Parse("{}"), fmt.Errorf("smartctl returned bad data for device %s", device)
|
||||
}
|
||||
return gjson.Parse("{}"), fmt.Errorf("Too early collect called for device %s", device)
|
||||
return cacheValue.JSON, nil
|
||||
}
|
||||
return gjson.Parse("{}"), fmt.Errorf("Device %s unavialable", device)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue