From d449833de919fd33fa12b0b978e1ffc92201ea7b Mon Sep 17 00:00:00 2001
From: Jan Willem Janssen <j.w.janssen@lxtreme.nl>
Date: Fri, 25 Aug 2017 20:54:06 +0200
Subject: [PATCH] Fix parsing of SHM remotes in ntpq input (#3163)

---
 plugins/inputs/ntpq/ntpq.go      |  2 +-
 plugins/inputs/ntpq/ntpq_test.go | 53 ++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/plugins/inputs/ntpq/ntpq.go b/plugins/inputs/ntpq/ntpq.go
index 692e1bc5..ce7bb96d 100644
--- a/plugins/inputs/ntpq/ntpq.go
+++ b/plugins/inputs/ntpq/ntpq.go
@@ -69,7 +69,7 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
 	// 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]*")
+	reg, err := regexp.Compile("\\s+\\([\\S]*")
 	if err != nil {
 		return err
 	}
diff --git a/plugins/inputs/ntpq/ntpq_test.go b/plugins/inputs/ntpq/ntpq_test.go
index d8da845d..33277221 100644
--- a/plugins/inputs/ntpq/ntpq_test.go
+++ b/plugins/inputs/ntpq/ntpq_test.go
@@ -260,6 +260,57 @@ func TestParserNTPQ(t *testing.T) {
 	}
 	acc := testutil.Accumulator{}
 	assert.NoError(t, acc.GatherError(n.Gather))
+
+	fields := map[string]interface{}{
+		"poll":   int64(64),
+		"when":   int64(60),
+		"reach":  int64(377),
+		"delay":  float64(0.0),
+		"offset": float64(0.045),
+		"jitter": float64(1.012),
+	}
+	tags := map[string]string{
+		"remote":       "SHM(0)",
+		"state_prefix": "*",
+		"refid":        ".PPS.",
+		"stratum":      "1",
+		"type":         "u",
+	}
+	acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
+
+	fields = map[string]interface{}{
+		"poll":   int64(128),
+		"when":   int64(121),
+		"reach":  int64(377),
+		"delay":  float64(0.0),
+		"offset": float64(10.105),
+		"jitter": float64(2.012),
+	}
+	tags = map[string]string{
+		"remote":       "SHM(1)",
+		"state_prefix": "-",
+		"refid":        ".GPS.",
+		"stratum":      "1",
+		"type":         "u",
+	}
+	acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
+
+	fields = map[string]interface{}{
+		"poll":   int64(1024),
+		"when":   int64(10),
+		"reach":  int64(377),
+		"delay":  float64(1.748),
+		"offset": float64(0.373),
+		"jitter": float64(0.101),
+	}
+	tags = map[string]string{
+		"remote":       "37.58.57.238",
+		"state_prefix": "+",
+		"refid":        "192.53.103.103",
+		"stratum":      "2",
+		"type":         "u",
+	}
+	acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
 }
 
 func TestMultiNTPQ(t *testing.T) {
@@ -480,7 +531,9 @@ var multiNTPQ = `     remote           refid      st t when poll reach   delay
 `
 var multiParserNTPQ = `     remote           refid      st t when poll reach   delay   offset  jitter
 ==============================================================================
+*SHM(0)          .PPS.                          1 u   60  64   377    0.000    0.045   1.012
 +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
+-SHM(1)          .GPS.                          1 u   121 128  377    0.000   10.105   2.012
 `
-- 
GitLab