-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add file input plugin and grok parser (#4332)
- Loading branch information
1 parent
3f87e5b
commit 774a9f0
Showing
24 changed files
with
558 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# File Input Plugin | ||
|
||
The file plugin updates a list of files every interval and parses the contents | ||
using the selected [input data format](/docs/DATA_FORMATS_INPUT.md). | ||
|
||
Files will always be read in their entirety, if you wish to tail/follow a file | ||
use the [tail input plugin](/plugins/inputs/tail) instead. | ||
|
||
### Configuration: | ||
```toml | ||
[[inputs.file]] | ||
## 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"] | ||
|
||
## Data format to consume. | ||
## 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 = "influx" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[[inputs.file]] | ||
files = ["/var/log/test.log"] | ||
data_format = "json" | ||
name_override = "json_file" | ||
|
||
[[outputs.file]] | ||
files = ["stdout"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package file | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/internal/globpath" | ||
"github.com/influxdata/telegraf/plugins/inputs" | ||
"github.com/influxdata/telegraf/plugins/parsers" | ||
) | ||
|
||
type File struct { | ||
Files []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 = "influx" | ||
` | ||
|
||
// SampleConfig returns the default configuration of the Input | ||
func (f *File) SampleConfig() string { | ||
return sampleConfig | ||
} | ||
|
||
func (f *File) Description() string { | ||
return "reload and gather from file[s] on telegraf's interval" | ||
} | ||
|
||
func (f *File) Gather(acc telegraf.Accumulator) error { | ||
err := f.refreshFilePaths() | ||
if err != nil { | ||
return err | ||
} | ||
for _, k := range f.filenames { | ||
metrics, err := f.readMetric(k) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, m := range metrics { | ||
acc.AddFields(m.Name(), m.Fields(), m.Tags(), m.Time()) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (f *File) SetParser(p parsers.Parser) { | ||
f.parser = p | ||
} | ||
|
||
func (f *File) refreshFilePaths() error { | ||
var allFiles []string | ||
for _, file := range f.Files { | ||
g, err := globpath.Compile(file) | ||
if err != nil { | ||
return fmt.Errorf("could not compile glob %v: %v", file, err) | ||
} | ||
files := g.Match() | ||
if len(files) <= 0 { | ||
return fmt.Errorf("could not find file: %v", file) | ||
} | ||
|
||
for k := range files { | ||
allFiles = append(allFiles, k) | ||
} | ||
} | ||
|
||
f.filenames = allFiles | ||
return nil | ||
} | ||
|
||
func (f *File) readMetric(filename string) ([]telegraf.Metric, error) { | ||
fileContents, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return nil, fmt.Errorf("E! Error file: %v could not be read, %s", filename, err) | ||
} | ||
return f.parser.Parse(fileContents) | ||
|
||
} | ||
|
||
func init() { | ||
inputs.Add("file", func() telegraf.Input { | ||
return &File{} | ||
}) | ||
} |
Oops, something went wrong.