def action(self) -> ActionResult:
        if request.var("_action") != "discard":
            return None

        if not transactions.check_transaction():
            return None

        if not self._may_discard_changes():
            return None

        if not self.has_changes():
            return None

        # Now remove all currently pending changes by simply restoring the last automatically
        # taken snapshot. Then activate the configuration. This should revert all pending changes.
        file_to_restore = self._get_last_wato_snapshot_file()

        if not file_to_restore:
            raise MKUserError(None,
                              _("There is no WATO snapshot to be restored."))

        msg = _("Discarded pending changes (Restored %s)") % file_to_restore

        # All sites and domains can be affected by a restore: Better restart everything.
        _changes.add_change(
            "changes-discarded",
            msg,
            domains=ABCConfigDomain.enabled_domains(),
            need_restart=True,
        )

        self._extract_snapshot(file_to_restore)
        activate_changes.execute_activate_changes([
            d.get_domain_request([])
            for d in ABCConfigDomain.enabled_domains()
        ])

        for site_id in activation_sites():
            self.confirm_site_changes(site_id)

        build_index_background()

        make_header(
            html,
            self.title(),
            breadcrumb=self.breadcrumb(),
            show_body_start=display_options.enabled(display_options.H),
            show_top_heading=display_options.enabled(display_options.T),
        )
        html.open_div(class_="wato")

        html.show_message(_("Successfully discarded all pending changes."))
        html.javascript("hide_changes_buttons();")
        html.footer()
        return FinalizeRequest(code=200)
Exemple #2
0
def save_global_settings(vars_, site_specific=False, custom_site_path=None):
    per_domain: Dict[str, Dict[Any, Any]] = {}
    # TODO: Uee _get_global_config_var_names() from domain class?
    for config_variable_class in config_variable_registry.values():
        config_variable = config_variable_class()
        domain = config_variable.domain()
        varname = config_variable.ident()
        if varname not in vars_:
            continue
        per_domain.setdefault(domain().ident(), {})[varname] = vars_[varname]

    # Some settings are handed over from the central site but are not registered in the
    # configuration domains since the user must not change it directly.
    for varname in [
            "wato_enabled",
            "userdb_automatic_sync",
            "user_login",
    ]:
        if varname in vars_:
            per_domain.setdefault(ConfigDomainGUI.ident(),
                                  {})[varname] = vars_[varname]

    for domain in ABCConfigDomain.enabled_domains():
        domain_config = per_domain.get(domain().ident(), {})
        if site_specific:
            domain().save_site_globals(domain_config,
                                       custom_site_path=custom_site_path)
        else:
            domain().save(domain_config, custom_site_path=custom_site_path)
Exemple #3
0
    def page_menu(self, breadcrumb: Breadcrumb) -> PageMenu:
        menu = make_simple_form_page_menu(_("Setting"),
                                          breadcrumb,
                                          form_name="value_editor",
                                          button_name="_save")

        reset_possible = self._config_variable.allow_reset(
        ) and self._is_configured()
        default_values = ABCConfigDomain.get_all_default_globals()
        defvalue = default_values[self._varname]
        value = self._current_settings.get(
            self._varname, self._global_settings.get(self._varname, defvalue))
        menu.dropdowns[0].topics[0].entries.append(
            PageMenuEntry(
                title=_("Remove explicit setting")
                if value == defvalue else _("Reset to default"),
                icon_name="reset",
                item=make_confirmed_form_submit_link(
                    form_name="value_editor",
                    button_name="_reset",
                    message=_(
                        "Do you really want to reset this configuration variable "
                        "back to its default value?"),
                ),
                is_enabled=reset_possible,
                is_shortcut=True,
                is_suggested=True,
            ))

        return menu
Exemple #4
0
def save_global_settings(vars_, site_specific=False, custom_site_path=None):
    per_domain = {}  # type: Dict[str, Dict[Any, Any]]
    # TODO: Uee _get_global_config_var_names() from domain class?
    for config_variable_class in config_variable_registry.values():
        config_variable = config_variable_class()
        domain = config_variable.domain()
        varname = config_variable.ident()
        if varname not in vars_:
            continue
        per_domain.setdefault(domain.ident, {})[varname] = vars_[varname]

    # The global setting wato_enabled is not registered in the configuration domains
    # since the user must not change it directly. It is set by D-WATO on slave sites.
    if "wato_enabled" in vars_:
        per_domain.setdefault(ConfigDomainGUI.ident,
                              {})["wato_enabled"] = vars_["wato_enabled"]
    if "userdb_automatic_sync" in vars_:
        per_domain.setdefault(
            ConfigDomainGUI.ident,
            {})["userdb_automatic_sync"] = vars_["userdb_automatic_sync"]

    for domain in ABCConfigDomain.enabled_domains():
        domain_config = per_domain.get(domain.ident, {})
        if site_specific:
            domain().save_site_globals(domain_config,
                                       custom_site_path=custom_site_path)
        else:
            domain().save(domain_config, custom_site_path=custom_site_path)
