Module: check_mk
Branch: master
Commit: 46338d867135728d2ab07712f38a7ecff895a0bc
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=46338d86713572…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue Mar 13 13:42:07 2018 +0100
Equalize IP lookup behaviour of check/discovery/inventory services
Change-Id: Ie5f9cf0a8a83b84f4000e15df82cec4e8cc5722f
---
cmk_base/checking.py | 11 +++++++++++
cmk_base/core_nagios.py | 2 +-
cmk_base/data_sources/abstract.py | 7 ++++---
cmk_base/discovery.py | 7 ++-----
cmk_base/exceptions.py | 5 +++++
cmk_base/ip_lookup.py | 4 ++--
cmk_base/modes/check_mk.py | 9 +--------
7 files changed, 26 insertions(+), 19 deletions(-)
diff --git a/cmk_base/checking.py b/cmk_base/checking.py
index b218e69..e7fd628 100644
--- a/cmk_base/checking.py
+++ b/cmk_base/checking.py
@@ -42,6 +42,7 @@ import cmk_base.console as console
import cmk_base.config as config
import cmk_base.checks as checks
import cmk_base.snmp as snmp
+import cmk_base.ip_lookup as ip_lookup
import cmk_base.data_sources as data_sources
import cmk_base.item_state as item_state
import cmk_base.core as core
@@ -86,6 +87,12 @@ def do_check(hostname, ipaddress, only_check_plugin_names=None):
state, output, perfdata = 0, [], []
try:
+ # In case of keepalive we always have an ipaddress (can be 0.0.0.0 or :: when
+ # address is unknown). When called as non keepalive ipaddress may be None or
+ # is already an address (2nd argument)
+ if ipaddress is None and not config.is_cluster(hostname):
+ ipaddress = ip_lookup.lookup_ip_address(hostname)
+
item_state.load(hostname)
sources = data_sources.DataSources(hostname, ipaddress)
@@ -114,6 +121,10 @@ def do_check(hostname, ipaddress, only_check_plugin_names=None):
except MKTimeout:
raise
+ except MKIPAddressLookupError, e:
+ output.append("%s" % e)
+ state = max(state, exit_spec.get("connection", 2))
+
except MKGeneralException, e:
if cmk.debug.enabled():
raise
diff --git a/cmk_base/core_nagios.py b/cmk_base/core_nagios.py
index 2e58a0e..e167a64 100644
--- a/cmk_base/core_nagios.py
+++ b/cmk_base/core_nagios.py
@@ -435,7 +435,7 @@ define service {
if host_attrs["address"] in [ "0.0.0.0", "::"
]:
command_name = "check-mk-custom"
- command = command_name + "!echo \"Failed to lookup IP address
and no explicit IP address configured\" && exit 3"
+ command = command_name + "!echo \"CRIT - Failed to lookup IP
address and no explicit IP address configured\" && exit 2"
custom_commands_to_define.add(command_name)
else:
command = "check_mk_active-%s!%s" % (acttype, args)
diff --git a/cmk_base/data_sources/abstract.py b/cmk_base/data_sources/abstract.py
index 1818772..2fa932b 100644
--- a/cmk_base/data_sources/abstract.py
+++ b/cmk_base/data_sources/abstract.py
@@ -45,7 +45,7 @@ import cmk_base.piggyback as piggyback
import cmk_base.checks as checks
import cmk_base.check_api as check_api
from cmk_base.exceptions import MKSkipCheck, MKAgentError, MKEmptyAgentData, MKSNMPError,
\
- MKParseFunctionError, MKTimeout
+ MKParseFunctionError, MKTimeout, MKIPAddressLookupError
from .host_sections import HostSections
@@ -317,10 +317,10 @@ class DataSource(object):
def _verify_ipaddress(self):
if not self._ipaddress:
- raise MKAgentError("Host as no IP address configured.")
+ raise MKIPAddressLookupError("Host as no IP address configured.")
if self._ipaddress in [ "0.0.0.0", "::" ]:
- raise MKAgentError("Failed to lookup IP address and no explicit IP
address configured")
+ raise MKIPAddressLookupError("Failed to lookup IP address and no
explicit IP address configured")
def set_max_cachefile_age(self, max_cachefile_age):
@@ -366,6 +366,7 @@ class DataSource(object):
status = exit_spec.get("empty_output", 2)
elif isinstance(self._exception, MKAgentError) \
+ or isinstance(self._exception, MKIPAddressLookupError) \
or isinstance(self._exception, MKSNMPError):
status = exit_spec.get("connection", 2)
diff --git a/cmk_base/discovery.py b/cmk_base/discovery.py
index 3024bb6..e372d28 100644
--- a/cmk_base/discovery.py
+++ b/cmk_base/discovery.py
@@ -308,11 +308,8 @@ def check_discovery(hostname, ipaddress):
try:
# In case of keepalive discovery we always have an ipaddress. When called as non
keepalive
# ipaddress is always None
- if ipaddress is None:
- if config.is_cluster(hostname):
- ipaddress = None
- else:
- ipaddress = ip_lookup.lookup_ip_address(hostname)
+ if ipaddress is None and not config.is_cluster(hostname):
+ ipaddress = ip_lookup.lookup_ip_address(hostname)
sources = _get_sources_for_discovery(hostname, ipaddress,
check_plugin_names=None,
do_snmp_scan=params["inventory_check_do_scan"],
diff --git a/cmk_base/exceptions.py b/cmk_base/exceptions.py
index 10d387f..1456bd5 100644
--- a/cmk_base/exceptions.py
+++ b/cmk_base/exceptions.py
@@ -27,6 +27,8 @@
# TODO: Inherit from cmk.MKGeneralException?
# TODO: Cleanup self.reason to use .args of standard exceptions
+from cmk.exceptions import MKGeneralException as _MKGeneralException
+
class MKAgentError(Exception):
def __init__(self, reason):
super(MKAgentError, self).__init__(reason)
@@ -37,6 +39,9 @@ class MKAgentError(Exception):
return self.reason
+class MKIPAddressLookupError(_MKGeneralException):
+ pass
+
class MKEmptyAgentData(MKAgentError):
pass
diff --git a/cmk_base/ip_lookup.py b/cmk_base/ip_lookup.py
index f43a4fa..d061ed0 100644
--- a/cmk_base/ip_lookup.py
+++ b/cmk_base/ip_lookup.py
@@ -33,7 +33,7 @@ import cmk_base
import cmk_base.console as console
import cmk_base.config as config
import cmk_base.rulesets as rulesets
-from cmk.exceptions import MKGeneralException
+from cmk_base.exceptions import MKIPAddressLookupError
_fake_dns = False
_enforce_localhost = False
@@ -138,7 +138,7 @@ def cached_dns_lookup(hostname, family):
return cached_ip
else:
cache[cache_id] = None
- raise MKGeneralException(
+ raise MKIPAddressLookupError(
"Failed to lookup IPv%d address of %s via DNS: %s" %
(family, hostname, e))
diff --git a/cmk_base/modes/check_mk.py b/cmk_base/modes/check_mk.py
index fe53266..14717be 100644
--- a/cmk_base/modes/check_mk.py
+++ b/cmk_base/modes/check_mk.py
@@ -1524,14 +1524,7 @@ def mode_check(options, args):
if len(args) == 2:
ipaddress = args[1]
else:
- if config.is_cluster(hostname):
- ipaddress = None
- else:
- try:
- ipaddress = ip_lookup.lookup_ip_address(hostname)
- except:
- console.error("Cannot resolve hostname '%s'.\n" %
hostname)
- return 2
+ ipaddress = None
return checking.do_check(hostname, ipaddress, options.get("checks"))