예제 #1
0
def test_get_cmk_passive_service_attributes(monkeypatch, hostname, result):
    CheckManager().load(["cpu"])

    ts = Scenario().add_host("localhost")
    ts.add_host("blub")
    ts.set_option(
        "extra_service_conf", {
            "contact_groups": [
                (u'ding', ['localhost'], ["CPU load$"]),
            ],
            "check_interval": [
                (40.0, ['blub'], ["Check_MK$"]),
                (33.0, ['localhost'], ["CPU load$"]),
            ],
        })
    config_cache = ts.apply(monkeypatch)
    host_config = config_cache.get_host_config(hostname)
    check_mk_attrs = core_config.get_service_attributes(
        hostname, "Check_MK", config_cache)

    service = Service("cpu.loads", None, "CPU load", {})
    service_spec = core_config.get_cmk_passive_service_attributes(
        config_cache, host_config, service, check_mk_attrs)
    assert service_spec == result
예제 #2
0
def test_tcpdatasource_restricted_address_mismatch(
    mode,
    monkeypatch,
    restricted_address_mismatch_state,
    only_from,
    rule,
    res,
):
    hostname = "hostname"
    ts = Scenario().add_host(hostname)
    ts.set_option("agent_config", {"only_from": [(rule, [], [hostname], {})]})

    if restricted_address_mismatch_state is not None:
        ts.set_ruleset("check_mk_exit_status", [
            ({
                "restricted_address_mismatch": restricted_address_mismatch_state,
            }, [], [hostname], {}),
        ])

    ts.apply(monkeypatch)
    source = TCPSource(hostname, "ipaddress", mode=mode)
    summarizer = AgentSummarizerDefault(source.exit_spec, source)

    assert summarizer._sub_result_only_from({"onlyfrom": only_from}) == res
예제 #3
0
def test_config_cache_extra_attributes_of_service(monkeypatch, hostname, result):
    ts = Scenario().add_host(hostname)
    ts.set_option(
        "extra_service_conf", {
            "check_interval": [("10", [], ["testhost2"], "CPU load$", {}),],
            "dingdong": [
                ([
                    "value1",
                ], [], ["testhost2"], "CPU load$", {}),
                ([
                    "value2",
                ], [], ["testhost2"], "CPU load$", {}),
            ],
            "_custom": [
                ([
                    "value1",
                ], [], ["testhost2"], "CPU load$", {}),
                ([
                    "value2",
                ], [], ["testhost2"], "CPU load$", {}),
            ],
        })
    config_cache = ts.apply(monkeypatch)
    assert config_cache.extra_attributes_of_service(hostname, "CPU load") == result
예제 #4
0
def test_mgmt_disabled(monkeypatch):
    ts = Scenario()
    ts.add_host("mgmt-host")
    ts.set_option("ipaddresses", {"mgmt-host": "127.0.0.1"})
    ts.set_option("management_protocol", {"mgmt-host": None})
    ts.set_option("host_attributes",
                  {"mgmt-host": {
                      "management_address": "127.0.0.1"
                  }})
    ts.set_option("management_snmp_credentials", {"mgmt-host": "HOST"})

    config_cache = ts.apply(monkeypatch)
    host_config = config_cache.get_host_config("mgmt-host")
    assert host_config.has_management_board is False
    assert host_config.management_protocol is None
    assert host_config.management_address == "127.0.0.1"
    assert host_config.management_credentials is None
예제 #5
0
def test_mgmt_explicit_settings(monkeypatch, protocol, cred_attribute,
                                credentials):
    ts = Scenario()
    ts.add_host("mgmt-host")
    ts.set_option("ipaddresses", {"mgmt-host": "127.0.0.1"})
    ts.set_option("management_protocol", {"mgmt-host": protocol})
    ts.set_option(cred_attribute, {"mgmt-host": credentials})

    config_cache = ts.apply(monkeypatch)
    host_config = config_cache.get_host_config("mgmt-host")
    assert host_config.has_management_board
    assert host_config.management_protocol == protocol
    assert host_config.management_address == "127.0.0.1"
    assert host_config.management_credentials == credentials
예제 #6
0
def test_mgmt_explicit_address(monkeypatch):
    ts = Scenario()
    ts.add_host("mgmt-host")
    ts.set_option("ipaddresses", {"mgmt-host": "127.0.0.1"})
    ts.set_option("management_protocol", {"mgmt-host": "snmp"})
    ts.set_option("host_attributes",
                  {"mgmt-host": {
                      "management_address": "127.0.0.2"
                  }})

    config_cache = ts.apply(monkeypatch)
    host_config = config_cache.get_host_config("mgmt-host")
    assert host_config.has_management_board
    assert host_config.management_protocol == "snmp"
    assert host_config.management_address == "127.0.0.2"
    assert host_config.management_credentials == "public"
