From 593ca96a991aadaa0722bb8ed98455431650af37 Mon Sep 17 00:00:00 2001 From: Christian Pedersen Date: Tue, 6 Oct 2020 13:05:00 +0200 Subject: [PATCH] Automatically load the available devices --- main.go | 11 +++++++++++ readjson.go | 9 +++++++++ smartctl_exporter.yaml | 10 +++++----- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 4c769b4..9cea81c 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,17 @@ func (i SMARTctlManagerCollector) Collect(ch chan<- prometheus.Metric) { func init() { options = loadOptions() + + if len(options.SMARTctl.Devices) == 0 { + logger.Debug("No devices specified, trying to load them automatically") + json := readSMARTctlDevices() + devices := json.Get("devices").Array() + for _, d := range devices { + device := d.Get("name").String() + logger.Debug("Found device: %s", device) + options.SMARTctl.Devices = append(options.SMARTctl.Devices, device) + } + } } func main() { diff --git a/readjson.go b/readjson.go index 39141e1..50bfdc1 100644 --- a/readjson.go +++ b/readjson.go @@ -55,6 +55,15 @@ func readSMARTctl(device string) gjson.Result { return parseJSON(string(out)) } +func readSMARTctlDevices() gjson.Result { + logger.Debug("Collecting devices") + out, err := exec.Command(options.SMARTctl.SMARTctlLocation, "--json", "--scan-open").Output() + if err != nil { + logger.Warning("S.M.A.R.T. output reading error: %s", err) + } + return parseJSON(string(out)) +} + // Select json source and parse func readData(device string) gjson.Result { if options.SMARTctl.FakeJSON { diff --git a/smartctl_exporter.yaml b/smartctl_exporter.yaml index 02a236d..f0cbf2f 100755 --- a/smartctl_exporter.yaml +++ b/smartctl_exporter.yaml @@ -4,8 +4,8 @@ smartctl_exporter: fake_json: no smartctl_location: /usr/sbin/smartctl collect_not_more_than_period: 20s - devices: - - /dev/nvme0 - - /dev/nvme1 - - /dev/nvme2 - - /dev/nvme3 +# devices: +# - /dev/nvme0 +# - /dev/nvme1 +# - /dev/nvme2 +# - /dev/nvme3