def do_commands(view, what, rows): command = None title, executor = views.core_command(what, rows[0], 0, len(rows))[1:3] # just get the title title_what = _("hosts") if what == "host" else _("services") r = html.confirm( _("Do you really want to %(title)s the %(count)d %(what)s?") % { "title": title, "count": len(rows), "what": title_what, }) if r != True: return r is None # Show commands on negative answer count = 0 already_executed = set([]) for nr, row in enumerate(rows): nagios_commands, title, executor = views.core_command(what, row, nr, len(rows)) for command in nagios_commands: if command not in already_executed: if isinstance(command, unicode): command = command.encode("utf-8") executor(command, row["site"]) already_executed.add(command) count += 1 if count > 0: html.message(_("Successfully sent %d commands.") % count) return True # Show commands again
def action(self) -> ActionResult: if self._may_edit_config() and html.request.has_var("_delete"): key_id_as_str = html.request.var("_delete") if key_id_as_str is None: raise Exception("cannot happen") key_id = int(key_id_as_str) if key_id not in self.keys: return None key = self.keys[key_id] if self._key_in_use(key_id, key): raise MKUserError("", _("This key is still used.")) message = self._delete_confirm_msg() if key["owner"] != config.user.id: message += _( "<br><b>Note</b>: this key has created by user <b>%s</b>" ) % key["owner"] c = html.confirm(message, add_header=self.title()) if c: self.delete(key_id) self.save(self.keys) elif c is False: return FinalizeRequest(code=200) return None
def handle_acknowledgement(): if html.request.var("_werk_ack") and html.check_transaction(): werk_id = html.request.get_integer_input_mandatory("_werk_ack") if werk_id not in g_werks: raise MKUserError("werk", _("This werk does not exist.")) werk = g_werks[werk_id] if werk["compatible"] == "incomp_unack": acknowledge_werk(werk) html.show_message( _("Werk %s - %s has been acknowledged.") % (render_werk_id( werk, with_link=True), render_werk_title(werk))) html.reload_sidebar() load_werks() # reload ack states after modification elif html.request.var("_ack_all"): if html.confirm(_( "Do you really want to acknowledge <b>all</b> incompatible werks?" ), method="GET"): num = len(unacknowledged_incompatible_werks()) acknowledge_all_werks() html.show_message( _("%d incompatible Werks have been acknowledged.") % num) html.reload_sidebar() load_werks() # reload ack states after modification render_unacknowleged_werks()
def do_commands(what, rows): # type: (str, Rows) -> bool command = None title, executor = views.core_command(what, rows[0], 0, len(rows))[1:3] # just get the title title_what = _("hosts") if what == "host" else _("services") r = html.confirm( _("Do you really want to %(title)s the %(count)d %(what)s?") % { "title": title, "count": len(rows), "what": title_what, }) if r is not True: return r is None # Show commands on negative answer count = 0 already_executed = set() # type: Set[str] for nr, row in enumerate(rows): nagios_commands, title, executor = views.core_command(what, row, nr, len(rows)) for command in nagios_commands: if command not in already_executed: executor(command, row["site"]) already_executed.add(command) count += 1 if count > 0: html.show_message(_("Successfully sent %d commands.") % count) return True # Show commands again
def delete_job(self): job_id = html.request.var(self.delete_job_var) if not job_id: return job = GUIBackgroundJob(job_id=job_id) if not job.is_available(): return html.header("Deletion of job") c = html.confirm(_("Delete job %s%s?") % (job_id, self._get_extra_info(job))) if c and job.may_delete(): job.delete() self._did_delete_job = True html.message(_("Background job has been deleted"))
def handle_delete_annotations(): if html.request.var("_delete_annotation"): _site_id, _hostname, _service, host_state, service_state, fromtime, \ untiltime, site_host_svc = _handle_anno_request_vars() annotations = availability.load_annotations() annotation = availability.find_annotation(annotations, site_host_svc, host_state, service_state, fromtime, untiltime) if not annotation: return if not html.confirm( _("Are you sure that you want to delete the annotation '%s'?") % annotation["text"]): return availability.delete_annotation(annotations, site_host_svc, host_state, service_state, fromtime, untiltime) availability.save_annotations(annotations)
def stop_job(self): job_id = html.request.var(self.stop_job_var) if not job_id: return job = GUIBackgroundJob(job_id) if not job.is_available(): return html.header("Interuption of job") if self.confirm_dialog_opened() and not job.is_running(): html.message(_("No longer able to stop job. Background job just finished.")) return c = html.confirm(_("Stop job %s%s?") % (job_id, self._get_extra_info(job))) if c and job.may_stop(): job.stop() self._did_stop_job = True html.message(_("Background job has been stopped"))
def handle_delete_annotations(): if html.request.var("_delete_annotation"): site_id = html.request.var("anno_site") or "" hostname = html.request.var("anno_host") service = html.request.var("anno_service") or None fromtime = float(html.request.var("anno_from")) untiltime = float(html.request.var("anno_until")) site_host_svc = (site_id, hostname, service) annotations = availability.load_annotations() annotation = availability.find_annotation(annotations, site_host_svc, fromtime, untiltime) if not annotation: return if not html.confirm( _("Are you sure that you want to delete the annotation '%s'?") % annotation["text"]): return availability.delete_annotation(annotations, site_host_svc, fromtime, untiltime) availability.save_annotations(annotations)
def action(self): if self._may_edit_config() and html.request.has_var("_delete"): key_id = int(html.request.var("_delete")) if key_id not in self.keys: return key = self.keys[key_id] if self._key_in_use(key_id, key): raise MKUserError("", _("This key is still used.")) message = self._delete_confirm_msg() if key["owner"] != config.user.id: message += _( "<br><b>Note</b>: this key has created by user <b>%s</b>" ) % key["owner"] c = html.confirm(message, add_header=self.title()) if c: self.delete(key_id) self.save(self.keys) elif c is False: return ""
def _bulk_delete_after_confirm(cls, what): to_delete = [] for varname, _value in html.request.itervars(prefix="_c_%s+" % what): if html.get_checkbox(varname): checkbox_ident = varname.split("_c_%s+" % what)[-1] to_delete.append(checkbox_ident.split("+", 1)) if not to_delete: return c = html.confirm( _("Do you really want to delete %d %s?") % (len(to_delete), cls.phrase("title_plural"))) if c: for owner, instance_id in to_delete: cls.remove_instance((owner, instance_id)) for owner in {e[0] for e in to_delete}: cls.save_user_instances(owner) html.reload_sidebar() elif c is False: return False
def do_log_ack(site, host_name, file_name): logs_to_ack = [] if not host_name and not file_name: # all logs on all hosts for this_site, this_host, logs in all_logs(): for int_filename in logs: file_display = form_file_to_ext(int_filename) logs_to_ack.append( (this_site, this_host, int_filename, file_display)) ack_msg = _('all logfiles on all hosts') elif host_name and not file_name: # all logs on one host for int_filename in logfiles_of_host(site, host_name): file_display = form_file_to_ext(int_filename) logs_to_ack.append((site, host_name, int_filename, file_display)) ack_msg = _('all logfiles of host %s') % host_name elif host_name and file_name: # one log on one host int_filename = form_file_to_int(file_name) logs_to_ack = [(site, host_name, int_filename, form_file_to_ext(int_filename))] ack_msg = _('the log file %s on host %s') % (file_name, host_name) else: for this_site, this_host, logs in all_logs(): file_display = form_file_to_ext(file_name) if file_name in logs: logs_to_ack.append( (this_site, this_host, file_name, file_display)) ack_msg = _('log file %s on all hosts') % file_name title = _("Acknowledge %s") % html.render_text(ack_msg) if host_name: breadcrumb = make_host_breadcrumb(host_name) else: breadcrumb = make_simple_page_breadcrumb(MegaMenuMonitoring, title) html.header(title, breadcrumb) html.begin_context_buttons() button_all_logfiles() if host_name: html.context_button(_("All Logfiles of Host"), html.makeuri([('file', '')])) if host_name and file_name: html.context_button(_("Back to Logfile"), html.makeuri([])) html.end_context_buttons() ack = html.request.var('_ack') if not html.confirm( _("Do you really want to acknowledge %s by <b>deleting</b> all stored messages?" ) % ack_msg): html.footer() return if not config.user.may("general.act"): html.h1(_('Permission denied'), class_=["error"]) html.div(_('You are not allowed to acknowledge %s') % ack_msg, class_=["error"]) html.footer() return # filter invalid values if ack != '1': raise MKUserError('_ack', _('Invalid value for ack parameter.')) for this_site, this_host, int_filename, display_name in logs_to_ack: try: acknowledge_logfile(this_site, this_host, int_filename, display_name) except Exception as e: html.show_error( _('The log file <tt>%s</tt> of host <tt>%s</tt> could not be deleted: %s.' ) % (display_name, this_host, e)) html.footer() return html.show_message('<b>%s</b><p>%s</p>' % (_('Acknowledged %s') % ack_msg, _('Acknowledged all messages in %s.') % ack_msg)) html.footer()
def page_list(cls): cls.load() # custom_columns = [] # render_custom_buttons = None # render_custom_columns = None # render_custom_context_buttons = None # check_deletable_handler = None cls.need_overriding_permission("edit") html.header(cls.phrase("title_plural")) html.begin_context_buttons() html.context_button(cls.phrase("new"), cls.create_url(), "new_" + cls.type_name()) # TODO: Remove this legacy code as soon as views, dashboards and reports have been # moved to pagetypes.py html.context_button(_("Views"), "edit_views.py", "view") html.context_button(_("Dashboards"), "edit_dashboards.py", "dashboard") def has_reporting(): try: # The suppression below is OK, we just want to check if the module is there. import cmk.gui.cee.reporting # noqa: F401 # pylint: disable=unused-variable,redefined-outer-name return True except ImportError: return False if has_reporting(): html.context_button(_("Reports"), "edit_reports.py", "report") # ## if render_custom_context_buttons: # ## render_custom_context_buttons() for other_type_name, other_pagetype in page_types.items(): if cls.type_name() != other_type_name: html.context_button( other_pagetype.phrase("title_plural").title(), '%ss.py' % other_type_name, other_type_name) html.end_context_buttons() # Deletion delname = html.request.var("_delete") if delname and html.transaction_valid(): owner = UserId(html.request.get_unicode_input_mandatory('_owner', config.user.id)) try: instance = cls.instance((owner, delname)) except KeyError: raise MKUserError( "_delete", _("The %s you are trying to delete " "does not exist.") % cls.phrase("title")) if not instance.may_delete(): raise MKUserError("_delete", _("You are not permitted to perform this action.")) try: if owner != config.user.id: owned_by = _(" (owned by %s)") % owner else: owned_by = "" c = html.confirm( _("Please confirm the deletion of \"%s\"%s.") % (instance.title(), owned_by)) if c: cls.remove_instance((owner, delname)) cls.save_user_instances(owner) html.reload_sidebar() elif c is False: html.footer() return except MKUserError as e: html.user_error(e) # Bulk delete if html.request.var("_bulk_delete_my") and html.transaction_valid(): if cls._bulk_delete_after_confirm("my") is False: html.footer() return elif html.request.var("_bulk_delete_foreign") and html.transaction_valid(): if cls._bulk_delete_after_confirm("foreign") is False: html.footer() return my_instances, foreign_instances, builtin_instances = cls.get_instances() for what, title, instances in [ ("my", _('Customized'), my_instances), ("foreign", _('Owned by other users'), foreign_instances), ("builtin", _('Builtin'), builtin_instances), ]: if not instances: continue html.open_h3() html.write(title) html.close_h3() if what != "builtin": html.begin_form("bulk_delete_%s" % what, method="POST") with table_element(limit=None) as table: for instance in instances: table.row() if what != "builtin" and instance.may_delete(): table.cell(html.render_input( "_toggle_group", type_="button", class_="checkgroup", onclick="cmk.selection.toggle_all_rows(this.form);", value='X'), sortable=False, css="checkbox") html.checkbox("_c_%s+%s+%s" % (what, instance.owner(), instance.name())) # Actions table.cell(_('Actions'), css='buttons visuals') # View if isinstance(instance, PageRenderer): html.icon_button(instance.page_url(), _("View"), "new_" + cls.type_name()) # Clone / Customize html.icon_button(instance.clone_url(), _("Create a customized copy of this"), "clone") # Delete if instance.may_delete(): html.icon_button(instance.delete_url(), _("Delete!"), "delete") # Edit if instance.may_edit(): html.icon_button(instance.edit_url(), _("Edit"), "edit") cls.custom_list_buttons(instance) # Internal ID of instance (we call that 'name') table.cell(_('ID'), instance.name(), css="narrow") # Title table.cell(_('Title')) html.write_text(instance.render_title()) html.help(_u(instance.description())) # Custom columns specific to that page type instance.render_extra_columns(table) # ## for title, renderer in custom_columns: # ## table.cell(title, renderer(visual)) # Owner if instance.is_builtin(): ownertxt = html.render_i(_("builtin")) else: ownertxt = instance.owner() table.cell(_('Owner'), ownertxt) table.cell(_('Public'), _("yes") if instance.is_public() else _("no")) table.cell(_('Hidden'), _("yes") if instance.is_hidden() else _("no")) # FIXME: WTF?!? # TODO: Haeeh? Another custom columns # ## if render_custom_columns: # ## render_custom_columns(visual_name, visual) if what != "builtin": html.button("_bulk_delete_%s" % what, _("Bulk delete"), "submit", style="margin-top:10px") html.hidden_fields() html.end_form() html.footer() return
def wato_confirm(html_title, message): if not html.request.has_var("_do_confirm") and not html.request.has_var("_do_actions"): # TODO: get the breadcrumb from all call sites wato_html_head(html_title, Breadcrumb()) return html.confirm(message)
def wato_confirm(html_title, message): if not html.request.has_var("_do_confirm") and not html.request.has_var( "_do_actions"): wato_html_head(html_title) return html.confirm(message)
def do_log_ack(site, host_name, file_name): sites.live().set_auth_domain('action') logs_to_ack = [] if not host_name and not file_name: # all logs on all hosts for this_site, this_host, logs in all_logs(): for int_filename in logs: file_display = form_file_to_ext(int_filename) logs_to_ack.append( (this_site, this_host, int_filename, file_display)) ack_msg = _('all logfiles on all hosts') elif host_name and not file_name: # all logs on one host for int_filename in logfiles_of_host(site, host_name): file_display = form_file_to_ext(int_filename) logs_to_ack.append((site, host_name, int_filename, file_display)) ack_msg = _('all logfiles of host %s') % host_name elif host_name and file_name: # one log on one host int_filename = form_file_to_int(file_name) logs_to_ack = [(site, host_name, int_filename, form_file_to_ext(int_filename))] ack_msg = _('the log file %s on host %s') % (file_name, host_name) else: for this_site, this_host, logs in all_logs(): file_display = form_file_to_ext(file_name) if file_name in logs: logs_to_ack.append( (this_site, this_host, file_name, file_display)) ack_msg = _('log file %s on all hosts') % file_name ack = html.request.var('_ack') if not html.confirm( _("Do you really want to acknowledge %s by <b>deleting</b> all stored messages?" ) % ack_msg): html.footer() return if not config.user.may("general.act"): html.h1(_('Permission denied'), class_=["error"]) html.div(_('You are not allowed to acknowledge %s') % ack_msg, class_=["error"]) html.footer() return # filter invalid values if ack != '1': raise MKUserError('_ack', _('Invalid value for ack parameter.')) for this_site, this_host, int_filename, display_name in logs_to_ack: try: acknowledge_logfile(this_site, this_host, int_filename, display_name) except Exception as e: html.show_error( _('The log file <tt>%s</tt> of host <tt>%s</tt> could not be deleted: %s.' ) % (display_name, this_host, e)) html.footer() return html.show_message('<b>%s</b><p>%s</p>' % (_('Acknowledged %s') % ack_msg, _('Acknowledged all messages in %s.') % ack_msg)) html.footer()