def test_api_alibaba_mongod(aggregator): log = mock.MagicMock() config = MongoConfig(common.INSTANCE_BASIC, log) payload = { 'isMaster': {}, 'replSetGetStatus': { 'myState': 1, 'set': 'foo', 'configsvr': False }, 'shardingState': { 'enabled': False }, } mocked_client = mock.MagicMock() mocked_client.__getitem__ = mock.MagicMock(return_value=mock.MagicMock( command=payload.__getitem__)) with mock.patch('datadog_checks.mongo.api.MongoClient', mock.MagicMock(return_value=mocked_client)): api = MongoApi(config, log) deployment_type = api._get_alibaba_deployment_type() assert isinstance(deployment_type, ReplicaSetDeployment) assert deployment_type.cluster_role is None assert deployment_type.replset_state_name == 'primary' assert deployment_type.use_shards is False assert deployment_type.is_primary is True assert deployment_type.is_secondary is False assert deployment_type.is_arbiter is False assert deployment_type.replset_state == 1 assert deployment_type.replset_name == 'foo'
def test_api_alibaba_mongos(aggregator): log = mock.MagicMock() config = MongoConfig(common.INSTANCE_BASIC, log) payload = {'isMaster': {'msg': 'isdbgrid'}} mocked_client = mock.MagicMock() mocked_client.__getitem__ = mock.MagicMock(return_value=mock.MagicMock( command=payload.__getitem__)) with mock.patch('datadog_checks.mongo.api.MongoClient', mock.MagicMock(return_value=mocked_client)): api = MongoApi(config, log) deployment_type = api._get_alibaba_deployment_type() assert isinstance(deployment_type, MongosDeployment)
def check(self, _): try: api = MongoApi(self._config, self.log) self.log.debug("Connected!") except Exception: self.service_check(SERVICE_CHECK_NAME, AgentCheck.CRITICAL, tags=self._config.service_check_tags) raise try: mongo_version = api.server_info().get('version', '0.0') self.set_metadata('version', mongo_version) except Exception: self.service_check(SERVICE_CHECK_NAME, AgentCheck.CRITICAL, tags=self._config.service_check_tags) self.log.exception("Error when collecting the version from the mongo server.") raise else: self.service_check(SERVICE_CHECK_NAME, AgentCheck.OK, tags=self._config.service_check_tags) tags = deepcopy(self._config.metric_tags) deployment = api.deployment_type if isinstance(deployment, ReplicaSetDeployment): tags.extend( [ "replset_name:{}".format(deployment.replset_name), "replset_state:{}".format(deployment.replset_state_name), ] ) if deployment.use_shards: tags.append('sharding_cluster_role:{}'.format(deployment.cluster_role)) elif isinstance(deployment, MongosDeployment): tags.append('sharding_cluster_role:mongos') if isinstance(deployment, ReplicaSetDeployment) and deployment.is_arbiter: dbnames = [] else: dbnames = api.list_database_names() self.gauge('mongodb.dbs', len(dbnames), tags=tags) self.refresh_collectors(api.deployment_type, mongo_version, dbnames, tags) for collector in self.collectors: try: collector.collect(api) except Exception: self.log.info( "Unable to collect logs from collector %s. Some metrics will be missing.", collector, exc_info=True )
def api_client(self): if self._api_client is None: try: self._api_client = MongoApi(self._config, self.log) self.log.debug("Connected!") except Exception: self.service_check(SERVICE_CHECK_NAME, AgentCheck.CRITICAL, tags=self._config.service_check_tags) raise return self._api_client
def get_votes_config(self, api): """On most nodes, simply collects the replset config from the system.replset collection in the local db. Unfortunately when the agent is connected to an arbiter, this can't be run without raising authentication errors. And because authenticating on an arbiter is not allowed, the workaround in that case is to run the command directly on the primary.""" if api.deployment_type.is_arbiter: try: api = MongoApi(self.check._config, self.log, replicaset=api.deployment_type.replset_name) except Exception: self.log.warning( "Current node is an arbiter, the extra connection to the primary was unsuccessful." " Votes metrics won't be reported.") return None return api['local']['system.replset'].find_one()
def get_replset_config(self, api): """On most nodes, simply runs `replSetGetConfig`. Unfortunately when the agent is connected to an arbiter, running the `replSetGetConfig` raises authentication errors. And because authenticating on an arbiter is not allowed, the workaround in that case is to run the command directly on the primary.""" if api.deployment_type.is_arbiter: try: api_primary = MongoApi( self.check.config, self.log, replicaset=api.deployment_type.replset_name) except Exception: self.log.warning( "Current node is an arbiter, the extra connection to the primary was unsuccessful." " Votes metrics won't be reported.") return None return api_primary['admin'].command('replSetGetConfig') return api['admin'].command('replSetGetConfig')