forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
151 lines (123 loc) · 3.36 KB
/
exec.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
package exec
import (
"bytes"
"fmt"
"io"
"log"
"os/exec"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/outputs"
"github.com/influxdata/telegraf/plugins/serializers"
)
const maxStderrBytes = 512
// Exec defines the exec output plugin.
type Exec struct {
Command []string `toml:"command"`
Timeout internal.Duration `toml:"timeout"`
runner Runner
serializer serializers.Serializer
}
var sampleConfig = `
## Command to ingest metrics via stdin.
command = ["tee", "-a", "/dev/null"]
## Timeout for command to complete.
# timeout = "5s"
## 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"
`
// SetSerializer sets the serializer for the output.
func (e *Exec) SetSerializer(serializer serializers.Serializer) {
e.serializer = serializer
}
// Connect satisfies the Output interface.
func (e *Exec) Connect() error {
return nil
}
// Close satisfies the Output interface.
func (e *Exec) Close() error {
return nil
}
// Description describes the plugin.
func (e *Exec) Description() string {
return "Send metrics to command as input over stdin"
}
// SampleConfig returns a sample configuration.
func (e *Exec) SampleConfig() string {
return sampleConfig
}
// Write writes the metrics to the configured command.
func (e *Exec) Write(metrics []telegraf.Metric) error {
var buffer bytes.Buffer
serializedMetrics, err := e.serializer.SerializeBatch(metrics)
if err != nil {
return err
}
buffer.Write(serializedMetrics)
if buffer.Len() <= 0 {
return nil
}
return e.runner.Run(e.Timeout.Duration, e.Command, &buffer)
}
// Runner provides an interface for running exec.Cmd.
type Runner interface {
Run(time.Duration, []string, io.Reader) error
}
// CommandRunner runs a command with the ability to kill the process before the timeout.
type CommandRunner struct {
cmd *exec.Cmd
}
// Run runs the command.
func (c *CommandRunner) Run(timeout time.Duration, command []string, buffer io.Reader) error {
cmd := exec.Command(command[0], command[1:]...)
cmd.Stdin = buffer
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := internal.RunTimeout(cmd, timeout)
s := stderr
if err != nil {
if err == internal.TimeoutErr {
return fmt.Errorf("%q timed out and was killed", command)
}
if s.Len() > 0 {
log.Printf("E! [outputs.exec] Command error: %q", truncate(s))
}
if status, ok := internal.ExitStatus(err); ok {
return fmt.Errorf("%q exited %d with %s", command, status, err.Error())
}
return fmt.Errorf("%q failed with %s", command, err.Error())
}
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{
runner: &CommandRunner{},
Timeout: internal.Duration{Duration: time.Second * 5},
}
})
}