diff --git a/plugins/inputs/logparser/logparser.go b/plugins/inputs/logparser/logparser.go index aabc8b980..912a5bc51 100644 --- a/plugins/inputs/logparser/logparser.go +++ b/plugins/inputs/logparser/logparser.go @@ -3,7 +3,9 @@ package logparser import ( + "fmt" "log" + "reflect" "strings" "sync" @@ -43,7 +45,7 @@ type LogParserPlugin struct { done chan struct{} wg sync.WaitGroup acc telegraf.Accumulator - parsers []parsers.Parser + parsers []LogParser sync.Mutex @@ -135,7 +137,7 @@ func (l *LogParserPlugin) Start(acc telegraf.Accumulator) error { l.tailers = make(map[string]*tail.Tail) // Looks for fields which implement LogParser interface - l.parsers = []parsers.Parser{} + l.parsers = []LogParser{} config := &parsers.Config{ Patterns: l.Patterns, NamedPatterns: l.NamedPatterns, @@ -144,12 +146,33 @@ func (l *LogParserPlugin) Start(acc telegraf.Accumulator) error { TimeZone: l.TimeZone, DataFormat: "grok", } + var err error l.GrokParser, err = parsers.NewParser(config) if err != nil { return err } + s := reflect.ValueOf(l).Elem() + for i := 0; i < s.NumField(); i++ { + f := s.Field(i) + + if !f.CanInterface() { + continue + } + + if lpPlugin, ok := f.Interface().(LogParser); ok { + if reflect.ValueOf(lpPlugin).IsNil() { + continue + } + l.parsers = append(l.parsers, lpPlugin) + } + } + + if len(l.parsers) == 0 { + return fmt.Errorf("logparser input plugin: no parser defined") + } + l.wg.Add(1) go l.parser() diff --git a/plugins/inputs/logparser/logparser_test.go b/plugins/inputs/logparser/logparser_test.go index 6b527ef85..ac7b2c82e 100644 --- a/plugins/inputs/logparser/logparser_test.go +++ b/plugins/inputs/logparser/logparser_test.go @@ -10,8 +10,6 @@ import ( "github.com/influxdata/telegraf/testutil" - "github.com/influxdata/telegraf/plugins/parsers" - "github.com/stretchr/testify/assert" ) @@ -27,21 +25,16 @@ func TestStartNoParsers(t *testing.T) { func TestGrokParseLogFilesNonExistPattern(t *testing.T) { thisdir := getCurrentDir() - c := &parsers.Config{ - Patterns: []string{"%{FOOBAR}"}, - CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"}, - DataFormat: "grok", - } - p, err := parsers.NewParser(c) logparser := &LogParserPlugin{ - FromBeginning: true, - Files: []string{thisdir + "grok/testdata/*.log"}, - GrokParser: p, + FromBeginning: true, + Files: []string{thisdir + "grok/testdata/*.log"}, + Patterns: []string{"%{FOOBAR}"}, + CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"}, } acc := testutil.Accumulator{} - err = logparser.Start(&acc) + err := logparser.Start(&acc) assert.Error(t, err) }