forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execd.go
170 lines (142 loc) · 4.21 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package execd
import (
"bufio"
"errors"
"fmt"
"io"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal/process"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/plugins/parsers/influx"
)
const sampleConfig = `
## Program to run as daemon
command = ["telegraf-smartctl", "-d", "/dev/sda"]
## Define how the process is signaled on each collection interval.
## Valid values are:
## "none" : Do not signal anything.
## The process must output metrics by itself.
## "STDIN" : Send a newline on STDIN.
## "SIGHUP" : Send a HUP signal. Not available on Windows.
## "SIGUSR1" : Send a USR1 signal. Not available on Windows.
## "SIGUSR2" : Send a USR2 signal. Not available on Windows.
signal = "none"
## Delay before the process is restarted after an unexpected termination
restart_delay = "10s"
## Data format to consume.
## 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 = "influx"
`
type Execd struct {
Command []string `toml:"command"`
Signal string `toml:"signal"`
RestartDelay config.Duration `toml:"restart_delay"`
Log telegraf.Logger `toml:"-"`
process *process.Process
acc telegraf.Accumulator
parser parsers.Parser
}
func (e *Execd) SampleConfig() string {
return sampleConfig
}
func (e *Execd) Description() string {
return "Run executable as long-running input plugin"
}
func (e *Execd) SetParser(parser parsers.Parser) {
e.parser = parser
}
func (e *Execd) Start(acc telegraf.Accumulator) error {
e.acc = acc
var err error
e.process, err = process.New(e.Command)
if err != nil {
return fmt.Errorf("error creating new process: %w", err)
}
e.process.Log = e.Log
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 inputs.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) Stop() {
e.process.Stop()
}
func (e *Execd) cmdReadOut(out io.Reader) {
if _, isInfluxParser := e.parser.(*influx.Parser); isInfluxParser {
// work around the lack of built-in streaming parser. :(
e.cmdReadOutStream(out)
return
}
scanner := bufio.NewScanner(out)
for scanner.Scan() {
metrics, err := e.parser.Parse(scanner.Bytes())
if err != nil {
e.acc.AddError(fmt.Errorf("parse error: %w", err))
}
for _, metric := range metrics {
e.acc.AddMetric(metric)
}
}
if err := scanner.Err(); err != nil {
e.acc.AddError(fmt.Errorf("error reading stdout: %w", err))
}
}
func (e *Execd) cmdReadOutStream(out io.Reader) {
parser := influx.NewStreamParser(out)
for {
metric, err := parser.Next()
if err != nil {
if err == influx.EOF {
break // stream ended
}
if parseErr, isParseError := err.(*influx.ParseError); isParseError {
// parse error.
e.acc.AddError(parseErr)
continue
}
// some non-recoverable error?
e.acc.AddError(err)
return
}
e.acc.AddMetric(metric)
}
}
func (e *Execd) cmdReadErr(out io.Reader) {
scanner := bufio.NewScanner(out)
for scanner.Scan() {
e.Log.Errorf("stderr: %q", scanner.Text())
}
if err := scanner.Err(); err != nil {
e.acc.AddError(fmt.Errorf("error reading stderr: %w", err))
}
}
func (e *Execd) Init() error {
if len(e.Command) == 0 {
return errors.New("no command specified")
}
return nil
}
func init() {
inputs.Add("execd", func() telegraf.Input {
return &Execd{
Signal: "none",
RestartDelay: config.Duration(10 * time.Second),
}
})
}