From e88442081c18fcac9025e4d806786e12c791cc10 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Mon, 3 Oct 2022 14:49:32 +0200 Subject: [PATCH] Adjust smartctl command args (#74) Update smartctl command arguments to reduce device wakeup from standby/idle. * Ignore exit code of 2 for sleeping devices. Fixes: https://github.com/prometheus-community/smartctl_exporter/issues/61 Signed-off-by: SuperQ Signed-off-by: SuperQ --- readjson.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/readjson.go b/readjson.go index 9977899..89b4494 100644 --- a/readjson.go +++ b/readjson.go @@ -64,7 +64,7 @@ func readFakeSMARTctl(logger log.Logger, device string) gjson.Result { // Get json from smartctl and parse it func readSMARTctl(logger log.Logger, device string) (gjson.Result, bool) { level.Debug(logger).Log("msg", "Collecting S.M.A.R.T. counters", "device", device) - out, err := exec.Command(*smartctlPath, "--json", "--xall", device).Output() + out, err := exec.Command(*smartctlPath, "--json", "--info", "--health", "--attributes", "--tolerance=verypermissive", "--nocheck=standby", "--format=brief", device).Output() if err != nil { level.Warn(logger).Log("msg", "S.M.A.R.T. output reading", "err", err) } @@ -75,10 +75,15 @@ func readSMARTctl(logger log.Logger, device string) (gjson.Result, bool) { } func readSMARTctlDevices(logger log.Logger) gjson.Result { - level.Debug(logger).Log("msg", "Collecting devices") - out, err := exec.Command(*smartctlPath, "--json", "--scan-open").Output() - if err != nil { - level.Warn(logger).Log("msg", "S.M.A.R.T. output reading error", "err", err) + level.Debug(logger).Log("msg", "Scanning for devices") + out, err := exec.Command(*smartctlPath, "--json", "--scan").Output() + if exiterr, ok := err.(*exec.ExitError); ok { + level.Debug(logger).Log("msg", "Exit Status", "exit_code", exiterr.ExitCode()) + // The smartctl command returns 2 if devices are sleeping, ignore this error. + if exiterr.ExitCode() != 2 { + level.Warn(logger).Log("msg", "S.M.A.R.T. output reading error", "err", err) + return gjson.Result{} + } } return parseJSON(string(out)) }