add setparser to reader

This commit is contained in:
Max U 2018-06-25 09:53:35 -07:00
parent 071b7b9731
commit fdf1b9b351
2 changed files with 45 additions and 3 deletions

View File

@ -24,6 +24,7 @@ type Reader struct {
namedPatterns []string namedPatterns []string
CustomPatterns string CustomPatterns string
CustomPatternFiles []string CustomPatternFiles []string
TZone string
} }
const sampleConfig = `## Files to parse. const sampleConfig = `## Files to parse.
@ -38,7 +39,40 @@ files = ["/var/log/apache/access.log"]
## Each data format has its own unique set of configuration options, read ## Each data format has its own unique set of configuration options, read
## more about them here: ## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
data_format = ""` data_format = ""
## Parse logstash-style "grok" patterns:
## Telegraf built-in parsing patterns: https://goo.gl/dkay10
[inputs.logparser.grok]
## This is a list of patterns to check the given log file(s) for.
## Note that adding patterns here increases processing time. The most
## efficient configuration is to have one pattern per logparser.
## Other common built-in patterns are:
## %{COMMON_LOG_FORMAT} (plain apache & nginx access logs)
## %{COMBINED_LOG_FORMAT} (access logs + referrer & agent)
patterns = ["%{COMBINED_LOG_FORMAT}"]
## Name of the outputted measurement name.
measurement = "apache_access_log"
## Full path(s) to custom pattern files.
custom_pattern_files = []
## Custom patterns can also be defined here. Put one pattern per line.
custom_patterns = '''
'''
## Timezone allows you to provide an override for timestamps that
## don't already include an offset
## e.g. 04/06/2016 12:41:45 data one two 5.43µs
##
## Default: "" which renders UTC
## Options are as follows:
## 1. Local -- interpret based on machine localtime
## 2. "Canada/Eastern" -- Unix TZ values like those found in https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
## 3. UTC -- or blank/unspecified, will return timestamp in UTC
timezone = "Canada/Eastern"
`
// SampleConfig returns the default configuration of the Input // SampleConfig returns the default configuration of the Input
func (r *Reader) SampleConfig() string { func (r *Reader) SampleConfig() string {
@ -64,6 +98,10 @@ func (r *Reader) Gather(acc telegraf.Accumulator) error {
return nil return nil
} }
func (r *Reader) SetParser(p parsers.Parser) {
r.Parser = p
}
func (r *Reader) compileParser() { func (r *Reader) compileParser() {
if r.DataFormat == "" { if r.DataFormat == "" {
log.Printf("E! No data_format specified") log.Printf("E! No data_format specified")
@ -78,6 +116,7 @@ func (r *Reader) compileParser() {
NamedPatterns: r.namedPatterns, NamedPatterns: r.namedPatterns,
CustomPatterns: r.CustomPatterns, CustomPatterns: r.CustomPatterns,
CustomPatternFiles: r.CustomPatternFiles, CustomPatternFiles: r.CustomPatternFiles,
TimeZone: r.TZone,
} }
nParser, err := parsers.NewParser(&r.ParserConfig) nParser, err := parsers.NewParser(&r.ParserConfig)
if err != nil { if err != nil {

View File

@ -94,6 +94,7 @@ type Config struct {
NamedPatterns []string NamedPatterns []string
CustomPatterns string CustomPatterns string
CustomPatternFiles []string CustomPatternFiles []string
TimeZone string
} }
// NewParser returns a Parser interface based on the given config. // NewParser returns a Parser interface based on the given config.
@ -133,7 +134,8 @@ func NewParser(config *Config) (Parser, error) {
config.Patterns, config.Patterns,
config.NamedPatterns, config.NamedPatterns,
config.CustomPatterns, config.CustomPatterns,
config.CustomPatternFiles) config.CustomPatternFiles,
config.TimeZone)
default: default:
err = fmt.Errorf("Invalid data format: %s", config.DataFormat) err = fmt.Errorf("Invalid data format: %s", config.DataFormat)
} }
@ -144,13 +146,14 @@ func NewGrokParser(metricName string,
patterns []string, patterns []string,
nPatterns []string, nPatterns []string,
cPatterns string, cPatterns string,
cPatternFiles []string) (Parser, error) { cPatternFiles []string, tZone string) (Parser, error) {
parser := grok.Parser{ parser := grok.Parser{
Measurement: metricName, Measurement: metricName,
Patterns: patterns, Patterns: patterns,
NamedPatterns: nPatterns, NamedPatterns: nPatterns,
CustomPatterns: cPatterns, CustomPatterns: cPatterns,
CustomPatternFiles: cPatternFiles, CustomPatternFiles: cPatternFiles,
Timezone: tZone,
} }
parser.Compile() parser.Compile()