def _table_query( properties, context, column_generator, table: str, infos: List[str] ) -> Tuple[List[str], LivestatusResponse]: filter_headers, only_sites = visuals.get_filter_headers(table, infos, context) columns = column_generator(properties, context) query = ( f"GET {table}\n" "Columns: %(cols)s\n" "%(filter)s" % { "cols": " ".join(columns), "filter": filter_headers, } ) with sites.only_sites(only_sites), sites.prepend_site(): try: rows = sites.live().query(query) except MKTimeout: raise except Exception: raise MKGeneralException(_("The query returned no data.")) return ["site"] + columns, rows
def _livestatus_get_labels( self, only_sites: List[str]) -> List[Dict[SiteId, _Labels]]: """ Get labels for all sites that need an update and the user is authorized for """ query: str = ( "GET services\n" # "Cache: reload\n" # "Columns: host_labels labels\n") with sites.prepend_site(), sites.only_sites(only_sites): rows = [(x[0], x[1], x[2]) for x in sites.live(user).query(query)] host_labels: Dict[SiteId, _Labels] = {} service_labels: Dict[SiteId, _Labels] = {} for row in rows: site_id = row[0] host_label = row[1] service_label = row[2] for key, value in host_label.items(): host_labels.setdefault(site_id, {}).update({key: value}) for key, value in service_label.items(): service_labels.setdefault(site_id, {}).update({key: value}) return [host_labels, service_labels]
def _query_livestatus( self, only_sites: List[SiteId], ) -> List[Tuple[SiteId, Dict[str, str], Dict[str, str]]]: query: str = "GET services\n" "Cache: reload\n" "Columns: host_labels labels\n" with sites.prepend_site(), sites.only_sites(only_sites): rows = [(x[0], x[1], x[2]) for x in sites.live(user).query(query)] return rows
def _resolve_site(host: str): with sites.prepend_site(): query = "GET hosts\nFilter: name = %s\nColumns: name" % livestatus.lqencode( host) try: return sites.live().query_value(query) except livestatus.MKLivestatusNotFoundError: raise MKUserError( "host", _("The host could not be found on any active site."))
def _resolve_site(host): # When the site is available via URL context, use it. Otherwise it is needed # to check all sites for the requested host if html.request.has_var('site'): return html.request.var('site') with sites.prepend_site(): query = "GET hosts\nFilter: name = %s\nColumns: name" % livestatus.lqencode(host) try: return sites.live().query_value(query) except livestatus.MKLivestatusNotFoundError: raise MKUserError("host", _("The host could not be found on any active site."))
def bi_livestatus_query( query: str, only_sites: Optional[List[SiteId]] = None, output_format: LivestatusOutputFormat = LivestatusOutputFormat.PYTHON, ) -> LivestatusResponse: with sites.output_format(output_format), sites.only_sites(only_sites), sites.prepend_site(): try: sites.live().set_auth_domain("bi") return sites.live().query(query) finally: sites.live().set_auth_domain("read")
def livestatus_query_bare( table: Literal["host", "service"], context: VisualContext, columns: List[str], cache: Optional[Literal["reload"]] = None, ) -> List[Dict[str, Any]]: """Return for the service table filtered by context the given columns. Optional cache reload. Return with site info in""" if query := livestatus_query_bare_string(table, context, columns, cache): selected_sites = get_only_sites_from_context(context) res_columns = ["site"] + columns with sites.only_sites(selected_sites), sites.prepend_site(): return [dict(zip(res_columns, row)) for row in sites.live().query(query)]
def get_graph_data_from_livestatus(only_sites, host_name, service_description): columns = ["perf_data", "metrics", "check_command"] query = livestatus_lql([host_name], columns, service_description) what = "host" if service_description == "_HOST_" else "service" labels = ["site"] + ["%s_%s" % (what, col) for col in columns] with sites.only_sites(only_sites), sites.prepend_site(): info = dict(zip(labels, sites.live().query_row(query))) info["host_name"] = host_name if what == "service": info["service_description"] = service_description return info
def get_graph_data_from_livestatus(only_sites, host_name, service_description): columns = [u'perf_data', u'metrics', u'check_command'] query = livestatus_lql([host_name], columns, service_description) what = 'host' if service_description == "_HOST_" else 'service' labels = [u"site"] + [u"%s_%s" % (what, col) for col in columns] with sites.only_sites(only_sites), sites.prepend_site(): info = dict(zip(labels, sites.live().query_row(query))) info['host_name'] = host_name if what == 'service': info['service_description'] = service_description return info
def _query_livestatus( self, only_sites: List[SiteId], ) -> _LivestatusLabelResponse: with sites.prepend_site(), sites.only_sites(only_sites): service_rows = sites.live().query("GET services\n" "Cache: reload\n" "Columns: labels\n") host_rows = sites.live().query("GET hosts\n" "Cache: reload\n" "Columns: labels\n") return _LivestatusLabelResponse(host_rows, service_rows)
def _get_default_view_hostnames(self, max_nodes: int) -> Set[HostName]: """ Returns all hosts without any parents """ query = "GET hosts\nColumns: name\nFilter: parents =" with sites.prepend_site(), sites.only_sites(html.request.var("site")): hosts = [(x[0], x[1]) for x in sites.live().query(query)] # If no explicit site is set and the number of initially displayed hosts # exceeds the auto growth range, only the hosts of the master site are shown if len(hosts) > max_nodes: hostnames = {x[1] for x in hosts if x[0] == config.omd_site()} else: hostnames = {x[1] for x in hosts} return hostnames
def bi_livestatus_query( query: str, only_sites: Optional[List[SiteId]] = None, output_format: LivestatusOutputFormat = LivestatusOutputFormat.PYTHON, fetch_full_data: bool = False, ) -> LivestatusResponse: with sites.output_format(output_format), sites.only_sites( only_sites), sites.prepend_site(): try: auth_domain = "bi_fetch_full_data" if fetch_full_data else "bi" sites.live().set_auth_domain(auth_domain) return sites.live().query(query) finally: sites.live().set_auth_domain("read")
def __call__(self, cls, properties, context): filter_headers, only_sites = visuals.get_filter_headers("log", ["host", "service"], context) columns = self.f(cls, properties, context) query = ("GET services\n" "Columns: %(cols)s\n" "%(filter)s" % { "cols": " ".join(columns), "filter": filter_headers, }) with sites.only_sites(only_sites), sites.prepend_site(): try: rows = sites.live().query(query) except MKTimeout: raise except Exception: raise MKGeneralException(_("The query returned no data.")) return ['site'] + columns, rows
def _get_data(cls, properties, context, return_column_headers=True): time_range = cls.int_time_range_from_rangespec(properties["time_range"]) c_headers = "ColumnHeaders: on\n" if return_column_headers else "" filter_headers, only_sites = get_filter_headers("log", ["host", "service"], context) metrics = { "CPU load": "load1", "CPU utilization": "util", } service_desc = properties["service"] query = ( "GET services\n" "Columns: host_name host_state service_description service_state service_check_command service_metrics service_perf_data rrddata:v1:%(metric)s:%(start)s:%(end)s:%(step)s\n" # rrddata:m1:load1.max:%(start)s:%(end)s:%(step)s rrddata:m5:load5.max:%(start)s:%(end)s:%(step)s rrddata:m15:load15.max:%(start)s:%(end)s:%(step)s "%(column)s" "Filter: service_description ~~ %(service)s\n" "%(filter)s" % { "metric": metrics[service_desc], "start": time_range[0], "end": time_range[1], "step": 300, "service": service_desc, "column": c_headers, "filter": filter_headers, }) with sites.only_sites(only_sites), sites.prepend_site(): try: rows = sites.live().query(query) except MKTimeout: raise except Exception: raise MKGeneralException(_("The query returned no data.")) if return_column_headers: return rows[0], rows[1:] return rows, ""