Fix ntpq parse issue when using dns_lookup (#3026)
This commit is contained in:
parent
880ff896ac
commit
9a8de6085f
|
@ -7,6 +7,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -67,6 +68,14 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
|
||||||
return err
|
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
|
lineCounter := 0
|
||||||
scanner := bufio.NewScanner(bytes.NewReader(out))
|
scanner := bufio.NewScanner(bytes.NewReader(out))
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
|
@ -80,6 +89,8 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
|
||||||
line = strings.TrimLeft(line, "*#o+x.-")
|
line = strings.TrimLeft(line, "*#o+x.-")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
line = reg.ReplaceAllString(line, "")
|
||||||
|
|
||||||
fields := strings.Fields(line)
|
fields := strings.Fields(line)
|
||||||
if len(fields) < 2 {
|
if len(fields) < 2 {
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -247,6 +247,21 @@ func TestBadWhenNTPQ(t *testing.T) {
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
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) {
|
func TestMultiNTPQ(t *testing.T) {
|
||||||
tt := tester{
|
tt := tester{
|
||||||
ret: []byte(multiNTPQ),
|
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.
|
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.
|
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
|
||||||
|
`
|
||||||
|
|
Loading…
Reference in New Issue