2015-08-06 00:29:27 +00:00
|
|
|
package exec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2016-02-03 03:25:01 +00:00
|
|
|
"sync"
|
2015-12-14 22:15:51 +00:00
|
|
|
|
|
|
|
"github.com/gonuts/go-shellquote"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-02-03 03:25:01 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/encoding"
|
|
|
|
"github.com/influxdata/telegraf/internal/encoding/graphite"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-08-06 00:29:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const sampleConfig = `
|
2016-02-03 03:25:01 +00:00
|
|
|
# Shell/commands array
|
|
|
|
commands = ["/tmp/test.sh","/tmp/test2.sh"]
|
2015-08-06 00:29:27 +00:00
|
|
|
|
2016-02-03 03:25:01 +00:00
|
|
|
# Data format to consume. This can be "json", "influx" or "graphite" (line-protocol)
|
2016-01-29 18:30:35 +00:00
|
|
|
# NOTE json only reads numerical measurements, strings and booleans are ignored.
|
|
|
|
data_format = "json"
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
# measurement name suffix (for separating different commands)
|
|
|
|
name_suffix = "_mycollector"
|
2016-02-03 03:25:01 +00:00
|
|
|
|
|
|
|
### Below configuration will be used for data_format = "graphite", can be ignored for other data_format
|
|
|
|
### If matching multiple measurement files, this string will be used to join the matched values.
|
|
|
|
separator = "."
|
|
|
|
|
|
|
|
### Default tags that will be added to all metrics. These can be overridden at the template level
|
|
|
|
### or by tags extracted from metric
|
|
|
|
tags = ["region=north-east", "zone=1c"]
|
|
|
|
|
|
|
|
### Each template line requires a template pattern. It can have an optional
|
|
|
|
### filter before the template and separated by spaces. It can also have optional extra
|
|
|
|
### tags following the template. Multiple tags should be separated by commas and no spaces
|
|
|
|
### similar to the line protocol format. The can be only one default template.
|
|
|
|
### Templates support below format:
|
|
|
|
### filter + template
|
|
|
|
### filter + template + extra tag
|
|
|
|
### filter + template with field key
|
|
|
|
### default template. Ignore the first graphite component "servers"
|
|
|
|
templates = [
|
|
|
|
"*.app env.service.resource.measurement",
|
|
|
|
"stats.* .host.measurement* region=us-west,agent=sensu",
|
|
|
|
"stats2.* .host.measurement.field",
|
|
|
|
"measurement*"
|
|
|
|
]
|
2015-09-23 18:21:42 +00:00
|
|
|
`
|
2015-08-06 00:29:27 +00:00
|
|
|
|
|
|
|
type Exec struct {
|
2016-02-03 03:25:01 +00:00
|
|
|
Commands []string
|
2016-01-29 18:30:35 +00:00
|
|
|
DataFormat string
|
2015-08-06 00:29:27 +00:00
|
|
|
|
2016-02-03 03:25:01 +00:00
|
|
|
Separator string
|
|
|
|
Tags []string
|
|
|
|
Templates []string
|
|
|
|
|
|
|
|
encodingParser *encoding.Parser
|
|
|
|
|
|
|
|
config *Config
|
|
|
|
|
|
|
|
initedConfig bool
|
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
sync.Mutex
|
|
|
|
|
2015-12-19 22:29:07 +00:00
|
|
|
runner Runner
|
2016-02-03 03:25:01 +00:00
|
|
|
errc chan error
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
type Runner interface {
|
2016-02-03 03:25:01 +00:00
|
|
|
Run(*Exec, string) ([]byte, error)
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
type CommandRunner struct{}
|
|
|
|
|
2016-02-03 03:25:01 +00:00
|
|
|
func (c CommandRunner) Run(e *Exec, command string) ([]byte, error) {
|
|
|
|
split_cmd, err := shellquote.Split(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:]...)
|
2016-02-03 03:25:01 +00:00
|
|
|
//name := strings.Replace(filepath.Base(cmd.Path), "/", "_", -1)
|
|
|
|
//name = strings.Replace(name, ".", "_", -1)
|
|
|
|
|
2015-08-06 00:29:27 +00:00
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
2016-02-03 03:25:01 +00:00
|
|
|
return nil, fmt.Errorf("exec: %s for command '%s'", err, 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
|
|
|
}
|
|
|
|
|
2016-02-03 03:25:01 +00:00
|
|
|
func (e *Exec) ProcessCommand(command string, acc telegraf.Accumulator) {
|
|
|
|
defer e.wg.Done()
|
|
|
|
|
|
|
|
out, err := e.runner.Run(e, command)
|
|
|
|
if err != nil {
|
|
|
|
e.errc <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = e.encodingParser.Parse(e.DataFormat, out, acc); err != nil {
|
|
|
|
e.errc <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exec) initConfig() error {
|
|
|
|
e.Lock()
|
|
|
|
defer e.Unlock()
|
|
|
|
|
|
|
|
c := NewConfig(e.Commands, e.Tags, e.Templates, e.Separator)
|
|
|
|
c.WithDefaults()
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("exec configuration is error! ", err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
e.config = c
|
|
|
|
|
|
|
|
graphiteParser, err := graphite.NewParserWithOptions(graphite.Options{
|
|
|
|
Templates: e.config.Templates,
|
|
|
|
DefaultTags: e.config.DefaultTags(),
|
|
|
|
Separator: e.config.Separator})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("exec input parser config is error! ", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
e.encodingParser = encoding.NewParser(graphiteParser)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-06 00:29:27 +00:00
|
|
|
func (e *Exec) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exec) Description() string {
|
2016-02-03 03:25:01 +00:00
|
|
|
return "Read metrics from one or more commands that output graphite line protocol to stdout"
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (e *Exec) Gather(acc telegraf.Accumulator) error {
|
2016-01-29 18:30:35 +00:00
|
|
|
|
2016-02-03 03:25:01 +00:00
|
|
|
if !e.initedConfig {
|
|
|
|
if err := e.initConfig(); err != nil {
|
2016-01-29 18:30:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-03 03:25:01 +00:00
|
|
|
e.initedConfig = true
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Lock()
|
|
|
|
e.errc = make(chan error, 10)
|
|
|
|
e.Unlock()
|
|
|
|
|
|
|
|
for _, command := range e.Commands {
|
|
|
|
e.wg.Add(1)
|
|
|
|
go e.ProcessCommand(command, acc)
|
|
|
|
}
|
|
|
|
e.wg.Wait()
|
|
|
|
|
|
|
|
select {
|
2016-01-29 18:30:35 +00:00
|
|
|
default:
|
2016-02-03 03:25:01 +00:00
|
|
|
close(e.errc)
|
|
|
|
return nil
|
|
|
|
case err := <-e.errc:
|
|
|
|
close(e.errc)
|
|
|
|
return err
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
2016-02-03 03:25:01 +00:00
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
}
|