Beispiel #1
0
def get_check_table(discovery_request: StartDiscoveryRequest) -> DiscoveryResult:
    """Gathers the check table using a background job

    Cares about handling local / remote sites using an automation call. In both cases
    the ServiceDiscoveryBackgroundJob is executed to care about collecting the check
    table asynchronously. In case of a remote site the chain is:

    Starting from central site:

    _get_check_table()
          |
          v
    automation service-discovery-job-discover
          |
          v
    to remote site
          |
          v
    AutomationServiceDiscoveryJob().execute()
          |
          v
    _get_check_table()
    """
    if discovery_request.options.action == DiscoveryAction.TABULA_RASA:
        watolib.add_service_change(
            discovery_request.host, "refresh-autochecks",
            _("Refreshed check configuration of host '%s'") % discovery_request.host.name())

    if config.site_is_local(discovery_request.host.site_id()):
        return execute_discovery_job(discovery_request)

    discovery_result = _get_check_table_from_remote(discovery_request)
    discovery_result = _add_missing_discovery_result_fields(discovery_result)
    return discovery_result
Beispiel #2
0
    def _discover_services(self, request):
        mode = request.get("mode", "new")
        hostname = request.get("hostname")

        check_hostname(hostname, should_exist=True)

        host = watolib.Host.host(hostname)

        host_attributes = host.effective_attributes()

        if host.is_cluster():
            # This is currently the only way to get some actual discovery statitics.
            # Start a dry-run -> Get statistics
            # Do an actual discovery on the nodes -> data is written
            try_result = watolib.check_mk_automation(host_attributes.get("site"), "try-inventory",
                                                     ["@scan"] + [hostname])

            new = 0
            old = 0
            for entry in try_result["check_table"]:
                if entry[0] == "new":
                    new += 1
                elif entry[0] == "old":
                    old += 1

            result = DiscoveryResult(self_new=new, self_kept=old, self_total=new + old)
            watolib.check_mk_automation(host_attributes.get("site"), "inventory",
                                        ["@scan", mode] + host.cluster_nodes())
        else:
            response = execute_automation_discovery(site_id=host_attributes.get("site"),
                                                    args=["@scan", mode, hostname])
            result = response.results[hostname]

        if result.error_text:
            if not host.discovery_failed():
                host.set_discovery_failed()
            raise MKUserError(None, _("Failed to discover %s: %s") % (hostname, result.error_text))

        if host.discovery_failed():
            host.clear_discovery_failed()

        if mode == "refresh":
            message = _("Refreshed check configuration of host [%s] with %d services") % (
                hostname, result.self_total)
            watolib.add_service_change(host, "refresh-autochecks", message)
        else:
            message = _("Saved check configuration of host [%s] with %d services") % (
                hostname, result.self_total)
            watolib.add_service_change(host, "set-autochecks", message)

        msg = _("Service discovery successful. Added %d, removed %d, kept %d, total %d services "
                "and %d new, %d total host labels") % (
                    result.self_new,
                    result.self_removed,
                    result.self_kept,
                    result.self_total,
                    result.self_new_host_labels,
                    result.self_total_host_labels,
                )
        return msg
Beispiel #3
0
    def _discover_services(self, request):
        mode = request.get("mode", "new")
        hostname = request.get("hostname")

        check_hostname(hostname, should_exist=True)

        host = watolib.Host.host(hostname)

        host_attributes = host.effective_attributes()

        if host.is_cluster():
            # This is currently the only way to get some actual discovery statitics.
            # Start a dry-run -> Get statistics
            # Do an actual discovery on the nodes -> data is written
            result = watolib.check_mk_automation(host_attributes.get("site"), "try-inventory",
                                                 ["@scan"] + [hostname])
            counts = {"new": 0, "old": 0}
            for entry in result["check_table"]:
                if entry[0] in counts:
                    counts[entry[0]] += 1

            counts = {
                hostname: (
                    counts["new"],
                    0,  # this info is not available for clusters
                    counts["old"],
                    counts["new"] + counts["old"])
            }

            # A cluster cannot fail, just the nodes. This information is currently discarded
            failed_hosts = None
            watolib.check_mk_automation(host_attributes.get("site"), "inventory",
                                        ["@scan", mode] + host.cluster_nodes())
        else:
            counts, failed_hosts = watolib.check_mk_automation(host_attributes.get("site"),
                                                               "inventory",
                                                               ["@scan", mode] + [hostname])

        if failed_hosts:
            if not host.discovery_failed():
                host.set_discovery_failed()
            raise MKUserError(
                None,
                _("Failed to inventorize %s: %s") % (hostname, failed_hosts[hostname]))

        if host.discovery_failed():
            host.clear_discovery_failed()

        if mode == "refresh":
            message = _("Refreshed check configuration of host [%s] with %d services") % (
                hostname, counts[hostname][3])
            watolib.add_service_change(host, "refresh-autochecks", message)
        else:
            message = _("Saved check configuration of host [%s] with %d services") % (
                hostname, counts[hostname][3])
            watolib.add_service_change(host, "set-autochecks", message)

        msg = _("Service discovery successful. Added %d, removed %d, kept %d, total %d services "
                "and %d new, %d total host labels") % tuple(counts[hostname])
        return msg
