2022-05-24 21:49:47 +08:00
|
|
|
//go:generate ../../../tools/readme_config_includer/generator
|
2015-08-06 08:29:27 +08:00
|
|
|
package exec
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2022-05-24 21:49:47 +08:00
|
|
|
_ "embed"
|
2015-08-06 08:29:27 +08:00
|
|
|
"fmt"
|
2021-03-19 05:21:30 +08:00
|
|
|
"io"
|
2022-05-11 05:34:05 +08:00
|
|
|
"os"
|
2021-06-21 23:07:52 +08:00
|
|
|
osExec "os/exec"
|
2016-04-29 21:07:01 +08:00
|
|
|
"path/filepath"
|
2016-08-09 02:45:18 +08:00
|
|
|
"runtime"
|
2016-04-29 21:07:01 +08:00
|
|
|
"strings"
|
2016-02-01 11:43:38 +08:00
|
|
|
"sync"
|
2016-04-29 09:23:45 +08:00
|
|
|
"time"
|
2015-12-15 06:15:51 +08:00
|
|
|
|
2021-06-21 23:07:52 +08:00
|
|
|
"github.com/kballard/go-shellquote"
|
|
|
|
|
|
2016-01-28 05:21:36 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
2021-04-10 01:15:04 +08:00
|
|
|
"github.com/influxdata/telegraf/config"
|
2016-04-29 09:23:45 +08:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-01-21 02:57:35 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2016-02-06 08:36:35 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers"
|
2016-02-25 12:32:22 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/nagios"
|
2015-08-06 08:29:27 +08:00
|
|
|
)
|
|
|
|
|
|
2022-05-24 21:49:47 +08:00
|
|
|
//go:embed sample.conf
|
|
|
|
|
var sampleConfig string
|
|
|
|
|
|
2021-03-19 05:21:30 +08:00
|
|
|
const MaxStderrBytes int = 512
|
2018-03-15 03:09:01 +08:00
|
|
|
|
2015-08-06 08:29:27 +08:00
|
|
|
type Exec struct {
|
2022-05-11 05:34:05 +08:00
|
|
|
Commands []string `toml:"commands"`
|
|
|
|
|
Command string `toml:"command"`
|
|
|
|
|
Environment []string `toml:"environment"`
|
|
|
|
|
Timeout config.Duration `toml:"timeout"`
|
2016-02-01 11:43:38 +08:00
|
|
|
|
2016-02-06 08:36:35 +08:00
|
|
|
parser parsers.Parser
|
2016-02-01 11:43:38 +08:00
|
|
|
|
2017-04-25 02:13:26 +08:00
|
|
|
runner Runner
|
2019-10-08 03:18:36 +08:00
|
|
|
Log telegraf.Logger `toml:"-"`
|
2016-02-06 08:36:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewExec() *Exec {
|
|
|
|
|
return &Exec{
|
2016-04-29 09:23:45 +08:00
|
|
|
runner: CommandRunner{},
|
2021-04-10 01:15:04 +08:00
|
|
|
Timeout: config.Duration(time.Second * 5),
|
2016-02-06 08:36:35 +08:00
|
|
|
}
|
2015-08-06 08:29:27 +08:00
|
|
|
}
|
|
|
|
|
|
2015-09-24 02:21:42 +08:00
|
|
|
type Runner interface {
|
2022-05-11 05:34:05 +08:00
|
|
|
Run(string, []string, time.Duration) ([]byte, []byte, error)
|
2015-08-06 08:29:27 +08:00
|
|
|
}
|
|
|
|
|
|
2015-09-24 02:21:42 +08:00
|
|
|
type CommandRunner struct{}
|
|
|
|
|
|
2016-04-29 09:23:45 +08:00
|
|
|
func (c CommandRunner) Run(
|
|
|
|
|
command string,
|
2022-05-11 05:34:05 +08:00
|
|
|
environments []string,
|
2019-03-26 07:24:42 +08:00
|
|
|
timeout time.Duration,
|
|
|
|
|
) ([]byte, []byte, error) {
|
2021-02-17 07:19:50 +08:00
|
|
|
splitCmd, err := shellquote.Split(command)
|
|
|
|
|
if err != nil || len(splitCmd) == 0 {
|
2019-03-26 07:24:42 +08:00
|
|
|
return nil, nil, fmt.Errorf("exec: unable to parse command, %s", err)
|
2015-09-24 02:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
2021-06-21 23:07:52 +08:00
|
|
|
cmd := osExec.Command(splitCmd[0], splitCmd[1:]...)
|
2016-02-01 11:43:38 +08:00
|
|
|
|
2022-05-11 05:34:05 +08:00
|
|
|
if len(environments) > 0 {
|
|
|
|
|
cmd.Env = append(os.Environ(), environments...)
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-15 03:09:01 +08:00
|
|
|
var (
|
|
|
|
|
out bytes.Buffer
|
|
|
|
|
stderr bytes.Buffer
|
|
|
|
|
)
|
2015-08-06 08:29:27 +08:00
|
|
|
cmd.Stdout = &out
|
2018-03-15 03:09:01 +08:00
|
|
|
cmd.Stderr = &stderr
|
2015-08-06 08:29:27 +08:00
|
|
|
|
2019-03-26 07:24:42 +08:00
|
|
|
runErr := internal.RunTimeout(cmd, timeout)
|
2015-08-06 08:29:27 +08:00
|
|
|
|
2021-03-19 05:21:30 +08:00
|
|
|
out = removeWindowsCarriageReturns(out)
|
|
|
|
|
if stderr.Len() > 0 && !telegraf.Debug {
|
|
|
|
|
stderr = removeWindowsCarriageReturns(stderr)
|
|
|
|
|
stderr = c.truncate(stderr)
|
2019-03-26 07:24:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return out.Bytes(), stderr.Bytes(), runErr
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-19 05:21:30 +08:00
|
|
|
func (c CommandRunner) truncate(buf bytes.Buffer) bytes.Buffer {
|
2019-03-26 07:24:42 +08:00
|
|
|
// Limit the number of bytes.
|
|
|
|
|
didTruncate := false
|
|
|
|
|
if buf.Len() > MaxStderrBytes {
|
|
|
|
|
buf.Truncate(MaxStderrBytes)
|
|
|
|
|
didTruncate = true
|
|
|
|
|
}
|
|
|
|
|
if i := bytes.IndexByte(buf.Bytes(), '\n'); i > 0 {
|
|
|
|
|
// Only show truncation if the newline wasn't the last character.
|
|
|
|
|
if i < buf.Len()-1 {
|
|
|
|
|
didTruncate = true
|
|
|
|
|
}
|
|
|
|
|
buf.Truncate(i)
|
|
|
|
|
}
|
|
|
|
|
if didTruncate {
|
2021-04-23 05:08:03 +08:00
|
|
|
//nolint:errcheck,revive // Will always return nil or panic
|
2019-03-26 07:24:42 +08:00
|
|
|
buf.WriteString("...")
|
|
|
|
|
}
|
|
|
|
|
return buf
|
2015-08-06 08:29:27 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-19 05:21:30 +08:00
|
|
|
// removeWindowsCarriageReturns removes all carriage returns from the input if the
|
2016-08-09 02:45:18 +08:00
|
|
|
// OS is Windows. It does not return any errors.
|
2021-03-19 05:21:30 +08:00
|
|
|
func removeWindowsCarriageReturns(b bytes.Buffer) bytes.Buffer {
|
2016-08-09 02:45:18 +08:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
for {
|
2021-03-19 05:21:30 +08:00
|
|
|
byt, err := b.ReadBytes(0x0D)
|
|
|
|
|
byt = bytes.TrimRight(byt, "\x0d")
|
|
|
|
|
if len(byt) > 0 {
|
|
|
|
|
_, _ = buf.Write(byt)
|
2016-08-09 02:45:18 +08:00
|
|
|
}
|
2021-03-19 05:21:30 +08:00
|
|
|
if err == io.EOF {
|
|
|
|
|
return buf
|
2016-08-09 02:45:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 21:49:47 +08:00
|
|
|
func (*Exec) SampleConfig() string {
|
|
|
|
|
return sampleConfig
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-11 21:58:00 +08:00
|
|
|
func (e *Exec) ProcessCommand(command string, acc telegraf.Accumulator, wg *sync.WaitGroup) {
|
|
|
|
|
defer wg.Done()
|
2022-07-01 02:20:32 +08:00
|
|
|
_, isNagios := e.parser.(*nagios.Parser)
|
2016-02-01 11:43:38 +08:00
|
|
|
|
2022-05-18 04:14:26 +08:00
|
|
|
out, errBuf, runErr := e.runner.Run(command, e.Environment, time.Duration(e.Timeout))
|
2019-03-26 07:24:42 +08:00
|
|
|
if !isNagios && runErr != nil {
|
2022-05-18 04:14:26 +08:00
|
|
|
err := fmt.Errorf("exec: %s for command '%s': %s", runErr, command, string(errBuf))
|
2017-04-25 02:13:26 +08:00
|
|
|
acc.AddError(err)
|
2016-02-01 11:43:38 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-06 08:36:35 +08:00
|
|
|
metrics, err := e.parser.Parse(out)
|
2016-02-01 11:43:38 +08:00
|
|
|
if err != nil {
|
2017-04-25 02:13:26 +08:00
|
|
|
acc.AddError(err)
|
2019-03-26 07:24:42 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if isNagios {
|
2022-05-18 04:14:26 +08:00
|
|
|
metrics = nagios.AddState(runErr, errBuf, metrics)
|
2016-02-01 11:43:38 +08:00
|
|
|
}
|
2019-03-26 07:24:42 +08:00
|
|
|
|
|
|
|
|
for _, m := range metrics {
|
|
|
|
|
acc.AddMetric(m)
|
|
|
|
|
}
|
2016-02-01 11:43:38 +08:00
|
|
|
}
|
|
|
|
|
|
2016-02-06 08:36:35 +08:00
|
|
|
func (e *Exec) SetParser(parser parsers.Parser) {
|
|
|
|
|
e.parser = parser
|
|
|
|
|
}
|
2016-02-01 11:43:38 +08:00
|
|
|
|
2016-02-06 08:36:35 +08:00
|
|
|
func (e *Exec) Gather(acc telegraf.Accumulator) error {
|
2016-07-11 21:58:00 +08:00
|
|
|
var wg sync.WaitGroup
|
2016-02-10 02:14:36 +08:00
|
|
|
// Legacy single command support
|
|
|
|
|
if e.Command != "" {
|
|
|
|
|
e.Commands = append(e.Commands, e.Command)
|
|
|
|
|
e.Command = ""
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-29 21:07:01 +08:00
|
|
|
commands := make([]string, 0, len(e.Commands))
|
|
|
|
|
for _, pattern := range e.Commands {
|
|
|
|
|
cmdAndArgs := strings.SplitN(pattern, " ", 2)
|
|
|
|
|
if len(cmdAndArgs) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
matches, err := filepath.Glob(cmdAndArgs[0])
|
|
|
|
|
if err != nil {
|
2017-04-25 02:13:26 +08:00
|
|
|
acc.AddError(err)
|
|
|
|
|
continue
|
2016-04-29 21:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
|
// There were no matches with the glob pattern, so let's assume
|
|
|
|
|
// that the command is in PATH and just run it as it is
|
|
|
|
|
commands = append(commands, pattern)
|
|
|
|
|
} else {
|
|
|
|
|
// There were matches, so we'll append each match together with
|
|
|
|
|
// the arguments to the commands slice
|
|
|
|
|
for _, match := range matches {
|
2016-06-10 18:20:50 +08:00
|
|
|
if len(cmdAndArgs) == 1 {
|
|
|
|
|
commands = append(commands, match)
|
|
|
|
|
} else {
|
|
|
|
|
commands = append(commands,
|
|
|
|
|
strings.Join([]string{match, cmdAndArgs[1]}, " "))
|
|
|
|
|
}
|
2016-04-29 21:07:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-11 21:58:00 +08:00
|
|
|
wg.Add(len(commands))
|
2016-04-29 21:07:01 +08:00
|
|
|
for _, command := range commands {
|
2016-07-11 21:58:00 +08:00
|
|
|
go e.ProcessCommand(command, acc, &wg)
|
2016-02-01 11:43:38 +08:00
|
|
|
}
|
2016-07-11 21:58:00 +08:00
|
|
|
wg.Wait()
|
2017-04-25 02:13:26 +08:00
|
|
|
return nil
|
2015-08-06 08:29:27 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-22 07:49:07 +08:00
|
|
|
func (e *Exec) Init() error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-06 08:29:27 +08:00
|
|
|
func init() {
|
2016-01-28 05:21:36 +08:00
|
|
|
inputs.Add("exec", func() telegraf.Input {
|
2015-08-06 08:29:27 +08:00
|
|
|
return NewExec()
|
|
|
|
|
})
|
|
|
|
|
}
|