def test_check_with_filters(aggregator):
    # type: (AggregatorStub) -> None
    check = MarklogicCheck('marklogic', {}, [INSTANCE_FILTERS])

    check.check(INSTANCE_FILTERS)

    _assert_metrics(aggregator, COMMON_TAGS)

    # Resource filter only
    for metric in STORAGE_HOST_METRICS + RESOURCE_STORAGE_FOREST_METRICS:
        aggregator.assert_metric_has_tag(metric,
                                         'forest_name:Security',
                                         count=1)
    for metric in RESOURCE_STATUS_DATABASE_METRICS:
        aggregator.assert_metric_has_tag(metric,
                                         'database_name:Documents',
                                         count=1)
    for metric in [
            'marklogic.requests.query-count',
            'marklogic.requests.total-requests',
            'marklogic.requests.update-count',
    ]:
        aggregator.assert_metric(metric,
                                 tags=COMMON_TAGS +
                                 ['server_name:Admin', 'group_name:Default'],
                                 count=1)

    aggregator.assert_all_metrics_covered()

    # Service checks
    _assert_service_checks(aggregator, COMMON_TAGS)

    aggregator.assert_no_duplicate_all()

    aggregator.assert_metrics_using_metadata(get_metadata_metrics())
def test_check(aggregator):
    # type: (AggregatorStub) -> None
    check = MarklogicCheck('marklogic', {}, [INSTANCE])

    check.check(INSTANCE)

    _assert_metrics(aggregator, COMMON_TAGS)

    aggregator.assert_all_metrics_covered()

    # Service checks
    _assert_service_checks(aggregator, COMMON_TAGS)

    aggregator.assert_no_duplicate_all()

    aggregator.assert_metrics_using_metadata(get_metadata_metrics())
Beispiel #3
0
def test_check_simple_user(aggregator):
    # type: (AggregatorStub) -> None
    check = MarklogicCheck('marklogic', {}, [INSTANCE_SIMPLE_USER])

    check.check(INSTANCE_SIMPLE_USER)

    _assert_metrics(aggregator, COMMON_TAGS)

    aggregator.assert_all_metrics_covered()

    # Service checks
    _assert_service_checks(aggregator, COMMON_TAGS, include_health_checks=False)

    len(aggregator._service_checks) == 1  # can_connect service check only

    aggregator.assert_no_duplicate_all()

    aggregator.assert_metrics_using_metadata(get_metadata_metrics())
def test_metadata_integration(aggregator, datadog_agent):
    # type: (AggregatorStub, Any) -> None
    c = MarklogicCheck('marklogic', {}, [INSTANCE])
    c.check_id = 'test:123'
    c.check(INSTANCE)

    parsed_version = version.parse(MARKLOGIC_VERSION)
    version_metadata = {
        'version.scheme': 'marklogic',
        'version.major': str(parsed_version.major),
        'version.minor': str(parsed_version.minor),
        'version.patch': str(parsed_version.post),
        'version.raw': MARKLOGIC_VERSION,
    }

    datadog_agent.assert_metadata('test:123', version_metadata)
    datadog_agent.assert_metadata_count(len(version_metadata))

    # Service checks
    _assert_service_checks(aggregator, COMMON_TAGS)
Beispiel #5
0
def test_collect_host_metrics(mock_requests, mock_status, aggregator):
    # type: (Any, Any, AggregatorStub) -> None
    check = MarklogicCheck('marklogic', {}, [INSTANCE_FILTERS])

    # Expected output when there is no exclude list
    check.resources_to_monitor = {
        'forest': [],
        'database': [],
        'host': [
            # Does not exist
            {
                'name': '9aea032c882e',
                'id': '17797492400840985949',
                'type': 'host',
                'uri': '/hosts/9aea032c882e'
            },
            {
                'name': 'ff0fef449486',
                'id': '3428441913043145991',
                'type': 'host',
                'uri': 'hosts/ff0fef449486'
            },
        ],
        'server': [],
    }

    check.collect_per_resource_metrics()

    expected_tags = COMMON_TAGS + ['marklogic_host_name:ff0fef449486']
    for m in HOST_STATUS_METRICS_GENERAL:
        aggregator.assert_metric(m, tags=expected_tags, count=1)
    for m in [
            'marklogic.requests.query-count',
            'marklogic.requests.total-requests',
            'marklogic.requests.update-count'
    ]:
        aggregator.assert_metric(m, tags=expected_tags, count=1)

    aggregator.assert_all_metrics_covered()
Beispiel #6
0
def test_bad_resource_storage(mock_requests, aggregator, caplog):
    # type: (Any, Any, AggregatorStub) -> None
    caplog.at_level(logging.WARNING)
    check = MarklogicCheck('marklogic', {}, [INSTANCE_FILTERS])

    check.resources_to_monitor = {
        'forest': [
            {
                'id': '4259429487027269237',
                'type': 'forest',
                'name': 'Documents',
                'uri': '/forests/Documents'
            },
        ],
        'database': [],
        'host': [],
        'server': [],
    }

    check.collect_per_resource_metrics()

    # This can happen when the database owning this forest is disabled
    assert "Status information unavailable for resource {" in caplog.text
    assert "Storage information unavailable for resource {" in caplog.text
