telegraf/plugins/processors/execd/execd.go

164 lines
4.0 KiB
Go
Raw Normal View History

2020-06-05 07:09:22 +08:00
package execd
import (
"bufio"
2020-06-27 04:38:07 +08:00
"errors"
2020-06-05 07:09:22 +08:00
"fmt"
"io"
"strings"
2020-06-05 07:09:22 +08:00
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal/process"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/plugins/processors"
"github.com/influxdata/telegraf/plugins/serializers"
)
const sampleConfig = `
2020-06-27 04:38:07 +08:00
## Program to run as daemon
## eg: command = ["/path/to/your_program", "arg1", "arg2"]
command = ["cat"]
2020-06-05 07:09:22 +08:00
## Delay before the process is restarted after an unexpected termination
restart_delay = "10s"
`
type Execd struct {
Command []string `toml:"command"`
RestartDelay config.Duration `toml:"restart_delay"`
2020-06-27 04:38:07 +08:00
Log telegraf.Logger
2020-06-05 07:09:22 +08:00
parserConfig *parsers.Config
parser parsers.Parser
serializerConfig *serializers.Config
serializer serializers.Serializer
acc telegraf.Accumulator
process *process.Process
}
func New() *Execd {
return &Execd{
RestartDelay: config.Duration(10 * time.Second),
parserConfig: &parsers.Config{
DataFormat: "influx",
},
serializerConfig: &serializers.Config{
DataFormat: "influx",
},
}
}
func (e *Execd) SampleConfig() string {
return sampleConfig
}
func (e *Execd) Description() string {
return "Run executable as long-running processor plugin"
}
func (e *Execd) Start(acc telegraf.Accumulator) error {
var err error
e.parser, err = parsers.NewParser(e.parserConfig)
if err != nil {
return fmt.Errorf("error creating parser: %w", err)
}
e.serializer, err = serializers.NewSerializer(e.serializerConfig)
if err != nil {
return fmt.Errorf("error creating serializer: %w", err)
}
e.acc = acc
e.process, err = process.New(e.Command)
if err != nil {
return fmt.Errorf("error creating new process: %w", err)
}
2020-06-27 04:38:07 +08:00
e.process.Log = e.Log
2020-06-05 07:09:22 +08:00
e.process.RestartDelay = time.Duration(e.RestartDelay)
e.process.ReadStdoutFn = e.cmdReadOut
e.process.ReadStderrFn = e.cmdReadErr
if err = e.process.Start(); err != nil {
// if there was only one argument, and it contained spaces, warn the user
// that they may have configured it wrong.
if len(e.Command) == 1 && strings.Contains(e.Command[0], " ") {
e.Log.Warn("The processors.execd Command contained spaces but no arguments. " +
"This setting expects the program and arguments as an array of strings, " +
"not as a space-delimited string. See the plugin readme for an example.")
}
2020-06-05 07:09:22 +08:00
return fmt.Errorf("failed to start process %s: %w", e.Command, err)
}
return nil
}
2020-06-25 01:06:05 +08:00
func (e *Execd) Add(m telegraf.Metric, acc telegraf.Accumulator) error {
2020-06-05 07:09:22 +08:00
b, err := e.serializer.Serialize(m)
if err != nil {
2020-06-25 01:06:05 +08:00
return fmt.Errorf("metric serializing error: %w", err)
2020-06-05 07:09:22 +08:00
}
_, err = e.process.Stdin.Write(b)
if err != nil {
2020-06-25 01:06:05 +08:00
return fmt.Errorf("error writing to process stdin: %w", err)
2020-06-05 07:09:22 +08:00
}
// We cannot maintain tracking metrics at the moment because input/output
// is done asynchronously and we don't have any metric metadata to tie the
// output metric back to the original input metric.
m.Drop()
2020-06-25 01:06:05 +08:00
return nil
2020-06-05 07:09:22 +08:00
}
func (e *Execd) Stop() error {
e.process.Stop()
return nil
}
func (e *Execd) cmdReadOut(out io.Reader) {
scanner := bufio.NewScanner(out)
2020-09-19 01:50:22 +08:00
scanBuf := make([]byte, 4096)
scanner.Buffer(scanBuf, 262144)
2020-06-05 07:09:22 +08:00
for scanner.Scan() {
metrics, err := e.parser.Parse(scanner.Bytes())
if err != nil {
2020-06-27 04:38:07 +08:00
e.Log.Errorf("Parse error: %s", err)
2020-06-05 07:09:22 +08:00
}
for _, metric := range metrics {
e.acc.AddMetric(metric)
}
}
if err := scanner.Err(); err != nil {
2020-06-27 04:38:07 +08:00
e.Log.Errorf("Error reading stdout: %s", err)
2020-06-05 07:09:22 +08:00
}
}
func (e *Execd) cmdReadErr(out io.Reader) {
scanner := bufio.NewScanner(out)
for scanner.Scan() {
2020-06-27 04:38:07 +08:00
e.Log.Errorf("stderr: %q", scanner.Text())
2020-06-05 07:09:22 +08:00
}
if err := scanner.Err(); err != nil {
2020-06-27 04:38:07 +08:00
e.Log.Errorf("Error reading stderr: %s", err)
2020-06-05 07:09:22 +08:00
}
}
2020-06-27 04:38:07 +08:00
func (e *Execd) Init() error {
if len(e.Command) == 0 {
return errors.New("no command specified")
}
return nil
}
2020-06-05 07:09:22 +08:00
func init() {
processors.AddStreaming("execd", func() telegraf.StreamingProcessor {
return New()
})
}