예제 #7
0
def scenario_fixture(monkeypatch):
    test_hosts = ["ds-test-host1", "ds-test-host2", "ds-test-node1", "ds-test-node2"]

    ts = Scenario()

    if is_enterprise_repo():
        ts.set_option("monitoring_core", "cmc")
    else:
        ts.set_option("monitoring_core", "nagios")

    for h in test_hosts:
        ts.add_host(h)

    ts.set_option("ipaddresses", dict((h, "127.0.0.1") for h in test_hosts))
    ts.add_cluster("ds-test-cluster1", nodes=["ds-test-node1", "ds-test-node2"])
    ts.fake_standard_linux_agent_output(*test_hosts)

    return ts.apply(monkeypatch)
예제 #8
0
def test_create_nagios_host_spec(hostname, result, monkeypatch):
    if cmk_version.is_managed_edition():
        result = result.copy()
        result['_CUSTOMER'] = 'provider'

    ts = Scenario().add_host("localhost")
    ts.add_host("host2")
    ts.add_cluster("cluster1")

    ts.add_cluster("cluster2", nodes=["node1", "node2"])
    ts.add_host("node1")
    ts.add_host("node2")
    ts.add_host("switch")
    ts.set_option("ipaddresses", {
        "node1": "127.0.0.1",
        "node2": "127.0.0.2",
    })

    ts.set_option("extra_host_conf", {
        "alias": [
            (u'lOCALhost', ['localhost']),
        ],
    })

    ts.set_option(
        "extra_host_conf", {
            "alias": [
                (u'lOCALhost', ['host2']),
                (u'CLUSTer', ['cluster2']),
            ],
            "parents": [
                ('switch', ['node1', 'node2']),
            ],
        })

    outfile = io.StringIO()
    cfg = core_nagios.NagiosConfig(outfile, [hostname])

    config_cache = ts.apply(monkeypatch)
    host_attrs = core_config.get_host_attributes(hostname, config_cache)

    host_spec = core_nagios._create_nagios_host_spec(cfg, config_cache,
                                                     hostname, host_attrs)
    assert host_spec == result
예제 #9
0
 def scenario(self, hostname, ipaddress, monkeypatch):
     ts = Scenario().add_host(hostname)
     ts.set_option("ipaddresses", {hostname: ipaddress})
     ts.apply(monkeypatch)
     return ts
예제 #10
0
def test_get_check_table_of_static_check(monkeypatch, hostname,
                                         expected_result):
    static_checks = {
        "df_host": [
            Service(CheckPluginName('df'), '/snap/core/9066',
                    u'Filesystem /snap/core/9066',
                    [{
                        'tp_values': [('24X7', {
                            'inodes_levels': None
                        })],
                        'tp_default_value': {}
                    }, {
                        'trend_range': 24,
                        'show_levels': 'onmagic',
                        'inodes_levels': (10.0, 5.0),
                        'magic_normsize': 20,
                        'show_inodes': 'onlow',
                        'levels': (80.0, 90.0),
                        'show_reserved': False,
                        'levels_low': (50.0, 60.0),
                        'trend_perfdata': True
                    }]),
        ],
        "df_host_1": [
            Service(
                CheckPluginName('df'), '/snap/core/9067',
                u'Filesystem /snap/core/9067', {
                    'trend_range': 24,
                    'show_levels': 'onmagic',
                    'inodes_levels': (10.0, 5.0),
                    'magic_normsize': 20,
                    'show_inodes': 'onlow',
                    'levels': (80.0, 90.0),
                    'tp_default_value': {
                        'levels': (87.0, 90.0)
                    },
                    'show_reserved': False,
                    'tp_values': [('24X7', {
                        'inodes_levels': None
                    })],
                    'levels_low': (50.0, 60.0),
                    'trend_perfdata': True
                })
        ],
        "df_host_2": [
            Service(CheckPluginName('df'), '/snap/core/9068',
                    u'Filesystem /snap/core/9068', None)
        ],
    }

    ts = Scenario().add_host(hostname, tags={"criticality": "test"})
    ts.add_host("df_host")
    ts.add_host("df_host_1")
    ts.add_host("df_host_2")
    ts.set_option(
        "static_checks",
        {
            "filesystem": [
                (('df', '/snap/core/9066', [{
                    'tp_values': [('24X7', {
                        'inodes_levels': None
                    })],
                    'tp_default_value': {}
                }, {
                    'trend_range': 24,
                    'show_levels': 'onmagic',
                    'inodes_levels': (10.0, 5.0),
                    'magic_normsize': 20,
                    'show_inodes': 'onlow',
                    'levels': (80.0, 90.0),
                    'show_reserved': False,
                    'levels_low': (50.0, 60.0),
                    'trend_perfdata': True
                }]), [], ["df_host"]),
                (('df', '/snap/core/9067', [{
                    'tp_values': [('24X7', {
                        'inodes_levels': None
                    })],
                    'tp_default_value': {}
                }, {
                    'trend_range': 24,
                    'show_levels': 'onmagic',
                    'inodes_levels': (10.0, 5.0),
                    'magic_normsize': 20,
                    'show_inodes': 'onlow',
                    'levels': (80.0, 90.0),
                    'show_reserved': False,
                    'levels_low': (50.0, 60.0),
                    'trend_perfdata': True
                }]), [], ["df_host_1"]),
                (('df', '/snap/core/9068', None), [], ["df_host_2"]),
            ],
        },
    )

    config_cache = ts.apply(monkeypatch)
    monkeypatch.setattr(config_cache, "get_autochecks_of",
                        lambda h: static_checks.get(h, []))

    assert list(
        check_table.get_check_table(hostname).keys()) == expected_result
