Add device type support

Allow passing a custom `-d` / `--device=` flag to smartctl. The default
is the same (`auto`) as upstream smartctl.

Fixes: https://github.com/prometheus-community/smartctl_exporter/issues/26

Signed-off-by: SuperQ <superq@gmail.com>
This commit is contained in:
SuperQ 2023-01-10 20:23:47 +01:00
parent d5aa2289fe
commit b173b886cc
No known key found for this signature in database
GPG key ID: C646B23C9E3245F1
2 changed files with 8 additions and 4 deletions

View file

@ -66,6 +66,9 @@ var (
smartctlInterval = kingpin.Flag("smartctl.interval", smartctlInterval = kingpin.Flag("smartctl.interval",
"The interval between smarctl polls", "The interval between smarctl polls",
).Default("60s").Duration() ).Default("60s").Duration()
smartctlDeviceType = kingpin.Flag("smartctl.device-type",
"The device type (-d / --device)",
).Default("auto").String()
smartctlDevices = kingpin.Flag("smartctl.device", smartctlDevices = kingpin.Flag("smartctl.device",
"The device to monitor (repeatable)", "The device to monitor (repeatable)",
).Strings() ).Strings()

View file

@ -62,9 +62,10 @@ func readFakeSMARTctl(logger log.Logger, device string) gjson.Result {
} }
// Get json from smartctl and parse it // Get json from smartctl and parse it
func readSMARTctl(logger log.Logger, device string) (gjson.Result, bool) { func readSMARTctl(logger log.Logger, deviceType string, device string) (gjson.Result, bool) {
level.Debug(logger).Log("msg", "Collecting S.M.A.R.T. counters", "device", device) level.Debug(logger).Log("msg", "Collecting S.M.A.R.T. counters", "device_type", deviceType, "device", device)
out, err := exec.Command(*smartctlPath, "--json", "--info", "--health", "--attributes", "--tolerance=verypermissive", "--nocheck=standby", "--format=brief", device).Output() deviceTypeFlag := fmt.Sprintf("--device=%s", deviceType)
out, err := exec.Command(*smartctlPath, "--json", "--info", "--health", "--attributes", "--tolerance=verypermissive", "--nocheck=standby", "--format=brief", deviceTypeFlag, device).Output()
if err != nil { if err != nil {
level.Warn(logger).Log("msg", "S.M.A.R.T. output reading", "err", err) level.Warn(logger).Log("msg", "S.M.A.R.T. output reading", "err", err)
} }
@ -96,7 +97,7 @@ func readData(logger log.Logger, device string) gjson.Result {
cacheValue, cacheOk := jsonCache.Load(device) cacheValue, cacheOk := jsonCache.Load(device)
if !cacheOk || time.Now().After(cacheValue.(JSONCache).LastCollect.Add(*smartctlInterval)) { if !cacheOk || time.Now().After(cacheValue.(JSONCache).LastCollect.Add(*smartctlInterval)) {
json, ok := readSMARTctl(logger, device) json, ok := readSMARTctl(logger, *smartctlDeviceType, device)
if ok { if ok {
jsonCache.Store(device, JSONCache{JSON: json, LastCollect: time.Now()}) jsonCache.Store(device, JSONCache{JSON: json, LastCollect: time.Now()})
j, found := jsonCache.Load(device) j, found := jsonCache.Load(device)