forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 4
/
execd.go
122 lines (100 loc) · 2.91 KB
/
execd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//go:generate ../../../tools/readme_config_includer/generator
package execd
import (
"bufio"
_ "embed"
"fmt"
"io"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal/process"
"github.com/influxdata/telegraf/plugins/outputs"
"github.com/influxdata/telegraf/plugins/serializers"
)
//go:embed sample.conf
var sampleConfig string
type Execd struct {
Command []string `toml:"command"`
Environment []string `toml:"environment"`
RestartDelay config.Duration `toml:"restart_delay"`
IgnoreSerializationError bool `toml:"ignore_serialization_error"`
Log telegraf.Logger
process *process.Process
serializer serializers.Serializer
}
func (*Execd) SampleConfig() string {
return sampleConfig
}
func (e *Execd) SetSerializer(s serializers.Serializer) {
e.serializer = s
}
func (e *Execd) Init() error {
if len(e.Command) == 0 {
return fmt.Errorf("no command specified")
}
var err error
e.process, err = process.New(e.Command, e.Environment)
if err != nil {
return fmt.Errorf("error creating process %s: %w", e.Command, err)
}
e.process.Log = e.Log
e.process.RestartDelay = time.Duration(e.RestartDelay)
e.process.ReadStdoutFn = e.cmdReadOut
e.process.ReadStderrFn = e.cmdReadErr
return nil
}
func (e *Execd) Connect() error {
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 outputs.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.")
}
return fmt.Errorf("failed to start process %s: %w", e.Command, err)
}
return nil
}
func (e *Execd) Close() error {
e.process.Stop()
return nil
}
func (e *Execd) Write(metrics []telegraf.Metric) error {
for _, m := range metrics {
b, err := e.serializer.Serialize(m)
if err != nil {
if !e.IgnoreSerializationError {
return fmt.Errorf("error serializing metrics: %w", err)
}
e.Log.Errorf("Skipping metric due to a serialization error: %v", err)
continue
}
if _, err = e.process.Stdin.Write(b); err != nil {
return fmt.Errorf("error writing metrics: %w", err)
}
}
return nil
}
func (e *Execd) cmdReadErr(out io.Reader) {
scanner := bufio.NewScanner(out)
for scanner.Scan() {
e.Log.Errorf("stderr: %s", scanner.Text())
}
if err := scanner.Err(); err != nil {
e.Log.Errorf("Error reading stderr: %s", err)
}
}
func (e *Execd) cmdReadOut(out io.Reader) {
scanner := bufio.NewScanner(out)
for scanner.Scan() {
e.Log.Info(scanner.Text())
}
}
func init() {
outputs.Add("execd", func() telegraf.Output {
return &Execd{}
})
}