Remove ntpq state prefixes and make their own tag

closes #1161
This commit is contained in:
Cameron Sparr 2016-05-18 13:37:09 +01:00
parent a87d6f0545
commit c292e3931a
2 changed files with 54 additions and 36 deletions

View File

@ -70,7 +70,17 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
lineCounter := 0 lineCounter := 0
scanner := bufio.NewScanner(bytes.NewReader(out)) scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() { for scanner.Scan() {
fields := strings.Fields(scanner.Text()) line := scanner.Text()
tags := make(map[string]string)
// if there is an ntpq state prefix, remove it and make it it's own tag
// see https://github.com/influxdata/telegraf/issues/1161
if strings.ContainsAny(string(line[0]), "*#o+x.-") {
tags["state_prefix"] = string(line[0])
line = strings.TrimLeft(line, "*#o+x.-")
}
fields := strings.Fields(line)
if len(fields) < 2 { if len(fields) < 2 {
continue continue
} }
@ -97,7 +107,6 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
} }
} }
} else { } else {
tags := make(map[string]string)
mFields := make(map[string]interface{}) mFields := make(map[string]interface{})
// Get tags from output // Get tags from output

View File

@ -32,7 +32,8 @@ func TestSingleNTPQ(t *testing.T) {
"jitter": float64(17.462), "jitter": float64(17.462),
} }
tags := map[string]string{ tags := map[string]string{
"remote": "*uschi5-ntp-002.", "remote": "uschi5-ntp-002.",
"state_prefix": "*",
"refid": "10.177.80.46", "refid": "10.177.80.46",
"stratum": "2", "stratum": "2",
"type": "u", "type": "u",
@ -60,7 +61,8 @@ func TestBadIntNTPQ(t *testing.T) {
"jitter": float64(17.462), "jitter": float64(17.462),
} }
tags := map[string]string{ tags := map[string]string{
"remote": "*uschi5-ntp-002.", "remote": "uschi5-ntp-002.",
"state_prefix": "*",
"refid": "10.177.80.46", "refid": "10.177.80.46",
"stratum": "2", "stratum": "2",
"type": "u", "type": "u",
@ -88,7 +90,8 @@ func TestBadFloatNTPQ(t *testing.T) {
"jitter": float64(17.462), "jitter": float64(17.462),
} }
tags := map[string]string{ tags := map[string]string{
"remote": "*uschi5-ntp-002.", "remote": "uschi5-ntp-002.",
"state_prefix": "*",
"refid": "10.177.80.46", "refid": "10.177.80.46",
"stratum": "2", "stratum": "2",
"type": "u", "type": "u",
@ -117,7 +120,8 @@ func TestDaysNTPQ(t *testing.T) {
"jitter": float64(17.462), "jitter": float64(17.462),
} }
tags := map[string]string{ tags := map[string]string{
"remote": "*uschi5-ntp-002.", "remote": "uschi5-ntp-002.",
"state_prefix": "*",
"refid": "10.177.80.46", "refid": "10.177.80.46",
"stratum": "2", "stratum": "2",
"type": "u", "type": "u",
@ -146,7 +150,8 @@ func TestHoursNTPQ(t *testing.T) {
"jitter": float64(17.462), "jitter": float64(17.462),
} }
tags := map[string]string{ tags := map[string]string{
"remote": "*uschi5-ntp-002.", "remote": "uschi5-ntp-002.",
"state_prefix": "*",
"refid": "10.177.80.46", "refid": "10.177.80.46",
"stratum": "2", "stratum": "2",
"type": "u", "type": "u",
@ -175,7 +180,8 @@ func TestMinutesNTPQ(t *testing.T) {
"jitter": float64(17.462), "jitter": float64(17.462),
} }
tags := map[string]string{ tags := map[string]string{
"remote": "*uschi5-ntp-002.", "remote": "uschi5-ntp-002.",
"state_prefix": "*",
"refid": "10.177.80.46", "refid": "10.177.80.46",
"stratum": "2", "stratum": "2",
"type": "u", "type": "u",
@ -203,7 +209,8 @@ func TestBadWhenNTPQ(t *testing.T) {
"jitter": float64(17.462), "jitter": float64(17.462),
} }
tags := map[string]string{ tags := map[string]string{
"remote": "*uschi5-ntp-002.", "remote": "uschi5-ntp-002.",
"state_prefix": "*",
"refid": "10.177.80.46", "refid": "10.177.80.46",
"stratum": "2", "stratum": "2",
"type": "u", "type": "u",
@ -278,7 +285,8 @@ func TestBadHeaderNTPQ(t *testing.T) {
"jitter": float64(17.462), "jitter": float64(17.462),
} }
tags := map[string]string{ tags := map[string]string{
"remote": "*uschi5-ntp-002.", "remote": "uschi5-ntp-002.",
"state_prefix": "*",
"refid": "10.177.80.46", "refid": "10.177.80.46",
"type": "u", "type": "u",
} }
@ -306,7 +314,8 @@ func TestMissingDelayColumnNTPQ(t *testing.T) {
"jitter": float64(17.462), "jitter": float64(17.462),
} }
tags := map[string]string{ tags := map[string]string{
"remote": "*uschi5-ntp-002.", "remote": "uschi5-ntp-002.",
"state_prefix": "*",
"refid": "10.177.80.46", "refid": "10.177.80.46",
"type": "u", "type": "u",
} }