From 3e31ae4ecaee89fad104e3e770149e9b0fcfd4c5 Mon Sep 17 00:00:00 2001 From: Jake Champlin Date: Tue, 20 Feb 2018 16:55:56 -0500 Subject: [PATCH] Add shard server stats to the mongodb input plugin (#3808) --- plugins/inputs/mongodb/README.md | 4 ++++ plugins/inputs/mongodb/mongodb_data.go | 8 +++++++ plugins/inputs/mongodb/mongodb_data_test.go | 25 +++++++++++++++++++++ plugins/inputs/mongodb/mongodb_server.go | 13 ++++++++++- plugins/inputs/mongodb/mongostat.go | 19 ++++++++++++++++ 5 files changed, 68 insertions(+), 1 deletion(-) diff --git a/plugins/inputs/mongodb/README.md b/plugins/inputs/mongodb/README.md index f09457821..5ddf4e2b2 100644 --- a/plugins/inputs/mongodb/README.md +++ b/plugins/inputs/mongodb/README.md @@ -49,6 +49,10 @@ and create a single measurement containing values e.g. * resident_megabytes * updates_per_sec * vsize_megabytes + * total_in_use + * total_available + * total_created + * total_refreshing * ttl_deletes_per_sec * ttl_passes_per_sec * repl_lag diff --git a/plugins/inputs/mongodb/mongodb_data.go b/plugins/inputs/mongodb/mongodb_data.go index 36eabc90c..4eab5b185 100644 --- a/plugins/inputs/mongodb/mongodb_data.go +++ b/plugins/inputs/mongodb/mongodb_data.go @@ -66,6 +66,13 @@ var DefaultClusterStats = map[string]string{ "jumbo_chunks": "JumboChunksCount", } +var DefaultShardStats = map[string]string{ + "total_in_use": "TotalInUse", + "total_available": "TotalAvailable", + "total_created": "TotalCreated", + "total_refreshing": "TotalRefreshing", +} + var MmapStats = map[string]string{ "mapped_megabytes": "Mapped", "non-mapped_megabytes": "NonMapped", @@ -127,6 +134,7 @@ func (d *MongodbData) AddDefaultStats() { d.addStat(statLine, DefaultReplStats) } d.addStat(statLine, DefaultClusterStats) + d.addStat(statLine, DefaultShardStats) if d.StatLine.StorageEngine == "mmapv1" { d.addStat(statLine, MmapStats) } else if d.StatLine.StorageEngine == "wiredTiger" { diff --git a/plugins/inputs/mongodb/mongodb_data_test.go b/plugins/inputs/mongodb/mongodb_data_test.go index 9082061d3..9a12be6a5 100644 --- a/plugins/inputs/mongodb/mongodb_data_test.go +++ b/plugins/inputs/mongodb/mongodb_data_test.go @@ -99,6 +99,27 @@ func TestAddWiredTigerStats(t *testing.T) { } } +func TestAddShardStats(t *testing.T) { + d := NewMongodbData( + &StatLine{ + TotalInUse: 0, + TotalAvailable: 0, + TotalCreated: 0, + TotalRefreshing: 0, + }, + tags, + ) + + var acc testutil.Accumulator + + d.AddDefaultStats() + d.flush(&acc) + + for key, _ := range DefaultShardStats { + assert.True(t, acc.HasInt64Field("mongodb", key)) + } +} + func TestStateTag(t *testing.T) { d := NewMongodbData( &StatLine{ @@ -147,6 +168,10 @@ func TestStateTag(t *testing.T) { "ttl_deletes_per_sec": int64(0), "ttl_passes_per_sec": int64(0), "jumbo_chunks": int64(0), + "total_in_use": int64(0), + "total_available": int64(0), + "total_created": int64(0), + "total_refreshing": int64(0), } acc.AssertContainsTaggedFields(t, "mongodb", fields, stateTags) } diff --git a/plugins/inputs/mongodb/mongodb_server.go b/plugins/inputs/mongodb/mongodb_server.go index 2bab8242b..51eb48a71 100644 --- a/plugins/inputs/mongodb/mongodb_server.go +++ b/plugins/inputs/mongodb/mongodb_server.go @@ -55,8 +55,18 @@ func (s *Server) gatherData(acc telegraf.Accumulator, gatherDbStats bool) error JumboChunksCount: int64(jumbo_chunks), } - result_db_stats := &DbStats{} + resultShards := &ShardStats{} + err = s.Session.DB("admin").Run(bson.D{ + { + Name: "shardConnPoolStats", + Value: 1, + }, + }, &resultShards) + if err != nil { + log.Println("E! Error getting database shard stats (" + err.Error() + ")") + } + result_db_stats := &DbStats{} if gatherDbStats == true { names := []string{} names, err = s.Session.DatabaseNames() @@ -88,6 +98,7 @@ func (s *Server) gatherData(acc telegraf.Accumulator, gatherDbStats bool) error ReplSetStatus: result_repl, ClusterStatus: result_cluster, DbStats: result_db_stats, + ShardStats: resultShards, } defer func() { diff --git a/plugins/inputs/mongodb/mongostat.go b/plugins/inputs/mongodb/mongostat.go index a2e270bbb..daf5a4504 100644 --- a/plugins/inputs/mongodb/mongostat.go +++ b/plugins/inputs/mongodb/mongostat.go @@ -34,6 +34,7 @@ type MongoStatus struct { ReplSetStatus *ReplSetStatus ClusterStatus *ClusterStatus DbStats *DbStats + ShardStats *ShardStats } type ServerStatus struct { @@ -116,6 +117,14 @@ type WiredTiger struct { Cache CacheStats `bson:"cache"` } +// ShardStats stores information from shardConnPoolStats. +type ShardStats struct { + TotalInUse int64 `bson:"totalInUse"` + TotalAvailable int64 `bson:"totalAvailable"` + TotalCreated int64 `bson:"totalCreated"` + TotalRefreshing int64 `bson:"totalRefreshing"` +} + type ConcurrentTransactions struct { Write ConcurrentTransStats `bson:"write"` Read ConcurrentTransStats `bson:"read"` @@ -450,6 +459,9 @@ type StatLine struct { // DB stats field DbStatsLines []DbStatLine + + // Shard stats + TotalInUse, TotalAvailable, TotalCreated, TotalRefreshing int64 } type DbStatLine struct { @@ -783,5 +795,12 @@ func NewStatLine(oldMongo, newMongo MongoStatus, key string, all bool, sampleSec returnVal.DbStatsLines = append(returnVal.DbStatsLines, *dbStatLine) } + // Set shard stats + newShardStats := *newMongo.ShardStats + returnVal.TotalInUse = newShardStats.TotalInUse + returnVal.TotalAvailable = newShardStats.TotalAvailable + returnVal.TotalCreated = newShardStats.TotalCreated + returnVal.TotalRefreshing = newShardStats.TotalRefreshing + return returnVal }