Example #1
0
    def action(self) -> ActionResult:
        folder = watolib.Folder.current()
        if not html.check_transaction():
            return redirect(mode_url("folder", folder=folder.path()))

        if html.request.var("_update_dns_cache") and self._should_use_dns_cache():
            config.user.need_permission("wato.update_dns_cache")
            num_updated, failed_hosts = watolib.check_mk_automation(self._host.site_id(),
                                                                    "update-dns-cache", [])
            infotext = _("Successfully updated IP addresses of %d hosts.") % num_updated
            if failed_hosts:
                infotext += "<br><br><b>Hostnames failed to lookup:</b> " \
                          + ", ".join(["<tt>%s</tt>" % h for h in failed_hosts])
            flash(infotext)
            return None

        if html.request.var("delete"):  # Delete this host
            folder.delete_hosts([self._host.name()])
            return redirect(mode_url("folder", folder=folder.path()))

        attributes = watolib.collect_attributes("host" if not self._is_cluster() else "cluster",
                                                new=False)
        watolib.Host.host(self._host.name()).edit(attributes, self._get_cluster_nodes())
        self._host = folder.host(self._host.name())

        if html.request.var("services"):
            return redirect(mode_url("inventory", folder=folder.path(), host=self._host.name()))
        if html.request.var("diag_host"):
            return redirect(
                mode_url("diag_host",
                         folder=folder.path(),
                         host=self._host.name(),
                         _start_on_load="1"))
        return redirect(mode_url("folder", folder=folder.path()))
Example #2
0
    def action(self):
        if html.request.var("_update_dns_cache"):
            if not html.check_transaction():
                return None
            config.user.need_permission("wato.update_dns_cache")
            num_updated, failed_hosts = watolib.check_mk_automation(
                self._host.site_id(), "update-dns-cache", [])
            infotext = _(
                "Successfully updated IP addresses of %d hosts.") % num_updated
            if failed_hosts:
                infotext += "<br><br><b>Hostnames failed to lookup:</b> " \
                          + ", ".join(["<tt>%s</tt>" % h for h in failed_hosts])
            return None, infotext

        if html.request.var("delete"):  # Delete this host
            if not html.transaction_valid():
                return "folder"
            return delete_host_after_confirm(self._host.name())

        if html.check_transaction():
            attributes = watolib.collect_attributes(
                "host" if not self._is_cluster() else "cluster", new=False)
            watolib.Host.host(self._host.name()).edit(
                attributes, self._get_cluster_nodes())
            self._host = watolib.Folder.current().host(self._host.name())

        if html.request.var("services"):
            return "inventory"
        if html.request.var("diag_host"):
            html.request.set_var("_start_on_load", "1")
            return "diag_host"
        return "folder"
Example #3
0
 def _get_cluster_ds_types(self) -> Tuple[str, str]:
     folder_attributes = watolib.Folder.current().attributes()
     attributes = watolib.collect_attributes("cluster", new=False)
     return (
         attributes.get("tag_agent", folder_attributes.get('tag_agent', "cmk-agent")),
         attributes.get("tag_snmp_ds", folder_attributes.get('tag_snmp_ds', "no-snmp")),
     )
Example #4
0
    def action(self):
        if not html.transaction_valid():
            return "folder"

        attributes = watolib.collect_attributes(self._host_type_name(), new=True)
        cluster_nodes = self._get_cluster_nodes()

        hostname = html.request.get_ascii_input_mandatory("host")
        Hostname().validate_value(hostname, "host")

        if html.check_transaction():
            watolib.Folder.current().create_hosts([(hostname, attributes, cluster_nodes)])

        self._host = watolib.Folder.current().host(hostname)

        inventory_url = watolib.folder_preserving_link([
            ("mode", "inventory"),
            ("host", self._host.name()),
            ("_scan", "1"),
        ])

        create_msg = None if self._host.is_ping_host() else (
            _('Successfully created the host. Now you should do a '
              '<a href="%s">service discovery</a> in order to auto-configure '
              'all services to be checked on this host.') % inventory_url)

        if html.request.var("services"):
            raise HTTPRedirect(inventory_url)

        if html.request.var("diag_host"):
            html.request.set_var("_try", "1")
            return "diag_host", create_msg

        return "folder", create_msg
Example #5
0
    def action(self) -> ActionResult:
        if not transactions.transaction_valid():
            return redirect(mode_url("folder"))

        attributes = watolib.collect_attributes(self._host_type_name(), new=True)
        cluster_nodes = self._get_cluster_nodes()

        hostname = request.get_ascii_input_mandatory("host")
        Hostname().validate_value(hostname, "host")

        folder = watolib.Folder.current()

        if transactions.check_transaction():
            folder.create_hosts([(hostname, attributes, cluster_nodes)])

        self._host = folder.load_host(hostname)

        inventory_url = watolib.folder_preserving_link(
            [
                ("mode", "inventory"),
                ("host", self._host.name()),
                ("_scan", "1"),
            ]
        )

        create_msg = (
            None
            if self._host.is_ping_host()
            else (
                _(
                    "Successfully created the host. Now you should do a "
                    '<a href="%s">service discovery</a> in order to auto-configure '
                    "all services to be checked on this host."
                )
                % inventory_url
            )
        )

        if request.var("_save"):
            return redirect(inventory_url)

        if create_msg:
            flash(create_msg)

        if request.var("diag_host"):
            return redirect(
                mode_url("diag_host", folder=folder.path(), host=self._host.name(), _try="1")
            )

        return redirect(mode_url("folder", folder=folder.path()))
Example #6
0
    def action(self):
        if not html.check_transaction():
            return "folder"

        # Title
        title = TextUnicode().from_html_vars("title")
        TextUnicode(allow_empty=False).validate_value(title, "title")

        attributes = watolib.collect_attributes("folder", new=self._folder.name() is None)
        self._save(title, attributes)

        # Edit icon on subfolder preview should bring user back to parent folder
        if html.request.has_var("backfolder"):
            watolib.Folder.set_current(watolib.Folder.folder(html.request.var("backfolder")))
        return "folder"
Example #7
0
    def action(self):
        if not html.check_transaction():
            return

        config.user.need_permission("wato.edit_hosts")

        changed_attributes = watolib.collect_attributes("bulk", new=False)
        host_names = get_hostnames_from_checkboxes()
        for host_name in host_names:
            host = watolib.Folder.current().host(host_name)
            host.update_attributes(changed_attributes)
            # call_hook_hosts_changed() is called too often.
            # Either offer API in class Host for bulk change or
            # delay saving until end somehow

        return "folder", _("Edited %d hosts") % len(host_names)
Example #8
0
    def _get_cluster_nodes(self):
        if not self._is_cluster():
            return None

        cluster_nodes = self._vs_cluster_nodes().from_html_vars("nodes")
        self._vs_cluster_nodes().validate_value(cluster_nodes, "nodes")
        if len(cluster_nodes) < 1:
            raise MKUserError("nodes_0",
                              _("The cluster must have at least one node"))
        for nr, cluster_node in enumerate(cluster_nodes):
            if cluster_node == self._host.name():
                raise MKUserError(
                    "nodes_%d" % nr,
                    _("The cluster can not be a node of it's own"))

            if not watolib.Host.host_exists(cluster_node):
                raise MKUserError(
                    "nodes_%d" % nr,
                    _("The node <b>%s</b> does not exist "
                      " (must be a host that is configured with WATO)") %
                    cluster_node)

            attributes = watolib.collect_attributes("cluster", new=False)
            cluster_agent_ds_type = attributes.get("tag_agent", "cmk-agent")
            cluster_snmp_ds_type = attributes.get("tag_snmp_ds", "no-snmp")

            node_agent_ds_type = watolib.hosts_and_folders.Host.host(
                cluster_node).tag_groups().get("agent")
            node_snmp_ds_type = watolib.hosts_and_folders.Host.host(
                cluster_node).tag_groups().get("snmp_ds")

            if node_agent_ds_type != cluster_agent_ds_type or \
                    node_snmp_ds_type != cluster_snmp_ds_type:
                raise MKUserError(
                    "nodes_%d" % nr,
                    _("Cluster and nodes must have the same "
                      "datasource! The node <b>%s</b> has datasources "
                      "<b>%s</b> and <b>%s</b> while the cluster has datasources "
                      "<b>%s</b> and <b>%s</b>.") % (
                          cluster_node,
                          node_agent_ds_type,
                          node_snmp_ds_type,
                          cluster_agent_ds_type,
                          cluster_snmp_ds_type,
                      ))

        return cluster_nodes
