| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package nvme |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/ndexec" |
| 12 | ) |
| 13 | |
| 14 | func (c *Collector) initNVMeCLIExec() (nvmeCli, error) { |
| 15 | if runtime.GOOS == "windows" { |
| 16 | return c.initDirectNvmeCliExec() |
| 17 | } |
| 18 | return c.initNdsudoNvmeCliExec() |
| 19 | } |
| 20 | |
| 21 | func (c *Collector) initNdsudoNvmeCliExec() (nvmeCli, error) { |
| 22 | nvmeExec := &ndsudoNvmeCliExec{timeout: c.Timeout.Duration()} |
| 23 | return nvmeExec, nil |
| 24 | } |
| 25 | |
| 26 | func (c *Collector) initDirectNvmeCliExec() (nvmeCli, error) { |
| 27 | path, err := ndexec.FindBinary( |
| 28 | []string{"nvme", "nvme-cli"}, |
| 29 | []string{ |
| 30 | filepath.Join(os.Getenv("ProgramFiles"), "nvme-cli", "nvme.exe"), |
| 31 | filepath.Join(os.Getenv("ProgramFiles(x86)"), "nvme-cli", "nvme.exe"), |
| 32 | }, |
| 33 | ) |
| 34 | if err != nil { |
| 35 | return nil, fmt.Errorf("nvme: %w", err) |
| 36 | } |
| 37 | |
| 38 | c.Debugf("found nvme at: %s", path) |
| 39 | |
| 40 | return &directNvmeCliExec{ |
| 41 | Logger: c.Logger, |
| 42 | nvmePath: path, |
| 43 | timeout: c.Timeout.Duration(), |
| 44 | }, nil |
| 45 | } |