fix: correctly read ntpq long poll output (#10790)

This commit is contained in:
Joshua Powers 2022-03-10 07:20:02 -07:00 committed by GitHub
parent 80d66dfed9
commit 2b57262617
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 5 deletions

View File

@ -122,13 +122,13 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
continue
}
if key == "when" {
if key == "when" || key == "poll" {
when := fields[index]
switch {
case strings.HasSuffix(when, "h"):
m, err := strconv.Atoi(strings.TrimSuffix(fields[index], "h"))
if err != nil {
acc.AddError(fmt.Errorf("error ntpq: parsing int: %s", fields[index]))
acc.AddError(fmt.Errorf("error ntpq: parsing %s as int: %s", key, fields[index]))
continue
}
// seconds in an hour
@ -137,7 +137,7 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
case strings.HasSuffix(when, "d"):
m, err := strconv.Atoi(strings.TrimSuffix(fields[index], "d"))
if err != nil {
acc.AddError(fmt.Errorf("error ntpq: parsing int: %s", fields[index]))
acc.AddError(fmt.Errorf("error ntpq: parsing %s as int: %s", key, fields[index]))
continue
}
// seconds in a day
@ -146,7 +146,7 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
case strings.HasSuffix(when, "m"):
m, err := strconv.Atoi(strings.TrimSuffix(fields[index], "m"))
if err != nil {
acc.AddError(fmt.Errorf("error ntpq: parsing int: %s", fields[index]))
acc.AddError(fmt.Errorf("error ntpq: parsing %s as int: %s", key, fields[index]))
continue
}
// seconds in a day
@ -157,7 +157,7 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
m, err := strconv.Atoi(fields[index])
if err != nil {
acc.AddError(fmt.Errorf("error ntpq: parsing int: %s", fields[index]))
acc.AddError(fmt.Errorf("error ntpq: parsing %s as int: %s", key, fields[index]))
continue
}
mFields[key] = int64(m)

View File

@ -375,6 +375,36 @@ func TestMissingDelayColumnNTPQ(t *testing.T) {
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
}
func TestLongPoll(t *testing.T) {
tt := tester{
ret: []byte(longPollTime),
err: nil,
}
n := newNTPQ()
n.runQ = tt.runqTest
acc := testutil.Accumulator{}
require.NoError(t, acc.GatherError(n.Gather))
fields := map[string]interface{}{
"when": int64(617),
"poll": int64(4080),
"reach": int64(377),
"offset": float64(2.849),
"jitter": float64(1.192),
"delay": float64(9.145),
}
tags := map[string]string{
"remote": "uschi5-ntp-002.",
"state_prefix": "-",
"refid": "10.177.80.46",
"type": "u",
"stratum": "3",
}
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
}
func TestFailedNTPQ(t *testing.T) {
tt := tester{
ret: []byte(singleNTPQ),
@ -520,3 +550,8 @@ var noRefID = ` remote refid st t when poll reach delay o
91.189.94.4 2 u 673 1024 377 143.047 274.726 449445.
131.188.3.221 10.177.80.37 2 u 783 1024 377 111.820 261.921 449528.
`
var longPollTime = ` remote refid st t when poll reach delay offset jitter
==============================================================================
-uschi5-ntp-002. 10.177.80.46 3 u 617 68m 377 9.145 +2.849 1.192
`