2015-07-07 01:20:11 +00:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
2019-09-23 22:39:50 +00:00
|
|
|
"fmt"
|
2015-07-07 01:20:11 +00:00
|
|
|
"net/url"
|
2018-10-17 18:44:48 +00:00
|
|
|
"strings"
|
2015-07-07 01:20:11 +00:00
|
|
|
"time"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2015-07-07 01:20:11 +00:00
|
|
|
"gopkg.in/mgo.v2"
|
|
|
|
"gopkg.in/mgo.v2/bson"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
Url *url.URL
|
|
|
|
Session *mgo.Session
|
2016-04-19 23:16:22 +00:00
|
|
|
lastResult *MongoStatus
|
2019-09-23 22:39:50 +00:00
|
|
|
|
|
|
|
Log telegraf.Logger
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getDefaultTags() map[string]string {
|
|
|
|
tags := make(map[string]string)
|
2015-07-09 20:06:18 +00:00
|
|
|
tags["hostname"] = s.Url.Host
|
2015-07-07 01:20:11 +00:00
|
|
|
return tags
|
|
|
|
}
|
|
|
|
|
2018-04-06 23:34:47 +00:00
|
|
|
type oplogEntry struct {
|
|
|
|
Timestamp bson.MongoTimestamp `bson:"ts"`
|
|
|
|
}
|
|
|
|
|
2018-10-17 18:44:48 +00:00
|
|
|
func IsAuthorization(err error) bool {
|
|
|
|
return strings.Contains(err.Error(), "not authorized")
|
|
|
|
}
|
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
func (s *Server) authLog(err error) {
|
2019-08-14 23:59:02 +00:00
|
|
|
if IsAuthorization(err) {
|
2019-09-23 22:39:50 +00:00
|
|
|
s.Log.Debug(err.Error())
|
2019-08-14 23:59:02 +00:00
|
|
|
} else {
|
2019-09-23 22:39:50 +00:00
|
|
|
s.Log.Error(err.Error())
|
2019-08-14 23:59:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:31:42 +00:00
|
|
|
func (s *Server) gatherServerStatus() (*ServerStatus, error) {
|
|
|
|
serverStatus := &ServerStatus{}
|
|
|
|
err := s.Session.DB("admin").Run(bson.D{
|
|
|
|
{
|
|
|
|
Name: "serverStatus",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "recordStats",
|
|
|
|
Value: 0,
|
|
|
|
},
|
|
|
|
}, serverStatus)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return serverStatus, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) gatherReplSetStatus() (*ReplSetStatus, error) {
|
|
|
|
replSetStatus := &ReplSetStatus{}
|
|
|
|
err := s.Session.DB("admin").Run(bson.D{
|
|
|
|
{
|
|
|
|
Name: "replSetGetStatus",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
}, replSetStatus)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return replSetStatus, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) gatherClusterStatus() (*ClusterStatus, error) {
|
|
|
|
chunkCount, err := s.Session.DB("config").C("chunks").Find(bson.M{"jumbo": true}).Count()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ClusterStatus{
|
|
|
|
JumboChunksCount: int64(chunkCount),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) gatherShardConnPoolStats() (*ShardStats, error) {
|
|
|
|
shardStats := &ShardStats{}
|
|
|
|
err := s.Session.DB("admin").Run(bson.D{
|
|
|
|
{
|
|
|
|
Name: "shardConnPoolStats",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
}, &shardStats)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return shardStats, nil
|
|
|
|
}
|
2018-04-06 23:34:47 +00:00
|
|
|
|
2019-08-27 17:31:42 +00:00
|
|
|
func (s *Server) gatherDBStats(name string) (*Db, error) {
|
|
|
|
stats := &DbStatsData{}
|
|
|
|
err := s.Session.DB(name).Run(bson.D{
|
|
|
|
{
|
|
|
|
Name: "dbStats",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
}, stats)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Db{
|
|
|
|
Name: name,
|
|
|
|
DbStatsData: stats,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getOplogReplLag(collection string) (*OplogStats, error) {
|
2018-04-06 23:34:47 +00:00
|
|
|
query := bson.M{"ts": bson.M{"$exists": true}}
|
|
|
|
|
2019-08-27 17:31:42 +00:00
|
|
|
var first oplogEntry
|
|
|
|
err := s.Session.DB("local").C(collection).Find(query).Sort("$natural").Limit(1).One(&first)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var last oplogEntry
|
|
|
|
err = s.Session.DB("local").C(collection).Find(query).Sort("-$natural").Limit(1).One(&last)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
firstTime := time.Unix(int64(first.Timestamp>>32), 0)
|
|
|
|
lastTime := time.Unix(int64(last.Timestamp>>32), 0)
|
|
|
|
stats := &OplogStats{
|
|
|
|
TimeDiff: int64(lastTime.Sub(firstTime).Seconds()),
|
|
|
|
}
|
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The "oplog.rs" collection is stored on all replica set members.
|
|
|
|
//
|
|
|
|
// The "oplog.$main" collection is created on the master node of a
|
|
|
|
// master-slave replicated deployment. As of MongoDB 3.2, master-slave
|
|
|
|
// replication has been deprecated.
|
|
|
|
func (s *Server) gatherOplogStats() (*OplogStats, error) {
|
|
|
|
stats, err := s.getOplogReplLag("oplog.rs")
|
|
|
|
if err == nil {
|
|
|
|
return stats, nil
|
2018-04-06 23:34:47 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 17:31:42 +00:00
|
|
|
return s.getOplogReplLag("oplog.$main")
|
2018-04-06 23:34:47 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 23:59:02 +00:00
|
|
|
func (s *Server) gatherCollectionStats(colStatsDbs []string) (*ColStats, error) {
|
|
|
|
names, err := s.Session.DatabaseNames()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
results := &ColStats{}
|
2019-09-23 22:39:50 +00:00
|
|
|
for _, dbName := range names {
|
|
|
|
if stringInSlice(dbName, colStatsDbs) || len(colStatsDbs) == 0 {
|
2019-08-14 23:59:02 +00:00
|
|
|
var colls []string
|
2019-09-23 22:39:50 +00:00
|
|
|
colls, err = s.Session.DB(dbName).CollectionNames()
|
2019-08-14 23:59:02 +00:00
|
|
|
if err != nil {
|
2019-09-23 22:39:50 +00:00
|
|
|
s.Log.Errorf("Error getting collection names: %s", err.Error())
|
2019-08-14 23:59:02 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-09-23 22:39:50 +00:00
|
|
|
for _, colName := range colls {
|
|
|
|
colStatLine := &ColStatsData{}
|
|
|
|
err = s.Session.DB(dbName).Run(bson.D{
|
2019-08-14 23:59:02 +00:00
|
|
|
{
|
|
|
|
Name: "collStats",
|
2019-09-23 22:39:50 +00:00
|
|
|
Value: colName,
|
2019-08-14 23:59:02 +00:00
|
|
|
},
|
2019-09-23 22:39:50 +00:00
|
|
|
}, colStatLine)
|
2019-08-14 23:59:02 +00:00
|
|
|
if err != nil {
|
2019-09-23 22:39:50 +00:00
|
|
|
s.authLog(fmt.Errorf("error getting col stats from %q: %v", colName, err))
|
2019-08-14 23:59:02 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
collection := &Collection{
|
2019-09-23 22:39:50 +00:00
|
|
|
Name: colName,
|
|
|
|
DbName: dbName,
|
|
|
|
ColStatsData: colStatLine,
|
2019-08-14 23:59:02 +00:00
|
|
|
}
|
|
|
|
results.Collections = append(results.Collections, *collection)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 04:36:19 +00:00
|
|
|
func (s *Server) gatherData(acc telegraf.Accumulator, gatherDbStats bool, gatherColStats bool, colStatsDbs []string) error {
|
2015-07-07 01:20:11 +00:00
|
|
|
s.Session.SetMode(mgo.Eventual, true)
|
|
|
|
s.Session.SetSocketTimeout(0)
|
2019-08-27 17:31:42 +00:00
|
|
|
|
|
|
|
serverStatus, err := s.gatherServerStatus()
|
2015-07-07 01:20:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-19 23:16:22 +00:00
|
|
|
|
2019-08-27 17:31:42 +00:00
|
|
|
// Get replica set status, an error indicates that the server is not a
|
|
|
|
// member of a replica set.
|
|
|
|
replSetStatus, err := s.gatherReplSetStatus()
|
|
|
|
if err != nil {
|
2019-09-23 22:39:50 +00:00
|
|
|
s.Log.Debugf("Unable to gather replica set status: %s", err.Error())
|
2019-08-27 17:31:42 +00:00
|
|
|
}
|
2016-04-29 16:40:26 +00:00
|
|
|
|
2019-08-27 17:31:42 +00:00
|
|
|
// Gather the oplog if we are a member of a replica set. Non-replica set
|
|
|
|
// members do not have the oplog collections.
|
|
|
|
var oplogStats *OplogStats
|
|
|
|
if replSetStatus != nil {
|
|
|
|
oplogStats, err = s.gatherOplogStats()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-29 16:40:26 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 17:31:42 +00:00
|
|
|
clusterStatus, err := s.gatherClusterStatus()
|
2018-02-20 21:55:56 +00:00
|
|
|
if err != nil {
|
2019-09-23 22:39:50 +00:00
|
|
|
s.Log.Debugf("Unable to gather cluster status: %s", err.Error())
|
2018-02-20 21:55:56 +00:00
|
|
|
}
|
2016-07-19 11:47:12 +00:00
|
|
|
|
2019-08-27 17:31:42 +00:00
|
|
|
shardStats, err := s.gatherShardConnPoolStats()
|
|
|
|
if err != nil {
|
2019-09-23 22:39:50 +00:00
|
|
|
s.authLog(fmt.Errorf("unable to gather shard connection pool stats: %s", err.Error()))
|
2019-08-27 17:31:42 +00:00
|
|
|
}
|
2018-04-06 23:34:47 +00:00
|
|
|
|
2019-09-09 22:55:46 +00:00
|
|
|
var collectionStats *ColStats
|
|
|
|
if gatherColStats {
|
|
|
|
stats, err := s.gatherCollectionStats(colStatsDbs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
collectionStats = stats
|
2019-08-27 17:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dbStats := &DbStats{}
|
|
|
|
if gatherDbStats {
|
|
|
|
names, err := s.Session.DatabaseNames()
|
2016-07-19 11:47:12 +00:00
|
|
|
if err != nil {
|
2019-08-27 17:31:42 +00:00
|
|
|
return err
|
2016-07-19 11:47:12 +00:00
|
|
|
}
|
2019-08-27 17:31:42 +00:00
|
|
|
|
|
|
|
for _, name := range names {
|
|
|
|
db, err := s.gatherDBStats(name)
|
2016-07-19 11:47:12 +00:00
|
|
|
if err != nil {
|
2019-09-23 22:39:50 +00:00
|
|
|
s.Log.Debugf("Error getting db stats from %q: %s", name, err.Error())
|
2016-07-19 11:47:12 +00:00
|
|
|
}
|
2019-08-27 17:31:42 +00:00
|
|
|
dbStats.Dbs = append(dbStats.Dbs, *db)
|
2016-07-19 11:47:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 23:16:22 +00:00
|
|
|
result := &MongoStatus{
|
2019-08-27 17:31:42 +00:00
|
|
|
ServerStatus: serverStatus,
|
|
|
|
ReplSetStatus: replSetStatus,
|
|
|
|
ClusterStatus: clusterStatus,
|
|
|
|
DbStats: dbStats,
|
|
|
|
ColStats: collectionStats,
|
|
|
|
ShardStats: shardStats,
|
2018-04-06 23:34:47 +00:00
|
|
|
OplogStats: oplogStats,
|
2016-04-19 23:16:22 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 01:20:11 +00:00
|
|
|
result.SampleTime = time.Now()
|
|
|
|
if s.lastResult != nil && result != nil {
|
|
|
|
duration := result.SampleTime.Sub(s.lastResult.SampleTime)
|
|
|
|
durationInSeconds := int64(duration.Seconds())
|
|
|
|
if durationInSeconds == 0 {
|
|
|
|
durationInSeconds = 1
|
|
|
|
}
|
|
|
|
data := NewMongodbData(
|
|
|
|
NewStatLine(*s.lastResult, *result, s.Url.Host, true, durationInSeconds),
|
|
|
|
s.getDefaultTags(),
|
|
|
|
)
|
2015-12-19 20:31:22 +00:00
|
|
|
data.AddDefaultStats()
|
2016-07-19 11:47:12 +00:00
|
|
|
data.AddDbStats()
|
2019-07-31 04:36:19 +00:00
|
|
|
data.AddColStats()
|
2018-04-11 00:10:29 +00:00
|
|
|
data.AddShardHostStats()
|
2015-12-19 20:31:22 +00:00
|
|
|
data.flush(acc)
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
2019-08-27 17:31:42 +00:00
|
|
|
|
|
|
|
s.lastResult = result
|
2015-07-07 01:20:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-07-31 04:36:19 +00:00
|
|
|
|
|
|
|
func stringInSlice(a string, list []string) bool {
|
|
|
|
for _, b := range list {
|
|
|
|
if b == a {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|