telegraf/plugins/inputs/ipmi/command.go

40 lines
702 B
Go
Raw Normal View History

2016-03-17 15:45:29 +00:00
// command
package ipmi
import (
"bytes"
"fmt"
"os/exec"
"strings"
)
2016-03-20 15:54:17 +00:00
type CommandRunner struct{}
2016-03-17 15:45:29 +00:00
2016-03-20 15:54:17 +00:00
func (t CommandRunner) cmd(conn *Connection, args ...string) *exec.Cmd {
path := conn.Path
opts := append(conn.options(), args...)
2016-03-17 15:45:29 +00:00
if path == "" {
path = "ipmitool"
}
return exec.Command(path, opts...)
}
2016-03-20 15:54:17 +00:00
func (t CommandRunner) Run(conn *Connection, args ...string) (string, error) {
cmd := t.cmd(conn, args...)
2016-03-17 15:45:29 +00:00
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return "", fmt.Errorf("run %s %s: %s (%s)",
cmd.Path, strings.Join(cmd.Args, " "), stderr.String(), err)
}
return stdout.String(), err
}