First is to write an internal CombinedOutput and Run function with a
timeout.
Second, the following instances of command runners need to have timeouts:
plugins/inputs/ping/ping.go
125: out, err := c.CombinedOutput()
plugins/inputs/exec/exec.go
91: if err := cmd.Run(); err != nil {
plugins/inputs/ipmi_sensor/command.go
31: err := cmd.Run()
plugins/inputs/sysstat/sysstat.go
194: out, err := cmd.CombinedOutput()
plugins/inputs/leofs/leofs.go
185: defer cmd.Wait()
plugins/inputs/sysstat/sysstat.go
282: if err := cmd.Wait(); err != nil {
closes #1067
36 lines
693 B
Go
36 lines
693 B
Go
package ipmi_sensor
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/influxdata/telegraf/internal"
|
|
)
|
|
|
|
type CommandRunner struct{}
|
|
|
|
func (t CommandRunner) cmd(conn *Connection, args ...string) *exec.Cmd {
|
|
path := conn.Path
|
|
opts := append(conn.options(), args...)
|
|
|
|
if path == "" {
|
|
path = "ipmitool"
|
|
}
|
|
|
|
return exec.Command(path, opts...)
|
|
}
|
|
|
|
func (t CommandRunner) Run(conn *Connection, args ...string) (string, error) {
|
|
cmd := t.cmd(conn, args...)
|
|
|
|
output, err := internal.CombinedOutputTimeout(cmd, time.Second*5)
|
|
if err != nil {
|
|
return "", fmt.Errorf("run %s %s: %s (%s)",
|
|
cmd.Path, strings.Join(cmd.Args, " "), string(output), err)
|
|
}
|
|
|
|
return string(output), err
|
|
}
|