def _perform_automatic_refresh(self, api_request): # TODO: In distributed sites this must not add a change on the remote site. We need to build # the way back to the central site and show the information there. execute_automation_discovery( site_id=api_request.host.site_id(), args=["@scan", "refresh", api_request.host.name()])
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
def _execute_discovery(self, task, mode, do_scan, error_handling) -> AutomationDiscoveryResponse: arguments = [mode] + task.host_names if do_scan: arguments = ["@scan"] + arguments if not error_handling: arguments = ["@raiseerrors"] + arguments timeout = request.request_timeout - 2 return execute_automation_discovery(site_id=task.site_id, args=arguments, timeout=timeout, non_blocking_http=True)