Beispiel #4
0
    def _save_services(self, old_autochecks: SetAutochecksTable,
                       checks: SetAutochecksTable, need_sync: bool) -> None:
        message = _(
            "Saved check configuration of host '%s' with %d services") % (
                self._host.name(),
                len(checks),
            )
        watolib.add_service_change(
            host=self._host,
            action_name="set-autochecks",
            text=message,
            need_sync=need_sync,
            diff_text=watolib.make_diff_text(
                _make_host_audit_log_object(old_autochecks),
                _make_host_audit_log_object(checks)),
        )

        site_id = self._host.site_id()
        site_status = states().get(site_id, SiteStatus({}))
        if is_pre_17_remote_site(site_status):
            # is this branch still needed?
            set_autochecks(
                site_id,
                self._host.name(),
                {x: y[1:3]
                 for x, y in checks.items()},  # type: ignore[misc]
            )
        else:
            set_autochecks(
                site_id,
                self._host.name(),
                checks,
            )
Beispiel #5
0
 def _save_services(self, checks, need_sync):
     message = _("Saved check configuration of host '%s' with %d services") % \
                 (self._host.name(), len(checks))
     watolib.add_service_change(self._host,
                                "set-autochecks",
                                message,
                                need_sync=need_sync)
     check_mk_automation(self._host.site_id(), "set-autochecks",
                         [self._host.name()], checks)
Beispiel #6
0
 def _save_services(self, old_autochecks: SetAutochecksTable,
                    checks: SetAutochecksTable, need_sync: bool) -> None:
     message = _("Saved check configuration of host '%s' with %d services"
                 ) % (self._host.name(), len(checks))
     watolib.add_service_change(
         host=self._host,
         action_name="set-autochecks",
         text=message,
         need_sync=need_sync,
         diff_text=watolib.make_diff_text(
             _make_host_audit_log_object(old_autochecks),
             _make_host_audit_log_object(checks)),
     )
     check_mk_automation(self._host.site_id(), "set-autochecks",
                         [self._host.name()], checks)
Beispiel #7
0
def get_check_table(
        discovery_request: StartDiscoveryRequest) -> DiscoveryResult:
    """Gathers the check table using a background job

    Cares about handling local / remote sites using an automation call. In both cases
    the ServiceDiscoveryBackgroundJob is executed to care about collecting the check
    table asynchronously. In case of a remote site the chain is:

    Starting from central site:

    _get_check_table()
          |
          v
    automation service-discovery-job-discover
          |
          v
    to remote site
          |
          v
    AutomationServiceDiscoveryJob().execute()
          |
          v
    _get_check_table()
    """
    if discovery_request.options.action == DiscoveryAction.TABULA_RASA:
        watolib.add_service_change(
            discovery_request.host,
            "refresh-autochecks",
            _("Refreshed check configuration of host '%s'") %
            discovery_request.host.name(),
        )

    if site_is_local(discovery_request.host.site_id()):
        return execute_discovery_job(discovery_request)

    sync_changes_before_remote_automation(discovery_request.host.site_id())

    return DiscoveryResult.deserialize(
        watolib.do_remote_automation(
            get_site_config(discovery_request.host.site_id()),
            "service-discovery-job",
            [
                ("host_name", discovery_request.host.name()),
                ("options", json.dumps(discovery_request.options._asdict())),
            ],
        ))