forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execd.go
129 lines (104 loc) · 3.02 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
123
124
125
126
127
128
129
package execd
import (
"bufio"
"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"
)
const sampleConfig = `
## Program to run as daemon
command = ["my-telegraf-output", "--some-flag", "value"]
## Delay before the process is restarted after an unexpected termination
restart_delay = "10s"
## Data format to export.
## 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"
`
type Execd struct {
Command []string `toml:"command"`
RestartDelay config.Duration `toml:"restart_delay"`
Log telegraf.Logger
process *process.Process
serializer serializers.Serializer
}
func (e *Execd) SampleConfig() string {
return sampleConfig
}
func (e *Execd) Description() string {
return "Run executable as long-running output plugin"
}
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)
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 {
return fmt.Errorf("error serializing metrics: %s", err)
}
if _, err = e.process.Stdin.Write(b); err != nil {
return fmt.Errorf("error writing metrics %s", 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{}
})
}