Module: check_mk
Branch: master
Commit: c0d1918591d8f2cac90e06ca25ec25217be52fdb
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=c0d1918591d8f2…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue Feb 26 15:52:40 2019 +0100
7186 Add painters & filters for host & service custom attributes
In addition to the already existing "Custom variables" painters that render a
table of custom variables, painters and filters have been added to work with
single custom attributes.
The naming has been cleaned up: All elements now use the term "Custom
attributes"
instead of "Custom variables".
CMK-1727
Change-Id: If1d8e4881a198769d9a63f733adeaf5d2865673f
---
.werks/7186 | 15 +++++
cmk/gui/plugins/views/painters.py | 110 +++++++++++++++++++++++++++++++++++--
cmk/gui/plugins/visuals/filters.py | 79 ++++++++++++++++++++++++++
3 files changed, 200 insertions(+), 4 deletions(-)
diff --git a/.werks/7186 b/.werks/7186
new file mode 100644
index 0000000..18d8a50
--- /dev/null
+++ b/.werks/7186
@@ -0,0 +1,15 @@
+Title: Add painters & filters for host & service custom attributes
+Level: 1
+Component: multisite
+Compatible: compat
+Edition: cre
+Version: 1.6.0i1
+Date: 1551192605
+Class: feature
+
+In addition to the already existing "Custom variables" painters that render a
+table of custom variables, painters and filters have been added to work with
+single custom attributes.
+
+The naming has been cleaned up: All elements now use the term "Custom
attributes"
+instead of "Custom variables".
diff --git a/cmk/gui/plugins/views/painters.py b/cmk/gui/plugins/views/painters.py
index 55e7440..c692a55 100644
--- a/cmk/gui/plugins/views/painters.py
+++ b/cmk/gui/plugins/views/painters.py
@@ -24,6 +24,7 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
+import abc
import os
import time
@@ -1750,14 +1751,14 @@ def paint_custom_vars(what, row, blacklist=None):
@painter_registry.register
-class PainterSvcCustomVars(Painter):
+class PainterServiceCustomVariables(Painter):
@property
def ident(self):
return "svc_custom_vars"
@property
def title(self):
- return _("Service custom variables")
+ return _("Service custom attributes")
@property
def columns(self):
@@ -1770,6 +1771,107 @@ class PainterSvcCustomVars(Painter):
return paint_custom_vars('service', row)
+class ABCPainterCustomVariable(Painter):
+ __metaclass__ = abc.ABCMeta
+
+ @property
+ def title(self):
+ return self._dynamic_title
+
+ @property
+ def short_title(self):
+ return self._dynamic_title
+
+ def _dynamic_title(self, params=None):
+ if params is None:
+ # Happens in view editor when adding a painter
+ return self._default_title
+
+ try:
+ return
config.custom_service_attributes[params["ident"]]["title"]
+ except KeyError:
+ return self._default_title
+
+ @abc.abstractproperty
+ def _default_title(self):
+ raise NotImplementedError()
+
+ @abc.abstractproperty
+ def _object_type(self):
+ raise NotImplementedError()
+
+ @abc.abstractmethod
+ def _custom_attribute_choices(self):
+ raise NotImplementedError()
+
+ @property
+ def parameters(self):
+ return Dictionary(
+ elements=[
+ ("ident", DropdownChoice(
+ choices=self._custom_attribute_choices,
+ title=_("ID"),
+ )),
+ ],
+ title=_("Options"),
+ optional_keys=[],
+ )
+
+ def render(self, row, cell):
+ params = cell.painter_parameters()
+ return paint_custom_var(self._object_type, params["ident"].upper(),
row)
+
+
+(a)painter_registry.register
+class PainterServiceCustomVariable(ABCPainterCustomVariable):
+ @property
+ def ident(self):
+ return "service_custom_variable"
+
+ @property
+ def columns(self):
+ return ['service_custom_variable_names',
'service_custom_variable_values']
+
+ @property
+ def _default_title(self):
+ return _("Service custom attribute")
+
+ @property
+ def _object_type(self):
+ return "service"
+
+ def _custom_attribute_choices(self):
+ choices = []
+ for ident, attr_spec in config.custom_service_attributes.items():
+ choices.append((ident, attr_spec["title"]))
+ return sorted(choices, key=lambda x: x[1])
+
+
+(a)painter_registry.register
+class PainterHostCustomVariable(ABCPainterCustomVariable):
+ @property
+ def ident(self):
+ return "host_custom_variable"
+
+ @property
+ def columns(self):
+ return ['host_custom_variable_names',
'host_custom_variable_values']
+
+ @property
+ def _default_title(self):
+ return _("Host custom attribute")
+
+ @property
+ def _object_type(self):
+ return "host"
+
+ def _custom_attribute_choices(self):
+ choices = []
+ for attr_spec in config.wato_host_attrs:
+ choices.append((attr_spec["name"], attr_spec["title"]))
+ return sorted(choices, key=lambda x: x[1])
+
+
#.
# .--Hosts---------------------------------------------------------------.
# | _ _ _ |
@@ -3288,14 +3390,14 @@ class PainterHostServicelevel(Painter):
@painter_registry.register
-class PainterHostCustomVars(Painter):
+class PainterHostCustomVariables(Painter):
@property
def ident(self):
return "host_custom_vars"
@property
def title(self):
- return _("Host custom variables")
+ return _("Host custom attributes")
@property
def columns(self):
diff --git a/cmk/gui/plugins/visuals/filters.py b/cmk/gui/plugins/visuals/filters.py
index 34c6439..bdb5eb5 100644
--- a/cmk/gui/plugins/visuals/filters.py
+++ b/cmk/gui/plugins/visuals/filters.py
@@ -24,6 +24,7 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
+import abc
import re
import json
@@ -2580,6 +2581,84 @@ class FilterHostAuxTags(Filter):
return True
+class ABCFilterCustomAttribute(Filter):
+ __metaclass__ = abc.ABCMeta
+
+ @property
+ def sort_index(self):
+ return 103
+
+ def __init__(self, info):
+ Filter.__init__(
+ self, info=info, htmlvars=[self.name_varname, self.value_varname],
link_columns=[])
+
+ @property
+ def name_varname(self):
+ return "%s_name" % self.ident
+
+ @property
+ def value_varname(self):
+ return "%s_value" % self.ident
+
+ def display(self):
+ html.dropdown(self.name_varname, [("", "")] +
self._custom_attribute_choices())
+ html.text_input(self.value_varname)
+
+ @abc.abstractmethod
+ def _custom_attribute_choices(self):
+ raise NotImplementedError()
+
+ def filter(self, infoname):
+ if not html.get_ascii_input(self.name_varname):
+ return ""
+
+ attribute_id = html.get_item_input(self.name_varname,
+ dict(self._custom_attribute_choices()))[1]
+ value = html.get_unicode_input(self.value_varname)
+ return "Filter: %s_custom_variables ~~ %s ^%s\n" % (
+ self.info, livestatus.lqencode(attribute_id.upper()),
livestatus.lqencode(value))
+
+
+(a)filter_registry.register
+class FilterCustomServiceAttribute(ABCFilterCustomAttribute):
+ @property
+ def ident(self):
+ return "service_custom_variable"
+
+ @property
+ def title(self):
+ return _("Service custom attribute")
+
+ def __init__(self):
+ ABCFilterCustomAttribute.__init__(self, info="service")
+
+ def _custom_attribute_choices(self):
+ choices = []
+ for ident, attr_spec in config.custom_service_attributes.items():
+ choices.append((ident, attr_spec["title"]))
+ return sorted(choices, key=lambda x: x[1])
+
+
+(a)filter_registry.register
+class FilterCustomHostAttribute(ABCFilterCustomAttribute):
+ @property
+ def ident(self):
+ return "host_custom_variable"
+
+ @property
+ def title(self):
+ return _("Host custom attribute")
+
+ def __init__(self):
+ ABCFilterCustomAttribute.__init__(self, info="host")
+
+ def _custom_attribute_choices(self):
+ choices = []
+ for attr_spec in config.wato_host_attrs:
+ choices.append((attr_spec["name"], attr_spec["title"]))
+ return sorted(choices, key=lambda x: x[1])
+
+
# choices = [ (value, "readable"), .. ]
class FilterECServiceLevelRange(Filter):
def __init__(self, info):