Module: check_mk
Branch: master
Commit: 200f3bc7326f8e33c92546e5a54488da23f34da4
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=200f3bc7326f8e…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Wed Jun 6 09:51:28 2018 +0200
Removed some autochecks-related import cycles.
This brings the number of cycles down to 11 (from 13).
Change-Id: Ia5dd024367363e6cabe2a8d868a6730b65fe642f
---
cmk_base/autochecks.py | 92 ++++++++++++++++++++++++++++++++++++++++
cmk_base/automations/check_mk.py | 3 +-
cmk_base/check_table.py | 7 ++-
cmk_base/discovery.py | 55 ------------------------
4 files changed, 97 insertions(+), 60 deletions(-)
diff --git a/cmk_base/autochecks.py b/cmk_base/autochecks.py
new file mode 100644
index 0000000..1ef81d1
--- /dev/null
+++ b/cmk_base/autochecks.py
@@ -0,0 +1,92 @@
+#!/usr/bin/python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2014 mk(a)mathias-kettner.de |
+# +------------------------------------------------------------------+
+#
+# This file is part of Check_MK.
+# The official homepage is at
http://mathias-kettner.de/check_mk.
+#
+# check_mk is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation in version 2. check_mk is distributed
+# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more de-
+# tails. You should have received a copy of the GNU General Public
+# License along with GNU Make; see the file COPYING. If not, write
+# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301 USA.
+
+import os
+import sys
+
+import cmk.debug
+import cmk.exceptions
+import cmk.paths
+
+import cmk_base.checks
+import cmk_base.config
+import cmk_base.console
+
+# Read automatically discovered checks of one host.
+# world: "config" -> File in var/check_mk/autochecks
+# "active" -> Copy in var/check_mk/core/autochecks
+# Returns a table with three columns:
+# 1. check_plugin_name
+# 2. item
+# 3. parameters evaluated!
+# TODO: use store.load_data_from_file()
+# TODO: Common code with parse_autochecks_file? Cleanup.
+def read_autochecks_of(hostname, world="config"):
+ if world == "config":
+ basedir = cmk.paths.autochecks_dir
+ else:
+ basedir = cmk.paths.var_dir + "/core/autochecks"
+ filepath = basedir + '/' + hostname + '.mk'
+
+ if not os.path.exists(filepath):
+ return []
+
+ check_config = cmk_base.checks.get_check_variables()
+ try:
+ autochecks_raw = eval(file(filepath).read(), check_config, check_config)
+ except SyntaxError,e:
+ cmk_base.console.verbose("Syntax error in file %s: %s\n", filepath, e,
stream=sys.stderr)
+ if cmk.debug.enabled():
+ raise
+ return []
+ except Exception, e:
+ cmk_base.console.verbose("Error in file %s:\n%s\n", filepath, e,
stream=sys.stderr)
+ if cmk.debug.enabled():
+ raise
+ return []
+
+ # Exchange inventorized check parameters with those configured by
+ # the user. Also merge with default levels for modern dictionary based checks.
+ autochecks = []
+ for entry in autochecks_raw:
+ if len(entry) == 4: # old format where hostname is at the first place
+ entry = entry[1:]
+ check_plugin_name, item, parameters = entry
+
+ # With Check_MK 1.2.7i3 items are now defined to be unicode strings. Convert
+ # items from existing autocheck files for compatibility. TODO remove this one
day
+ if type(item) == str:
+ item = cmk_base.config.decode_incoming_string(item)
+
+ if type(check_plugin_name) not in (str, unicode):
+ raise cmk.exceptions.MKGeneralException("Invalid entry '%r' in
check table of host '%s': "
+ "The check type must be a
string." % (entry, hostname))
+
+ autochecks.append((check_plugin_name, item,
+ cmk_base.checks.compute_check_parameters(hostname, check_plugin_name, item,
parameters)))
+ return autochecks
+
+
diff --git a/cmk_base/automations/check_mk.py b/cmk_base/automations/check_mk.py
index 52d2a6a..ce81950 100644
--- a/cmk_base/automations/check_mk.py
+++ b/cmk_base/automations/check_mk.py
@@ -44,6 +44,7 @@ import cmk_base.checks as checks
import cmk_base.discovery as discovery
from cmk_base.automations import automations, Automation, MKAutomationError
import cmk_base.check_utils
+import cmk_base.autochecks
class DiscoveryAutomation(Automation):
@@ -570,7 +571,7 @@ class AutomationAnalyseServices(Automation):
# 2. Load all autochecks of the host in question and try to find
# our service there
- for entry in discovery.read_autochecks_of(hostname):
+ for entry in cmk_base.autochecks.read_autochecks_of(hostname):
ct, item, params = entry # new format without host name
if (ct, item) not in table:
diff --git a/cmk_base/check_table.py b/cmk_base/check_table.py
index 5bac9b5..cca9a11 100644
--- a/cmk_base/check_table.py
+++ b/cmk_base/check_table.py
@@ -36,6 +36,7 @@ import cmk_base.rulesets as rulesets
import cmk_base.piggyback as piggyback
import cmk_base.item_state as item_state
import cmk_base.check_utils
+import cmk_base.autochecks
# TODO: Refactor this to OO. The check table needs to be an object.
@@ -47,8 +48,6 @@ import cmk_base.check_utils
# filter_mode: "include_clustered" -> returns checks of own host, including
clustered checks
def get_check_table(hostname, remove_duplicates=False, use_cache=True,
world='config', skip_autochecks=False, filter_mode=None,
skip_ignored=True):
- import cmk_base.discovery as discovery
-
if config.is_ping_host(hostname):
skip_autochecks = True
@@ -148,7 +147,7 @@ def get_check_table(hostname, remove_duplicates=False,
use_cache=True,
# Now process all entries that are specific to the host
# in search (single host) or that might match the host.
if not skip_autochecks:
- for entry in discovery.read_autochecks_of(hostname, world):
+ for entry in cmk_base.autochecks.read_autochecks_of(hostname, world):
handle_entry(entry)
for entry in single_host_checks.get(hostname, []):
@@ -164,7 +163,7 @@ def get_check_table(hostname, remove_duplicates=False,
use_cache=True,
for node in config.nodes_of(hostname):
node_checks = single_host_checks.get(node, [])
if not skip_autochecks:
- node_checks = node_checks + discovery.read_autochecks_of(node, world)
+ node_checks = node_checks + cmk_base.autochecks.read_autochecks_of(node,
world)
for entry in node_checks:
if len(entry) == 4:
entry = entry[1:] # drop hostname from single_host_checks
diff --git a/cmk_base/discovery.py b/cmk_base/discovery.py
index d07577f..43407ac 100644
--- a/cmk_base/discovery.py
+++ b/cmk_base/discovery.py
@@ -1274,61 +1274,6 @@ def get_check_preview(hostname, use_caches, do_snmp_scan,
on_error):
# | Reading, parsing, writing, modifying autochecks files |
# '----------------------------------------------------------------------'
-# Read automatically discovered checks of one host.
-# world: "config" -> File in var/check_mk/autochecks
-# "active" -> Copy in var/check_mk/core/autochecks
-# Returns a table with three columns:
-# 1. check_plugin_name
-# 2. item
-# 3. parameters evaluated!
-# TODO: use store.load_data_from_file()
-# TODO: Common code with parse_autochecks_file? Cleanup.
-def read_autochecks_of(hostname, world="config"):
- if world == "config":
- basedir = cmk.paths.autochecks_dir
- else:
- basedir = cmk.paths.var_dir + "/core/autochecks"
- filepath = basedir + '/' + hostname + '.mk'
-
- if not os.path.exists(filepath):
- return []
-
- check_config = checks.get_check_variables()
- try:
- autochecks_raw = eval(file(filepath).read(), check_config, check_config)
- except SyntaxError,e:
- console.verbose("Syntax error in file %s: %s\n", filepath, e,
stream=sys.stderr)
- if cmk.debug.enabled():
- raise
- return []
- except Exception, e:
- console.verbose("Error in file %s:\n%s\n", filepath, e,
stream=sys.stderr)
- if cmk.debug.enabled():
- raise
- return []
-
- # Exchange inventorized check parameters with those configured by
- # the user. Also merge with default levels for modern dictionary based checks.
- autochecks = []
- for entry in autochecks_raw:
- if len(entry) == 4: # old format where hostname is at the first place
- entry = entry[1:]
- check_plugin_name, item, parameters = entry
-
- # With Check_MK 1.2.7i3 items are now defined to be unicode strings. Convert
- # items from existing autocheck files for compatibility. TODO remove this one
day
- if type(item) == str:
- item = config.decode_incoming_string(item)
-
- if type(check_plugin_name) not in (str, unicode):
- raise MKGeneralException("Invalid entry '%r' in check table of
host '%s': "
- "The check type must be a string." %
(entry, hostname))
-
- autochecks.append((check_plugin_name, item,
- checks.compute_check_parameters(hostname, check_plugin_name, item,
parameters)))
- return autochecks
-
-
# Read autochecks, but do not compute final check parameters,
# also return a forth column with the raw string of the parameters.
# Returns a table with three columns: