Exemplo n.º 1
0
def _create_core_config(
    core: MonitoringCore, hosts_to_update: HostsToUpdate = None
) -> ConfigurationWarnings:
    initialize_warnings()

    _verify_non_duplicate_hosts()
    _verify_non_deprecated_checkgroups()

    config_path = next(VersionedConfigPath.current())
    config_cache = config.get_config_cache()
    with config_path.create(is_cmc=config.is_cmc()), _backup_objects_file(core):
        core.create_config(config_path, config_cache, hosts_to_update=hosts_to_update)
        # TODO: Remove once we drop the binary config
        # Purpose of code is to delete the old config file after format switching to have precisely
        # one microcore config in core config directory.
        if config.is_cmc():
            try:
                if config.get_microcore_config_format() == "protobuf":
                    os.remove(cmk.utils.paths.var_dir + "/core/config")
                else:
                    os.remove(cmk.utils.paths.var_dir + "/core/config.pb")
            except OSError as _:
                pass

    cmk.utils.password_store.save(config.stored_passwords)

    return get_configuration_warnings()
Exemplo n.º 2
0
 def _make_fetcher(self) -> ProgramFetcher:
     return ProgramFetcher(
         self._make_file_cache(),
         cmdline=self.cmdline,
         stdin=self.stdin,
         is_cmc=config.is_cmc(),
     )
Exemplo n.º 3
0
 def configure_fetcher(self) -> Dict[str, Any]:
     return {
         "file_cache": self.file_cache.configure(),
         "cmdline": self.cmdline,
         "stdin": self.stdin,
         "is_cmc": config.is_cmc(),
     }
Exemplo n.º 4
0
def get_sorted_service_list(
    hostname: HostName,
    *,
    filter_mode: Optional[Literal["only_clustered", "include_clustered"]] = None,
    skip_ignored: bool = True,
) -> List[Service]:

    sorted_services_unresolved = sorted(
        get_check_table(hostname, filter_mode=filter_mode, skip_ignored=skip_ignored).values(),
        key=lambda service: service.description,
    )

    if config.is_cmc():
        return sorted_services_unresolved

    unresolved = [(service, set(config.service_depends_on(hostname, service.description)))
                  for service in sorted_services_unresolved]

    resolved: List[Service] = []
    while unresolved:
        resolved_descriptions = {service.description for service in resolved}
        newly_resolved = [
            service for service, dependencies in unresolved if dependencies <= resolved_descriptions
        ]
        if not newly_resolved:
            problems = [
                "%r (%s / %s)" % (s.description, s.check_plugin_name, s.item) for s, _ in unresolved
            ]
            raise MKGeneralException("Cyclic service dependency of host %s. Problematic are: %s" %
                                     (hostname, ", ".join(problems)))

        unresolved = [(s, d) for s, d in unresolved if s not in newly_resolved]
        resolved.extend(newly_resolved)

    return resolved
Exemplo n.º 5
0
 def _make_fetcher(self) -> ProgramFetcher:
     return ProgramFetcher(
         self._make_file_cache(),
         cluster_nodes=self.host_config.nodes or (),
         cmdline=self.cmdline,
         stdin=self.stdin,
         is_cmc=config.is_cmc(),
     )
Exemplo n.º 6
0
 def _execute(self) -> RawAgentData:
     self._logger.debug("Calling external program %r" % (self.source_cmdline))
     with ProgramDataFetcher(
             self.source_cmdline,
             self.source_stdin,
             config.is_cmc(),
     ) as fetcher:
         return fetcher.data()
     raise MKAgentError("Failed to read data")
Exemplo n.º 7
0
def _create_core_config(core: MonitoringCore) -> ConfigurationWarnings:
    initialize_warnings()

    _verify_non_duplicate_hosts()
    _verify_non_deprecated_checkgroups()

    config_path = next(VersionedConfigPath.current())
    with config_path.create(is_cmc=config.is_cmc()), _backup_objects_file(core):
        core.create_config(ConfigSerial(str(config_path.serial)))

    cmk.utils.password_store.save(config.stored_passwords)

    return get_configuration_warnings()
Exemplo n.º 8
0
def _create_core_config(
    core: MonitoringCore, hosts_to_update: HostsToUpdate = None
) -> ConfigurationWarnings:
    initialize_warnings()

    _verify_non_duplicate_hosts()
    _verify_non_deprecated_checkgroups()

    config_path = next(VersionedConfigPath.current())
    config_cache = config.get_config_cache()
    with config_path.create(is_cmc=config.is_cmc()), _backup_objects_file(core):
        core.create_config(config_path, config_cache, hosts_to_update=hosts_to_update)

    cmk.utils.password_store.save_for_helpers(config_path)

    return get_configuration_warnings()
Exemplo n.º 9
0
def _get_sorted_check_table(hostname,
                            remove_duplicates=False,
                            filter_mode=None,
                            skip_ignored=True):
    # type: (HostName, bool, Optional[str], bool) -> List[Service]
    # Convert from dictionary into simple tuple list. Then sort it according to
    # the service dependencies.
    # TODO: Use the Service objects from get_check_table once it returns these objects
    is_cmc = config.is_cmc()
    unsorted = [
        (service, [] if is_cmc else config.service_depends_on(
            hostname, service.description))
        for service in get_check_table(hostname,
                                       remove_duplicates=remove_duplicates,
                                       filter_mode=filter_mode,
                                       skip_ignored=skip_ignored).values()
    ]

    unsorted.sort(key=lambda x: x[0].description)

    ordered = []  # type: List[Service]
    while len(unsorted) > 0:
        unsorted_descrs = {entry[0].description for entry in unsorted}
        left = []
        at_least_one_hit = False
        for check in unsorted:
            deps_fulfilled = True
            for dep in check[1]:  # dependencies
                if dep in unsorted_descrs:
                    deps_fulfilled = False
                    break
            if deps_fulfilled:
                ordered.append(check[0])
                at_least_one_hit = True
            else:
                left.append(check)
        if len(left) == 0:
            break
        if not at_least_one_hit:
            raise MKGeneralException(
                "Cyclic service dependency of host %s. Problematic are: %s" %
                (hostname, ",".join(unsorted_descrs)))
        unsorted = left
    return ordered