From c292e3931a7430ea9b879ac3db0f34ad21c74d5f Mon Sep 17 00:00:00 2001
From: Cameron Sparr <cameronsparr@gmail.com>
Date: Wed, 18 May 2016 13:37:09 +0100
Subject: [PATCH] Remove ntpq state prefixes and make their own tag

closes #1161
---
 plugins/inputs/ntpq/ntpq.go      | 13 +++++-
 plugins/inputs/ntpq/ntpq_test.go | 77 ++++++++++++++++++--------------
 2 files changed, 54 insertions(+), 36 deletions(-)

diff --git a/plugins/inputs/ntpq/ntpq.go b/plugins/inputs/ntpq/ntpq.go
index 5e8ff653..edcdac3d 100644
--- a/plugins/inputs/ntpq/ntpq.go
+++ b/plugins/inputs/ntpq/ntpq.go
@@ -70,7 +70,17 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
 	lineCounter := 0
 	scanner := bufio.NewScanner(bytes.NewReader(out))
 	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 {
 			continue
 		}
@@ -97,7 +107,6 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
 				}
 			}
 		} else {
-			tags := make(map[string]string)
 			mFields := make(map[string]interface{})
 
 			// Get tags from output
diff --git a/plugins/inputs/ntpq/ntpq_test.go b/plugins/inputs/ntpq/ntpq_test.go
index 228eddc6..7e83243c 100644
--- a/plugins/inputs/ntpq/ntpq_test.go
+++ b/plugins/inputs/ntpq/ntpq_test.go
@@ -32,10 +32,11 @@ func TestSingleNTPQ(t *testing.T) {
 		"jitter": float64(17.462),
 	}
 	tags := map[string]string{
-		"remote":  "*uschi5-ntp-002.",
-		"refid":   "10.177.80.46",
-		"stratum": "2",
-		"type":    "u",
+		"remote":       "uschi5-ntp-002.",
+		"state_prefix": "*",
+		"refid":        "10.177.80.46",
+		"stratum":      "2",
+		"type":         "u",
 	}
 	acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
 }
@@ -60,10 +61,11 @@ func TestBadIntNTPQ(t *testing.T) {
 		"jitter": float64(17.462),
 	}
 	tags := map[string]string{
-		"remote":  "*uschi5-ntp-002.",
-		"refid":   "10.177.80.46",
-		"stratum": "2",
-		"type":    "u",
+		"remote":       "uschi5-ntp-002.",
+		"state_prefix": "*",
+		"refid":        "10.177.80.46",
+		"stratum":      "2",
+		"type":         "u",
 	}
 	acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
 }
@@ -88,10 +90,11 @@ func TestBadFloatNTPQ(t *testing.T) {
 		"jitter": float64(17.462),
 	}
 	tags := map[string]string{
-		"remote":  "*uschi5-ntp-002.",
-		"refid":   "10.177.80.46",
-		"stratum": "2",
-		"type":    "u",
+		"remote":       "uschi5-ntp-002.",
+		"state_prefix": "*",
+		"refid":        "10.177.80.46",
+		"stratum":      "2",
+		"type":         "u",
 	}
 	acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
 }
@@ -117,10 +120,11 @@ func TestDaysNTPQ(t *testing.T) {
 		"jitter": float64(17.462),
 	}
 	tags := map[string]string{
-		"remote":  "*uschi5-ntp-002.",
-		"refid":   "10.177.80.46",
-		"stratum": "2",
-		"type":    "u",
+		"remote":       "uschi5-ntp-002.",
+		"state_prefix": "*",
+		"refid":        "10.177.80.46",
+		"stratum":      "2",
+		"type":         "u",
 	}
 	acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
 }
@@ -146,10 +150,11 @@ func TestHoursNTPQ(t *testing.T) {
 		"jitter": float64(17.462),
 	}
 	tags := map[string]string{
-		"remote":  "*uschi5-ntp-002.",
-		"refid":   "10.177.80.46",
-		"stratum": "2",
-		"type":    "u",
+		"remote":       "uschi5-ntp-002.",
+		"state_prefix": "*",
+		"refid":        "10.177.80.46",
+		"stratum":      "2",
+		"type":         "u",
 	}
 	acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
 }
@@ -175,10 +180,11 @@ func TestMinutesNTPQ(t *testing.T) {
 		"jitter": float64(17.462),
 	}
 	tags := map[string]string{
-		"remote":  "*uschi5-ntp-002.",
-		"refid":   "10.177.80.46",
-		"stratum": "2",
-		"type":    "u",
+		"remote":       "uschi5-ntp-002.",
+		"state_prefix": "*",
+		"refid":        "10.177.80.46",
+		"stratum":      "2",
+		"type":         "u",
 	}
 	acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
 }
@@ -203,10 +209,11 @@ func TestBadWhenNTPQ(t *testing.T) {
 		"jitter": float64(17.462),
 	}
 	tags := map[string]string{
-		"remote":  "*uschi5-ntp-002.",
-		"refid":   "10.177.80.46",
-		"stratum": "2",
-		"type":    "u",
+		"remote":       "uschi5-ntp-002.",
+		"state_prefix": "*",
+		"refid":        "10.177.80.46",
+		"stratum":      "2",
+		"type":         "u",
 	}
 	acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
 }
@@ -278,9 +285,10 @@ func TestBadHeaderNTPQ(t *testing.T) {
 		"jitter": float64(17.462),
 	}
 	tags := map[string]string{
-		"remote": "*uschi5-ntp-002.",
-		"refid":  "10.177.80.46",
-		"type":   "u",
+		"remote":       "uschi5-ntp-002.",
+		"state_prefix": "*",
+		"refid":        "10.177.80.46",
+		"type":         "u",
 	}
 	acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
 }
@@ -306,9 +314,10 @@ func TestMissingDelayColumnNTPQ(t *testing.T) {
 		"jitter": float64(17.462),
 	}
 	tags := map[string]string{
-		"remote": "*uschi5-ntp-002.",
-		"refid":  "10.177.80.46",
-		"type":   "u",
+		"remote":       "uschi5-ntp-002.",
+		"state_prefix": "*",
+		"refid":        "10.177.80.46",
+		"type":         "u",
 	}
 	acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
 }
-- 
GitLab