예제 #11
0
def test_get_check_table(monkeypatch, hostname, expected_result):
    autochecks = {
        "ping-host": [
            Service(
                CheckPluginName("smart_temp"),
                "bla",
                u'Temperature SMART bla',
                {},
            )
        ],
        "autocheck-overwrite": [
            Service(
                CheckPluginName('smart_temp'),
                '/dev/sda',
                u'Temperature SMART /dev/sda',
                {"is_autocheck": True},
            ),
            Service(
                CheckPluginName('smart_temp'),
                '/dev/sdb',
                u'Temperature SMART /dev/sdb',
                {"is_autocheck": True},
            ),
        ],
        "ignore-not-existing-checks": [
            Service(
                CheckPluginName("bla_blub"),
                "ITEM",
                u'Blub ITEM',
                {},
            ),
        ],
        "node1": [
            Service(
                CheckPluginName("smart_temp"),
                "auto-clustered",
                u"Temperature SMART auto-clustered",
                {},
            ),
            Service(
                CheckPluginName("smart_temp"),
                "auto-not-clustered",
                u'Temperature SMART auto-not-clustered',
                {},
            )
        ],
    }

    ts = Scenario().add_host(hostname, tags={"criticality": "test"})
    ts.add_host("ping-host", tags={"agent": "no-agent"})
    ts.add_host("node1")
    ts.add_cluster("cluster1", nodes=["node1"])
    ts.set_option(
        "static_checks",
        {
            "temperature": [
                (('smart.temp', '/dev/sda', {}), [],
                 ["no-autochecks", "autocheck-overwrite"]),
                (('blub.bla', 'ITEM', {}), [], ["ignore-not-existing-checks"]),
                (('smart.temp', 'ITEM1', {}), [], ["ignore-disabled-rules"], {
                    "disabled": True
                }),
                (('smart.temp', 'ITEM2', {}), [], ["ignore-disabled-rules"]),
                (('smart.temp', '/dev/sda', {
                    "rule": 1
                }), [], ["static-check-overwrite"]),
                (('smart.temp', '/dev/sda', {
                    "rule": 2
                }), [], ["static-check-overwrite"]),
                (('smart.temp', 'static-node1', {}), [], ["node1"]),
                (('smart.temp', 'static-cluster', {}), [], ["cluster1"]),
            ]
        },
    )
    ts.set_ruleset("clustered_services", [
        ([], ['node1'], [u'Temperature SMART auto-clustered$']),
    ])
    config_cache = ts.apply(monkeypatch)
    monkeypatch.setattr(config_cache, "get_autochecks_of",
                        lambda h: autochecks.get(h, []))

    assert check_table.get_check_table(hostname) == expected_result
예제 #12
0
def fixture_core_scenario(monkeypatch):
    ts = Scenario().add_host("test-host")
    ts.set_option("ipaddresses", {"test-host": "127.0.0.1"})
    return ts.apply(monkeypatch)