From b860bdfa2d8e81131204a187bdd9c3908be06f4c Mon Sep 17 00:00:00 2001 From: Conor Branagan Date: Tue, 14 Jan 2014 21:50:28 +0000 Subject: [PATCH] Handle multiple servers for mongo replica status change --- checks.d/mongo.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/checks.d/mongo.py b/checks.d/mongo.py index 6288c3458e..f0e9f8578c 100644 --- a/checks.d/mongo.py +++ b/checks.d/mongo.py @@ -58,8 +58,7 @@ class MongoDb(AgentCheck): def __init__(self, name, init_config, agentConfig): AgentCheck.__init__(self, name, init_config, agentConfig) - - self._last_state = -1 + self._last_state_by_server = {} def get_library_versions(self): try: @@ -72,12 +71,12 @@ def get_library_versions(self): return {"pymongo": version} - def checkLastState(self, state, agentConfig): - if self._last_state != state: - self._last_state = state - return self.create_event(state, agentConfig) + def check_last_state(self, state, server, agentConfig): + if self._last_state_by_server.get(server, -1) != state: + self._last_state_by_server[server] = state + return self.create_event(state, server, agentConfig) - def create_event(self, state, agentConfig): + def create_event(self, state, server, agentConfig): """Create an event with a message describing the replication state of a mongo node""" @@ -95,8 +94,8 @@ def get_state_description(state): status = get_state_description(state) hostname = get_hostname(agentConfig) - msg_title = "%s is %s" % (hostname, status) - msg = "MongoDB: %s just reported as %s" % (hostname, status) + msg_title = "%s is %s" % (server, status) + msg = "MongoDB %s just reported as %s" % (server, status) self.event({ 'timestamp': int(time.time()), @@ -191,7 +190,7 @@ def check(self, instance): data['health'] = current['health'] data['state'] = replSet['myState'] - self.checkLastState(data['state'], self.agentConfig) + self.check_last_state(data['state'], server, self.agentConfig) status['replSet'] = data except Exception, e: if "OperationFailure" in repr(e) and "replSetGetStatus" in str(e):