1
0
Fork 0

Adding upstream version 1.34.4.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-24 07:26:29 +02:00
parent e393c3af3f
commit 4978089aab
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
4963 changed files with 677545 additions and 0 deletions

View file

@ -0,0 +1,100 @@
# Exec Input Plugin
This plugin executes the given `commands` on every interval and parses metrics
from their output in any one of the supported [data formats][data_formats].
This plugin can be used to poll for custom metrics from any source.
⭐ Telegraf v0.1.5
🏷️ system
💻 all
[data_formats]: /docs/DATA_FORMATS_INPUT.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
# Read metrics from one or more commands that can output to stdout
[[inputs.exec]]
## Commands array
commands = []
## 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 each command to complete.
# timeout = "5s"
## Measurement name suffix
## Used for separating different commands
# name_suffix = ""
## Ignore Error Code
## If set to true, a non-zero error code in not considered an error and the
## plugin will continue to parse the output.
# ignore_error = false
## Data format
## By default, exec expects JSON. This was done for historical reasons and is
## different than other inputs that use the influx line protocol. 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_INPUT.md
# data_format = "json"
```
Glob patterns in the `command` option are matched on every run, so adding new
scripts that match the pattern will cause them to be picked up immediately.
## Example
This script produces static values, since no timestamp is specified the values
are at the current time. Ensure that int values are followed with `i` for proper
parsing.
```sh
#!/bin/sh
echo 'example,tag1=a,tag2=b i=42i,j=43i,k=44i'
```
It can be paired with the following configuration and will be run at the
`interval` of the agent.
```toml
[[inputs.exec]]
commands = ["sh /tmp/test.sh"]
timeout = "5s"
data_format = "influx"
```
## Common Issues
### My script works when I run it by hand, but not when Telegraf is running as a service
This may be related to the Telegraf service running as a different user. The
official packages run Telegraf as the `telegraf` user and group on Linux
systems.
### With a PowerShell on Windows, the output of the script appears to be truncated
You may need to set a variable in your script to increase the number of columns
available for output:
```shell
$host.UI.RawUI.BufferSize = new-object System.Management.Automation.Host.Size(1024,50)
```
## Metrics
## Example Output

View file

@ -0,0 +1,26 @@
[agent]
interval="1s"
flush_interval="1s"
[[inputs.exec]]
timeout = "1s"
data_format = "influx"
commands = [
"echo 'deal,computer_name=hosta message=\"stuff\" 1530654676316265790'",
"echo 'deal,computer_name=hostb message=\"stuff\" 1530654676316265790'",
]
[[processors.regex]]
[[processors.regex.tags]]
key = "computer_name"
pattern = "^(.*?)a$"
replacement = "${1}"
result_key = "server_name"
[[processors.regex.tags]]
key = "computer_name"
pattern = "^(.*?)b$"
replacement = "${1}"
result_key = "server_name"
[[outputs.file]]
files = ["stdout"]

192
plugins/inputs/exec/exec.go Normal file
View file

@ -0,0 +1,192 @@
//go:generate ../../../tools/readme_config_includer/generator
package exec
import (
"bytes"
_ "embed"
"fmt"
"path/filepath"
"strings"
"sync"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/models"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/parsers/nagios"
)
//go:embed sample.conf
var sampleConfig string
var once sync.Once
const maxStderrBytes int = 512
type Exec struct {
Commands []string `toml:"commands"`
Command string `toml:"command"`
Environment []string `toml:"environment"`
IgnoreError bool `toml:"ignore_error"`
Timeout config.Duration `toml:"timeout"`
Log telegraf.Logger `toml:"-"`
parser telegraf.Parser
runner runner
// Allow post-processing of command exit codes
exitCodeHandler exitCodeHandlerFunc
parseDespiteError bool
}
type exitCodeHandlerFunc func([]telegraf.Metric, error, []byte) []telegraf.Metric
type runner interface {
run(string) ([]byte, []byte, error)
}
type commandRunner struct {
environment []string
timeout time.Duration
debug bool
}
func (*Exec) SampleConfig() string {
return sampleConfig
}
func (e *Exec) Init() error {
// Legacy single command support
if e.Command != "" {
e.Commands = append(e.Commands, e.Command)
}
e.runner = &commandRunner{
environment: e.Environment,
timeout: time.Duration(e.Timeout),
debug: e.Log.Level().Includes(telegraf.Debug),
}
return nil
}
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 = func(metrics []telegraf.Metric, err error, msg []byte) []telegraf.Metric {
return nagios.AddState(err, msg, metrics)
}
e.parseDespiteError = true
}
}
}
func (e *Exec) Gather(acc telegraf.Accumulator) error {
commands := e.updateRunners()
var wg sync.WaitGroup
for _, cmd := range commands {
wg.Add(1)
go func(c string) {
defer wg.Done()
acc.AddError(e.processCommand(acc, c))
}(cmd)
}
wg.Wait()
return nil
}
func (e *Exec) updateRunners() []string {
commands := make([]string, 0, len(e.Commands))
for _, pattern := range e.Commands {
if pattern == "" {
continue
}
// Try to expand globbing expressions
cmd, args, found := strings.Cut(pattern, " ")
matches, err := filepath.Glob(cmd)
if err != nil {
e.Log.Errorf("Matching command %q failed: %v", cmd, err)
continue
}
if len(matches) == 0 {
// There were no matches with the glob pattern, so let's assume
// 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 found {
match += " " + args
}
commands = append(commands, match)
}
}
}
return commands
}
func (e *Exec) processCommand(acc telegraf.Accumulator, cmd string) error {
out, errBuf, runErr := e.runner.run(cmd)
if !e.IgnoreError && !e.parseDespiteError && runErr != nil {
return fmt.Errorf("exec: %w for command %q: %s", runErr, cmd, string(errBuf))
}
metrics, err := e.parser.Parse(out)
if err != nil {
return err
}
if len(metrics) == 0 {
once.Do(func() {
e.Log.Debug(internal.NoMetricsCreatedMsg)
})
}
if e.exitCodeHandler != nil {
metrics = e.exitCodeHandler(metrics, runErr, errBuf)
}
for _, m := range metrics {
acc.AddMetric(m)
}
return nil
}
func truncate(buf *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("...")
}
}
func init() {
inputs.Add("exec", func() telegraf.Input {
return &Exec{
Timeout: config.Duration(5 * time.Second),
}
})
}

View file

@ -0,0 +1,512 @@
//go:build !windows
// TODO: Windows - should be enabled for Windows when super asterisk is fixed on Windows
// https://github.com/influxdata/telegraf/issues/6248
package exec
import (
"bytes"
"errors"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/parsers/csv"
"github.com/influxdata/telegraf/plugins/parsers/json"
"github.com/influxdata/telegraf/plugins/parsers/value"
"github.com/influxdata/telegraf/testutil"
)
const validJSON = `
{
"status": "green",
"num_processes": 82,
"cpu": {
"status": "red",
"nil_status": null,
"used": 8234,
"free": 32
},
"percent": 0.81,
"users": [0, 1, 2, 3]
}`
const malformedJSON = `
{
"status": "green",
`
type runnerMock struct {
out []byte
errout []byte
err error
}
func (r runnerMock) run(string) (out, errout []byte, err error) {
return r.out, r.errout, r.err
}
func TestExec(t *testing.T) {
// Setup parser
parser := &json.Parser{MetricName: "exec"}
require.NoError(t, parser.Init())
// Setup plugin
plugin := &Exec{
Commands: []string{"testcommand arg1"},
Log: testutil.Logger{},
}
plugin.SetParser(parser)
require.NoError(t, plugin.Init())
plugin.runner = &runnerMock{out: []byte(validJSON)}
// Gather the metrics and check the result
var acc testutil.Accumulator
require.NoError(t, acc.GatherError(plugin.Gather))
expected := []telegraf.Metric{
metric.New(
"exec",
map[string]string{},
map[string]interface{}{
"num_processes": float64(82),
"cpu_used": float64(8234),
"cpu_free": float64(32),
"percent": float64(0.81),
"users_0": float64(0),
"users_1": float64(1),
"users_2": float64(2),
"users_3": float64(3),
},
time.Unix(0, 0),
),
}
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
}
func TestExecMalformed(t *testing.T) {
// Setup parser
parser := &json.Parser{MetricName: "exec"}
require.NoError(t, parser.Init())
// Setup plugin
plugin := &Exec{
Commands: []string{"badcommand arg1"},
Log: testutil.Logger{},
}
plugin.SetParser(parser)
require.NoError(t, plugin.Init())
plugin.runner = &runnerMock{out: []byte(malformedJSON)}
// Gather the metrics and check the result
var acc testutil.Accumulator
require.ErrorContains(t, acc.GatherError(plugin.Gather), "unexpected end of JSON input")
require.Empty(t, acc.GetTelegrafMetrics())
}
func TestCommandError(t *testing.T) {
// Setup parser
parser := &json.Parser{MetricName: "exec"}
require.NoError(t, parser.Init())
// Setup plugin
plugin := &Exec{
Commands: []string{"badcommand"},
Log: testutil.Logger{},
}
plugin.SetParser(parser)
require.NoError(t, plugin.Init())
plugin.runner = &runnerMock{err: errors.New("exit status code 1")}
// Gather the metrics and check the result
var acc testutil.Accumulator
require.ErrorContains(t, acc.GatherError(plugin.Gather), "exit status code 1 for command")
require.Equal(t, 0, acc.NFields(), "No new points should have been added")
}
func TestCommandIgnoreError(t *testing.T) {
// Setup parser
parser := &json.Parser{MetricName: "exec"}
require.NoError(t, parser.Init())
// Setup plugin
plugin := &Exec{
Commands: []string{"badcommand"},
IgnoreError: true,
Log: testutil.Logger{},
}
plugin.SetParser(parser)
require.NoError(t, plugin.Init())
plugin.runner = &runnerMock{
out: []byte(validJSON),
errout: []byte("error"),
err: errors.New("exit status code 1"),
}
// Gather the metrics and check the result
var acc testutil.Accumulator
require.NoError(t, acc.GatherError(plugin.Gather))
expected := []telegraf.Metric{
metric.New(
"exec",
map[string]string{},
map[string]interface{}{
"num_processes": float64(82),
"cpu_used": float64(8234),
"cpu_free": float64(32),
"percent": float64(0.81),
"users_0": float64(0),
"users_1": float64(1),
"users_2": float64(2),
"users_3": float64(3),
},
time.Unix(0, 0),
),
}
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
}
func TestExecCommandWithGlob(t *testing.T) {
// Setup parser
parser := value.Parser{
MetricName: "metric",
DataType: "string",
}
require.NoError(t, parser.Init())
// Setup plugin
plugin := &Exec{
Commands: []string{"/bin/ech* metric_value"},
Timeout: config.Duration(5 * time.Second),
Log: testutil.Logger{},
}
plugin.SetParser(&parser)
require.NoError(t, plugin.Init())
// Gather the metrics and check the result
var acc testutil.Accumulator
require.NoError(t, acc.GatherError(plugin.Gather))
expected := []telegraf.Metric{
metric.New(
"metric",
map[string]string{},
map[string]interface{}{
"value": "metric_value",
},
time.Unix(0, 0),
),
}
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
}
func TestExecCommandWithoutGlob(t *testing.T) {
// Setup parser
parser := value.Parser{
MetricName: "metric",
DataType: "string",
}
require.NoError(t, parser.Init())
// Setup plugin
plugin := &Exec{
Commands: []string{"/bin/echo metric_value"},
Timeout: config.Duration(5 * time.Second),
Log: testutil.Logger{},
}
plugin.SetParser(&parser)
require.NoError(t, plugin.Init())
// Gather the metrics and check the result
var acc testutil.Accumulator
require.NoError(t, acc.GatherError(plugin.Gather))
expected := []telegraf.Metric{
metric.New(
"metric",
map[string]string{},
map[string]interface{}{
"value": "metric_value",
},
time.Unix(0, 0),
),
}
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
}
func TestExecCommandWithoutGlobAndPath(t *testing.T) {
// Setup parser
parser := value.Parser{
MetricName: "metric",
DataType: "string",
}
require.NoError(t, parser.Init())
// Setup plugin
plugin := &Exec{
Commands: []string{"echo metric_value"},
Timeout: config.Duration(5 * time.Second),
Log: testutil.Logger{},
}
plugin.SetParser(&parser)
require.NoError(t, plugin.Init())
// Gather the metrics and check the result
var acc testutil.Accumulator
require.NoError(t, acc.GatherError(plugin.Gather))
expected := []telegraf.Metric{
metric.New(
"metric",
map[string]string{},
map[string]interface{}{
"value": "metric_value",
},
time.Unix(0, 0),
),
}
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
}
func TestExecCommandWithEnv(t *testing.T) {
// Setup parser
parser := value.Parser{
MetricName: "metric",
DataType: "string",
}
require.NoError(t, parser.Init())
// Setup plugin
plugin := &Exec{
Commands: []string{"/bin/sh -c 'echo ${METRIC_NAME}'"},
Environment: []string{"METRIC_NAME=metric_value"},
Timeout: config.Duration(5 * time.Second),
Log: testutil.Logger{},
}
plugin.SetParser(&parser)
require.NoError(t, plugin.Init())
// Gather the metrics and check the result
var acc testutil.Accumulator
require.NoError(t, acc.GatherError(plugin.Gather))
expected := []telegraf.Metric{
metric.New(
"metric",
map[string]string{},
map[string]interface{}{
"value": "metric_value",
},
time.Unix(0, 0),
),
}
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
}
func TestTruncate(t *testing.T) {
tests := []struct {
name string
bufF func() *bytes.Buffer
expected string
}{
{
name: "should not truncate",
bufF: func() *bytes.Buffer {
return bytes.NewBufferString("hello world")
},
expected: "hello world",
},
{
name: "should truncate up to the new line",
bufF: func() *bytes.Buffer {
return bytes.NewBufferString("hello world\nand all the people")
},
expected: "hello world...",
},
{
name: "should truncate to the maxStderrBytes",
bufF: func() *bytes.Buffer {
var b bytes.Buffer
for i := 0; i < 2*maxStderrBytes; i++ {
b.WriteByte('b')
}
return &b
},
expected: strings.Repeat("b", maxStderrBytes) + "...",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := tt.bufF()
truncate(buf)
require.Equal(t, tt.expected, buf.String())
})
}
}
func TestCSVBehavior(t *testing.T) {
// Setup the CSV parser
parser := &csv.Parser{
MetricName: "exec",
HeaderRowCount: 1,
ResetMode: "always",
}
require.NoError(t, parser.Init())
// Setup the plugin
plugin := &Exec{
Commands: []string{"echo \"a,b\n1,2\n3,4\""},
Timeout: config.Duration(5 * time.Second),
Log: testutil.Logger{},
}
plugin.SetParser(parser)
require.NoError(t, plugin.Init())
expected := []telegraf.Metric{
metric.New(
"exec",
map[string]string{},
map[string]interface{}{
"a": int64(1),
"b": int64(2),
},
time.Unix(0, 1),
),
metric.New(
"exec",
map[string]string{},
map[string]interface{}{
"a": int64(3),
"b": int64(4),
},
time.Unix(0, 2),
),
metric.New(
"exec",
map[string]string{},
map[string]interface{}{
"a": int64(1),
"b": int64(2),
},
time.Unix(0, 3),
),
metric.New(
"exec",
map[string]string{},
map[string]interface{}{
"a": int64(3),
"b": int64(4),
},
time.Unix(0, 4),
),
}
var acc testutil.Accumulator
// Run gather once
require.NoError(t, plugin.Gather(&acc))
// Run gather a second time
require.NoError(t, plugin.Gather(&acc))
require.Eventuallyf(t, func() bool {
acc.Lock()
defer acc.Unlock()
return acc.NMetrics() >= uint64(len(expected))
}, time.Second, 100*time.Millisecond, "Expected %d metrics found %d", len(expected), acc.NMetrics())
// Check the result
options := []cmp.Option{
testutil.SortMetrics(),
testutil.IgnoreTime(),
}
actual := acc.GetTelegrafMetrics()
testutil.RequireMetricsEqual(t, expected, actual, options...)
}
func TestCases(t *testing.T) {
// Register the plugin
inputs.Add("exec", func() telegraf.Input {
return &Exec{
Timeout: config.Duration(5 * time.Second),
Log: testutil.Logger{},
}
})
// Setup the plugin
cfg := config.NewConfig()
require.NoError(t, cfg.LoadConfigData([]byte(`
[[inputs.exec]]
commands = [ "echo \"a,b\n1,2\n3,4\"" ]
data_format = "csv"
csv_header_row_count = 1
`), config.EmptySourcePath))
require.Len(t, cfg.Inputs, 1)
plugin := cfg.Inputs[0]
require.NoError(t, plugin.Init())
expected := []telegraf.Metric{
metric.New(
"exec",
map[string]string{},
map[string]interface{}{
"a": int64(1),
"b": int64(2),
},
time.Unix(0, 1),
),
metric.New(
"exec",
map[string]string{},
map[string]interface{}{
"a": int64(3),
"b": int64(4),
},
time.Unix(0, 2),
),
metric.New(
"exec",
map[string]string{},
map[string]interface{}{
"a": int64(1),
"b": int64(2),
},
time.Unix(0, 3),
),
metric.New(
"exec",
map[string]string{},
map[string]interface{}{
"a": int64(3),
"b": int64(4),
},
time.Unix(0, 4),
),
}
var acc testutil.Accumulator
// Run gather once
require.NoError(t, plugin.Gather(&acc))
// Run gather a second time
require.NoError(t, plugin.Gather(&acc))
require.Eventuallyf(t, func() bool {
acc.Lock()
defer acc.Unlock()
return acc.NMetrics() >= uint64(len(expected))
}, time.Second, 100*time.Millisecond, "Expected %d metrics found %d", len(expected), acc.NMetrics())
// Check the result
options := []cmp.Option{
testutil.SortMetrics(),
testutil.IgnoreTime(),
}
actual := acc.GetTelegrafMetrics()
testutil.RequireMetricsEqual(t, expected, actual, options...)
}

View file

@ -0,0 +1,41 @@
//go:build !windows
package exec
import (
"bytes"
"fmt"
"os"
"os/exec"
"syscall"
"github.com/kballard/go-shellquote"
"github.com/influxdata/telegraf/internal"
)
func (c *commandRunner) run(command string) (out, errout []byte, err error) {
splitCmd, err := shellquote.Split(command)
if err != nil || len(splitCmd) == 0 {
return nil, nil, fmt.Errorf("exec: unable to parse command %q: %w", command, err)
}
cmd := exec.Command(splitCmd[0], splitCmd[1:]...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
if len(c.environment) > 0 {
cmd.Env = append(os.Environ(), c.environment...)
}
var outbuf, stderr bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &stderr
runErr := internal.RunTimeout(cmd, c.timeout)
if stderr.Len() > 0 && !c.debug {
truncate(&stderr)
}
return outbuf.Bytes(), stderr.Bytes(), runErr
}

View file

@ -0,0 +1,61 @@
//go:build windows
package exec
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"syscall"
"github.com/kballard/go-shellquote"
"github.com/influxdata/telegraf/internal"
)
func (c *commandRunner) run(command string) (out, errout []byte, err error) {
splitCmd, err := shellquote.Split(command)
if err != nil || len(splitCmd) == 0 {
return nil, nil, fmt.Errorf("exec: unable to parse command: %w", err)
}
cmd := exec.Command(splitCmd[0], splitCmd[1:]...)
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
}
if len(c.environment) > 0 {
cmd.Env = append(os.Environ(), c.environment...)
}
var outbuf, stderr bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &stderr
runErr := internal.RunTimeout(cmd, c.timeout)
outbuf = removeWindowsCarriageReturns(outbuf)
stderr = removeWindowsCarriageReturns(stderr)
if stderr.Len() > 0 && !c.debug {
truncate(&stderr)
}
return outbuf.Bytes(), stderr.Bytes(), runErr
}
func removeWindowsCarriageReturns(b bytes.Buffer) bytes.Buffer {
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
}
}
}

View file

@ -0,0 +1,30 @@
# Read metrics from one or more commands that can output to stdout
[[inputs.exec]]
## Commands array
commands = []
## 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 each command to complete.
# timeout = "5s"
## Measurement name suffix
## Used for separating different commands
# name_suffix = ""
## Ignore Error Code
## If set to true, a non-zero error code in not considered an error and the
## plugin will continue to parse the output.
# ignore_error = false
## Data format
## By default, exec expects JSON. This was done for historical reasons and is
## different than other inputs that use the influx line protocol. 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_INPUT.md
# data_format = "json"