Exemplo n.º 1
0
def _connect_multiple_sites(user: LoggedInUser) -> None:
    enabled_sites, disabled_sites = _get_enabled_and_disabled_sites(user)
    _set_initial_site_states(enabled_sites, disabled_sites)

    if is_managed_edition():
        import cmk.gui.cme.managed as managed  # pylint: disable=no-name-in-module

        g.live = managed.CMEMultiSiteConnection(enabled_sites, disabled_sites)
    else:
        g.live = MultiSiteConnection(enabled_sites, disabled_sites)

    # Fetch status of sites by querying the version of Nagios and livestatus
    # This may be cached by a proxy for up to the next configuration reload.
    g.live.set_prepend_site(True)
    for response in g.live.query(
            "GET status\n"
            "Cache: reload\n"
            "Columns: livestatus_version program_version program_start num_hosts num_services "
            "core_pid"):

        try:
            site_id, v1, v2, ps, num_hosts, num_services, pid = response
        except ValueError:
            e = MKLivestatusQueryError("Invalid response to status query: %s" %
                                       response)

            site_id = response[0]
            g.site_status[site_id].update({
                "exception":
                e,
                "status_host_state":
                None,
                "state":
                _status_host_state_name(None),
            })
            continue

        g.site_status[site_id].update({
            "state":
            "online",
            "livestatus_version":
            v1,
            "program_version":
            v2,
            "program_start":
            ps,
            "num_hosts":
            num_hosts,
            "num_services":
            num_services,
            "core":
            v2.startswith("Check_MK") and "cmc" or "nagios",
            "core_pid":
            pid,
        })
    g.live.set_prepend_site(False)

    # TODO(lm): Find a better way to make the Livestatus object trigger the update
    # once self.deadsites is updated.
    update_site_states_from_dead_sites()
Exemplo n.º 2
0
    def _query_callback(
            sites_live: MultiSiteConnection) -> Collection[LivestatusColumn]:
        """
        we search for hosts having a certain label
        ('cmk/kubernets/object:{object_type}') and want a list of unique labels
        values of labels with the key label_name.
        """
        label_filter = encode_label_for_livestatus(
            column="labels",
            label=Label("cmk/kubernetes/object", object_type, False),
        )
        query = f"GET hosts\nColumns: labels\n{label_filter}"

        query_result = sites_live.query_column(query)
        label_values = set()
        for element in query_result:
            for label_key, label_value in element.items():
                if label_key == label_name:
                    label_values.add(label_value)
        return label_values
Exemplo n.º 3
0
 def _query_callback(
         sites_live: MultiSiteConnection) -> Collection[LivestatusColumn]:
     return sites_live.query_column_unique(query)