def test_single_instance_counter_with_instance(
        aggregator, pdh_mocks_fixture):  # noqa: F811
    initialize_pdh_tests()
    instance = DEFAULT_INSTANCE
    with pytest.raises(AttributeError):
        PDHBaseCheck("testcheck", {}, {}, [instance],
                     INSTANCE_OF_SINGLE_INSTANCE_COUNTER)
def test_winpdhcounter_throws_on_bad_input(pdh_mocks_fixture):  # noqa: F811
    initialize_pdh_tests()
    with pytest.raises(AttributeError):
        WinPDHCounter('Ssystem', 'Processor Queue Length', logger)

    with pytest.raises(AttributeError):
        WinPDHCounter('System', 'PProcessor Queue Length', logger)
def test_single_instance_counter(aggregator, pdh_mocks_fixture):  # noqa: F811
    initialize_pdh_tests()
    instance = DEFAULT_INSTANCE
    c = PDHBaseCheck("testcheck", {}, {}, [instance], SINGLE_INSTANCE_COUNTER)
    c.check(instance)

    aggregator.assert_metric("test.system.mem.available", tags=None, count=1)
def test_winpdhcounter_bad_strings_english(
        pdh_mocks_fixture_bad_perf_strings):  # noqa F811
    initialize_pdh_tests()
    counter = WinPDHCounter('System', 'Processor Queue Length', logger)

    vals = counter.get_all_values()
    assert len(vals) == 1  # single instance key, should only have one value
    assert SINGLE_INSTANCE_KEY in vals
def test_winpdhcounter_non_english(pdh_mocks_fixture):  # noqa: F811
    WinPDHCounter._use_en_counter_names = False
    WinPDHCounter.pdh_counter_dict = defaultdict(list)
    initialize_pdh_tests(lang="se-sv")
    counter = WinPDHCounter('System', 'Processor Queue Length', logger)

    vals = counter.get_all_values()
    assert len(vals) == 1  # single instance key, should only have one value
    assert SINGLE_INSTANCE_KEY in vals
def test_multi_instance_counter_specific_instances(
        aggregator, pdh_mocks_fixture):  # noqa: F811
    initialize_pdh_tests()
    instance = DEFAULT_INSTANCE
    c = PDHBaseCheck("testcheck", {}, {}, [instance],
                     MULTI_INSTANCE_COUNTER_WITH_INSTANCES)
    c.check(instance)
    for t in ['test.processor_time_0', 'test.processor_time_1']:
        aggregator.assert_metric(t, tags=None, count=1)
    assert aggregator.metrics_asserted_pct == 100.0
def test_multi_instance_counter(aggregator, pdh_mocks_fixture):  # noqa: F811
    initialize_pdh_tests()
    instance = DEFAULT_INSTANCE
    c = PDHBaseCheck("testcheck", {}, {}, [instance], MULTI_INSTANCE_COUNTER)
    c.check(instance)
    for t in ['instance:0', 'instance:1', 'instance:_Total']:
        aggregator.assert_metric("test.processor_time",
                                 tags=['%s' % t],
                                 count=1)
    assert aggregator.metrics_asserted_pct == 100.0
def test_winpdhcounter_bad_strings_not_english(
        pdh_mocks_fixture_bad_perf_strings):  # noqa: F811
    WinPDHCounter._use_en_counter_names = False
    WinPDHCounter.pdh_counter_dict = defaultdict(list)

    initialize_pdh_tests(lang="se-sv")
    '''
    expectation is that the initialization will fail.  We attempt to fall
    back to english counters if the strings database isn't present; however,
    on non-english windows the english counters won't work
    '''
    with pytest.raises(AttributeError):
        WinPDHCounter('System', 'Processor Queue Length', logger)
def test_returns_partial_metrics(aggregator, pdh_mocks_fixture):  # noqa: F811
    COUNTER_LIST = [
        [
            "NTDS", None, "LDAP Client Sessions",
            "active_directory.ldap.client_sessions", "gauge"
        ],
        [
            "NTDS", None, "LDAP Bind Time", "active_directory.ldap.bind_time",
            "gauge"
        ],
        [
            "NTDS", None, "LDAP Successful Binds/sec",
            "active_directory.ldap.successful_binds_persec", "gauge"
        ],
        [
            "NTDS", None, "LDAP Searches/sec",
            "active_directory.ldap.searches_persec", "gauge"
        ],
        # these two don't exist
        [
            "NTDS", None, "Kerberos Authentications/sec",
            "active_directory.kerberos.auths_persec", "gauge"
        ],
        [
            "NTDS", None, "NTLM Authentications/sec",
            "active_directory.ntlm.auths_persec", "gauge"
        ],
    ]
    initialize_pdh_tests()
    instance = DEFAULT_INSTANCE
    c = PDHBaseCheck("testcheck", {}, {}, [instance], COUNTER_LIST)
    c.check(instance)

    aggregator.assert_metric("active_directory.ldap.client_sessions",
                             tags=None,
                             count=1)
    aggregator.assert_metric("active_directory.ldap.bind_time",
                             tags=None,
                             count=1)
    aggregator.assert_metric("active_directory.ldap.successful_binds_persec",
                             tags=None,
                             count=1)
    aggregator.assert_metric("active_directory.ldap.searches_persec",
                             tags=None,
                             count=1)
    assert aggregator.metrics_asserted_pct == 100.0