Add config option for manual device configuration

This commit is contained in:
Holger Obermaier
2026-03-06 13:39:25 +01:00
parent 510e916c0f
commit 162b8cb6d4
2 changed files with 33 additions and 5 deletions

View File

@@ -14,6 +14,10 @@ import (
type SmartMonCollectorConfig struct { type SmartMonCollectorConfig struct {
UseSudo bool `json:"use_sudo,omitempty"` UseSudo bool `json:"use_sudo,omitempty"`
ExcludeDevices []string `json:"exclude_devices,omitempty"` ExcludeDevices []string `json:"exclude_devices,omitempty"`
Devices []struct {
Name string `json:"name"`
Type string `json:"type"`
} `json:"devices,omitempty"`
} }
type deviceT struct { type deviceT struct {
@@ -33,10 +37,26 @@ type SmartMonCollector struct {
} }
func (m *SmartMonCollector) getSmartmonDevices() error { func (m *SmartMonCollector) getSmartmonDevices() error {
var scan struct { // Use configured devices
Devices []deviceT `json:"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 var scanCmd []string
if m.config.UseSudo { if m.config.UseSudo {
scanCmd = append(scanCmd, m.sudoCmd) scanCmd = append(scanCmd, m.sudoCmd)
@@ -50,14 +70,18 @@ func (m *SmartMonCollector) getSmartmonDevices() error {
"%s getSmartmonDevices(): Failed to execute device scan command %s: %w", "%s getSmartmonDevices(): Failed to execute device scan command %s: %w",
m.name, command.String(), err) 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 { if err != nil {
return fmt.Errorf("%s getSmartmonDevices(): Failed to parse JSON output from device scan command: %w", return fmt.Errorf("%s getSmartmonDevices(): Failed to parse JSON output from device scan command: %w",
m.name, err) m.name, err)
} }
m.devices = make([]deviceT, 0) m.devices = make([]deviceT, 0)
for _, d := range scan.Devices { for _, d := range scanOutput.Devices {
if !slices.Contains(m.config.ExcludeDevices, d.Name) { if !slices.Contains(m.config.ExcludeDevices, d.Name) {
if m.config.UseSudo { if m.config.UseSudo {
d.queryCommand = append(d.queryCommand, m.sudoCmd) d.queryCommand = append(d.queryCommand, m.sudoCmd)

View File

@@ -4,7 +4,11 @@
"smartmon": { "smartmon": {
"use_sudo" : true, "use_sudo" : true,
"exclude_devices": [ "exclude_devices": [
"/dev/sda", "/dev/sda"
],
"devices": [
"name": "/dev/nvme0"
"type": "nvme"
] ]
} }
``` ```