github.com/netdata/go.d.plugin@v0.58.1/modules/nvidia_smi/exec.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package nvidia_smi 4 5 import ( 6 "context" 7 "errors" 8 "fmt" 9 "os/exec" 10 "strings" 11 "time" 12 13 "github.com/netdata/go.d.plugin/logger" 14 ) 15 16 func newNvidiaSMIExec(path string, cfg Config, log *logger.Logger) (*nvidiaSMIExec, error) { 17 return &nvidiaSMIExec{ 18 binPath: path, 19 timeout: cfg.Timeout.Duration, 20 Logger: log, 21 }, nil 22 } 23 24 type nvidiaSMIExec struct { 25 binPath string 26 timeout time.Duration 27 *logger.Logger 28 } 29 30 func (e *nvidiaSMIExec) queryGPUInfoXML() ([]byte, error) { 31 ctx, cancel := context.WithTimeout(context.Background(), e.timeout) 32 defer cancel() 33 34 cmd := exec.CommandContext(ctx, e.binPath, "-q", "-x") 35 36 e.Debugf("executing '%s'", cmd) 37 bs, err := cmd.Output() 38 if err != nil { 39 return nil, fmt.Errorf("error on '%s': %v", cmd, err) 40 } 41 42 return bs, nil 43 } 44 45 func (e *nvidiaSMIExec) queryGPUInfoCSV(properties []string) ([]byte, error) { 46 if len(properties) == 0 { 47 return nil, errors.New("can not query CSV GPU Info without properties") 48 } 49 50 ctx, cancel := context.WithTimeout(context.Background(), e.timeout) 51 defer cancel() 52 53 cmd := exec.CommandContext(ctx, e.binPath, "--query-gpu="+strings.Join(properties, ","), "--format=csv,nounits") 54 55 e.Debugf("executing '%s'", cmd) 56 57 bs, err := cmd.Output() 58 if err != nil { 59 return nil, fmt.Errorf("error on '%s': %v", cmd, err) 60 } 61 62 return bs, nil 63 } 64 65 func (e *nvidiaSMIExec) queryHelpQueryGPU() ([]byte, error) { 66 ctx, cancel := context.WithTimeout(context.Background(), e.timeout) 67 defer cancel() 68 69 cmd := exec.CommandContext(ctx, e.binPath, "--help-query-gpu") 70 71 e.Debugf("executing '%s'", cmd) 72 bs, err := cmd.Output() 73 if err != nil { 74 return nil, fmt.Errorf("error on '%s': %v", cmd, err) 75 } 76 77 return bs, err 78 }