예제 #1
0
파일: tcp.py 프로젝트: m3rlinux/checkmk
    def _get_agent_data(self) -> Tuple[AgentRawData, TransportProtocol]:
        try:
            raw_protocol = self._socket.recv(2, socket.MSG_WAITALL)
        except socket.error as e:
            raise MKFetcherError(f"Communication failed: {e}") from e

        protocol = self._detect_transport_protocol(
            raw_protocol, empty_msg="Empty output from host %s:%d" % self.address
        )

        controller_uuid = get_uuid_link_manager().get_uuid(self.host_name)
        self._validate_protocol(protocol, is_registered=controller_uuid is not None)

        if protocol is TransportProtocol.TLS:
            with self._wrap_tls(controller_uuid) as ssock:
                raw_agent_data = self._recvall(ssock)
            try:
                agent_data = AgentCtlMessage.from_bytes(raw_agent_data).payload
            except ValueError as e:
                raise MKFetcherError(f"Failed to deserialize versioned agent data: {e!r}") from e
            return AgentRawData(agent_data[2:]), self._detect_transport_protocol(
                agent_data[:2], empty_msg="Empty payload from controller at %s:%d" % self.address
            )

        return AgentRawData(self._recvall(self._socket, socket.MSG_WAITALL)), protocol
예제 #2
0
def _link_with_uuid(
    host_name: HostName,
    host: CREHost,
    uuid: UUID,
) -> None:
    uuid_link_manager = get_uuid_link_manager()
    uuid_link_manager.create_link(
        host_name,
        uuid,
        create_target_dir=host.effective_attributes().get("cmk_agent_connection") == "push-agent",
    )
예제 #3
0
파일: tcp.py 프로젝트: troelsarvin/checkmk
    def _wrap_tls(self) -> ssl.SSLSocket:
        controller_uuid = get_uuid_link_manager().get_uuid(self.host_name)

        if controller_uuid is None:
            raise MKFetcherError("Agent controller not registered")

        self._logger.debug("Reading data from agent via TLS socket")
        try:
            ctx = ssl.create_default_context(cafile=str(paths.root_cert_file))
            ctx.load_cert_chain(certfile=paths.site_cert_file)
            return ctx.wrap_socket(self._socket,
                                   server_hostname=str(controller_uuid))
        except ssl.SSLError as e:
            raise MKFetcherError("Error establishing TLS connection") from e
예제 #4
0
def perform_rename_hosts(renamings, job_interface=None):
    """Rename hosts mechanism

    Args:
        renamings:
            tuple consisting of folder, oldname, newname

        job_interface:
            only relevant for WATO interaction, allows to update the interface with the current
            update info
    """
    def update_interface(message: str) -> None:
        if job_interface is None:
            return
        job_interface.send_progress_update(message)

    actions = []
    all_hosts = Host.all()

    # 1. Fix WATO configuration itself ----------------
    auth_problems = []
    successful_renamings = []
    update_interface(_("Renaming WATO configuration..."))
    for folder, oldname, newname in renamings:
        try:
            this_host_actions = []
            update_interface(_("Renaming host(s) in folders..."))
            this_host_actions += _rename_host_in_folder(
                folder, oldname, newname)
            update_interface(_("Renaming host(s) in cluster nodes..."))
            this_host_actions += _rename_host_as_cluster_node(
                all_hosts, oldname, newname)
            update_interface(_("Renaming host(s) in parents..."))
            this_host_actions += _rename_host_in_parents(oldname, newname)
            update_interface(_("Renaming host(s) in rulesets..."))
            this_host_actions += _rename_host_in_rulesets(
                folder, oldname, newname)
            update_interface(_("Renaming host(s) in BI aggregations..."))
            this_host_actions += _rename_host_in_bi(oldname, newname)
            actions += this_host_actions
            successful_renamings.append((folder, oldname, newname))
        except MKAuthException as e:
            auth_problems.append((oldname, e))

    # 2. Checkmk stuff ------------------------------------------------
    update_interface(
        _("Renaming host(s) in base configuration, rrd, history files, etc."))
    update_interface(
        _("This might take some time and involves a core restart..."))
    action_counts = _rename_hosts_in_check_mk(successful_renamings)

    # 3. Notification settings ----------------------------------------------
    # Notification rules - both global and users' ones
    update_interface(_("Renaming host(s) in notification rules..."))
    for folder, oldname, newname in successful_renamings:
        actions += _rename_host_in_event_rules(oldname, newname)
        actions += _rename_host_in_multisite(oldname, newname)

    # 4. Update UUID links
    update_interface(_("Renaming host(s): Update UUID links..."))
    actions += _rename_host_in_uuid_link_manager(
        get_uuid_link_manager(),
        [(oldname, newname)
         for _folder, oldname, newname in successful_renamings],
    )

    for action in actions:
        action_counts.setdefault(action, 0)
        action_counts[action] += 1

    update_interface(_("Calling final hooks"))
    call_hook_hosts_changed(Folder.root_folder())
    return action_counts, auth_problems
예제 #5
0
def _remove_tls_registration(host_names: Sequence[HostName]) -> None:
    get_uuid_link_manager().unlink_sources(host_names)