Example #9
0
    def action(self) -> ActionResult:
        if not transactions.check_transaction():
            return None

        user.need_permission("wato.edit_hosts")

        changed_attributes = watolib.collect_attributes("bulk", new=False)
        host_names = get_hostnames_from_checkboxes()
        for host_name in host_names:
            host = watolib.Folder.current().load_host(host_name)
            host.update_attributes(changed_attributes)
            # call_hook_hosts_changed() is called too often.
            # Either offer API in class Host for bulk change or
            # delay saving until end somehow

        flash(_("Edited %d hosts") % len(host_names))
        return redirect(watolib.Folder.current().url())
Example #10
0
    def action(self) -> ActionResult:
        folder = watolib.Folder.current()
        if not transactions.check_transaction():
            return redirect(mode_url("folder", folder=folder.path()))

        if request.var("_update_dns_cache") and self._should_use_dns_cache():
            user.need_permission("wato.update_dns_cache")
            update_dns_cache_result = update_dns_cache(self._host.site_id())
            infotext = (_("Successfully updated IP addresses of %d hosts.") %
                        update_dns_cache_result.n_updated)
            if update_dns_cache_result.failed_hosts:
                infotext += "<br><br><b>Hostnames failed to lookup:</b> " + ", ".join(
                    [
                        "<tt>%s</tt>" % h
                        for h in update_dns_cache_result.failed_hosts
                    ])
            flash(infotext)
            return None

        if request.var("delete"):  # Delete this host
            folder.delete_hosts([self._host.name()])
            return redirect(mode_url("folder", folder=folder.path()))

        attributes = watolib.collect_attributes(
            "host" if not self._is_cluster() else "cluster", new=False)
        host = watolib.Host.host(self._host.name())
        if host is None:
            flash(f"Host {self._host.name()} could not be found.")
            return None

        host.edit(attributes, self._get_cluster_nodes())
        self._host = folder.load_host(self._host.name())

        if request.var("_save"):
            return redirect(
                mode_url("inventory",
                         folder=folder.path(),
                         host=self._host.name()))
        if request.var("diag_host"):
            return redirect(
                mode_url("diag_host",
                         folder=folder.path(),
                         host=self._host.name(),
                         _start_on_load="1"))
        return redirect(mode_url("folder", folder=folder.path()))
Example #11
0
    def _get_cluster_nodes(self):
        if not self._is_cluster():
            return None

        cluster_nodes = self._vs_cluster_nodes().from_html_vars("nodes")
        self._vs_cluster_nodes().validate_value(cluster_nodes, "nodes")
        if len(cluster_nodes) < 1:
            raise MKUserError("nodes_0", _("The cluster must have at least one node"))

        # Fake a cluster host in order to get calculated tag groups via effective attributes...
        cluster_computed_datasources = cmk.utils.tags.compute_datasources(
            watolib.Host(
                watolib.Folder.current(),
                self._host.name(),
                watolib.collect_attributes("cluster", new=False),
                [],
            ).tag_groups()
        )

        for nr, cluster_node in enumerate(cluster_nodes):
            if cluster_node == self._host.name():
                raise MKUserError("nodes_%d" % nr, _("The cluster can not be a node of it's own"))

            if not watolib.Host.host_exists(cluster_node):
                raise MKUserError(
                    "nodes_%d" % nr,
                    _(
                        "The node <b>%s</b> does not exist "
                        " (must be a host that is configured with WATO)"
                    )
                    % cluster_node,
                )

            node_computed_datasources = cmk.utils.tags.compute_datasources(
                watolib.Host.load_host(cluster_node).tag_groups()
            )

            if datasource_differences := cluster_computed_datasources.get_differences_to(
                node_computed_datasources
            ):
                raise MKUserError(
                    "nodes_%d" % nr,
                    _("Cluster and nodes must have the same datasource. ")
                    + self._format_datasource_differences(cluster_node, datasource_differences),
                )
Example #12
0
    def action(self) -> ActionResult:
        if html.request.has_var("backfolder"):
            # Edit icon on subfolder preview should bring user back to parent folder
            folder = watolib.Folder.folder(html.request.var("backfolder"))
        else:
            folder = watolib.Folder.current()

        if not html.check_transaction():
            return redirect(mode_url("folder", folder=folder.path()))

        # Title
        title = TextUnicode().from_html_vars("title")
        TextUnicode(allow_empty=False).validate_value(title, "title")

        attributes = watolib.collect_attributes("folder", new=self._folder.name() is None)
        self._save(title, attributes)

        return redirect(mode_url("folder", folder=folder.path()))