Beispiel #7
0
def test_get_resources_to_monitor():
    # type: () -> None
    check = MarklogicCheck('marklogic', {}, [INSTANCE_FILTERS])
    response_value = read_fixture_file('cluster-query.yaml')

    # Expected output when there is no exclude list
    complete_filtered = {
        'forest': [
            {
                'name': 'Security',
                'id': '1112331563215633422',
                'type': 'forest',
                'uri': '/forests/Security'
            },
            {
                'name': 'Schemas',
                'id': '5750304059804042419',
                'type': 'forest',
                'uri': '/forests/Schemas'
            },
        ],
        'database': [{
            'id': '5004266825873163057',
            'name': 'Documents',
            'type': 'database',
            'uri': '/databases/Documents'
        }],
        'host': [],
        'server': [{
            'name': 'Admin',
            'id': '9403936238896063877',
            'type': 'server',
            'uri': "/servers/Admin?group-id=Default",
            'group': 'Default',
        }],
    }  # type: Dict[str, List[Any]]

    # Called in the check function
    check.resources = parse_resources(response_value)
    # Include list + exclude list
    filtered_res = check.get_resources_to_monitor()
    assert filtered_res == {
        'forest': [complete_filtered['forest'][0]],
        'database': complete_filtered['database'],
        'host': [],
        'server': complete_filtered['server'],
    }

    # No exclude list
    check._config.resource_filters['excluded'] = []
    filtered_res = check.get_resources_to_monitor()
    assert filtered_res == complete_filtered

    # Useless exclude list
    check._config.resource_filters[
        'excluded'] = check._config.build_resource_filters([{
            'resource_type': 'forest',
            'pattern': 'Security',
            'group': 'Default'
        }])['excluded']
    filtered_res = check.get_resources_to_monitor()
    assert filtered_res == complete_filtered

    # No include list
    check._config.resource_filters['included'] = []
    filtered_res = check.get_resources_to_monitor()
    assert filtered_res == {
        'forest': [],
        'database': [],
        'host': [],
        'server': [],
    }
def test_submit_health_service_checks(aggregator, caplog):
    # type: (AggregatorStub, Any) -> None
    check = MarklogicCheck('marklogic', {}, [INSTANCE])

    health_mocked_data = {
        'cluster-health-report': [
            {
                "state": "info",
                "resource-type": "database",
                "resource-name": "Security",
                "code": "HEALTH-DATABASE-NO-BACKUP",
                "message": "Database has never been backed up.",
            },
            {
                'resource-type': 'database',
                'resource-name': 'Fab',
                'code': 'UNKNOWN'
            },
        ]
    }

    check.resources = [
        {
            'id': '255818103205892753',
            'type': 'database',
            'name': 'Security',
            'uri': "/databases/Security"
        },
        {
            'id': '5004266825873163057',
            'type': 'database',
            'name': 'Fab',
            'uri': "/databases/Fab"
        },
        {
            'id': '16024526243775340149',
            'type': 'forest',
            'name': 'Modules',
            'uri': "/forests/Modules"
        },
        {
            'id': '17254568917360711355',
            'type': 'forest',
            'name': 'Extensions',
            'uri': "/forests/Extensions"
        },
    ]

    # If there is no error
    with mock.patch('datadog_checks.marklogic.api.MarkLogicApi.get_health',
                    return_value=health_mocked_data):
        check.submit_health_service_checks()

        aggregator.assert_service_check(
            'marklogic.database.health',
            MarklogicCheck.OK,
            tags=['foo:bar', 'database_name:Security'],
            message=None,
            count=1,
        )
        aggregator.assert_service_check(
            'marklogic.database.health',
            MarklogicCheck.UNKNOWN,
            tags=['foo:bar', 'database_name:Fab'],
            message='UNKNOWN (unknown): No message.',
            count=1,
        )
        aggregator.assert_service_check(
            'marklogic.forest.health',
            MarklogicCheck.OK,
            tags=['foo:bar', 'forest_name:Modules'],
            message=None,
            count=1)
        aggregator.assert_service_check(
            'marklogic.forest.health',
            MarklogicCheck.OK,
            tags=['foo:bar', 'forest_name:Extensions'],
            message=None,
            count=1,
        )

    aggregator.reset()
    caplog.clear()

    # If the user doesn't have enough permissions
    with mock.patch('datadog_checks.marklogic.api.MarkLogicApi.get_health',
                    return_value={'code': 'HEALTH-CLUSTER-ERROR'}):
        check.submit_health_service_checks()

        assert "The user needs `manage-admin` permission to monitor databases health." in caplog.text

    aggregator.reset()
    caplog.clear()

    # If MarkLogic can't be reached
    with mock.patch('datadog_checks.marklogic.api.MarkLogicApi.get_health',
                    side_effect=Exception("exception")):
        check.submit_health_service_checks()

        assert "Failed to monitor databases health" in caplog.text