2015-07-07 01:20:11 +00:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
2016-04-19 23:16:22 +00:00
|
|
|
"log"
|
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
|
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")
|
|
|
|
}
|
|
|
|
|
2018-04-06 23:34:47 +00:00
|
|
|
func (s *Server) gatherOplogStats() *OplogStats {
|
|
|
|
stats := &OplogStats{}
|
|
|
|
localdb := s.Session.DB("local")
|
|
|
|
|
|
|
|
op_first := oplogEntry{}
|
|
|
|
op_last := oplogEntry{}
|
|
|
|
query := bson.M{"ts": bson.M{"$exists": true}}
|
|
|
|
|
|
|
|
for _, collection_name := range []string{"oplog.rs", "oplog.$main"} {
|
|
|
|
if err := localdb.C(collection_name).Find(query).Sort("$natural").Limit(1).One(&op_first); err != nil {
|
|
|
|
if err == mgo.ErrNotFound {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-17 18:44:48 +00:00
|
|
|
if IsAuthorization(err) {
|
|
|
|
log.Println("D! Error getting first oplog entry (" + err.Error() + ")")
|
|
|
|
} else {
|
|
|
|
log.Println("E! Error getting first oplog entry (" + err.Error() + ")")
|
|
|
|
}
|
2018-04-06 23:34:47 +00:00
|
|
|
return stats
|
|
|
|
}
|
|
|
|
if err := localdb.C(collection_name).Find(query).Sort("-$natural").Limit(1).One(&op_last); err != nil {
|
2018-10-17 18:44:48 +00:00
|
|
|
if err == mgo.ErrNotFound || IsAuthorization(err) {
|
2018-04-06 23:34:47 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-10-17 18:44:48 +00:00
|
|
|
if IsAuthorization(err) {
|
|
|
|
log.Println("D! Error getting first oplog entry (" + err.Error() + ")")
|
|
|
|
} else {
|
|
|
|
log.Println("E! Error getting first oplog entry (" + err.Error() + ")")
|
|
|
|
}
|
2018-04-06 23:34:47 +00:00
|
|
|
return stats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
op_first_time := time.Unix(int64(op_first.Timestamp>>32), 0)
|
|
|
|
op_last_time := time.Unix(int64(op_last.Timestamp>>32), 0)
|
|
|
|
stats.TimeDiff = int64(op_last_time.Sub(op_first_time).Seconds())
|
|
|
|
return stats
|
|
|
|
}
|
|
|
|
|
2016-07-19 11:47:12 +00:00
|
|
|
func (s *Server) gatherData(acc telegraf.Accumulator, gatherDbStats bool) error {
|
2015-07-07 01:20:11 +00:00
|
|
|
s.Session.SetMode(mgo.Eventual, true)
|
|
|
|
s.Session.SetSocketTimeout(0)
|
2016-04-19 23:16:22 +00:00
|
|
|
result_server := &ServerStatus{}
|
2016-09-09 14:46:31 +00:00
|
|
|
err := s.Session.DB("admin").Run(bson.D{
|
|
|
|
{
|
|
|
|
Name: "serverStatus",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "recordStats",
|
|
|
|
Value: 0,
|
|
|
|
},
|
|
|
|
}, result_server)
|
2015-07-07 01:20:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-19 23:16:22 +00:00
|
|
|
result_repl := &ReplSetStatus{}
|
2017-01-11 17:50:01 +00:00
|
|
|
// ignore error because it simply indicates that the db is not a member
|
|
|
|
// in a replica set, which is fine.
|
|
|
|
_ = s.Session.DB("admin").Run(bson.D{
|
2016-09-09 14:46:31 +00:00
|
|
|
{
|
|
|
|
Name: "replSetGetStatus",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
}, result_repl)
|
2016-04-19 23:16:22 +00:00
|
|
|
|
2016-04-29 16:40:26 +00:00
|
|
|
jumbo_chunks, _ := s.Session.DB("config").C("chunks").Find(bson.M{"jumbo": true}).Count()
|
|
|
|
|
|
|
|
result_cluster := &ClusterStatus{
|
|
|
|
JumboChunksCount: int64(jumbo_chunks),
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:55:56 +00:00
|
|
|
resultShards := &ShardStats{}
|
|
|
|
err = s.Session.DB("admin").Run(bson.D{
|
|
|
|
{
|
|
|
|
Name: "shardConnPoolStats",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
}, &resultShards)
|
|
|
|
if err != nil {
|
2018-10-17 18:44:48 +00:00
|
|
|
if IsAuthorization(err) {
|
|
|
|
log.Println("D! Error getting database shard stats (" + err.Error() + ")")
|
|
|
|
} else {
|
|
|
|
log.Println("E! Error getting database shard stats (" + err.Error() + ")")
|
|
|
|
}
|
2018-02-20 21:55:56 +00:00
|
|
|
}
|
2016-07-19 11:47:12 +00:00
|
|
|
|
2018-04-06 23:34:47 +00:00
|
|
|
oplogStats := s.gatherOplogStats()
|
|
|
|
|
2018-02-20 21:55:56 +00:00
|
|
|
result_db_stats := &DbStats{}
|
2016-07-19 11:47:12 +00:00
|
|
|
if gatherDbStats == true {
|
|
|
|
names := []string{}
|
|
|
|
names, err = s.Session.DatabaseNames()
|
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Println("E! Error getting database names (" + err.Error() + ")")
|
2016-07-19 11:47:12 +00:00
|
|
|
}
|
|
|
|
for _, db_name := range names {
|
|
|
|
db_stat_line := &DbStatsData{}
|
2016-09-09 14:46:31 +00:00
|
|
|
err = s.Session.DB(db_name).Run(bson.D{
|
|
|
|
{
|
|
|
|
Name: "dbStats",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
}, db_stat_line)
|
2016-07-19 11:47:12 +00:00
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Println("E! Error getting db stats from " + db_name + "(" + err.Error() + ")")
|
2016-07-19 11:47:12 +00:00
|
|
|
}
|
|
|
|
db := &Db{
|
|
|
|
Name: db_name,
|
|
|
|
DbStatsData: db_stat_line,
|
|
|
|
}
|
|
|
|
|
|
|
|
result_db_stats.Dbs = append(result_db_stats.Dbs, *db)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 23:16:22 +00:00
|
|
|
result := &MongoStatus{
|
|
|
|
ServerStatus: result_server,
|
|
|
|
ReplSetStatus: result_repl,
|
2016-04-29 16:40:26 +00:00
|
|
|
ClusterStatus: result_cluster,
|
2016-07-19 11:47:12 +00:00
|
|
|
DbStats: result_db_stats,
|
2018-02-20 21:55:56 +00:00
|
|
|
ShardStats: resultShards,
|
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
|
|
|
defer func() {
|
|
|
|
s.lastResult = result
|
|
|
|
}()
|
|
|
|
|
|
|
|
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()
|
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
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|