示例#1
0
def reload_config():
    # Needs to be done together, even when the checks are not directly needed
    import cmk.base.check_api as check_api
    config.load_all_checks(check_api.get_check_api_context)
    config.load()

    config_cache = config.get_config_cache()
    config_cache.initialize()
    return config_cache
示例#2
0
def test_is_tcp_check():
    config.load_all_checks(check_api.get_check_api_context)
    assert cmk.base.check_utils.is_tcp_check("xxx") is False
    assert cmk.base.check_utils.is_tcp_check("uptime") is True
    assert cmk.base.check_utils.is_tcp_check("uptime") is True
    assert cmk.base.check_utils.is_tcp_check("snmp_uptime") is False
    assert cmk.base.check_utils.is_tcp_check("mem") is True
    assert cmk.base.check_utils.is_tcp_check("mem.linux") is True
    assert cmk.base.check_utils.is_tcp_check("mem.ding") is True
    assert cmk.base.check_utils.is_tcp_check("apc_humidity") is False
示例#3
0
def _config_load_all_checks():
    # this is needed in cmk/base *and* checks/ :-(
    import cmk.base.config as config  # pylint: disable=bad-option-value,import-outside-toplevel
    import cmk.base.check_api as check_api  # pylint: disable=bad-option-value,import-outside-toplevel

    config._initialize_data_structures()
    assert config.check_info == {}

    with cmk_debug_enabled():  # fail if a plugin can't be loaded
        config.load_all_checks(check_api.get_check_api_context)

    assert len(config.check_info) > 1000  # sanitiy check
示例#4
0
    def load(self, file_names=None):
        """Load either all check plugins or the given file_names"""
        import cmk.base.config as config  # pylint: disable=import-outside-toplevel
        import cmk.base.check_api as check_api  # pylint: disable=import-outside-toplevel
        import cmk.utils.paths  # pylint: disable=import-outside-toplevel

        if file_names is None:
            config.load_all_checks(check_api.get_check_api_context)  # loads all checks
        else:
            config._initialize_data_structures()
            config.load_checks(check_api.get_check_api_context,
                               [os.path.join(cmk.utils.paths.checks_dir, f) for f in file_names])

        return self
示例#5
0
def test_find_missing_manpages():
    all_check_manuals = man_pages.all_man_pages()

    import cmk.base.check_api as check_api
    config.load_all_checks(check_api.get_check_api_context)
    checks_sorted = sorted([ (name, entry) for (name, entry) in config.check_info.items()
                      if not _is_pure_section_declaration(entry) ] + \
       [ ("check_" + name, entry) for (name, entry) in config.active_check_info.items() ])
    assert len(checks_sorted) > 1000

    for check_plugin_name, _check in checks_sorted:
        if check_plugin_name in ["labels", "esx_systeminfo"]:
            continue  # this check's discovery function can only create labels, never a service
        assert check_plugin_name in all_check_manuals, "Manpage missing: %s" % check_plugin_name
示例#6
0
文件: __init__.py 项目: n00rm/checkmk
    def load(self, file_names=None):
        """Load either all check plugins or the given file_names"""
        if sys.version_info[0] < 3:
            # This has not been ported to Python 3 yet. Prevent mypy in Python 3 mode from following
            import cmk.base.config as config
            import cmk.base.check_api as check_api
            import cmk.utils.paths

        if file_names is None:
            config.load_all_checks(
                check_api.get_check_api_context)  # loads all checks
        else:
            config._initialize_data_structures()
            config.load_checks(check_api.get_check_api_context, [
                os.path.join(cmk.utils.paths.checks_dir, f) for f in file_names
            ])

        return self
示例#7
0
    def execute(self, cmd, args):
        # type: (str, List[str]) -> Any
        self._handle_generic_arguments(args)

        try:
            try:
                automation = self._automations[cmd]
            except KeyError:
                raise MKAutomationError(
                    "Automation command '%s' is not implemented." % cmd)

            if automation.needs_checks:
                config.load_all_checks(check_api.get_check_api_context)

            if automation.needs_config:
                config.load(validate_hosts=False)

            result = automation.execute(args)

        except (MKAutomationError, MKTimeout) as e:
            console.error("%s\n" % e)
            if cmk.utils.debug.enabled():
                raise
            return 1

        except Exception as e:
            if cmk.utils.debug.enabled():
                raise
            console.error("%s\n" % e)
            return 2

        finally:
            profiling.output_profile()

        if cmk.utils.debug.enabled():
            console.output(pprint.pformat(result) + "\n")
        else:
            console.output("%r\n" % (result, ))

        return 0
