Automatically load the available devices

This commit is contained in:
Christian Pedersen 2020-10-06 13:05:00 +02:00
parent 315d1538aa
commit 593ca96a99
3 changed files with 25 additions and 5 deletions

11
main.go
View file

@ -36,6 +36,17 @@ func (i SMARTctlManagerCollector) Collect(ch chan<- prometheus.Metric) {
func init() { func init() {
options = loadOptions() 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() { func main() {

View file

@ -55,6 +55,15 @@ func readSMARTctl(device string) gjson.Result {
return parseJSON(string(out)) 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 // Select json source and parse
func readData(device string) gjson.Result { func readData(device string) gjson.Result {
if options.SMARTctl.FakeJSON { if options.SMARTctl.FakeJSON {

View file

@ -4,8 +4,8 @@ smartctl_exporter:
fake_json: no fake_json: no
smartctl_location: /usr/sbin/smartctl smartctl_location: /usr/sbin/smartctl
collect_not_more_than_period: 20s collect_not_more_than_period: 20s
devices: # devices:
- /dev/nvme0 # - /dev/nvme0
- /dev/nvme1 # - /dev/nvme1
- /dev/nvme2 # - /dev/nvme2
- /dev/nvme3 # - /dev/nvme3