2015-08-06 00:29:27 +00:00
|
|
|
package exec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-10-16 22:13:32 +00:00
|
|
|
"errors"
|
2015-08-06 00:29:27 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/gonuts/go-shellquote"
|
|
|
|
"github.com/influxdb/telegraf/plugins"
|
2015-09-23 18:21:42 +00:00
|
|
|
"math"
|
2015-08-06 00:29:27 +00:00
|
|
|
"os/exec"
|
2015-10-16 22:13:32 +00:00
|
|
|
"strings"
|
2015-08-06 00:29:27 +00:00
|
|
|
"sync"
|
2015-09-23 18:21:42 +00:00
|
|
|
"time"
|
2015-08-06 00:29:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const sampleConfig = `
|
2015-10-15 21:53:29 +00:00
|
|
|
# specify commands via an array of tables
|
|
|
|
[[exec.commands]]
|
|
|
|
# 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-08-06 00:29:27 +00:00
|
|
|
|
2015-10-15 21:53:29 +00:00
|
|
|
# Only run this command if it has been at least this many
|
|
|
|
# seconds since it last ran
|
|
|
|
interval = 10
|
2015-09-23 18:21:42 +00:00
|
|
|
`
|
2015-08-06 00:29:27 +00:00
|
|
|
|
|
|
|
type Exec struct {
|
|
|
|
Commands []*Command
|
|
|
|
runner Runner
|
2015-09-23 18:21:42 +00:00
|
|
|
clock Clock
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
type Command struct {
|
|
|
|
Command string
|
|
|
|
Name string
|
|
|
|
Interval int
|
|
|
|
lastRunAt time.Time
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
type Runner interface {
|
|
|
|
Run(*Command) ([]byte, error)
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
type Clock interface {
|
|
|
|
Now() time.Time
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
type CommandRunner struct{}
|
|
|
|
|
|
|
|
type RealClock struct{}
|
|
|
|
|
|
|
|
func (c CommandRunner) Run(command *Command) ([]byte, error) {
|
|
|
|
command.lastRunAt = time.Now()
|
|
|
|
split_cmd, err := shellquote.Split(command.Command)
|
|
|
|
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-09-23 21:45:06 +00:00
|
|
|
return nil, fmt.Errorf("exec: %s for command '%s'", err, command.Command)
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
func (c RealClock) Now() time.Time {
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExec() *Exec {
|
|
|
|
return &Exec{runner: CommandRunner{}, clock: RealClock{}}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
errorChannel := make(chan error, len(e.Commands))
|
2015-08-06 00:29:27 +00:00
|
|
|
|
|
|
|
for _, c := range e.Commands {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(c *Command, acc plugins.Accumulator) {
|
|
|
|
defer wg.Done()
|
2015-10-16 22:13:32 +00:00
|
|
|
err := e.gatherCommand(c, acc)
|
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
}
|
2015-08-06 00:29:27 +00:00
|
|
|
}(c, acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2015-10-16 22:13:32 +00:00
|
|
|
close(errorChannel)
|
2015-08-06 00:29:27 +00:00
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
// Get all errors and return them as one giant error
|
|
|
|
errorStrings := []string{}
|
|
|
|
for err := range errorChannel {
|
|
|
|
errorStrings = append(errorStrings, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errorStrings) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New(strings.Join(errorStrings, "\n"))
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exec) gatherCommand(c *Command, acc plugins.Accumulator) error {
|
2015-09-23 18:21:42 +00:00
|
|
|
secondsSinceLastRun := 0.0
|
2015-08-06 00:29:27 +00:00
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
if c.lastRunAt.Unix() == 0 { // means time is uninitialized
|
|
|
|
secondsSinceLastRun = math.Inf(1)
|
|
|
|
} else {
|
|
|
|
secondsSinceLastRun = (e.clock.Now().Sub(c.lastRunAt)).Seconds()
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
if secondsSinceLastRun >= float64(c.Interval) {
|
|
|
|
out, err := e.runner.Run(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-06 00:29:27 +00:00
|
|
|
|
2015-09-23 18:21:42 +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", c.Command, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
processResponse(acc, c.Name, map[string]string{}, jsonOut)
|
|
|
|
}
|
2015-08-14 21:41:01 +00:00
|
|
|
return nil
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 21:41:01 +00:00
|
|
|
func processResponse(acc plugins.Accumulator, prefix string, tags map[string]string, v interface{}) {
|
2015-08-06 00:29:27 +00:00
|
|
|
switch t := v.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
for k, v := range t {
|
2015-08-14 21:41:01 +00:00
|
|
|
processResponse(acc, prefix+"_"+k, tags, v)
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
2015-08-06 19:01:42 +00:00
|
|
|
case float64:
|
|
|
|
acc.Add(prefix, v, tags)
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
plugins.Add("exec", func() plugins.Plugin {
|
|
|
|
return NewExec()
|
|
|
|
})
|
|
|
|
}
|