From 40432353ef083a7c7a860b5c5898985f46af6ffe Mon Sep 17 00:00:00 2001 From: Ayrdrie Date: Thu, 21 Jun 2018 15:19:15 -0600 Subject: [PATCH] Add support for comma in logparser timestamp format (#4311) --- plugins/inputs/logparser/README.md | 4 +++- plugins/inputs/logparser/grok/grok.go | 3 +++ plugins/inputs/logparser/grok/grok_test.go | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/plugins/inputs/logparser/README.md b/plugins/inputs/logparser/README.md index 7a1df8bc8..1caa3830c 100644 --- a/plugins/inputs/logparser/README.md +++ b/plugins/inputs/logparser/README.md @@ -108,7 +108,9 @@ You must capture at least one field per line. - ts-"CUSTOM" CUSTOM time layouts must be within quotes and be the representation of the -"reference time", which is `Mon Jan 2 15:04:05 -0700 MST 2006` +"reference time", which is `Mon Jan 2 15:04:05 -0700 MST 2006`. +To match a comma decimal point you can use a period. For example `%{TIMESTAMP:timestamp:ts-"2006-01-02 15:04:05.000"}` can be used to match `"2018-01-02 15:04:05,000"` +To match a comma decimal point you can use a period in the pattern string. See https://golang.org/pkg/time/#Parse for more details. Telegraf has many of its own [built-in patterns](./grok/patterns/influx-patterns), diff --git a/plugins/inputs/logparser/grok/grok.go b/plugins/inputs/logparser/grok/grok.go index 57d8691cf..766d149fe 100644 --- a/plugins/inputs/logparser/grok/grok.go +++ b/plugins/inputs/logparser/grok/grok.go @@ -335,6 +335,9 @@ func (p *Parser) ParseLine(line string) (telegraf.Metric, error) { case DROP: // goodbye! default: + // Replace commas with dot character + v = strings.Replace(v, ",", ".", -1) + ts, err := time.ParseInLocation(t, v, p.loc) if err == nil { timestamp = ts diff --git a/plugins/inputs/logparser/grok/grok_test.go b/plugins/inputs/logparser/grok/grok_test.go index 1008ce941..6a143bb7d 100644 --- a/plugins/inputs/logparser/grok/grok_test.go +++ b/plugins/inputs/logparser/grok/grok_test.go @@ -982,3 +982,21 @@ func TestSyslogTimestampParser(t *testing.T) { require.NotNil(t, m) require.Equal(t, 2018, m.Time().Year()) } + +func TestReplaceTimestampComma(t *testing.T) { + + p := &Parser{ + Patterns: []string{`%{TIMESTAMP_ISO8601:timestamp:ts-"2006-01-02 15:04:05.000"} successfulMatches=%{NUMBER:value:int}`}, + } + + require.NoError(t, p.Compile()) + m, err := p.ParseLine("2018-02-21 13:10:34,555 successfulMatches=1") + require.NoError(t, err) + require.NotNil(t, m) + + require.Equal(t, 2018, m.Time().Year()) + require.Equal(t, 13, m.Time().Hour()) + require.Equal(t, 34, m.Time().Second()) + //Convert Nanosecond to milisecond for compare + require.Equal(t, 555, m.Time().Nanosecond()/1000000) +}