mirror of
https://github.com/prometheus-community/smartctl_exporter.git
synced 2024-11-16 01:33:07 +01:00
FIX: remove os.stat
in order to fit in windows
Signed-off-by: 赵栩彬 <zhaoxubin2016@mail.com>
This commit is contained in:
parent
d3d5180048
commit
daf1182285
2 changed files with 34 additions and 21 deletions
35
main.go
35
main.go
|
@ -94,19 +94,36 @@ func main() {
|
||||||
level.Info(logger).Log("msg", "Starting systemd_exporter", "version", version.Info())
|
level.Info(logger).Log("msg", "Starting systemd_exporter", "version", version.Info())
|
||||||
level.Info(logger).Log("msg", "Build context", "build_context", version.BuildContext())
|
level.Info(logger).Log("msg", "Build context", "build_context", version.BuildContext())
|
||||||
|
|
||||||
devices := *smartctlDevices
|
// Scan the host devices
|
||||||
|
json := readSMARTctlDevices(logger)
|
||||||
|
scanDevices := json.Get("devices").Array()
|
||||||
|
scanDevicesSet := make(map[string]bool)
|
||||||
|
var scanDeviceNames []string
|
||||||
|
for _, d := range scanDevices {
|
||||||
|
deviceName := d.Get("name").String()
|
||||||
|
level.Debug(logger).Log("msg", "Found device", "name", deviceName)
|
||||||
|
scanDevicesSet[deviceName] = true
|
||||||
|
scanDeviceNames = append(scanDeviceNames, deviceName)
|
||||||
|
}
|
||||||
|
|
||||||
if len(devices) == 0 {
|
// Read the configuration and verify that it is available
|
||||||
level.Info(logger).Log("msg", "No devices specified, trying to load them automatically")
|
devices := *smartctlDevices
|
||||||
json := readSMARTctlDevices(logger)
|
var readDeviceNames []string
|
||||||
scannedDevices := json.Get("devices").Array()
|
for _, device := range devices {
|
||||||
for _, d := range scannedDevices {
|
if _, ok := scanDevicesSet[device]; ok {
|
||||||
device := d.Get("name").String()
|
readDeviceNames = append(readDeviceNames, device)
|
||||||
level.Info(logger).Log("msg", "Found device", "device", device)
|
} else {
|
||||||
devices = append(devices, device)
|
level.Warn(logger).Log("msg", "Device unavailable", "name", device)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(readDeviceNames) > 0 {
|
||||||
|
devices = readDeviceNames
|
||||||
|
} else {
|
||||||
|
level.Info(logger).Log("msg", "No devices specified, trying to load them automatically")
|
||||||
|
devices = scanDeviceNames
|
||||||
|
}
|
||||||
|
|
||||||
if len(devices) == 0 {
|
if len(devices) == 0 {
|
||||||
level.Error(logger).Log("msg", "No devices found")
|
level.Error(logger).Log("msg", "No devices found")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
20
readjson.go
20
readjson.go
|
@ -16,7 +16,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -94,19 +93,16 @@ func readData(logger log.Logger, device string) (gjson.Result, error) {
|
||||||
return readFakeSMARTctl(logger, device), nil
|
return readFakeSMARTctl(logger, device), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(device); err == nil {
|
cacheValue, cacheOk := jsonCache[device]
|
||||||
cacheValue, cacheOk := jsonCache[device]
|
if !cacheOk || time.Now().After(cacheValue.LastCollect.Add(*smartctlInterval)) {
|
||||||
if !cacheOk || time.Now().After(cacheValue.LastCollect.Add(*smartctlInterval)) {
|
json, ok := readSMARTctl(logger, device)
|
||||||
json, ok := readSMARTctl(logger, device)
|
if ok {
|
||||||
if ok {
|
jsonCache[device] = JSONCache{JSON: json, LastCollect: time.Now()}
|
||||||
jsonCache[device] = JSONCache{JSON: json, LastCollect: time.Now()}
|
return jsonCache[device].JSON, nil
|
||||||
return jsonCache[device].JSON, nil
|
|
||||||
}
|
|
||||||
return gjson.Parse("{}"), fmt.Errorf("smartctl returned bad data for device %s", device)
|
|
||||||
}
|
}
|
||||||
return cacheValue.JSON, nil
|
return gjson.Parse("{}"), fmt.Errorf("smartctl returned bad data for device %s", device)
|
||||||
}
|
}
|
||||||
return gjson.Parse("{}"), fmt.Errorf("Device %s unavialable", device)
|
return cacheValue.JSON, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse smartctl return code
|
// Parse smartctl return code
|
||||||
|
|
Loading…
Reference in a new issue