ntpq: don't index ntp fields that dont exist

closes #1634
This commit is contained in:
Cameron Sparr 2016-08-16 09:06:19 +01:00
parent 4ce8dd5f9a
commit 7600757f16
3 changed files with 37 additions and 2 deletions

View File

@ -138,6 +138,7 @@ consistent with the behavior of `collection_jitter`.
- [#1586](https://github.com/influxdata/telegraf/pull/1586): Remove IF NOT EXISTS from influxdb output database creation. - [#1586](https://github.com/influxdata/telegraf/pull/1586): Remove IF NOT EXISTS from influxdb output database creation.
- [#1600](https://github.com/influxdata/telegraf/issues/1600): Fix quoting with text values in postgresql_extensible plugin. - [#1600](https://github.com/influxdata/telegraf/issues/1600): Fix quoting with text values in postgresql_extensible plugin.
- [#1425](https://github.com/influxdata/telegraf/issues/1425): Fix win_perf_counter "index out of range" panic. - [#1425](https://github.com/influxdata/telegraf/issues/1425): Fix win_perf_counter "index out of range" panic.
- [#1634](https://github.com/influxdata/telegraf/issues/1634): Fix ntpq panic when field is missing.
## v0.13.1 [2016-05-24] ## v0.13.1 [2016-05-24]

View File

@ -119,7 +119,7 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
// Get integer metrics from output // Get integer metrics from output
for key, index := range intI { for key, index := range intI {
if index == -1 { if index == -1 || index >= len(fields) {
continue continue
} }
if fields[index] == "-" { if fields[index] == "-" {
@ -169,7 +169,7 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
// get float metrics from output // get float metrics from output
for key, index := range floatI { for key, index := range floatI {
if index == -1 { if index == -1 || index >= len(fields) {
continue continue
} }
if fields[index] == "-" { if fields[index] == "-" {

View File

@ -41,6 +41,35 @@ func TestSingleNTPQ(t *testing.T) {
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags) acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
} }
func TestMissingJitterField(t *testing.T) {
tt := tester{
ret: []byte(missingJitterField),
err: nil,
}
n := &NTPQ{
runQ: tt.runqTest,
}
acc := testutil.Accumulator{}
assert.NoError(t, n.Gather(&acc))
fields := map[string]interface{}{
"when": int64(101),
"poll": int64(256),
"reach": int64(37),
"delay": float64(51.016),
"offset": float64(233.010),
}
tags := map[string]string{
"remote": "uschi5-ntp-002.",
"state_prefix": "*",
"refid": "10.177.80.46",
"stratum": "2",
"type": "u",
}
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
}
func TestBadIntNTPQ(t *testing.T) { func TestBadIntNTPQ(t *testing.T) {
tt := tester{ tt := tester{
ret: []byte(badIntParseNTPQ), ret: []byte(badIntParseNTPQ),
@ -381,6 +410,11 @@ var singleNTPQ = ` remote refid st t when poll reach delay
*uschi5-ntp-002. 10.177.80.46 2 u 101 256 37 51.016 233.010 17.462 *uschi5-ntp-002. 10.177.80.46 2 u 101 256 37 51.016 233.010 17.462
` `
var missingJitterField = ` remote refid st t when poll reach delay offset jitter
==============================================================================
*uschi5-ntp-002. 10.177.80.46 2 u 101 256 37 51.016 233.010
`
var badHeaderNTPQ = `remote refid foobar t when poll reach delay offset jitter var badHeaderNTPQ = `remote refid foobar t when poll reach delay offset jitter
============================================================================== ==============================================================================
*uschi5-ntp-002. 10.177.80.46 2 u 101 256 37 51.016 233.010 17.462 *uschi5-ntp-002. 10.177.80.46 2 u 101 256 37 51.016 233.010 17.462