Skip to content

Commit

Permalink
Handle multiple servers for mongo replica status change
Browse files Browse the repository at this point in the history
  • Loading branch information
conorbranagan committed Jan 14, 2014
1 parent 43071c7 commit b860bdf
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions checks.d/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"""

Expand All @@ -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()),
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit b860bdf

Please sign in to comment.