Skip to content

Commit

Permalink
[mongo] allow disabling of replica access (#1516)
Browse files Browse the repository at this point in the history
* [mongo] allow disabling of replica access

* address review

* fix
  • Loading branch information
ofek authored May 7, 2018
1 parent ca0ad36 commit e591866
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 76 deletions.
6 changes: 6 additions & 0 deletions mongo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG - mongo

1.5.4 / Unreleased
==================
### Changes

* [IMPROVEMENT] Allow disabling of replica access. See #1516

1.5.3 / Unreleased
==================
### Changes
Expand Down
6 changes: 5 additions & 1 deletion mongo/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ instances:
# - optional_tag1
# - optional_tag2

# Whether or not to read from available replicas (default true).
# Disable this if any replicas are inaccessible to the agent.
replica_check: true

# Optional SSL parameters, see https://github.com/mongodb/mongo-python-driver/blob/2.6.3/pymongo/mongo_client.py#L193-L212
# for more details
#
Expand Down Expand Up @@ -63,7 +67,7 @@ instances:
# source : (mandatory) attribute that defines which integration is sending the logs
# sourcecategory : (optional) Multiple value attribute. Can be used to refine the source attribtue
# tags: (optional) add tags to each logs collected

# - type: file
# path: /var/log/mongodb/mongodb.log
# service: mongo
Expand Down
2 changes: 1 addition & 1 deletion mongo/datadog_checks/mongo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

MongoDb = mongo.MongoDb

__version__ = "1.5.3"
__version__ = "1.5.4"

__all__ = ['mongo']
147 changes: 74 additions & 73 deletions mongo/datadog_checks/mongo/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,80 +777,81 @@ def total_seconds(td):
# Handle replica data, if any
# See
# http://www.mongodb.org/display/DOCS/Replica+Set+Commands#ReplicaSetCommands-replSetGetStatus # noqa
try:
data = {}
dbnames = []

replSet = admindb.command('replSetGetStatus')
if replSet:
primary = None
current = None

# need a new connection to deal with replica sets
setname = replSet.get('set')
cli_rs = pymongo.mongo_client.MongoClient(
server,
socketTimeoutMS=timeout,
connectTimeoutMS=timeout,
serverSelectionTimeoutMS=timeout,
replicaset=setname,
read_preference=pymongo.ReadPreference.NEAREST,
**ssl_params)

if do_auth:
if auth_source:
self._authenticate(cli_rs[auth_source], username, password, use_x509, server, service_check_tags)
else:
self._authenticate(cli_rs[db_name], username, password, use_x509, server, service_check_tags)

# Replication set information
replset_name = replSet['set']
replset_state = self.get_state_name(replSet['myState']).lower()

tags.extend([
u"replset_name:{0}".format(replset_name),
u"replset_state:{0}".format(replset_state),
])

# Find nodes: master and current node (ourself)
for member in replSet.get('members'):
if member.get('self'):
current = member
if int(member.get('state')) == 1:
primary = member

# Compute a lag time
if current is not None and primary is not None:
if 'optimeDate' in primary and 'optimeDate' in current:
lag = primary['optimeDate'] - current['optimeDate']
data['replicationLag'] = total_seconds(lag)

if current is not None:
data['health'] = current['health']

data['state'] = replSet['myState']

if current is not None:
total = 0.0
cfg = cli_rs['local']['system.replset'].find_one()
for member in cfg.get('members'):
total += member.get('votes', 1)
if member['_id'] == current['_id']:
data['votes'] = member.get('votes', 1)
data['voteFraction'] = data['votes'] / total

status['replSet'] = data

# Submit events
self._report_replica_set_state(
data['state'], clean_server_name, replset_name, self.agentConfig
)
if _is_affirmative(instance.get('replica_check', True)):
try:
data = {}
dbnames = []

replSet = admindb.command('replSetGetStatus')
if replSet:
primary = None
current = None

# need a new connection to deal with replica sets
setname = replSet.get('set')
cli_rs = pymongo.mongo_client.MongoClient(
server,
socketTimeoutMS=timeout,
connectTimeoutMS=timeout,
serverSelectionTimeoutMS=timeout,
replicaset=setname,
read_preference=pymongo.ReadPreference.NEAREST,
**ssl_params)

if do_auth:
if auth_source:
self._authenticate(cli_rs[auth_source], username, password, use_x509, server, service_check_tags)
else:
self._authenticate(cli_rs[db_name], username, password, use_x509, server, service_check_tags)

# Replication set information
replset_name = replSet['set']
replset_state = self.get_state_name(replSet['myState']).lower()

tags.extend([
u"replset_name:{0}".format(replset_name),
u"replset_state:{0}".format(replset_state),
])

# Find nodes: master and current node (ourself)
for member in replSet.get('members'):
if member.get('self'):
current = member
if int(member.get('state')) == 1:
primary = member

# Compute a lag time
if current is not None and primary is not None:
if 'optimeDate' in primary and 'optimeDate' in current:
lag = primary['optimeDate'] - current['optimeDate']
data['replicationLag'] = total_seconds(lag)

if current is not None:
data['health'] = current['health']

data['state'] = replSet['myState']

if current is not None:
total = 0.0
cfg = cli_rs['local']['system.replset'].find_one()
for member in cfg.get('members'):
total += member.get('votes', 1)
if member['_id'] == current['_id']:
data['votes'] = member.get('votes', 1)
data['voteFraction'] = data['votes'] / total

status['replSet'] = data

# Submit events
self._report_replica_set_state(
data['state'], clean_server_name, replset_name, self.agentConfig
)

except Exception as e:
if "OperationFailure" in repr(e) and ("not running with --replSet" in str(e) or "replSetGetStatus" in str(e)):
pass
else:
raise e
except Exception as e:
if "OperationFailure" in repr(e) and ("not running with --replSet" in str(e) or "replSetGetStatus" in str(e)):
pass
else:
raise e

# If these keys exist, remove them for now as they cannot be serialized
try:
Expand Down
2 changes: 1 addition & 1 deletion mongo/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"mac_os",
"windows"
],
"version": "1.5.3",
"version": "1.5.4",
"guid": "d51c342e-7a02-4611-a47f-1e8eade5735c",
"public_title": "Datadog-MongoDB Integration",
"categories":["data store", "log collection"],
Expand Down

0 comments on commit e591866

Please sign in to comment.