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 +`