-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Plugin/reader each interval #4332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 22 commits
e12eced
08a11d7
9c4b522
4e24a1b
ec7f131
504d978
542c030
554b960
36a23ea
f40371e
9c84595
cc40629
79d9ea4
bbd68b3
bf7220d
a931eb1
e450b26
001658a
7fa27f4
1be2a8e
aa750ec
892c95a
04f09d6
8063b38
bfc13a7
67db143
8a9da28
cafa95e
c6087ab
e4b6f23
d224673
f52ceeb
285cf0b
0c3ac29
74900ed
d0f5389
dd778a9
5449eb7
1f58dd7
2a18ca2
50f49fe
08d1397
63da4e6
88a85a7
2638186
1fe3adb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Reader Input Plugin | ||
|
|
||
| The Reader Plugin updates a list of files every interval and parses the data inside. | ||
| Files will always be read from the beginning. | ||
| This plugin can parse any "data_format" formats. | ||
|
|
||
| ### Configuration: | ||
| ```toml | ||
| [[inputs.reader]] | ||
| ## Files to parse each interval. | ||
| ## These accept standard unix glob matching rules, but with the addition of | ||
| ## ** as a "super asterisk". ie: | ||
| ## /var/log/**.log -> recursively find all .log files in /var/log | ||
| ## /var/log/*/*.log -> find all .log files with a parent dir in /var/log | ||
| ## /var/log/apache.log -> only tail the apache log file | ||
| files = ["/var/log/apache/access.log"] | ||
|
|
||
| ## The dataformat to be read from files | ||
| ## Each data format has its 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 = "" | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| version: '3' | ||
|
|
||
| services: | ||
| telegraf: | ||
| image: glinton/scratch | ||
| volumes: | ||
| - ./telegraf.conf:/telegraf.conf | ||
| - ../../../../telegraf:/telegraf | ||
| - ./json_a.log:/var/log/test.log | ||
| entrypoint: | ||
| - /telegraf | ||
| - --config | ||
| - /telegraf.conf |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "parent": { | ||
| "child": 3.0, | ||
| "ignored_child": "hi" | ||
| }, | ||
| "ignored_null": null, | ||
| "integer": 4, | ||
| "list": [3, 4], | ||
| "ignored_parent": { | ||
| "another_ignored_null": null, | ||
| "ignored_string": "hello, world!" | ||
| }, | ||
| "another_list": [4] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [[inputs.reader]] | ||
| files = ["/var/log/test.log"] | ||
| data_format = "json" | ||
| name_override = "json_reader" | ||
|
|
||
| [[outputs.file]] | ||
| files = ["stdout"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package reader | ||
|
|
||
| import ( | ||
| "io/ioutil" | ||
| "log" | ||
|
|
||
| "github.com/influxdata/telegraf" | ||
| "github.com/influxdata/telegraf/internal/globpath" | ||
| "github.com/influxdata/telegraf/plugins/inputs" | ||
| "github.com/influxdata/telegraf/plugins/parsers" | ||
| ) | ||
|
|
||
| type Reader struct { | ||
| Filepaths []string `toml:"files"` | ||
|
||
| FromBeginning bool | ||
| parser parsers.Parser | ||
|
|
||
| Filenames []string | ||
|
||
| } | ||
|
|
||
| const sampleConfig = `## Files to parse each interval. | ||
| ## These accept standard unix glob matching rules, but with the addition of | ||
| ## ** as a "super asterisk". ie: | ||
| ## /var/log/**.log -> recursively find all .log files in /var/log | ||
| ## /var/log/*/*.log -> find all .log files with a parent dir in /var/log | ||
| ## /var/log/apache.log -> only tail the apache log file | ||
| files = ["/var/log/apache/access.log"] | ||
|
|
||
| ## The dataformat to be read from files | ||
| ## Each data format has its 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 = "" | ||
|
||
| ` | ||
|
|
||
| // SampleConfig returns the default configuration of the Input | ||
| func (r *Reader) SampleConfig() string { | ||
| return sampleConfig | ||
| } | ||
|
|
||
| func (r *Reader) Description() string { | ||
| return "reload and gather from file[s] on telegraf's interval" | ||
| } | ||
|
|
||
| func (r *Reader) Gather(acc telegraf.Accumulator) error { | ||
| r.refreshFilePaths() | ||
| for _, k := range r.Filenames { | ||
| metrics, err := r.readMetric(k) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for i, m := range metrics { | ||
|
|
||
| //error if m is nil | ||
| if m == nil { | ||
|
||
| log.Printf("E! Metric could not be parsed from: %v, on line %v", k, i) | ||
|
||
| continue | ||
| } | ||
| acc.AddFields(m.Name(), m.Fields(), m.Tags()) | ||
|
||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (r *Reader) SetParser(p parsers.Parser) { | ||
| r.parser = p | ||
| } | ||
|
|
||
| func (r *Reader) refreshFilePaths() { | ||
| var allFiles []string | ||
| for _, filepath := range r.Filepaths { | ||
| g, err := globpath.Compile(filepath) | ||
|
||
| if err != nil { | ||
| log.Printf("E! Error Glob %s failed to compile, %s", filepath, err) | ||
|
||
| continue | ||
| } | ||
| files := g.Match() | ||
|
|
||
| for k := range files { | ||
| allFiles = append(allFiles, k) | ||
| } | ||
| } | ||
|
|
||
| r.Filenames = allFiles | ||
| } | ||
|
|
||
| //requires that Parser has been compiled | ||
| func (r *Reader) readMetric(filename string) ([]telegraf.Metric, error) { | ||
| fileContents, err := ioutil.ReadFile(filename) | ||
| if err != nil { | ||
| log.Printf("E! File could not be opened: %v", filename) | ||
|
||
| } | ||
| return r.parser.Parse(fileContents) | ||
|
|
||
| } | ||
|
|
||
| func init() { | ||
| inputs.Add("reader", func() telegraf.Input { | ||
| return &Reader{} | ||
| }) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since these all are at plugin level now, lets prefix them with
grok_