From 162b8cb6d40ecc9e66932a27ec202367b4cdc2a6 Mon Sep 17 00:00:00 2001 From: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> Date: Fri, 6 Mar 2026 13:39:25 +0100 Subject: [PATCH] Add config option for manual device configuration --- collectors/smartmonMetric.go | 32 ++++++++++++++++++++++++++++---- collectors/smartmonMetric.md | 6 +++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/collectors/smartmonMetric.go b/collectors/smartmonMetric.go index 95d74ff..25c650d 100644 --- a/collectors/smartmonMetric.go +++ b/collectors/smartmonMetric.go @@ -14,6 +14,10 @@ import ( type SmartMonCollectorConfig struct { UseSudo bool `json:"use_sudo,omitempty"` ExcludeDevices []string `json:"exclude_devices,omitempty"` + Devices []struct { + Name string `json:"name"` + Type string `json:"type"` + } `json:"devices,omitempty"` } type deviceT struct { @@ -33,10 +37,26 @@ type SmartMonCollector struct { } func (m *SmartMonCollector) getSmartmonDevices() error { - var scan struct { - Devices []deviceT `json:"devices"` + // Use configured devices + if len(m.config.Devices) > 0 { + for _, configDevice := range m.config.Devices { + if !slices.Contains(m.config.ExcludeDevices, configDevice.Name) { + d := deviceT{ + Name: configDevice.Name, + Type: configDevice.Type, + } + if m.config.UseSudo { + d.queryCommand = append(d.queryCommand, m.sudoCmd) + } + d.queryCommand = append(d.queryCommand, m.smartCtlCmd, "--json=c", "--device="+d.Type, "--all", d.Name) + + m.devices = append(m.devices, d) + } + } + return nil } + // Use scan command var scanCmd []string if m.config.UseSudo { scanCmd = append(scanCmd, m.sudoCmd) @@ -50,14 +70,18 @@ func (m *SmartMonCollector) getSmartmonDevices() error { "%s getSmartmonDevices(): Failed to execute device scan command %s: %w", m.name, command.String(), err) } - err = json.Unmarshal(stdout, &scan) + + var scanOutput struct { + Devices []deviceT `json:"devices"` + } + err = json.Unmarshal(stdout, &scanOutput) if err != nil { return fmt.Errorf("%s getSmartmonDevices(): Failed to parse JSON output from device scan command: %w", m.name, err) } m.devices = make([]deviceT, 0) - for _, d := range scan.Devices { + for _, d := range scanOutput.Devices { if !slices.Contains(m.config.ExcludeDevices, d.Name) { if m.config.UseSudo { d.queryCommand = append(d.queryCommand, m.sudoCmd) diff --git a/collectors/smartmonMetric.md b/collectors/smartmonMetric.md index a69aacd..5599eb3 100644 --- a/collectors/smartmonMetric.md +++ b/collectors/smartmonMetric.md @@ -4,7 +4,11 @@ "smartmon": { "use_sudo" : true, "exclude_devices": [ - "/dev/sda", + "/dev/sda" + ], + "devices": [ + "name": "/dev/nvme0" + "type": "nvme" ] } ```