Skip to content

Commit cee06ec

Browse files
authored
Merge pull request #86 from CrabappleProject/fit_windows
FIX: remove `os.stat` in order to fit in windows
2 parents 3a8968a + daf1182 commit cee06ec

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

Diff for: main.go

+25-8
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,34 @@ func main() {
9494
level.Info(logger).Log("msg", "Starting smartctl_exporter", "version", version.Info())
9595
level.Info(logger).Log("msg", "Build context", "build_context", version.BuildContext())
9696

97+
// Scan the host devices
98+
json := readSMARTctlDevices(logger)
99+
scanDevices := json.Get("devices").Array()
100+
scanDevicesSet := make(map[string]bool)
101+
var scanDeviceNames []string
102+
for _, d := range scanDevices {
103+
deviceName := d.Get("name").String()
104+
level.Debug(logger).Log("msg", "Found device", "name", deviceName)
105+
scanDevicesSet[deviceName] = true
106+
scanDeviceNames = append(scanDeviceNames, deviceName)
107+
}
108+
109+
// Read the configuration and verify that it is available
97110
devices := *smartctlDevices
111+
var readDeviceNames []string
112+
for _, device := range devices {
113+
if _, ok := scanDevicesSet[device]; ok {
114+
readDeviceNames = append(readDeviceNames, device)
115+
} else {
116+
level.Warn(logger).Log("msg", "Device unavailable", "name", device)
117+
}
118+
}
98119

99-
if len(devices) == 0 {
120+
if len(readDeviceNames) > 0 {
121+
devices = readDeviceNames
122+
} else {
100123
level.Info(logger).Log("msg", "No devices specified, trying to load them automatically")
101-
json := readSMARTctlDevices(logger)
102-
scannedDevices := json.Get("devices").Array()
103-
for _, d := range scannedDevices {
104-
device := d.Get("name").String()
105-
level.Info(logger).Log("msg", "Found device", "device", device)
106-
devices = append(devices, device)
107-
}
124+
devices = scanDeviceNames
108125
}
109126

110127
if len(devices) == 0 {

Diff for: readjson.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package main
1616
import (
1717
"fmt"
1818
"io/ioutil"
19-
"os"
2019
"os/exec"
2120
"strings"
2221
"time"
@@ -94,19 +93,16 @@ func readData(logger log.Logger, device string) (gjson.Result, error) {
9493
return readFakeSMARTctl(logger, device), nil
9594
}
9695

97-
if _, err := os.Stat(device); err == nil {
98-
cacheValue, cacheOk := jsonCache[device]
99-
if !cacheOk || time.Now().After(cacheValue.LastCollect.Add(*smartctlInterval)) {
100-
json, ok := readSMARTctl(logger, device)
101-
if ok {
102-
jsonCache[device] = JSONCache{JSON: json, LastCollect: time.Now()}
103-
return jsonCache[device].JSON, nil
104-
}
105-
return gjson.Parse("{}"), fmt.Errorf("smartctl returned bad data for device %s", device)
96+
cacheValue, cacheOk := jsonCache[device]
97+
if !cacheOk || time.Now().After(cacheValue.LastCollect.Add(*smartctlInterval)) {
98+
json, ok := readSMARTctl(logger, device)
99+
if ok {
100+
jsonCache[device] = JSONCache{JSON: json, LastCollect: time.Now()}
101+
return jsonCache[device].JSON, nil
106102
}
107-
return cacheValue.JSON, nil
103+
return gjson.Parse("{}"), fmt.Errorf("smartctl returned bad data for device %s", device)
108104
}
109-
return gjson.Parse("{}"), fmt.Errorf("Device %s unavialable", device)
105+
return cacheValue.JSON, nil
110106
}
111107

112108
// Parse smartctl return code

0 commit comments

Comments
 (0)