2015-08-06 00:29:27 +00:00
|
|
|
package exec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2015-12-14 22:15:51 +00:00
|
|
|
|
|
|
|
"github.com/gonuts/go-shellquote"
|
|
|
|
|
|
|
|
"github.com/influxdb/telegraf/internal"
|
|
|
|
"github.com/influxdb/telegraf/plugins"
|
2015-08-06 00:29:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const sampleConfig = `
|
2015-10-15 21:53:29 +00:00
|
|
|
# the command to run
|
|
|
|
command = "/usr/bin/mycollector --foo=bar"
|
2015-08-06 00:29:27 +00:00
|
|
|
|
2015-10-15 21:53:29 +00:00
|
|
|
# name of the command (used as a prefix for measurements)
|
|
|
|
name = "mycollector"
|
2015-09-23 18:21:42 +00:00
|
|
|
`
|
2015-08-06 00:29:27 +00:00
|
|
|
|
|
|
|
type Exec struct {
|
2015-12-19 22:29:07 +00:00
|
|
|
Command string
|
|
|
|
Name string
|
2015-08-06 00:29:27 +00:00
|
|
|
|
2015-12-19 22:29:07 +00:00
|
|
|
runner Runner
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
type Runner interface {
|
2015-12-19 22:29:07 +00:00
|
|
|
Run(*Exec) ([]byte, error)
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
type CommandRunner struct{}
|
|
|
|
|
2015-12-19 22:29:07 +00:00
|
|
|
func (c CommandRunner) Run(e *Exec) ([]byte, error) {
|
|
|
|
split_cmd, err := shellquote.Split(e.Command)
|
2015-09-23 18:21:42 +00:00
|
|
|
if err != nil || len(split_cmd) == 0 {
|
|
|
|
return nil, fmt.Errorf("exec: unable to parse command, %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(split_cmd[0], split_cmd[1:]...)
|
2015-08-06 00:29:27 +00:00
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
2015-12-19 22:29:07 +00:00
|
|
|
return nil, fmt.Errorf("exec: %s for command '%s'", err, e.Command)
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
func NewExec() *Exec {
|
2015-12-19 22:29:07 +00:00
|
|
|
return &Exec{runner: CommandRunner{}}
|
2015-09-23 18:21:42 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 00:29:27 +00:00
|
|
|
func (e *Exec) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exec) Description() string {
|
|
|
|
return "Read flattened metrics from one or more commands that output JSON to stdout"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exec) Gather(acc plugins.Accumulator) error {
|
2015-12-19 22:29:07 +00:00
|
|
|
out, err := e.runner.Run(e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 22:29:07 +00:00
|
|
|
var jsonOut interface{}
|
|
|
|
err = json.Unmarshal(out, &jsonOut)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("exec: unable to parse output of '%s' as JSON, %s",
|
|
|
|
e.Command, err)
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 22:29:07 +00:00
|
|
|
f := internal.JSONFlattener{}
|
|
|
|
err = f.FlattenJSON("", jsonOut)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
2015-08-06 00:29:27 +00:00
|
|
|
|
2015-12-19 22:29:07 +00:00
|
|
|
var msrmnt_name string
|
|
|
|
if e.Name == "" {
|
|
|
|
msrmnt_name = "exec"
|
2015-09-23 18:21:42 +00:00
|
|
|
} else {
|
2015-12-19 22:29:07 +00:00
|
|
|
msrmnt_name = "exec_" + e.Name
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
2015-12-19 22:29:07 +00:00
|
|
|
acc.AddFields(msrmnt_name, f.Fields, nil)
|
2015-12-14 22:15:51 +00:00
|
|
|
return nil
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
plugins.Add("exec", func() plugins.Plugin {
|
|
|
|
return NewExec()
|
|
|
|
})
|
|
|
|
}
|