2015-08-06 00:29:27 +00:00
|
|
|
package exec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2016-04-29 13:07:01 +00:00
|
|
|
"path/filepath"
|
2016-08-08 18:45:18 +00:00
|
|
|
"runtime"
|
2016-04-29 13:07:01 +00:00
|
|
|
"strings"
|
2016-02-01 03:43:38 +00:00
|
|
|
"sync"
|
2016-04-29 01:23:45 +00:00
|
|
|
"time"
|
2015-12-14 22:15:51 +00:00
|
|
|
|
2016-11-16 16:24:11 +00:00
|
|
|
"github.com/kballard/go-shellquote"
|
2015-12-14 22:15:51 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-04-29 01:23:45 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2016-02-06 00:36:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers"
|
2016-02-25 04:32:22 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/nagios"
|
2015-08-06 00:29:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const sampleConfig = `
|
2016-05-02 18:07:17 +00:00
|
|
|
## Commands array
|
2016-04-29 13:07:01 +00:00
|
|
|
commands = [
|
|
|
|
"/tmp/test.sh",
|
|
|
|
"/usr/bin/mycollector --foo=bar",
|
|
|
|
"/tmp/collect_*.sh"
|
|
|
|
]
|
2016-01-29 18:30:35 +00:00
|
|
|
|
2016-04-29 01:23:45 +00:00
|
|
|
## Timeout for each command to complete.
|
|
|
|
timeout = "5s"
|
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## measurement name suffix (for separating different commands)
|
2016-01-07 20:39:43 +00:00
|
|
|
name_suffix = "_mycollector"
|
2016-02-01 03:43:38 +00:00
|
|
|
|
2016-03-31 23:50:24 +00:00
|
|
|
## Data format to consume.
|
2017-04-27 21:59:18 +00:00
|
|
|
## Each data format has its own unique set of configuration options, read
|
2016-02-18 21:26:51 +00:00
|
|
|
## more about them here:
|
|
|
|
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
|
2016-02-06 00:36:35 +00:00
|
|
|
data_format = "influx"
|
2015-09-23 18:21:42 +00:00
|
|
|
`
|
2015-08-06 00:29:27 +00:00
|
|
|
|
2018-03-14 19:09:01 +00:00
|
|
|
const MaxStderrBytes = 512
|
|
|
|
|
2015-08-06 00:29:27 +00:00
|
|
|
type Exec struct {
|
2016-02-06 00:36:35 +00:00
|
|
|
Commands []string
|
|
|
|
Command string
|
2016-04-29 01:23:45 +00:00
|
|
|
Timeout internal.Duration
|
2016-02-01 03:43:38 +00:00
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
parser parsers.Parser
|
2016-02-01 03:43:38 +00:00
|
|
|
|
2017-04-24 18:13:26 +00:00
|
|
|
runner Runner
|
2019-08-21 23:49:07 +00:00
|
|
|
log telegraf.Logger
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewExec() *Exec {
|
|
|
|
return &Exec{
|
2016-04-29 01:23:45 +00:00
|
|
|
runner: CommandRunner{},
|
|
|
|
Timeout: internal.Duration{Duration: time.Second * 5},
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
type Runner interface {
|
2019-03-25 23:24:42 +00:00
|
|
|
Run(string, time.Duration) ([]byte, []byte, error)
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
type CommandRunner struct{}
|
|
|
|
|
2016-04-29 01:23:45 +00:00
|
|
|
func (c CommandRunner) Run(
|
|
|
|
command string,
|
2019-03-25 23:24:42 +00:00
|
|
|
timeout time.Duration,
|
|
|
|
) ([]byte, []byte, error) {
|
2016-02-01 03:43:38 +00:00
|
|
|
split_cmd, err := shellquote.Split(command)
|
2015-09-23 18:21:42 +00:00
|
|
|
if err != nil || len(split_cmd) == 0 {
|
2019-03-25 23:24:42 +00:00
|
|
|
return nil, nil, fmt.Errorf("exec: unable to parse command, %s", err)
|
2015-09-23 18:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(split_cmd[0], split_cmd[1:]...)
|
2016-02-01 03:43:38 +00:00
|
|
|
|
2018-03-14 19:09:01 +00:00
|
|
|
var (
|
|
|
|
out bytes.Buffer
|
|
|
|
stderr bytes.Buffer
|
|
|
|
)
|
2015-08-06 00:29:27 +00:00
|
|
|
cmd.Stdout = &out
|
2018-03-14 19:09:01 +00:00
|
|
|
cmd.Stderr = &stderr
|
2015-08-06 00:29:27 +00:00
|
|
|
|
2019-03-25 23:24:42 +00:00
|
|
|
runErr := internal.RunTimeout(cmd, timeout)
|
2015-08-06 00:29:27 +00:00
|
|
|
|
2016-08-08 18:45:18 +00:00
|
|
|
out = removeCarriageReturns(out)
|
2019-03-25 23:24:42 +00:00
|
|
|
if stderr.Len() > 0 {
|
|
|
|
stderr = removeCarriageReturns(stderr)
|
|
|
|
stderr = truncate(stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.Bytes(), stderr.Bytes(), runErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func truncate(buf bytes.Buffer) 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("...")
|
|
|
|
}
|
|
|
|
return buf
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2016-08-08 18:45:18 +00:00
|
|
|
// removeCarriageReturns removes all carriage returns from the input if the
|
|
|
|
// OS is Windows. It does not return any errors.
|
|
|
|
func removeCarriageReturns(b bytes.Buffer) bytes.Buffer {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for {
|
|
|
|
byt, er := b.ReadBytes(0x0D)
|
|
|
|
end := len(byt)
|
|
|
|
if nil == er {
|
|
|
|
end -= 1
|
|
|
|
}
|
|
|
|
if nil != byt {
|
|
|
|
buf.Write(byt[:end])
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if nil != er {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b = buf
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-11 13:58:00 +00:00
|
|
|
func (e *Exec) ProcessCommand(command string, acc telegraf.Accumulator, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
2019-03-25 23:24:42 +00:00
|
|
|
_, isNagios := e.parser.(*nagios.NagiosParser)
|
2016-02-01 03:43:38 +00:00
|
|
|
|
2019-03-25 23:24:42 +00:00
|
|
|
out, errbuf, runErr := e.runner.Run(command, e.Timeout.Duration)
|
|
|
|
if !isNagios && runErr != nil {
|
|
|
|
err := fmt.Errorf("exec: %s for command '%s': %s", runErr, command, string(errbuf))
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(err)
|
2016-02-01 03:43:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
metrics, err := e.parser.Parse(out)
|
2016-02-01 03:43:38 +00:00
|
|
|
if err != nil {
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(err)
|
2019-03-25 23:24:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if isNagios {
|
|
|
|
metrics, err = nagios.TryAddState(runErr, metrics)
|
|
|
|
if err != nil {
|
2019-08-21 23:49:07 +00:00
|
|
|
e.log.Errorf("failed to add nagios state: %s", err)
|
2016-02-01 03:43:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-25 23:24:42 +00:00
|
|
|
|
|
|
|
for _, m := range metrics {
|
|
|
|
acc.AddMetric(m)
|
|
|
|
}
|
2016-02-01 03:43:38 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 00:29:27 +00:00
|
|
|
func (e *Exec) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exec) Description() string {
|
2016-02-06 00:36:35 +00:00
|
|
|
return "Read metrics from one or more commands that can output to stdout"
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
func (e *Exec) SetParser(parser parsers.Parser) {
|
|
|
|
e.parser = parser
|
|
|
|
}
|
2016-02-01 03:43:38 +00:00
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
func (e *Exec) Gather(acc telegraf.Accumulator) error {
|
2016-07-11 13:58:00 +00:00
|
|
|
var wg sync.WaitGroup
|
2016-02-09 18:14:36 +00:00
|
|
|
// Legacy single command support
|
|
|
|
if e.Command != "" {
|
|
|
|
e.Commands = append(e.Commands, e.Command)
|
|
|
|
e.Command = ""
|
|
|
|
}
|
|
|
|
|
2016-04-29 13:07:01 +00:00
|
|
|
commands := make([]string, 0, len(e.Commands))
|
|
|
|
for _, pattern := range e.Commands {
|
|
|
|
cmdAndArgs := strings.SplitN(pattern, " ", 2)
|
|
|
|
if len(cmdAndArgs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
matches, err := filepath.Glob(cmdAndArgs[0])
|
|
|
|
if err != nil {
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(err)
|
|
|
|
continue
|
2016-04-29 13:07:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
// There were no matches with the glob pattern, so let's assume
|
|
|
|
// that 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 {
|
2016-06-10 10:20:50 +00:00
|
|
|
if len(cmdAndArgs) == 1 {
|
|
|
|
commands = append(commands, match)
|
|
|
|
} else {
|
|
|
|
commands = append(commands,
|
|
|
|
strings.Join([]string{match, cmdAndArgs[1]}, " "))
|
|
|
|
}
|
2016-04-29 13:07:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 13:58:00 +00:00
|
|
|
wg.Add(len(commands))
|
2016-04-29 13:07:01 +00:00
|
|
|
for _, command := range commands {
|
2016-07-11 13:58:00 +00:00
|
|
|
go e.ProcessCommand(command, acc, &wg)
|
2016-02-01 03:43:38 +00:00
|
|
|
}
|
2016-07-11 13:58:00 +00:00
|
|
|
wg.Wait()
|
2017-04-24 18:13:26 +00:00
|
|
|
return nil
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 23:49:07 +00:00
|
|
|
func (e *Exec) Init() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-06 00:29:27 +00:00
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("exec", func() telegraf.Input {
|
2015-08-06 00:29:27 +00:00
|
|
|
return NewExec()
|
|
|
|
})
|
|
|
|
}
|