telegraf/plugins/inputs/exec/exec.go

198 lines
4.3 KiB
Go
Raw Normal View History

//go:generate ../../../tools/readme_config_includer/generator
2015-08-06 08:29:27 +08:00
package exec
import (
"bytes"
_ "embed"
"errors"
2015-08-06 08:29:27 +08:00
"fmt"
"io"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/models"
2016-01-21 02:57:35 +08:00
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/parsers/nagios"
2015-08-06 08:29:27 +08:00
)
//go:embed sample.conf
var sampleConfig string
const MaxStderrBytes int = 512
type exitcodeHandlerFunc func([]telegraf.Metric, error, []byte) []telegraf.Metric
2015-08-06 08:29:27 +08:00
type Exec struct {
Commands []string `toml:"commands"`
Command string `toml:"command"`
Environment []string `toml:"environment"`
Timeout config.Duration `toml:"timeout"`
Log telegraf.Logger `toml:"-"`
parser telegraf.Parser
2017-04-25 02:13:26 +08:00
runner Runner
// Allow post processing of command exit codes
exitcodeHandler exitcodeHandlerFunc
parseDespiteError bool
}
func NewExec() *Exec {
return &Exec{
runner: CommandRunner{},
Timeout: config.Duration(time.Second * 5),
}
2015-08-06 08:29:27 +08:00
}
2015-09-24 02:21:42 +08:00
type Runner interface {
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{}
func (c CommandRunner) truncate(buf bytes.Buffer) bytes.Buffer {
// 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 {
buf.WriteString("...")
}
return buf
2015-08-06 08:29:27 +08:00
}
// removeWindowsCarriageReturns removes all carriage returns from the input if the
// OS is Windows. It does not return any errors.
func removeWindowsCarriageReturns(b bytes.Buffer) bytes.Buffer {
if runtime.GOOS == "windows" {
var buf bytes.Buffer
for {
byt, err := b.ReadBytes(0x0D)
byt = bytes.TrimRight(byt, "\x0d")
if len(byt) > 0 {
_, _ = buf.Write(byt)
}
if errors.Is(err, io.EOF) {
return buf
}
}
}
return b
}
func (*Exec) SampleConfig() string {
return sampleConfig
}
func (e *Exec) ProcessCommand(command string, acc telegraf.Accumulator, wg *sync.WaitGroup) {
defer wg.Done()
out, errBuf, runErr := e.runner.Run(command, e.Environment, time.Duration(e.Timeout))
if !e.parseDespiteError && runErr != nil {
err := fmt.Errorf("exec: %w for command %q: %s", runErr, command, string(errBuf))
2017-04-25 02:13:26 +08:00
acc.AddError(err)
return
}
metrics, err := e.parser.Parse(out)
if err != nil {
2017-04-25 02:13:26 +08:00
acc.AddError(err)
return
}
if e.exitcodeHandler != nil {
metrics = e.exitcodeHandler(metrics, runErr, errBuf)
}
for _, m := range metrics {
acc.AddMetric(m)
}
}
func (e *Exec) SetParser(parser telegraf.Parser) {
e.parser = parser
unwrapped, ok := parser.(*models.RunningParser)
if ok {
if _, ok := unwrapped.Parser.(*nagios.Parser); ok {
e.exitcodeHandler = nagiosHandler
e.parseDespiteError = true
}
}
}
func (e *Exec) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup
// Legacy single command support
if e.Command != "" {
e.Commands = append(e.Commands, e.Command)
e.Command = ""
}
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
}
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 {
if len(cmdAndArgs) == 1 {
commands = append(commands, match)
} else {
commands = append(commands,
strings.Join([]string{match, cmdAndArgs[1]}, " "))
}
}
}
}
wg.Add(len(commands))
for _, command := range commands {
go e.ProcessCommand(command, acc, &wg)
}
wg.Wait()
2017-04-25 02:13:26 +08:00
return nil
2015-08-06 08:29:27 +08:00
}
func (e *Exec) Init() error {
return nil
}
func nagiosHandler(metrics []telegraf.Metric, err error, msg []byte) []telegraf.Metric {
return nagios.AddState(err, msg, metrics)
}
2015-08-06 08:29:27 +08:00
func init() {
inputs.Add("exec", func() telegraf.Input {
2015-08-06 08:29:27 +08:00
return NewExec()
})
}