fix: Set the default value correctly (#9980)

This commit is contained in:
Fan Zhang 2021-11-09 05:42:55 +08:00 committed by GitHub
parent 9871b676a5
commit 0133f1206b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 5 deletions

View File

@ -7,13 +7,19 @@ This plugin uses a query on the [`nvidia-smi`](https://developer.nvidia.com/nvid
```toml ```toml
# Pulls statistics from nvidia GPUs attached to the host # Pulls statistics from nvidia GPUs attached to the host
[[inputs.nvidia_smi]] [[inputs.nvidia_smi]]
## Optional: path to nvidia-smi binary, defaults to $PATH via exec.LookPath ## Optional: path to nvidia-smi binary, defaults "/usr/bin/nvidia-smi"
## We will first try to locate the nvidia-smi binary with the explicitly specified value (or default value),
## if it is not found, we will try to locate it on PATH(exec.LookPath), if it is still not found, an error will be returned
# bin_path = "/usr/bin/nvidia-smi" # bin_path = "/usr/bin/nvidia-smi"
## Optional: timeout for GPU polling ## Optional: timeout for GPU polling
# timeout = "5s" # timeout = "5s"
``` ```
#### Linux
On Linux, `nvidia-smi` is generally located at `/usr/bin/nvidia-smi`
#### Windows #### Windows
On Windows, `nvidia-smi` is generally located at `C:\Program Files\NVIDIA Corporation\NVSMI\nvidia-smi.exe` On Windows, `nvidia-smi` is generally located at `C:\Program Files\NVIDIA Corporation\NVSMI\nvidia-smi.exe`

View File

@ -31,7 +31,9 @@ func (smi *NvidiaSMI) Description() string {
// SampleConfig returns the sample configuration for the NvidiaSMI plugin // SampleConfig returns the sample configuration for the NvidiaSMI plugin
func (smi *NvidiaSMI) SampleConfig() string { func (smi *NvidiaSMI) SampleConfig() string {
return ` return `
## Optional: path to nvidia-smi binary, defaults to $PATH via exec.LookPath ## Optional: path to nvidia-smi binary, defaults "/usr/bin/nvidia-smi"
## We will first try to locate the nvidia-smi binary with the explicitly specified value (or default value),
## if it is not found, we will try to locate it on PATH(exec.LookPath), if it is still not found, an error will be returned
# bin_path = "/usr/bin/nvidia-smi" # bin_path = "/usr/bin/nvidia-smi"
## Optional: timeout for GPU polling ## Optional: timeout for GPU polling
@ -39,12 +41,21 @@ func (smi *NvidiaSMI) SampleConfig() string {
` `
} }
// Gather implements the telegraf interface func (smi *NvidiaSMI) Init() error {
func (smi *NvidiaSMI) Gather(acc telegraf.Accumulator) error {
if _, err := os.Stat(smi.BinPath); os.IsNotExist(err) { if _, err := os.Stat(smi.BinPath); os.IsNotExist(err) {
return fmt.Errorf("nvidia-smi binary not at path %s, cannot gather GPU data", smi.BinPath) binPath, err := exec.LookPath("nvidia-smi")
// fail-fast
if err != nil {
return fmt.Errorf("nvidia-smi not found in %q and not in PATH; please make sure nvidia-smi is installed and/or is in PATH", smi.BinPath)
}
smi.BinPath = binPath
} }
return nil
}
// Gather implements the telegraf interface
func (smi *NvidiaSMI) Gather(acc telegraf.Accumulator) error {
data, err := smi.pollSMI() data, err := smi.pollSMI()
if err != nil { if err != nil {
return err return err