示例#8
0
def load_plugins():
    #     CheckManager().load(["df", "cpu", "chrony", "lnx_if", "livestatus_status", "omd_status"])
    config.load_all_checks(check_api.get_check_api_context)
    InventoryPluginManager().load()
示例#9
0
def load_all_checks():
    config.load_all_checks(check_api.get_check_api_context)
示例#10
0
 def __init__(self):
     config.load_all_checks(check_api.get_check_api_context)
     self.info = copy.deepcopy(config.check_info)
     self.cache = {}
示例#11
0
    def __init__(self):
        with _cmk_debug_enabled():  # fail if any check plugin cannot be loaded!
            config.load_all_checks(check_api.get_check_api_context)

        self.info = copy.deepcopy(config.check_info)
        self.cache = {}
示例#12
0
def update():
    config.load_all_checks(check_api.get_check_api_context)
    config.load()

    config_cache = config.get_config_cache()
    update_service_info(config_cache, get_hostnames(config_cache))
示例#13
0
def test_load_checks():
    config._initialize_data_structures()
    assert config.check_info == {}
    config.load_all_checks(check_api.get_check_api_context)
    assert len(config.check_info) > 1000
示例#14
0
def test_discoverable_tcp_checks():
    config.load_all_checks(check_api.get_check_api_context)
    assert "uptime" in config.discoverable_tcp_checks()
    assert "snmp_uptime" not in config.discoverable_tcp_checks()
    assert "logwatch" in config.discoverable_tcp_checks()
示例#15
0
def test_test_check_1_merged_rule(request, test_cfg, site, web):  # noqa: F811 # pylint: disable=redefined-outer-name

    test_check_path = "local/lib/check_mk/base/plugins/agent_based/test_check_1.py"

    def cleanup():
        if site.file_exists("etc/check_mk/conf.d/test_check_1.mk"):
            site.delete_file("etc/check_mk/conf.d/test_check_1.mk")

        if site.file_exists("var/check_mk/autochecks/modes-test-host.mk"):
            site.delete_file("var/check_mk/autochecks/modes-test-host.mk")

        site.delete_file(test_check_path)

    request.addfinalizer(cleanup)

    site.write_file(
        test_check_path, """
import pprint

from .agent_based_api.v0 import register, Service


def discover(params, section):
    yield Service(item=pprint.pformat(params))


def check(item, section):
    return
    yield


register.check_plugin(
    name="test_check_1",
    discovery_function=discover,
    discovery_ruleset_name="discover_test_check_1",
    discovery_ruleset_type="merged",
    discovery_default_parameters={"default": 42},
    check_function=check,
    service_name="Foo %s",
)
""")

    site.write_file("var/check_mk/agent_output/modes-test-host",
                    "<<<test_check_1>>>\n1 2\n")

    config.load_all_checks(check_api.get_check_api_context)
    config.load(with_conf_d=False)

    web.discover_services("modes-test-host")

    # Verify that the discovery worked as expected
    services = autochecks.parse_autochecks_file("modes-test-host",
                                                config.service_description)
    for service in services:
        if str(service.check_plugin_name) == "test_check_1":
            assert service.item == "Parameters({'default': 42})"
            break
    else:
        assert False, '"test_check_1" not discovered'

    # And now overwrite the setting in the config
    site.write_file(
        "etc/check_mk/conf.d/test_check_1.mk",
        "discover_test_check_1 = [{'value': {'levels': (1, 2)}, 'condition': {}}]\n"
    )

    # rediscover with the setting in the config
    site.delete_file("var/check_mk/autochecks/modes-test-host.mk")
    web.discover_services("modes-test-host")
    services = autochecks.parse_autochecks_file("modes-test-host",
                                                config.service_description)
    for service in services:
        if str(service.check_plugin_name) == "test_check_1":
            assert service.item == "Parameters({'default': 42, 'levels': (1, 2)})"
            break
    else:
        assert False, '"test_check_1" not discovered'