From d4f59a1bd77f2847860d1b8515e9daa73a79a81a Mon Sep 17 00:00:00 2001 From: DanKans Date: Tue, 18 Jul 2017 19:01:08 +0100 Subject: [PATCH] Fix ntpq parse issue when using dns_lookup (#3026) (cherry picked from commit d2626f1da66557c3e85f264d97cefa5eb0494cdb) --- plugins/inputs/ntpq/ntpq.go | 11 +++++++++++ plugins/inputs/ntpq/ntpq_test.go | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/plugins/inputs/ntpq/ntpq.go b/plugins/inputs/ntpq/ntpq.go index 8280e51c1..597db0bd1 100644 --- a/plugins/inputs/ntpq/ntpq.go +++ b/plugins/inputs/ntpq/ntpq.go @@ -7,6 +7,7 @@ import ( "bytes" "fmt" "os/exec" + "regexp" "strconv" "strings" @@ -67,6 +68,14 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error { return err } + // Due to problems with a parsing, we have to use regexp expression in order + // to remove string that starts from '(' and ends with space + // see: https://github.com/influxdata/telegraf/issues/2386 + reg, err := regexp.Compile("\\([\\S]*") + if err != nil { + return err + } + lineCounter := 0 scanner := bufio.NewScanner(bytes.NewReader(out)) for scanner.Scan() { @@ -80,6 +89,8 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error { line = strings.TrimLeft(line, "*#o+x.-") } + line = reg.ReplaceAllString(line, "") + fields := strings.Fields(line) if len(fields) < 2 { continue diff --git a/plugins/inputs/ntpq/ntpq_test.go b/plugins/inputs/ntpq/ntpq_test.go index 4b356e1f1..d8da845d1 100644 --- a/plugins/inputs/ntpq/ntpq_test.go +++ b/plugins/inputs/ntpq/ntpq_test.go @@ -247,6 +247,21 @@ func TestBadWhenNTPQ(t *testing.T) { acc.AssertContainsTaggedFields(t, "ntpq", fields, tags) } +// TestParserNTPQ - realated to: +// https://github.com/influxdata/telegraf/issues/2386 +func TestParserNTPQ(t *testing.T) { + tt := tester{ + ret: []byte(multiParserNTPQ), + err: nil, + } + + n := &NTPQ{ + runQ: tt.runqTest, + } + acc := testutil.Accumulator{} + assert.NoError(t, acc.GatherError(n.Gather)) +} + func TestMultiNTPQ(t *testing.T) { tt := tester{ ret: []byte(multiNTPQ), @@ -463,3 +478,9 @@ var multiNTPQ = ` remote refid st t when poll reach delay 5.9.29.107 10.177.80.37 2 u 703 1024 377 205.704 160.406 449602. 91.189.94.4 10.177.80.37 2 u 673 1024 377 143.047 274.726 449445. ` +var multiParserNTPQ = ` remote refid st t when poll reach delay offset jitter +============================================================================== ++37.58.57.238 (d 192.53.103.103 2 u 10 1024 377 1.748 0.373 0.101 ++37.58.57.238 (domain) 192.53.103.103 2 u 10 1024 377 1.748 0.373 0.101 ++37.58.57.238 ( 192.53.103.103 2 u 10 1024 377 1.748 0.373 0.101 +`