Module: check_mk
Branch: master
Commit: 2b6bb3eb1f4d7a6110c81185bc52282f9315fd24
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=2b6bb3eb1f4d7a…
Author: Kenneth Okoh <ko(a)mathias-kettner.de>
Date: Thu Nov 22 09:15:39 2018 +0100
Refactored hosts to new snapin API
Change-Id: Id753b61c57a9dc0af829e9745af444d3eb264949
---
cmk/gui/plugins/sidebar/hosts.py | 203 +++++++++++++++++++++++----------------
1 file changed, 118 insertions(+), 85 deletions(-)
diff --git a/cmk/gui/plugins/sidebar/hosts.py b/cmk/gui/plugins/sidebar/hosts.py
index c40c586..e5c9cc8 100644
--- a/cmk/gui/plugins/sidebar/hosts.py
+++ b/cmk/gui/plugins/sidebar/hosts.py
@@ -24,99 +24,132 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
+import abc
import cmk.gui.config as config
import cmk.gui.views as views
import cmk.gui.sites as sites
from cmk.gui.i18n import _
from cmk.gui.globals import html
-from cmk.gui.plugins.sidebar import sidebar_snapins, link
-
-
-def render_hosts(mode):
- sites.live().set_prepend_site(True)
- query = "GET hosts\nColumns: name state worst_service_state\nLimit: 100\n"
- view = "host"
-
- if mode == "problems":
- view = "problemsofhost"
- # Exclude hosts and services in downtime
- svc_query = "GET services\nColumns: host_name\n"\
- "Filter: state > 0\nFilter: scheduled_downtime_depth =
0\n"\
- "Filter: host_scheduled_downtime_depth = 0\nAnd: 3"
- problem_hosts = {x[1] for x in sites.live().query(svc_query)}
-
- query += "Filter: state > 0\nFilter: scheduled_downtime_depth = 0\nAnd:
2\n"
- for host in problem_hosts:
- query += "Filter: name = %s\n" % host
- query += "Or: %d\n" % (len(problem_hosts) + 1)
-
- hosts = sites.live().query(query)
- sites.live().set_prepend_site(False)
- hosts.sort()
-
- longestname = 0
- for site, host, state, worstsvc in hosts:
- longestname = max(longestname, len(host))
- if longestname > 15:
- num_columns = 1
- else:
- num_columns = 2
-
- views.load_views()
- target = views.get_context_link(config.user.id, view)
- html.open_table(class_="allhosts")
- col = 1
- for site, host, state, worstsvc in hosts:
- if col == 1:
- html.open_tr()
- html.open_td()
- html.open_td()
-
- if state > 0 or worstsvc == 2:
- statecolor = 2
- elif worstsvc == 1:
- statecolor = 1
- elif worstsvc == 3:
- statecolor = 3
- else:
- statecolor = 0
- html.open_div(class_=["statebullet", "state%d" %
statecolor])
- html.nbsp()
- html.close_div()
- link(host, target + "&host=%s&site=%s" % (html.urlencode(host),
html.urlencode(site)))
- html.close_td()
- if col == num_columns:
- html.close_tr()
- col = 1
+from cmk.gui.plugins.sidebar import (
+ link,
+ SidebarSnapin,
+ snapin_registry,
+)
+
+
+class HostSnapin(SidebarSnapin):
+ __metaclass__ = abc.ABCMeta
+
+ @abc.abstractmethod
+ def _host_mode_ident(self):
+ raise NotImplementedError()
+
+ def show(self):
+ mode = self._host_mode_ident()
+ sites.live().set_prepend_site(True)
+ query = "GET hosts\nColumns: name state worst_service_state\nLimit:
100\n"
+ view = "host"
+
+ if mode == "problems":
+ view = "problemsofhost"
+ # Exclude hosts and services in downtime
+ svc_query = "GET services\nColumns: host_name\n"\
+ "Filter: state > 0\nFilter: scheduled_downtime_depth =
0\n"\
+ "Filter: host_scheduled_downtime_depth = 0\nAnd: 3"
+ problem_hosts = {x[1] for x in sites.live().query(svc_query)}
+
+ query += "Filter: state > 0\nFilter: scheduled_downtime_depth =
0\nAnd: 2\n"
+ for host in problem_hosts:
+ query += "Filter: name = %s\n" % host
+ query += "Or: %d\n" % (len(problem_hosts) + 1)
+
+ hosts = sites.live().query(query)
+ sites.live().set_prepend_site(False)
+ hosts.sort()
+
+ longestname = 0
+ for site, host, state, worstsvc in hosts:
+ longestname = max(longestname, len(host))
+ if longestname > 15:
+ num_columns = 1
else:
- col += 1
+ num_columns = 2
+
+ views.load_views()
+ target = views.get_context_link(config.user.id, view)
+ html.open_table(class_="allhosts")
+ col = 1
+ for site, host, state, worstsvc in hosts:
+ if col == 1:
+ html.open_tr()
+ html.open_td()
- if col < num_columns:
- html.close_tr()
- html.close_table()
+ if state > 0 or worstsvc == 2:
+ statecolor = 2
+ elif worstsvc == 1:
+ statecolor = 1
+ elif worstsvc == 3:
+ statecolor = 3
+ else:
+ statecolor = 0
+ html.open_div(class_=["statebullet", "state%d" %
statecolor])
+ html.nbsp()
+ html.close_div()
+ link(host, target + "&host=%s&site=%s" %
(html.urlencode(host), html.urlencode(site)))
+ html.close_td()
+ if col == num_columns:
+ html.close_tr()
+ col = 1
+ else:
+ col += 1
+ if col < num_columns:
+ html.close_tr()
+ html.close_table()
+
+ @classmethod
+ def refresh_on_restart(cls):
+ return True
-snapin_allhosts_styles = """
- .snapin table.allhosts { width: 100%; }
- .snapin table.allhosts td { width: 50%; padding: 0px 0px; }
+ def styles(self):
+ return """
+.snapin table.allhosts { width: 100%; }
+.snapin table.allhosts td { width: 50%; padding: 0px 0px; }
"""
-sidebar_snapins["hosts"] = {
- "title": _("All Hosts"),
- "description": _("A summary state of each host with a link to the view
"
- "showing its services"),
- "render": lambda: render_hosts("hosts"),
- "allowed": ["user", "admin", "guest"],
- "refresh": True,
- "styles": snapin_allhosts_styles,
-}
-
-sidebar_snapins["problem_hosts"] = {
- "title": _("Problem Hosts"),
- "description": _("A summary state of all hosts that have a problem,
with "
- "links to problems of those hosts"),
- "render": lambda: render_hosts("problems"),
- "allowed": ["user", "admin", "guest"],
- "refresh": True,
- "styles": snapin_allhosts_styles,
-}
+
+(a)snapin_registry.register
+class Hosts(HostSnapin):
+ def _host_mode_ident(self):
+ return "hosts"
+
+ @staticmethod
+ def type_name():
+ return "hosts"
+
+ @classmethod
+ def title(cls):
+ return _("All Hosts")
+
+ @classmethod
+ def description(cls):
+ return _("A summary state of each host with a link to the view showing its
services")
+
+
+(a)snapin_registry.register
+class ProblemHosts(HostSnapin):
+ def _host_mode_ident(self):
+ return "problems"
+
+ @staticmethod
+ def type_name():
+ return "problem_hosts"
+
+ @classmethod
+ def title(cls):
+ return _("Problem Hosts")
+
+ @classmethod
+ def description(cls):
+ return _("A summary state of all hosts that have a problem, with "
+ "links to problems of those hosts")