From e8bf968c78f63dc619d3d205de622941a1e45cb2 Mon Sep 17 00:00:00 2001
From: Vincent <vincent_wangxi@126.com>
Date: Sat, 17 Dec 2016 01:29:04 +0800
Subject: [PATCH] fix mongodb replica set lag awalys 0 #1449 (#2125)

---
 plugins/inputs/mongodb/mongostat.go | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/plugins/inputs/mongodb/mongostat.go b/plugins/inputs/mongodb/mongostat.go
index e77c67e1..c5ccf5a8 100644
--- a/plugins/inputs/mongodb/mongostat.go
+++ b/plugins/inputs/mongodb/mongostat.go
@@ -11,8 +11,6 @@ import (
 	"sort"
 	"strings"
 	"time"
-
-	"gopkg.in/mgo.v2/bson"
 )
 
 const (
@@ -105,10 +103,10 @@ type ReplSetStatus struct {
 
 // ReplSetMember stores information related to a replica set member
 type ReplSetMember struct {
-	Name       string               `bson:"name"`
-	State      int64                `bson:"state"`
-	StateStr   string               `bson:"stateStr"`
-	OptimeDate *bson.MongoTimestamp `bson:"optimeDate"`
+	Name       string    `bson:"name"`
+	State      int64     `bson:"state"`
+	StateStr   string    `bson:"stateStr"`
+	OptimeDate time.Time `bson:"optimeDate"`
 }
 
 // WiredTiger stores information related to the WiredTiger storage engine.
@@ -712,9 +710,9 @@ func NewStatLine(oldMongo, newMongo MongoStatus, key string, all bool, sampleSec
 			}
 		}
 
-		if me.OptimeDate != nil && master.OptimeDate != nil && me.State == 2 {
-			// MongoTimestamp type is int64 where the first 32bits are the unix timestamp
-			lag := int64(*master.OptimeDate>>32 - *me.OptimeDate>>32)
+		if me.State == 2 {
+			// OptimeDate.Unix() type is int64
+			lag := master.OptimeDate.Unix() - me.OptimeDate.Unix()
 			if lag < 0 {
 				returnVal.ReplLag = 0
 			} else {
-- 
GitLab