Skip to content

Commit b860bdf

Browse files
committed
Handle multiple servers for mongo replica status change
1 parent 43071c7 commit b860bdf

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

checks.d/mongo.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ class MongoDb(AgentCheck):
5858

5959
def __init__(self, name, init_config, agentConfig):
6060
AgentCheck.__init__(self, name, init_config, agentConfig)
61-
62-
self._last_state = -1
61+
self._last_state_by_server = {}
6362

6463
def get_library_versions(self):
6564
try:
@@ -72,12 +71,12 @@ def get_library_versions(self):
7271

7372
return {"pymongo": version}
7473

75-
def checkLastState(self, state, agentConfig):
76-
if self._last_state != state:
77-
self._last_state = state
78-
return self.create_event(state, agentConfig)
74+
def check_last_state(self, state, server, agentConfig):
75+
if self._last_state_by_server.get(server, -1) != state:
76+
self._last_state_by_server[server] = state
77+
return self.create_event(state, server, agentConfig)
7978

80-
def create_event(self, state, agentConfig):
79+
def create_event(self, state, server, agentConfig):
8180
"""Create an event with a message describing the replication
8281
state of a mongo node"""
8382

@@ -95,8 +94,8 @@ def get_state_description(state):
9594

9695
status = get_state_description(state)
9796
hostname = get_hostname(agentConfig)
98-
msg_title = "%s is %s" % (hostname, status)
99-
msg = "MongoDB: %s just reported as %s" % (hostname, status)
97+
msg_title = "%s is %s" % (server, status)
98+
msg = "MongoDB %s just reported as %s" % (server, status)
10099

101100
self.event({
102101
'timestamp': int(time.time()),
@@ -191,7 +190,7 @@ def check(self, instance):
191190
data['health'] = current['health']
192191

193192
data['state'] = replSet['myState']
194-
self.checkLastState(data['state'], self.agentConfig)
193+
self.check_last_state(data['state'], server, self.agentConfig)
195194
status['replSet'] = data
196195
except Exception, e:
197196
if "OperationFailure" in repr(e) and "replSetGetStatus" in str(e):

0 commit comments

Comments
 (0)