diff --git a/internal/exec.go b/internal/exec.go index 795822f46..7fe95c0b9 100644 --- a/internal/exec.go +++ b/internal/exec.go @@ -20,6 +20,20 @@ func CombinedOutputTimeout(c *exec.Cmd, timeout time.Duration) ([]byte, error) { return b.Bytes(), err } +// StdOutputTimeout runs the given command with the given timeout and +// returns the output of stdout. +// If the command times out, it attempts to kill the process. +func StdOutputTimeout(c *exec.Cmd, timeout time.Duration) ([]byte, error) { + var b bytes.Buffer + c.Stdout = &b + c.Stderr = nil + if err := c.Start(); err != nil { + return nil, err + } + err := WaitTimeout(c, timeout) + return b.Bytes(), err +} + // RunTimeout runs the given command with the given timeout. // If the command times out, it attempts to kill the process. func RunTimeout(c *exec.Cmd, timeout time.Duration) error { diff --git a/plugins/inputs/sensors/sensors.go b/plugins/inputs/sensors/sensors.go index 864da8121..1df88466b 100644 --- a/plugins/inputs/sensors/sensors.go +++ b/plugins/inputs/sensors/sensors.go @@ -60,7 +60,7 @@ func (s *Sensors) parse(acc telegraf.Accumulator) error { fields := map[string]interface{}{} chip := "" cmd := execCommand(s.path, "-A", "-u") - out, err := internal.CombinedOutputTimeout(cmd, s.Timeout.Duration) + out, err := internal.StdOutputTimeout(cmd, s.Timeout.Duration) if err != nil { return fmt.Errorf("failed to run command %s: %s - %s", strings.Join(cmd.Args, " "), err, string(out)) }