| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package nvidia_smi |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "runtime" |
| 11 | |
| 12 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/ndexec" |
| 13 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/pathvalidate" |
| 14 | ) |
| 15 | |
| 16 | func (c *Collector) initNvidiaSmiExec() (nvidiaSmiBinary, error) { |
| 17 | binPath := c.BinaryPath |
| 18 | if binPath == "" || !fileExists(binPath) { |
| 19 | path, err := exec.LookPath(c.binName) |
| 20 | if err != nil { |
| 21 | if runtime.GOOS == "windows" { |
| 22 | path, err = ndexec.FindBinary( |
| 23 | nil, |
| 24 | []string{ |
| 25 | filepath.Join(os.Getenv("ProgramFiles"), "NVIDIA Corporation", "NVSMI", "nvidia-smi.exe"), |
| 26 | filepath.Join(os.Getenv("SystemRoot"), "System32", "nvidia-smi.exe"), |
| 27 | }, |
| 28 | ) |
| 29 | } |
| 30 | if err != nil { |
| 31 | return nil, fmt.Errorf("error on lookup '%s': %v", c.binName, err) |
| 32 | } |
| 33 | } |
| 34 | binPath = path |
| 35 | } |
| 36 | |
| 37 | validatedPath, err := pathvalidate.ValidateBinaryPath(binPath) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | |
| 42 | return newNvidiaSmiBinary(validatedPath, c.Config, c.Logger) |
| 43 | } |
| 44 | |
| 45 | func fileExists(path string) bool { |
| 46 | _, err := os.Stat(path) |
| 47 | return !os.IsNotExist(err) |
| 48 | } |