Module: check_mk
Branch: master
Commit: 4592633f14377f18af97c0fecd16d1b61f82f2ea
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=4592633f14377f…
Author: Andreas Boesl <ab(a)mathias-kettner.de>
Date: Tue Jul 12 14:40:51 2016 +0200
refactored some BI code before introducing a new aggregation option
---
web/htdocs/bi.py | 71 ++++++++++++++++++++++++++++--------------------
web/plugins/wato/bi.py | 56 ++++++++++++++++++++++----------------
2 files changed, 75 insertions(+), 52 deletions(-)
diff --git a/web/htdocs/bi.py b/web/htdocs/bi.py
index 0f85da5..612abc0 100644
--- a/web/htdocs/bi.py
+++ b/web/htdocs/bi.py
@@ -179,17 +179,14 @@ def aggregation_groups():
migrate_bi_configuration() # convert bi_packs into legacy variables
# on demand: show all configured groups
group_names = set([])
- for entry in config.aggregations + config.host_aggregations:
- if entry[0] == config.DISABLED:
+ for aggr_def in config.aggregations + config.host_aggregations:
+ if aggr_def[0].get("disabled"):
continue
- if entry[0] == config.HARD_STATES:
- entry = entry[1:]
-
- if type(entry[0]) == list:
- group_names.update(entry[0])
+ if type(aggr_def[1]) == list:
+ group_names.update(aggr_def[1])
else:
- group_names.add(entry[0])
+ group_names.add(aggr_def[1])
group_names = list(group_names)
@@ -314,30 +311,23 @@ def compile_forest(user, only_hosts = None, only_groups = None):
single_affected_hosts = []
for aggr_type, aggregations in aggr_list:
- for entry in aggregations:
- if entry[0] == config.DISABLED:
- continue
+ for aggr_def in aggregations:
+ options = aggr_def[0]
- if entry[0] == config.DT_AGGR_WARN:
- downtime_aggr_warn = True
- entry = entry[1:]
- else:
- downtime_aggr_warn = False
+ if options.get("disabled"):
+ continue
- if entry[0] == config.HARD_STATES:
- use_hard_states = True
- entry = entry[1:]
- else:
- use_hard_states = False
+ downtime_aggr_warn = options.get("downtime_aggr_warn", False)
+ use_hard_states = options.get("use_hard_states", False)
- if len(entry) < 3:
+ if len(aggr_def) < 3:
raise MKConfigError(_("<h1>Invalid aggregation
<tt>%s</tt></h1>"
- "Must have at least 3 entries (has %d)")
% (entry, len(entry)))
+ "Must have at least 3 entries (has %d)")
% (aggr_def, len(aggr_def)))
- if type(entry[0]) == list:
- groups = entry[0]
+ if type(aggr_def[1]) == list:
+ groups = aggr_def[1]
else:
- groups = [ entry[0] ]
+ groups = [ aggr_def[1] ]
groups_set = set(groups)
if only_groups and not groups_set.intersection(only_groups):
@@ -348,7 +338,7 @@ def compile_forest(user, only_hosts = None, only_groups = None):
log('Skip aggr (All groups have already been compiled')
continue # skip if all groups have already been compiled
- new_entries = compile_rule_node(aggr_type, entry[1:], 0)
+ new_entries = compile_rule_node(aggr_type, aggr_def[2:], 0)
for this_entry in new_entries:
remove_empty_nodes(this_entry)
@@ -2117,9 +2107,32 @@ def get_state_name(node):
def migrate_bi_configuration():
+ def convert_aggregation(aggr_tuple):
+ if type(aggr_tuple[0]) == dict:
+ return aggr_tuple # already converted
+
+ options = {}
+ map_class_to_key = {
+ config.DISABLED: "disabled",
+ config.HARD_STATES: "hard_states",
+ config.DT_AGGR_WARN: "downtime_aggr_warn",
+ }
+ for idx, token in enumerate(list(aggr_tuple)):
+ if token in map_class_to_key:
+ options[map_class_to_key[token]] = True
+ else:
+ aggr_tuple = aggr_tuple[idx:]
+ break
+ return (options,) + aggr_tuple
+
if config.bi_packs:
for pack in config.bi_packs.values():
- config.aggregations += pack["aggregations"]
- config.host_aggregations += pack["host_aggregations"]
+ config.aggregations += map(convert_aggregation,
pack["aggregations"])
+ config.host_aggregations += map(convert_aggregation,
pack["host_aggregations"])
config.aggregation_rules.update(pack["rules"])
config.bi_packs = {}
+ else:
+ if config.host_aggregations and type(config.host_aggregations[0]) != dict:
+ config.host_aggregations = map(convert_aggregation, config.host_aggregation)
+ if config.aggregations and type(config.aggregations[0]) != dict:
+ config.aggregations = map(convert_aggregation, config.aggregation)
diff --git a/web/plugins/wato/bi.py b/web/plugins/wato/bi.py
index 7dcbf70..2d98aa6 100644
--- a/web/plugins/wato/bi.py
+++ b/web/plugins/wato/bi.py
@@ -205,12 +205,15 @@ class ModeBI(WatoMode):
conv = (aggr["groups"],)
node = self._convert_node_to_bi(aggr["node"])
convaggr = conv + node
- if aggr["hard_states"]:
- convaggr = (self._bi_constants["HARD_STATES"],) + convaggr
- if aggr["downtime_aggr_warn"]:
- convaggr = (self._bi_constants["DT_AGGR_WARN"],) + convaggr
- if aggr["disabled"]:
- convaggr = (self._bi_constants["DISABLED"],) + convaggr
+
+ # Create dict with all aggregation options
+ options = {}
+ for option in ["hard_states",
+ "downtime_aggr_warn",
+ "disabled"]:
+ options[option] = aggr.get(option, False)
+
+ convaggr = (options,) + convaggr
return convaggr
@@ -252,37 +255,44 @@ class ModeBI(WatoMode):
def _convert_aggregation_from_bi(self, aggr, single_host):
- if aggr[0] == self._bi_constants["DISABLED"]:
- disabled = True
+ if type(aggr[0]) == dict:
+ options = aggr[0]
aggr = aggr[1:]
else:
- disabled = False
+ # Legacy configuration
+ options = {}
+ if aggr[0] == self._bi_constants["DISABLED"]:
+ options["disabled"] = True
+ aggr = aggr[1:]
+ else:
+ options["disabled"] = False
- if aggr[0] == self._bi_constants["DT_AGGR_WARN"]:
- downtime_aggr_warn = True
- aggr = aggr[1:]
- else:
- downtime_aggr_warn = False
+ if aggr[0] == self._bi_constants["DT_AGGR_WARN"]:
+ options["downtime_aggr_warn"] = True
+ aggr = aggr[1:]
+ else:
+ options["downtime_aggr_warn"] = False
+
+ if aggr[0] == self._bi_constants["HARD_STATES"]:
+ options["hard_states"] = True
+ aggr = aggr[1:]
+ else:
+ options["hard_states"] = False
- if aggr[0] == self._bi_constants["HARD_STATES"]:
- hard_states = True
- aggr = aggr[1:]
- else:
- hard_states = False
if type(aggr[0]) != list:
groups = [aggr[0]]
else:
groups = aggr[0]
+
node = self._convert_node_from_bi(aggr[1:])
- return {
- "disabled" : disabled,
- "hard_states" : hard_states,
- "downtime_aggr_warn" : downtime_aggr_warn,
+ aggr_dict = {
"groups" : groups,
"node" : node,
"single_host" : single_host,
}
+ aggr_dict.update(options)
+ return aggr_dict
# Make some conversions so that the format of the
# valuespecs is matched