def show(self): host = self._dashlet_spec['context'].get("host", html.request.var("host")) if not host: raise MKUserError('host', _('Missing needed host parameter.')) service = self._dashlet_spec['context'].get("service") if not service: service = "_HOST_" metric = self._dashlet_spec["metric"] if "metric" in self._dashlet_spec else "" site = self._get_site_by_host_name(host) metric_spec = {"site": site, "host": host, "service": service, "metric": metric} svc_url = "view.py?view_name=service&site=%s&host=%s&service=%s" % ( html.urlencode(site), html.urlencode(host), html.urlencode(service)) links = { "site": html.render_a(site, "view.py?view_name=sitehosts&site=%s" % (html.urlencode(site))), "host": html.render_a( host, "view.py?view_name=host&site=%s&host=%s" % (html.urlencode(site), html.urlencode(host))), "service": html.render_a(service, svc_url) } render_options = self._dashlet_spec["metric_render_options"] metrics = self._query_for_metrics_of_host(site, host, service) t_metrics = self._get_translated_metrics_from_perf_data(metrics) chosen_metric_name, metric_choices = self._get_chosen_metric(t_metrics, metric) svc_state = metrics["svc_state"] html.open_div(class_="metric") if metrics: if chosen_metric_name: chosen_metric = t_metrics[chosen_metric_name] titles = self._get_titles(metric_spec, links, render_options) self._render_metric_content(chosen_metric, render_options, titles, svc_state, svc_url) else: html.open_div(class_="no_metric_match") if metric_choices: # TODO: Fix this handling of no available/matching metric # after the implementation of host/site contexts warning_txt = HTML( _("The given metric \"%s\" could not be found.\ For the selected service \"%s\" you can choose from the following metrics:" % (metric, service))) warning_txt += html.render_ul("".join( [str(html.render_li(b)) for _a, b in metric_choices])) html.show_warning(warning_txt) else: html.show_warning(_("No metric could be found.")) html.close_div() else: html.show_warning(_("There are no metrics meeting your context filters.")) html.close_div()
def show_without_timeseries(self): @site_query def query(cls, properties, context): return [ "host_name", "service_check_command", "service_description", "service_perf_data", "service_state" ] col_names, data = query( # pylint: disable=unbalanced-tuple-unpacking self, json.dumps(self.vs_parameters().value_to_json(self._dashlet_spec)), self._dashlet_spec["context"]) row = dict(zip(col_names, data[0])) site = row["site"] host = row["host_name"] service = row["service_description"] metric = self._dashlet_spec.get("metric", "") t_metrics = translate_perf_data(row["service_perf_data"], row["service_check_command"]) chosen_metric = t_metrics.get(metric) if chosen_metric is None: html.show_warning( _("There are no metrics meeting your context filters.")) warning_txt = HTML( _("The given metric \"%s\" could not be found.\ For the selected service \"%s\" you can choose from the following metrics:" % (metric, service))) warning_txt += html.render_ul("".join( [str(html.render_li(m["title"])) for m in t_metrics.values()])) html.show_warning(warning_txt) return svc_url = "view.py?view_name=service&site=%s&host=%s&service=%s" % ( html.urlencode(site), html.urlencode(host), html.urlencode(service)) links = { "site": html.render_a( site, "view.py?view_name=sitehosts&site=%s" % (html.urlencode(site))), "host": html.render_a( host, "view.py?view_name=host&site=%s&host=%s" % (html.urlencode(site), html.urlencode(host))), "service": html.render_a(service, svc_url) } render_options = self._dashlet_spec["render_options"] svc_state = row["service_state"] html.open_div(class_="metric") metric_spec = { "site": site, "host": host, "service": service, "metric": chosen_metric.get("title", metric) } titles = self._get_titles(metric_spec, links, render_options) self._render_metric_content(chosen_metric, render_options, titles, svc_state, svc_url) html.close_div()
def _process_message_message(msg): msg["id"] = utils.gen_id() msg["time"] = time.time() if isinstance(msg["dest"], str): dest_what = msg["dest"] else: dest_what = msg["dest"][0] if dest_what == "all_users": recipients = list(config.multisite_users.keys()) elif dest_what == "online": recipients = userdb.get_online_user_ids() elif dest_what == "list": recipients = msg["dest"][1] else: recipients = [] num_recipients = len(recipients) num_success: Dict[str, int] = {} for method in msg["methods"]: num_success[method] = 0 # Now loop all messaging methods to send the messages errors: Dict[str, List[Tuple]] = {} for user_id in recipients: for method in msg["methods"]: try: handler = _messaging_methods()[method]["handler"] handler(user_id, msg) num_success[method] = num_success[method] + 1 except MKInternalError as e: errors.setdefault(method, []).append((user_id, e)) message = escape_html_permissive( _("The message has successfully been sent...")) message += html.render_br() parts = [] for method in msg["methods"]: parts.append( html.render_li( _messaging_methods()[method]["confirmation_title"] + (_(" for all recipients.") if num_success[method] == num_recipients else _(" for %d of %d recipients.") % (num_success[method], num_recipients)))) message += html.render_ul(HTML().join(parts)) message += html.render_p(_("Recipients: %s") % ", ".join(recipients)) html.show_message(message) if errors: error_message = HTML() for method, method_errors in errors.items(): error_message += _( "Failed to send %s messages to the following users:") % method table_rows = HTML() for user_id, exception in method_errors: table_rows += html.render_tr( html.render_td(html.render_tt(user_id)) + html.render_td(str(exception))) error_message += html.render_table(table_rows) + html.render_br() html.show_error(error_message)