Beispiel #1
0
def discover_marked_hosts(core: MonitoringCore) -> None:
    """Autodiscovery"""

    console.verbose("Doing discovery for all marked hosts:\n")
    autodiscovery_queue = _AutodiscoveryQueue()

    config_cache = config.get_config_cache()

    autodiscovery_queue.cleanup(
        valid_hosts=config_cache.all_configured_hosts(),
        logger=console.verbose,
    )

    oldest_queued = autodiscovery_queue.oldest()
    if oldest_queued is None:
        console.verbose(
            "  Nothing to do. No hosts marked by discovery check.\n")
        return

    process_hosts = EVERYTHING if (up_hosts :=
                                   _get_up_hosts()) is None else up_hosts

    activation_required = False
    rediscovery_reference_time = time.time()

    with TimeLimitFilter(limit=120, grace=10, label="hosts") as time_limited:
        for host_name in time_limited(autodiscovery_queue.queued_hosts()):
            if host_name not in process_hosts:
                continue

            activation_required |= _discover_marked_host(
                config_cache=config_cache,
                host_config=config_cache.get_host_config(host_name),
                autodiscovery_queue=autodiscovery_queue,
                reference_time=rediscovery_reference_time,
                oldest_queued=oldest_queued,
            )

    if not activation_required:
        return

    console.verbose(
        "\nRestarting monitoring core with updated configuration...\n")
    with config.set_use_core_config(use_core_config=False):
        try:
            _config_cache.clear_all()
            config.get_config_cache().initialize()

            # reset these to their original value to create a correct config
            cmk.core_helpers.cache.FileCacheFactory.use_outdated = False
            cmk.core_helpers.cache.FileCacheFactory.maybe = True
            if config.monitoring_core == "cmc":
                cmk.base.core.do_reload(core)
            else:
                cmk.base.core.do_restart(core)
        finally:
            _config_cache.clear_all()
            config.get_config_cache().initialize()
Beispiel #2
0
def discover_marked_hosts(core: MonitoringCore) -> None:
    console.verbose("Doing discovery for all marked hosts:\n")
    autodiscovery_dir = _get_autodiscovery_dir()

    if not os.path.exists(autodiscovery_dir):
        # there is obviously nothing to do
        console.verbose("  Nothing to do. %s is missing.\n" %
                        autodiscovery_dir)
        return

    config_cache = config.get_config_cache()

    oldest_queued = _queue_age()
    hosts = os.listdir(autodiscovery_dir)
    if not hosts:
        console.verbose(
            "  Nothing to do. No hosts marked by discovery check.\n")

    # Fetch host state information from livestatus
    host_states = _fetch_host_states()
    activation_required = False
    rediscovery_reference_time = time.time()

    with TimeLimitFilter(limit=120, grace=10, label="hosts") as time_limited:
        for host_name in time_limited(hosts):
            host_config = config_cache.get_host_config(host_name)

            if not _discover_marked_host_exists(config_cache, host_name):
                continue

            # Only try to discover hosts with UP state
            if host_states and host_states.get(host_name) != 0:
                continue

            if _discover_marked_host(config_cache, host_config,
                                     rediscovery_reference_time,
                                     oldest_queued):
                activation_required = True

    if activation_required:
        console.verbose(
            "\nRestarting monitoring core with updated configuration...\n")
        with config.set_use_core_config(use_core_config=False):
            try:
                _config_cache.clear_all()
                config.get_config_cache().initialize()

                if config.monitoring_core == "cmc":
                    cmk.base.core.do_reload(core)
                else:
                    cmk.base.core.do_restart(core)
            finally:
                _config_cache.clear_all()
                config.get_config_cache().initialize()