Exemple #5
0
def load_configuration_settings(site_specific=False):
    settings = {}
    for domain in ABCConfigDomain.enabled_domains():
        if site_specific:
            settings.update(domain().load_site_globals())
        else:
            settings.update(domain().load())
    return settings
    def __init__(self):
        self._search = None
        self._show_only_modified = False

        super(GlobalSettingsMode, self).__init__()

        self._default_values = ABCConfigDomain.get_all_default_globals()
        self._global_settings = {}
        self._current_settings = {}
Exemple #7
0
    def __init__(self) -> None:
        self._search = None
        self._show_only_modified = False

        super().__init__()

        self._default_values = ABCConfigDomain.get_all_default_globals()
        self._global_settings: dict[str, Any] = {}
        self._current_settings: dict[str, Any] = {}
Exemple #8
0
    def page(self) -> None:
        is_configured = self._is_configured()
        is_configured_globally = self._varname in self._global_settings

        default_values = ABCConfigDomain.get_all_default_globals()

        defvalue = default_values[self._varname]
        value = self._current_settings.get(
            self._varname, self._global_settings.get(self._varname, defvalue))

        hint = self._config_variable.hint()
        if hint:
            html.show_warning(hint)

        html.begin_form("value_editor", method="POST")
        title = self._valuespec.title()
        assert isinstance(title, str)
        forms.header(title)
        if not active_config.wato_hide_varnames:
            forms.section(_("Configuration variable:"))
            html.tt(self._varname)

        forms.section(_("Current setting"))
        self._valuespec.render_input("ve", value)
        self._valuespec.set_focus("ve")
        html.help(self._valuespec.help())

        if is_configured_globally:
            self._show_global_setting()

        forms.section(_("Factory setting"))
        html.write_text(self._valuespec.value_to_html(defvalue))

        forms.section(_("Current state"))
        if is_configured_globally:
            html.write_text(
                _('This variable is configured in <a href="%s">global settings</a>.'
                  ) %
                ("wato.py?mode=edit_configvar&varname=%s" % self._varname))
        elif not is_configured:
            html.write_text(_("This variable is at factory settings."))
        else:
            curvalue = self._current_settings[self._varname]
            if is_configured_globally and curvalue == self._global_settings[
                    self._varname]:
                html.write_text(
                    _("Site setting and global setting are identical."))
            elif curvalue == defvalue:
                html.write_text(
                    _("Your setting and factory settings are identical."))
            else:
                html.write_text(self._valuespec.value_to_html(curvalue))

        forms.end()
        html.hidden_fields()
        html.end_form()
Exemple #9
0
def load_configuration_settings(site_specific=False, custom_site_path=None, full_config=False):
    settings = {}
    for domain in ABCConfigDomain.enabled_domains():
        if full_config:
            settings.update(domain().load_full_config())
        elif site_specific:
            settings.update(domain().load_site_globals(custom_site_path=custom_site_path))
        else:
            settings.update(domain().load())
    return settings
Exemple #10
0
    def _get_effective_global_setting(self, varname: str) -> Any:
        global_settings = load_configuration_settings()
        default_values = ABCConfigDomain.get_all_default_globals()

        if cmk.gui.config.is_wato_slave_site():
            current_settings = load_configuration_settings(site_specific=True)
        else:
            sites = SiteManagementFactory.factory().load_sites()
            current_settings = sites[config.omd_site()].get("globals", {})

        if varname in current_settings:
            return current_settings[varname]
        if varname in global_settings:
            return global_settings[varname]
        return default_values[varname]
Exemple #11
0
def get_effective_global_setting(site_id: SiteId, is_wato_slave_site: bool, varname: str) -> Any:
    global_settings = load_configuration_settings()
    default_values = ABCConfigDomain.get_all_default_globals()

    if is_wato_slave_site:
        current_settings = load_configuration_settings(site_specific=True)
    else:
        sites = SiteManagementFactory.factory().load_sites()
        current_settings = sites.get(site_id, {}).get("globals", {})

    if varname in current_settings:
        return current_settings[varname]

    if varname in global_settings:
        return global_settings[varname]

    return default_values[varname]
Exemple #12
0
    def execute(self) -> Iterator[ACResult]:
        local_connection = LocalConnection()
        row = local_connection.query_row(
            "GET status\nColumns: helper_usage_checker average_latency_fetcher\n"
        )

        checker_usage_perc = 100 * row[0]
        fetcher_latency = row[1]

        usage_warn, usage_crit = 85, 95
        if checker_usage_perc >= usage_crit:
            cls: Type[ACResult] = ACResultCRIT
        elif checker_usage_perc >= usage_warn:
            cls = ACResultWARN
        else:
            cls = ACResultOK

        yield cls(
            _(
                "The current checker usage is %.2f%%. "
                "The checks have an average check latency of %.3fs."
            )
            % (checker_usage_perc, fetcher_latency)
        )

        # Only report this as warning in case the user increased the default helper configuration
        default_values = ABCConfigDomain.get_all_default_globals()
        if (
            self._get_effective_global_setting("cmc_checker_helpers")
            > default_values["cmc_checker_helpers"]
            and checker_usage_perc < 50
        ):
            yield ACResultWARN(
                _(
                    "The checker usage is below 50%, you may decrease the number of "
                    "checkers to reduce the memory consumption."
                )
            )