From bc13d32d53caba8c035a9b853d1a0fb7e83f264e Mon Sep 17 00:00:00 2001
From: Doug Reese <doug@reesesystems.com>
Date: Fri, 16 Dec 2016 05:46:32 -0800
Subject: [PATCH] MongoDB input plugin: Improve state data (#2001)

* MongoDB input plugin: Improve state data

Adds ARB as a "member_status" (replica set arbiter).
Uses MongoDB replica set state string for "state" value.

* MongoDB input plugin: Improve state data - changelog update
---
 CHANGELOG.md                                | 1 +
 plugins/inputs/mongodb/mongodb_data.go      | 4 +---
 plugins/inputs/mongodb/mongodb_data_test.go | 3 ++-
 plugins/inputs/mongodb/mongostat.go         | 6 ++++++
 4 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1357e6d9..efb599de 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -25,6 +25,7 @@ in their config file.
 - [#2068](https://github.com/influxdata/telegraf/issues/2068): Accept strings for StatsD sets.
 - [#1893](https://github.com/influxdata/telegraf/issues/1893): Change StatsD default "reset" behavior.
 - [#2079](https://github.com/influxdata/telegraf/pull/2079): Enable setting ClientID in MQTT output.
+- [#2001](https://github.com/influxdata/telegraf/pull/2001): MongoDB input plugin: Improve state data.
 
 ### Bugfixes
 
diff --git a/plugins/inputs/mongodb/mongodb_data.go b/plugins/inputs/mongodb/mongodb_data.go
index afa4ddd2..47f35f19 100644
--- a/plugins/inputs/mongodb/mongodb_data.go
+++ b/plugins/inputs/mongodb/mongodb_data.go
@@ -21,9 +21,6 @@ type DbData struct {
 }
 
 func NewMongodbData(statLine *StatLine, tags map[string]string) *MongodbData {
-	if statLine.NodeType != "" && statLine.NodeType != "UNK" {
-		tags["state"] = statLine.NodeType
-	}
 	return &MongodbData{
 		StatLine: statLine,
 		Tags:     tags,
@@ -61,6 +58,7 @@ var DefaultReplStats = map[string]string{
 	"repl_getmores_per_sec": "GetMoreR",
 	"repl_commands_per_sec": "CommandR",
 	"member_status":         "NodeType",
+	"state":                 "NodeState",
 	"repl_lag":              "ReplLag",
 }
 
diff --git a/plugins/inputs/mongodb/mongodb_data_test.go b/plugins/inputs/mongodb/mongodb_data_test.go
index a08549cf..a74d9ed6 100644
--- a/plugins/inputs/mongodb/mongodb_data_test.go
+++ b/plugins/inputs/mongodb/mongodb_data_test.go
@@ -95,12 +95,12 @@ func TestStateTag(t *testing.T) {
 			Insert:        0,
 			Query:         0,
 			NodeType:      "PRI",
+			NodeState:     "PRIMARY",
 		},
 		tags,
 	)
 
 	stateTags := make(map[string]string)
-	stateTags["state"] = "PRI"
 
 	var acc testutil.Accumulator
 
@@ -115,6 +115,7 @@ func TestStateTag(t *testing.T) {
 		"getmores_per_sec":      int64(0),
 		"inserts_per_sec":       int64(0),
 		"member_status":         "PRI",
+		"state":                 "PRIMARY",
 		"net_in_bytes":          int64(0),
 		"net_out_bytes":         int64(0),
 		"open_connections":      int64(0),
diff --git a/plugins/inputs/mongodb/mongostat.go b/plugins/inputs/mongodb/mongostat.go
index da539f8a..e77c67e1 100644
--- a/plugins/inputs/mongodb/mongostat.go
+++ b/plugins/inputs/mongodb/mongostat.go
@@ -107,6 +107,7 @@ type ReplSetStatus struct {
 type ReplSetMember struct {
 	Name       string               `bson:"name"`
 	State      int64                `bson:"state"`
+	StateStr   string               `bson:"stateStr"`
 	OptimeDate *bson.MongoTimestamp `bson:"optimeDate"`
 }
 
@@ -420,6 +421,7 @@ type StatLine struct {
 	NumConnections                                        int64
 	ReplSetName                                           string
 	NodeType                                              string
+	NodeState                                             string
 
 	// Cluster fields
 	JumboChunksCount int64
@@ -566,6 +568,8 @@ func NewStatLine(oldMongo, newMongo MongoStatus, key string, all bool, sampleSec
 			returnVal.NodeType = "PRI"
 		} else if newStat.Repl.Secondary.(bool) {
 			returnVal.NodeType = "SEC"
+		} else if newStat.Repl.ArbiterOnly != nil && newStat.Repl.ArbiterOnly.(bool) {
+			returnVal.NodeType = "ARB"
 		} else {
 			returnVal.NodeType = "UNK"
 		}
@@ -692,6 +696,8 @@ func NewStatLine(oldMongo, newMongo MongoStatus, key string, all bool, sampleSec
 		me := ReplSetMember{}
 		for _, member := range newReplStat.Members {
 			if member.Name == myName {
+				// Store my state string
+				returnVal.NodeState = member.StateStr
 				if member.State == 1 {
 					// I'm the master
 					returnVal.ReplLag = 0
-- 
GitLab