Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
54
plugins/outputs/exec/README.md
Normal file
54
plugins/outputs/exec/README.md
Normal file
|
@ -0,0 +1,54 @@
|
|||
# Executable Output Plugin
|
||||
|
||||
This plugin writes metrics to an external application via `stdin`. The command
|
||||
will be executed on each write creating a new process. Metrics are passed in
|
||||
one of the supported [data formats][data_formats].
|
||||
|
||||
The executable and the individual parameters must be defined as a list.
|
||||
All outputs of the executable to `stderr` will be logged in the Telegraf log.
|
||||
|
||||
> [!TIP]
|
||||
> For better performance consider execd which runs continuously.
|
||||
|
||||
⭐ Telegraf v1.12.0
|
||||
🏷️ system
|
||||
💻 all
|
||||
|
||||
[data_formats]: /docs/DATA_FORMATS_OUTPUT.md
|
||||
|
||||
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
|
||||
|
||||
In addition to the plugin-specific configuration settings, plugins support
|
||||
additional global and plugin configuration settings. These settings are used to
|
||||
modify metrics, tags, and field or create aliases and configure ordering, etc.
|
||||
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
||||
|
||||
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
|
||||
|
||||
## Configuration
|
||||
|
||||
```toml @sample.conf
|
||||
# Send metrics to command as input over stdin
|
||||
[[outputs.exec]]
|
||||
## Command to ingest metrics via stdin.
|
||||
command = ["tee", "-a", "/dev/null"]
|
||||
|
||||
## Environment variables
|
||||
## Array of "key=value" pairs to pass as environment variables
|
||||
## e.g. "KEY=value", "USERNAME=John Doe",
|
||||
## "LD_LIBRARY_PATH=/opt/custom/lib64:/usr/local/libs"
|
||||
# environment = []
|
||||
|
||||
## Timeout for command to complete.
|
||||
# timeout = "5s"
|
||||
|
||||
## Whether the command gets executed once per metric, or once per metric batch
|
||||
## The serializer will also run in batch mode when this is true.
|
||||
# use_batch_format = true
|
||||
|
||||
## Data format to output.
|
||||
## Each data format has its own unique set of configuration options, read
|
||||
## more about them here:
|
||||
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
|
||||
# data_format = "influx"
|
||||
```
|
190
plugins/outputs/exec/exec.go
Normal file
190
plugins/outputs/exec/exec.go
Normal file
|
@ -0,0 +1,190 @@
|
|||
//go:generate ../../../tools/readme_config_includer/generator
|
||||
package exec
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/config"
|
||||
"github.com/influxdata/telegraf/internal"
|
||||
"github.com/influxdata/telegraf/plugins/outputs"
|
||||
)
|
||||
|
||||
//go:embed sample.conf
|
||||
var sampleConfig string
|
||||
|
||||
const maxStderrBytes = 512
|
||||
|
||||
// Exec defines the exec output plugin.
|
||||
type Exec struct {
|
||||
Command []string `toml:"command"`
|
||||
Environment []string `toml:"environment"`
|
||||
Timeout config.Duration `toml:"timeout"`
|
||||
UseBatchFormat bool `toml:"use_batch_format"`
|
||||
Log telegraf.Logger `toml:"-"`
|
||||
|
||||
runner Runner
|
||||
serializer telegraf.Serializer
|
||||
}
|
||||
|
||||
func (*Exec) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
func (e *Exec) Init() error {
|
||||
e.runner = &CommandRunner{log: e.Log}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetSerializer sets the serializer for the output.
|
||||
func (e *Exec) SetSerializer(serializer telegraf.Serializer) {
|
||||
e.serializer = serializer
|
||||
}
|
||||
|
||||
// Connect satisfies the Output interface.
|
||||
func (*Exec) Connect() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close satisfies the Output interface.
|
||||
func (*Exec) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Write writes the metrics to the configured command.
|
||||
func (e *Exec) Write(metrics []telegraf.Metric) error {
|
||||
var buffer bytes.Buffer
|
||||
if e.UseBatchFormat {
|
||||
serializedMetrics, err := e.serializer.SerializeBatch(metrics)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
buffer.Write(serializedMetrics)
|
||||
|
||||
if buffer.Len() <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return e.runner.Run(time.Duration(e.Timeout), e.Command, e.Environment, &buffer)
|
||||
}
|
||||
errs := make([]error, 0, len(metrics))
|
||||
for _, metric := range metrics {
|
||||
serializedMetric, err := e.serializer.Serialize(metric)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
buffer.Reset()
|
||||
buffer.Write(serializedMetric)
|
||||
|
||||
err = e.runner.Run(time.Duration(e.Timeout), e.Command, e.Environment, &buffer)
|
||||
errs = append(errs, err)
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
// Runner provides an interface for running exec.Cmd.
|
||||
type Runner interface {
|
||||
Run(time.Duration, []string, []string, io.Reader) error
|
||||
}
|
||||
|
||||
// CommandRunner runs a command with the ability to kill the process before the timeout.
|
||||
type CommandRunner struct {
|
||||
cmd *exec.Cmd
|
||||
log telegraf.Logger
|
||||
}
|
||||
|
||||
// Run runs the command.
|
||||
func (c *CommandRunner) Run(timeout time.Duration, command, environments []string, buffer io.Reader) error {
|
||||
cmd := exec.Command(command[0], command[1:]...)
|
||||
if len(environments) > 0 {
|
||||
cmd.Env = append(os.Environ(), environments...)
|
||||
}
|
||||
cmd.Stdin = buffer
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
err := internal.RunTimeout(cmd, timeout)
|
||||
s := stderr
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, internal.ErrTimeout) {
|
||||
return fmt.Errorf("%q timed out and was killed", command)
|
||||
}
|
||||
|
||||
s = removeWindowsCarriageReturns(s)
|
||||
if s.Len() > 0 {
|
||||
if c.log.Level() < telegraf.Debug {
|
||||
c.log.Errorf("Command error: %q", truncate(s))
|
||||
} else {
|
||||
c.log.Debugf("Command error: %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
if status, ok := internal.ExitStatus(err); ok {
|
||||
return fmt.Errorf("%q exited %d with %w", command, status, err)
|
||||
}
|
||||
|
||||
return fmt.Errorf("%q failed with %w", command, err)
|
||||
}
|
||||
|
||||
c.cmd = cmd
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func truncate(buf bytes.Buffer) string {
|
||||
// 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.String()
|
||||
}
|
||||
|
||||
func init() {
|
||||
outputs.Add("exec", func() telegraf.Output {
|
||||
return &Exec{
|
||||
Timeout: config.Duration(time.Second * 5),
|
||||
UseBatchFormat: true,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
185
plugins/outputs/exec/exec_test.go
Normal file
185
plugins/outputs/exec/exec_test.go
Normal file
|
@ -0,0 +1,185 @@
|
|||
package exec
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/config"
|
||||
"github.com/influxdata/telegraf/metric"
|
||||
parsers_influx "github.com/influxdata/telegraf/plugins/parsers/influx"
|
||||
"github.com/influxdata/telegraf/plugins/serializers/influx"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
)
|
||||
|
||||
var now = time.Date(2020, 6, 30, 16, 16, 0, 0, time.UTC)
|
||||
|
||||
type MockRunner struct {
|
||||
runs []int
|
||||
}
|
||||
|
||||
// Run runs the command.
|
||||
func (c *MockRunner) Run(_ time.Duration, _, _ []string, buffer io.Reader) error {
|
||||
parser := parsers_influx.NewStreamParser(buffer)
|
||||
numMetrics := 0
|
||||
|
||||
for {
|
||||
_, err := parser.Next()
|
||||
if err != nil {
|
||||
if errors.Is(err, parsers_influx.EOF) {
|
||||
break // stream ended
|
||||
}
|
||||
continue
|
||||
}
|
||||
numMetrics++
|
||||
}
|
||||
|
||||
c.runs = append(c.runs, numMetrics)
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestExternalOutputBatch(t *testing.T) {
|
||||
serializer := &influx.Serializer{}
|
||||
require.NoError(t, serializer.Init())
|
||||
|
||||
runner := MockRunner{}
|
||||
|
||||
e := &Exec{
|
||||
UseBatchFormat: true,
|
||||
serializer: serializer,
|
||||
Log: testutil.Logger{},
|
||||
runner: &runner,
|
||||
}
|
||||
|
||||
m := metric.New(
|
||||
"cpu",
|
||||
map[string]string{"name": "cpu1"},
|
||||
map[string]interface{}{"idle": 50, "sys": 30},
|
||||
now,
|
||||
)
|
||||
|
||||
require.NoError(t, e.Connect())
|
||||
require.NoError(t, e.Write([]telegraf.Metric{m, m}))
|
||||
// Make sure it executed the command once, with 2 metrics
|
||||
require.Equal(t, []int{2}, runner.runs)
|
||||
require.NoError(t, e.Close())
|
||||
}
|
||||
|
||||
func TestExternalOutputNoBatch(t *testing.T) {
|
||||
serializer := &influx.Serializer{}
|
||||
require.NoError(t, serializer.Init())
|
||||
runner := MockRunner{}
|
||||
|
||||
e := &Exec{
|
||||
UseBatchFormat: false,
|
||||
serializer: serializer,
|
||||
Log: testutil.Logger{},
|
||||
runner: &runner,
|
||||
}
|
||||
|
||||
m := metric.New(
|
||||
"cpu",
|
||||
map[string]string{"name": "cpu1"},
|
||||
map[string]interface{}{"idle": 50, "sys": 30},
|
||||
now,
|
||||
)
|
||||
|
||||
require.NoError(t, e.Connect())
|
||||
require.NoError(t, e.Write([]telegraf.Metric{m, m}))
|
||||
// Make sure it executed the command twice, both with a single metric
|
||||
require.Equal(t, []int{1, 1}, runner.runs)
|
||||
require.NoError(t, e.Close())
|
||||
}
|
||||
|
||||
func TestExec(t *testing.T) {
|
||||
t.Skip("Skipping test due to OS/executable dependencies and race condition when ran as part of a test-all")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
command []string
|
||||
err bool
|
||||
metrics []telegraf.Metric
|
||||
}{
|
||||
{
|
||||
name: "test success",
|
||||
command: []string{"tee"},
|
||||
err: false,
|
||||
metrics: testutil.MockMetrics(),
|
||||
},
|
||||
{
|
||||
name: "test doesn't accept stdin",
|
||||
command: []string{"sleep", "5s"},
|
||||
err: true,
|
||||
metrics: testutil.MockMetrics(),
|
||||
},
|
||||
{
|
||||
name: "test command not found",
|
||||
command: []string{"/no/exist", "-h"},
|
||||
err: true,
|
||||
metrics: testutil.MockMetrics(),
|
||||
},
|
||||
{
|
||||
name: "test no metrics output",
|
||||
command: []string{"tee"},
|
||||
err: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
e := &Exec{
|
||||
Command: tt.command,
|
||||
Timeout: config.Duration(time.Second),
|
||||
runner: &CommandRunner{},
|
||||
}
|
||||
|
||||
s := &influx.Serializer{}
|
||||
require.NoError(t, s.Init())
|
||||
e.SetSerializer(s)
|
||||
|
||||
require.NoError(t, e.Connect())
|
||||
require.Equal(t, tt.err, e.Write(tt.metrics) != nil)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTruncate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
buf *bytes.Buffer
|
||||
len int
|
||||
}{
|
||||
{
|
||||
name: "long out",
|
||||
buf: bytes.NewBufferString(strings.Repeat("a", maxStderrBytes+100)),
|
||||
len: maxStderrBytes + len("..."),
|
||||
},
|
||||
{
|
||||
name: "multiline out",
|
||||
buf: bytes.NewBufferString("hola\ngato\n"),
|
||||
len: len("hola") + len("..."),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := truncate(*tt.buf)
|
||||
require.Len(t, s, tt.len)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecDocs(t *testing.T) {
|
||||
e := &Exec{}
|
||||
e.SampleConfig()
|
||||
require.NoError(t, e.Close())
|
||||
|
||||
e = &Exec{runner: &CommandRunner{}}
|
||||
require.NoError(t, e.Close())
|
||||
}
|
23
plugins/outputs/exec/sample.conf
Normal file
23
plugins/outputs/exec/sample.conf
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Send metrics to command as input over stdin
|
||||
[[outputs.exec]]
|
||||
## Command to ingest metrics via stdin.
|
||||
command = ["tee", "-a", "/dev/null"]
|
||||
|
||||
## Environment variables
|
||||
## Array of "key=value" pairs to pass as environment variables
|
||||
## e.g. "KEY=value", "USERNAME=John Doe",
|
||||
## "LD_LIBRARY_PATH=/opt/custom/lib64:/usr/local/libs"
|
||||
# environment = []
|
||||
|
||||
## Timeout for command to complete.
|
||||
# timeout = "5s"
|
||||
|
||||
## Whether the command gets executed once per metric, or once per metric batch
|
||||
## The serializer will also run in batch mode when this is true.
|
||||
# use_batch_format = true
|
||||
|
||||
## Data format to output.
|
||||
## Each data format has its own unique set of configuration options, read
|
||||
## more about them here:
|
||||
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
|
||||
# data_format = "influx"
|
Loading…
Add table
Add a link
Reference in a new issue