Ejemplo n.º 1
0
def load_plugins(get_check_api_context: config.GetCheckApiContext,
                 get_inventory_context: config.GetInventoryApiContext) -> None:
    loaded_files: Set[str] = set()
    filelist = config.get_plugin_paths(str(cmk.utils.paths.local_inventory_dir),
                                       cmk.utils.paths.inventory_dir)

    for f in filelist:
        if f[0] == "." or f[-1] == "~":
            continue  # ignore editor backup / temp files

        file_name = os.path.basename(f)
        if file_name in loaded_files:
            continue  # skip already loaded files (e.g. from local)

        try:
            plugin_context = _new_inv_context(get_check_api_context, get_inventory_context)

            _load_plugin_includes(f, plugin_context)

            exec(open(f).read(), plugin_context)  # yapf: disable
            loaded_files.add(file_name)
        except Exception as e:
            console.error("Error in inventory plugin file %s: %s\n", f, e)
            if cmk.utils.debug.enabled():
                raise
            continue

        # Now store the check context for all plugins found in this file
        for check_plugin_name in inv_info:
            _plugin_contexts.setdefault(check_plugin_name, plugin_context)
            _plugin_file_lookup.setdefault(check_plugin_name, f)

    _extract_snmp_sections(inv_info, _plugin_file_lookup)
    _extract_inventory_plugins(inv_info)
Ejemplo n.º 2
0
def load_plugins(get_check_api_context, get_inventory_context):
    # type: (config.GetCheckApiContext, config.GetInventoryApiContext) -> None
    loaded_files = set()  # type: Set[str]
    filelist = config.get_plugin_paths(
        str(cmk.utils.paths.local_inventory_dir),
        cmk.utils.paths.inventory_dir)

    for f in filelist:
        if f[0] == "." or f[-1] == "~":
            continue  # ignore editor backup / temp files

        file_name = os.path.basename(f)
        if file_name in loaded_files:
            continue  # skip already loaded files (e.g. from local)

        try:
            plugin_context = _new_inv_context(get_check_api_context,
                                              get_inventory_context)
            known_plugins = inv_info

            _load_plugin_includes(f, plugin_context)

            exec(open(f).read(), plugin_context)
            loaded_files.add(file_name)
        except Exception as e:
            console.error("Error in inventory plugin file %s: %s\n", f, e)
            if cmk.utils.debug.enabled():
                raise
            continue

        # Now store the check context for all plugins found in this file
        for check_plugin_name in set(inv_info).difference(known_plugins):
            _plugin_contexts[check_plugin_name] = plugin_context
Ejemplo n.º 3
0
def test_imports_in_checks():
    check_files = config.get_plugin_paths(cmk.utils.paths.checks_dir)
    with_from_imports = [
        finding  #
        for check_file_path in check_files  #
        for finding in _search_from_imports(check_file_path)
    ]
    assert not with_from_imports, "Found %d from-imports:\n%s" % (
        len(with_from_imports), "\n".join(with_from_imports))
Ejemplo n.º 4
0
def load_legacy_inventory_plugins(
    get_check_api_context: config.GetCheckApiContext,
    get_inventory_context: GetInventoryApiContext,
) -> Sequence[str]:
    """load all old-school inventory plugins into the modern agent based registry
    """
    errors = []
    loaded_files: Set[str] = set()
    file_list = config.get_plugin_paths(
        str(cmk.utils.paths.local_inventory_dir),
        cmk.utils.paths.inventory_dir)

    for f in file_list:
        if f[0] == "." or f[-1] == "~":
            continue  # ignore editor backup / temp files

        file_name = os.path.basename(f)
        if file_name in loaded_files:
            continue  # skip already loaded files (e.g. from local)

        try:
            plugin_context = _new_inv_context(get_check_api_context,
                                              get_inventory_context)

            _load_plugin_includes(f, plugin_context)

            exec(open(f).read(), plugin_context)  # yapf: disable
            loaded_files.add(file_name)
        except Exception as exc:
            errors.append(f"Error in inventory plugin file {f}: {exc}\n")
            if cmk.utils.debug.enabled():
                raise
            continue

        # Now store the check context for all plugins found in this file
        for check_plugin_name in _inv_info:
            _plugin_contexts.setdefault(check_plugin_name, plugin_context)
            _plugin_file_lookup.setdefault(check_plugin_name, f)

    errors.extend(_extract_snmp_sections(_inv_info, _plugin_file_lookup))
    errors.extend(_extract_inventory_plugins(_inv_info))

    return errors