exec plugin: allow using glob pattern in command list
Allow using glob pattern in the command list in configuration. This enables for example placing all commands in a single directory and using /path/to/dir/*.sh as one of the commands to run all shell scripts in that directory. Glob patterns are applied on every run of the commands, so matching commands can be added without restarting telegraf. closes #1127
This commit is contained in:
parent
85ee66efb9
commit
6381efa7ce
|
@ -68,6 +68,9 @@ based on _prefix_ in addition to globs. This means that a filter like
|
||||||
- [#1107](https://github.com/influxdata/telegraf/issues/1107): Support lustre2 job stats. Thanks @hanleyja!
|
- [#1107](https://github.com/influxdata/telegraf/issues/1107): Support lustre2 job stats. Thanks @hanleyja!
|
||||||
- [#1122](https://github.com/influxdata/telegraf/pull/1122): Support setting config path through env variable and default paths.
|
- [#1122](https://github.com/influxdata/telegraf/pull/1122): Support setting config path through env variable and default paths.
|
||||||
- [#1128](https://github.com/influxdata/telegraf/pull/1128): MongoDB jumbo chunks metric for MongoDB input plugin
|
- [#1128](https://github.com/influxdata/telegraf/pull/1128): MongoDB jumbo chunks metric for MongoDB input plugin
|
||||||
|
- [#1110](https://github.com/influxdata/telegraf/pull/1110): Sanitize * to - in graphite serializer. Thanks @goodeggs!
|
||||||
|
- [#1118](https://github.com/influxdata/telegraf/pull/1118): Sanitize Counter names for `win_perf_counters` input.
|
||||||
|
- [#1127](https://github.com/influxdata/telegraf/pull/1127): Support for glob patterns in exec plugin commands configuration.
|
||||||
|
|
||||||
### Bugfixes
|
### Bugfixes
|
||||||
|
|
||||||
|
|
|
@ -6,22 +6,32 @@ Please also see: [Telegraf Input Data Formats](https://github.com/influxdata/tel
|
||||||
|
|
||||||
#### Configuration
|
#### Configuration
|
||||||
|
|
||||||
In this example a script called ```/tmp/test.sh``` and a script called ```/tmp/test2.sh```
|
In this example a script called ```/tmp/test.sh```, a script called ```/tmp/test2.sh```, and
|
||||||
are configured for ```[[inputs.exec]]``` in JSON format.
|
all scripts matching glob pattern ```/tmp/collect_*.sh``` are configured for ```[[inputs.exec]]```
|
||||||
|
in JSON format. Glob patterns are matched on every run, so adding new scripts that match the pattern
|
||||||
|
will cause them to be picked up immediately.
|
||||||
|
|
||||||
```
|
```
|
||||||
# Read flattened metrics from one or more commands that output JSON to stdout
|
# Read flattened metrics from one or more commands that output JSON to stdout
|
||||||
[[inputs.exec]]
|
[[inputs.exec]]
|
||||||
# Shell/commands array
|
## Full path to executable with parameters,
|
||||||
commands = ["/tmp/test.sh", "/tmp/test2.sh"]
|
## or a glob pattern to run all matching files.
|
||||||
|
## the glob pattern will be run at every interval, so new files will
|
||||||
|
## automatically be picked up.
|
||||||
|
commands = ["/tmp/test.sh", "/usr/bin/mycollector --foo=bar", "/tmp/collect_*.sh"]
|
||||||
|
|
||||||
# Data format to consume.
|
## Timeout for each command to complete.
|
||||||
# NOTE json only reads numerical measurements, strings and booleans are ignored.
|
timeout = "5s"
|
||||||
data_format = "json"
|
|
||||||
|
|
||||||
# measurement name suffix (for separating different commands)
|
## measurement name suffix (for separating different commands)
|
||||||
name_suffix = "_mycollector"
|
name_suffix = "_mycollector"
|
||||||
|
|
||||||
|
## Data format to consume.
|
||||||
|
## 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
|
||||||
|
data_format = "json"
|
||||||
|
|
||||||
## Below configuration will be used for data_format = "graphite", can be ignored for other data_format
|
## 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.
|
## If matching multiple measurement files, this string will be used to join the matched values.
|
||||||
#separator = "."
|
#separator = "."
|
||||||
|
@ -180,4 +190,3 @@ sensu.metric.net.server0.eth0.rx_dropped 0 1444234982
|
||||||
The templates configuration will be used to parse the graphite metrics to support influxdb/opentsdb tagging store engines.
|
The templates configuration will be used to parse the graphite metrics to support influxdb/opentsdb tagging store engines.
|
||||||
|
|
||||||
More detail information about templates, please refer to [The graphite Input](https://github.com/influxdata/influxdb/blob/master/services/graphite/README.md)
|
More detail information about templates, please refer to [The graphite Input](https://github.com/influxdata/influxdb/blob/master/services/graphite/README.md)
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
@ -18,8 +19,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const sampleConfig = `
|
const sampleConfig = `
|
||||||
## Commands array
|
## Full path to executable with parameters,
|
||||||
commands = ["/tmp/test.sh", "/usr/bin/mycollector --foo=bar"]
|
## or a glob pattern to run all matching files.
|
||||||
|
## the glob pattern will be run at every interval, so new files will
|
||||||
|
## automatically be picked up.
|
||||||
|
commands = ["/tmp/test.sh", "/usr/bin/collector --foo=bar", "/tmp/collect_*.sh"]
|
||||||
|
|
||||||
## Timeout for each command to complete.
|
## Timeout for each command to complete.
|
||||||
timeout = "5s"
|
timeout = "5s"
|
||||||
|
@ -150,10 +154,19 @@ func (e *Exec) Gather(acc telegraf.Accumulator) error {
|
||||||
e.Command = ""
|
e.Command = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
e.errChan = make(chan error, len(e.Commands))
|
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.wg.Add(len(e.Commands))
|
e.errChan = make(chan error, len(commands))
|
||||||
for _, command := range e.Commands {
|
|
||||||
|
e.wg.Add(len(commands))
|
||||||
|
for _, command := range commands {
|
||||||
go e.ProcessCommand(command, acc)
|
go e.ProcessCommand(command, acc)
|
||||||
}
|
}
|
||||||
e.wg.Wait()
|
e.wg.Wait()
|
||||||
|
|
Loading…
Reference in New Issue