2015-07-07 01:20:11 +00:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2015-07-07 01:20:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MongodbData struct {
|
|
|
|
StatLine *StatLine
|
2015-12-19 20:31:22 +00:00
|
|
|
Fields map[string]interface{}
|
2015-07-07 01:20:11 +00:00
|
|
|
Tags map[string]string
|
2016-07-19 11:47:12 +00:00
|
|
|
DbData []DbData
|
|
|
|
}
|
|
|
|
|
|
|
|
type DbData struct {
|
|
|
|
Name string
|
|
|
|
Fields map[string]interface{}
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMongodbData(statLine *StatLine, tags map[string]string) *MongodbData {
|
|
|
|
return &MongodbData{
|
|
|
|
StatLine: statLine,
|
|
|
|
Tags: tags,
|
2015-12-19 20:31:22 +00:00
|
|
|
Fields: make(map[string]interface{}),
|
2016-07-19 11:47:12 +00:00
|
|
|
DbData: []DbData{},
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultStats = map[string]string{
|
2016-04-19 18:55:03 +00:00
|
|
|
"inserts_per_sec": "Insert",
|
|
|
|
"queries_per_sec": "Query",
|
|
|
|
"updates_per_sec": "Update",
|
|
|
|
"deletes_per_sec": "Delete",
|
|
|
|
"getmores_per_sec": "GetMore",
|
|
|
|
"commands_per_sec": "Command",
|
|
|
|
"flushes_per_sec": "Flushes",
|
|
|
|
"vsize_megabytes": "Virtual",
|
|
|
|
"resident_megabytes": "Resident",
|
|
|
|
"queued_reads": "QueuedReaders",
|
|
|
|
"queued_writes": "QueuedWriters",
|
|
|
|
"active_reads": "ActiveReaders",
|
|
|
|
"active_writes": "ActiveWriters",
|
|
|
|
"net_in_bytes": "NetIn",
|
|
|
|
"net_out_bytes": "NetOut",
|
|
|
|
"open_connections": "NumConnections",
|
|
|
|
"ttl_deletes_per_sec": "DeletedDocuments",
|
|
|
|
"ttl_passes_per_sec": "Passes",
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultReplStats = map[string]string{
|
|
|
|
"repl_inserts_per_sec": "InsertR",
|
|
|
|
"repl_queries_per_sec": "QueryR",
|
|
|
|
"repl_updates_per_sec": "UpdateR",
|
|
|
|
"repl_deletes_per_sec": "DeleteR",
|
|
|
|
"repl_getmores_per_sec": "GetMoreR",
|
|
|
|
"repl_commands_per_sec": "CommandR",
|
|
|
|
"member_status": "NodeType",
|
2016-12-16 13:46:32 +00:00
|
|
|
"state": "NodeState",
|
2016-04-19 23:16:22 +00:00
|
|
|
"repl_lag": "ReplLag",
|
2018-04-10 00:04:31 +00:00
|
|
|
"repl_oplog_window_sec": "OplogTimeDiff",
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 16:40:26 +00:00
|
|
|
var DefaultClusterStats = map[string]string{
|
|
|
|
"jumbo_chunks": "JumboChunksCount",
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:55:56 +00:00
|
|
|
var DefaultShardStats = map[string]string{
|
|
|
|
"total_in_use": "TotalInUse",
|
|
|
|
"total_available": "TotalAvailable",
|
|
|
|
"total_created": "TotalCreated",
|
|
|
|
"total_refreshing": "TotalRefreshing",
|
|
|
|
}
|
|
|
|
|
2015-07-07 01:20:11 +00:00
|
|
|
var MmapStats = map[string]string{
|
|
|
|
"mapped_megabytes": "Mapped",
|
|
|
|
"non-mapped_megabytes": "NonMapped",
|
|
|
|
"page_faults_per_sec": "Faults",
|
|
|
|
}
|
|
|
|
|
|
|
|
var WiredTigerStats = map[string]string{
|
|
|
|
"percent_cache_dirty": "CacheDirtyPercent",
|
|
|
|
"percent_cache_used": "CacheUsedPercent",
|
|
|
|
}
|
|
|
|
|
2017-10-03 00:38:51 +00:00
|
|
|
var WiredTigerExtStats = map[string]string{
|
|
|
|
"wtcache_tracked_dirty_bytes": "TrackedDirtyBytes",
|
|
|
|
"wtcache_current_bytes": "CurrentCachedBytes",
|
|
|
|
"wtcache_max_bytes_configured": "MaxBytesConfigured",
|
|
|
|
"wtcache_app_threads_page_read_count": "AppThreadsPageReadCount",
|
|
|
|
"wtcache_app_threads_page_read_time": "AppThreadsPageReadTime",
|
|
|
|
"wtcache_app_threads_page_write_count": "AppThreadsPageWriteCount",
|
|
|
|
"wtcache_bytes_written_from": "BytesWrittenFrom",
|
|
|
|
"wtcache_bytes_read_into": "BytesReadInto",
|
|
|
|
"wtcache_pages_evicted_by_app_thread": "PagesEvictedByAppThread",
|
|
|
|
"wtcache_pages_queued_for_eviction": "PagesQueuedForEviction",
|
|
|
|
"wtcache_server_evicting_pages": "ServerEvictingPages",
|
|
|
|
"wtcache_worker_thread_evictingpages": "WorkerThreadEvictingPages",
|
|
|
|
}
|
|
|
|
|
2016-07-19 11:47:12 +00:00
|
|
|
var DbDataStats = map[string]string{
|
|
|
|
"collections": "Collections",
|
|
|
|
"objects": "Objects",
|
|
|
|
"avg_obj_size": "AvgObjSize",
|
|
|
|
"data_size": "DataSize",
|
|
|
|
"storage_size": "StorageSize",
|
|
|
|
"num_extents": "NumExtents",
|
|
|
|
"indexes": "Indexes",
|
|
|
|
"index_size": "IndexSize",
|
|
|
|
"ok": "Ok",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MongodbData) AddDbStats() {
|
|
|
|
for _, dbstat := range d.StatLine.DbStatsLines {
|
|
|
|
dbStatLine := reflect.ValueOf(&dbstat).Elem()
|
|
|
|
newDbData := &DbData{
|
|
|
|
Name: dbstat.Name,
|
|
|
|
Fields: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
newDbData.Fields["type"] = "db_stat"
|
|
|
|
for key, value := range DbDataStats {
|
|
|
|
val := dbStatLine.FieldByName(value).Interface()
|
|
|
|
newDbData.Fields[key] = val
|
|
|
|
}
|
|
|
|
d.DbData = append(d.DbData, *newDbData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-19 20:31:22 +00:00
|
|
|
func (d *MongodbData) AddDefaultStats() {
|
2015-07-07 01:20:11 +00:00
|
|
|
statLine := reflect.ValueOf(d.StatLine).Elem()
|
2015-12-19 20:31:22 +00:00
|
|
|
d.addStat(statLine, DefaultStats)
|
2015-07-07 01:20:11 +00:00
|
|
|
if d.StatLine.NodeType != "" {
|
2015-12-19 20:31:22 +00:00
|
|
|
d.addStat(statLine, DefaultReplStats)
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
2016-04-29 16:40:26 +00:00
|
|
|
d.addStat(statLine, DefaultClusterStats)
|
2018-02-20 21:55:56 +00:00
|
|
|
d.addStat(statLine, DefaultShardStats)
|
2018-03-29 20:32:05 +00:00
|
|
|
if d.StatLine.StorageEngine == "mmapv1" || d.StatLine.StorageEngine == "rocksdb" {
|
2015-12-19 20:31:22 +00:00
|
|
|
d.addStat(statLine, MmapStats)
|
2015-07-07 01:20:11 +00:00
|
|
|
} else if d.StatLine.StorageEngine == "wiredTiger" {
|
|
|
|
for key, value := range WiredTigerStats {
|
|
|
|
val := statLine.FieldByName(value).Interface()
|
|
|
|
percentVal := fmt.Sprintf("%.1f", val.(float64)*100)
|
|
|
|
floatVal, _ := strconv.ParseFloat(percentVal, 64)
|
2015-12-19 20:31:22 +00:00
|
|
|
d.add(key, floatVal)
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
2017-10-03 00:38:51 +00:00
|
|
|
d.addStat(statLine, WiredTigerExtStats)
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 00:38:51 +00:00
|
|
|
func (d *MongodbData) addStat(statLine reflect.Value, stats map[string]string) {
|
2015-07-07 01:20:11 +00:00
|
|
|
for key, value := range stats {
|
|
|
|
val := statLine.FieldByName(value).Interface()
|
2015-12-19 20:31:22 +00:00
|
|
|
d.add(key, val)
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-19 20:31:22 +00:00
|
|
|
func (d *MongodbData) add(key string, val interface{}) {
|
|
|
|
d.Fields[key] = val
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (d *MongodbData) flush(acc telegraf.Accumulator) {
|
2015-10-16 22:13:32 +00:00
|
|
|
acc.AddFields(
|
2015-12-19 20:31:22 +00:00
|
|
|
"mongodb",
|
|
|
|
d.Fields,
|
2015-07-07 01:20:11 +00:00
|
|
|
d.Tags,
|
|
|
|
d.StatLine.Time,
|
|
|
|
)
|
2015-12-19 20:31:22 +00:00
|
|
|
d.Fields = make(map[string]interface{})
|
2016-07-19 11:47:12 +00:00
|
|
|
|
|
|
|
for _, db := range d.DbData {
|
|
|
|
d.Tags["db_name"] = db.Name
|
|
|
|
acc.AddFields(
|
|
|
|
"mongodb_db_stats",
|
|
|
|
db.Fields,
|
|
|
|
d.Tags,
|
|
|
|
d.StatLine.Time,
|
|
|
|
)
|
|
|
|
db.Fields = make(map[string]interface{})
|
|
|
|
}
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|