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-02-01 03:43:38 +00:00
|
|
|
"sync"
|
2016-02-25 04:32:22 +00:00
|
|
|
"syscall"
|
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-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-02-18 21:26:51 +00:00
|
|
|
## Commands array
|
2016-02-06 00:36:35 +00:00
|
|
|
commands = ["/tmp/test.sh", "/usr/bin/mycollector --foo=bar"]
|
2016-01-29 18:30:35 +00:00
|
|
|
|
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.
|
2016-02-18 21:26:51 +00:00
|
|
|
## Each data format has it's own unique set of configuration options, read
|
|
|
|
## 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
|
|
|
|
|
|
|
type Exec struct {
|
2016-02-06 00:36:35 +00:00
|
|
|
Commands []string
|
|
|
|
Command string
|
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
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
runner Runner
|
|
|
|
errChan chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExec() *Exec {
|
|
|
|
return &Exec{
|
|
|
|
runner: CommandRunner{},
|
|
|
|
}
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
type Runner interface {
|
2016-02-25 04:32:22 +00:00
|
|
|
Run(*Exec, string, telegraf.Accumulator) ([]byte, error)
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:21:42 +00:00
|
|
|
type CommandRunner struct{}
|
|
|
|
|
2016-02-25 04:32:22 +00:00
|
|
|
func AddNagiosState(exitCode error, acc telegraf.Accumulator) error {
|
|
|
|
nagiosState := 0
|
|
|
|
if exitCode != nil {
|
|
|
|
exiterr, ok := exitCode.(*exec.ExitError)
|
|
|
|
if ok {
|
|
|
|
status, ok := exiterr.Sys().(syscall.WaitStatus)
|
|
|
|
if ok {
|
|
|
|
nagiosState = status.ExitStatus()
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("exec: unable to get nagios plugin exit code")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("exec: unable to get nagios plugin exit code")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fields := map[string]interface{}{"state": nagiosState}
|
|
|
|
acc.AddFields("nagios_state", fields, nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CommandRunner) Run(e *Exec, command string, acc telegraf.Accumulator) ([]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 {
|
|
|
|
return nil, fmt.Errorf("exec: unable to parse command, %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(split_cmd[0], split_cmd[1:]...)
|
2016-02-01 03:43:38 +00:00
|
|
|
|
2015-08-06 00:29:27 +00:00
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
2016-02-25 04:32:22 +00:00
|
|
|
switch e.parser.(type) {
|
|
|
|
case *nagios.NagiosParser:
|
|
|
|
AddNagiosState(err, acc)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("exec: %s for command '%s'", err, command)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch e.parser.(type) {
|
|
|
|
case *nagios.NagiosParser:
|
|
|
|
AddNagiosState(nil, acc)
|
|
|
|
}
|
2015-08-06 00:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2016-02-01 03:43:38 +00:00
|
|
|
func (e *Exec) ProcessCommand(command string, acc telegraf.Accumulator) {
|
|
|
|
defer e.wg.Done()
|
|
|
|
|
2016-02-25 04:32:22 +00:00
|
|
|
out, err := e.runner.Run(e, command, acc)
|
2016-02-01 03:43:38 +00:00
|
|
|
if err != nil {
|
2016-02-06 00:36:35 +00:00
|
|
|
e.errChan <- 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 {
|
2016-02-06 00:36:35 +00:00
|
|
|
e.errChan <- err
|
2016-02-01 03:43:38 +00:00
|
|
|
} else {
|
|
|
|
for _, metric := range metrics {
|
|
|
|
acc.AddFields(metric.Name(), metric.Fields(), metric.Tags(), metric.Time())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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, len(e.Commands))
|
|
|
|
for _, pattern := range e.Commands {
|
|
|
|
matches, err := filepath.Glob(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
commands = append(commands, matches...)
|
|
|
|
}
|
|
|
|
|
|
|
|
e.errChan = make(chan error, len(commands))
|
2016-02-01 03:43:38 +00:00
|
|
|
|
2016-04-29 13:07:01 +00:00
|
|
|
e.wg.Add(len(commands))
|
|
|
|
for _, command := range commands {
|
2016-02-01 03:43:38 +00:00
|
|
|
go e.ProcessCommand(command, acc)
|
|
|
|
}
|
|
|
|
e.wg.Wait()
|
|
|
|
|
|
|
|
select {
|
2016-01-29 18:30:35 +00:00
|
|
|
default:
|
2016-02-06 00:36:35 +00:00
|
|
|
close(e.errChan)
|
2016-02-01 03:43:38 +00:00
|
|
|
return nil
|
2016-02-06 00:36:35 +00:00
|
|
|
case err := <-e.errChan:
|
|
|
|
close(e.errChan)
|
2016-02-01 03:43:38 +00:00
|
|
|
return err
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
2016-02-01 03:43:38 +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()
|
|
|
|
})
|
|
|
|
}
|