Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ntpq parse issue when using dns_lookup #3026

Merged
merged 3 commits into from
Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions plugins/inputs/ntpq/ntpq.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions plugins/inputs/ntpq/ntpq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
`