diff options
Diffstat (limited to 'src/go/collectors/go.d.plugin/modules/nvme/init.go')
-rw-r--r-- | src/go/collectors/go.d.plugin/modules/nvme/init.go | 66 |
1 files changed, 9 insertions, 57 deletions
diff --git a/src/go/collectors/go.d.plugin/modules/nvme/init.go b/src/go/collectors/go.d.plugin/modules/nvme/init.go index 44ff90f4e2..51f1400a01 100644 --- a/src/go/collectors/go.d.plugin/modules/nvme/init.go +++ b/src/go/collectors/go.d.plugin/modules/nvme/init.go @@ -3,72 +3,24 @@ package nvme import ( - "context" - "errors" "fmt" "os" - "os/exec" "path/filepath" -) - -func (n *NVMe) validateConfig() error { - if n.BinaryPath == "" { - return errors.New("'binary_path' can not be empty") - } - return nil -} + "github.com/netdata/netdata/go/go.d.plugin/agent/executable" +) func (n *NVMe) initNVMeCLIExec() (nvmeCLI, error) { - if exePath, err := os.Executable(); err == nil { - ndsudoPath := filepath.Join(filepath.Dir(exePath), "ndsudo") - - if fi, err := os.Stat(ndsudoPath); err == nil { - // executable by owner or group - if fi.Mode().Perm()&0110 != 0 { - n.Debug("using ndsudo") - return &nvmeCLIExec{ - ndsudoPath: ndsudoPath, - timeout: n.Timeout.Duration(), - }, nil - } - } - } - - // TODO: remove after next minor release of Netdata (latest is v1.44.0) - // can't remove now because it will break "from source + stable channel" installations - nvmePath, err := exec.LookPath(n.BinaryPath) - if err != nil { - return nil, err - } + ndsudoPath := filepath.Join(executable.Directory, "ndsudo") - var sudoPath string - if os.Getuid() != 0 { - sudoPath, err = exec.LookPath("sudo") - if err != nil { - return nil, err - } + if _, err := os.Stat(ndsudoPath); err != nil { + return nil, fmt.Errorf("ndsudo executable not found: %v", err) } - if sudoPath != "" { - ctx1, cancel1 := context.WithTimeout(context.Background(), n.Timeout.Duration()) - defer cancel1() - - if _, err := exec.CommandContext(ctx1, sudoPath, "-n", "-v").Output(); err != nil { - return nil, fmt.Errorf("can not run sudo on this host: %v", err) - } - - ctx2, cancel2 := context.WithTimeout(context.Background(), n.Timeout.Duration()) - defer cancel2() - - if _, err := exec.CommandContext(ctx2, sudoPath, "-n", "-l", nvmePath).Output(); err != nil { - return nil, fmt.Errorf("can not run '%s' with sudo: %v", n.BinaryPath, err) - } + nvmeExec := &nvmeCLIExec{ + ndsudoPath: ndsudoPath, + timeout: n.Timeout.Duration(), } - return &nvmeCLIExec{ - sudoPath: sudoPath, - nvmePath: nvmePath, - timeout: n.Timeout.Duration(), - }, nil + return nvmeExec, nil } |