def _perform_service_scan(self, api_request): """The try-inventory automation refreshes the Check_MK internal cache and makes the new information available to the next try-inventory call made by get_result().""" sys.stdout.write( try_discovery( api_request.host.site_id(), self._get_automation_flags(api_request), api_request.host.name(), ).output)
def _get_check_table_from_remote(api_request): """Gathers the check table from a remote site Cares about pre 1.6 sites that does not support the new service-discovery-job API call. Falling back to the previously existing try-inventry and inventory automation calls. """ try: sync_changes_before_remote_automation(api_request.host.site_id()) return _deserialize_remote_result( watolib.do_remote_automation( get_site_config(api_request.host.site_id()), "service-discovery-job", [ ("host_name", api_request.host.name()), ("options", json.dumps(api_request.options._asdict())), ], )) except watolib.MKAutomationException as e: if "Invalid automation command: service-discovery-job" not in "%s" % e: raise # Compatibility for pre 1.6 remote sites. if api_request.options.action == DiscoveryAction.TABULA_RASA: raise MKAutomationException( _("Tabula rasa not supported any more")) if api_request.options.action == DiscoveryAction.REFRESH: options = ["@scan"] else: options = ["@noscan"] if not api_request.options.ignore_errors: options.append("@raiseerrors") return DiscoveryResult( job_status={ "is_active": False, "state": JobStatusStates.INITIALIZED, }, check_table=try_discovery( api_request.host.site_id(), options, api_request.host.name(), ).check_table, check_table_created=int(time.time()), host_labels={}, new_labels={}, vanished_labels={}, changed_labels={}, )
def _get_try_discovery( api_request: StartDiscoveryRequest, ) -> Tuple[int, TryDiscoveryResult]: # TODO: Use the correct time. This is difficult because cmk.base does not have a single # time for all data of a host. The data sources should be able to provide this information # somehow. return ( int(time.time()), try_discovery( api_request.host.site_id(), ["@noscan"], 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.load_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 = try_discovery( host_attributes.get("site"), ["@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) discovery( host_attributes.get("site"), mode, ["@scan"], host.cluster_nodes(), ) else: result = discovery( host_attributes.get("site"), mode, ["@scan"], [hostname], non_blocking_http=True, ).hosts[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