Module: check_mk
Branch: master
Commit: 86a8123ae52bff6491e18aee3b2d6b7b23e9e7f2
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=86a8123ae52bff…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Wed Sep 18 12:36:08 2013 +0200
Implement DNS caching. This can be disabled with use_dns_cache = False
---
ChangeLog | 1 +
modules/check_mk.py | 48 +++++++++++++++++++++++-----
web/plugins/wato/check_mk_configuration.py | 18 +++++++++++
3 files changed, 59 insertions(+), 8 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 8176726..d8b89fd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -15,6 +15,7 @@
* Preparations for significant SNMP monitoring performance improvement
(It's named Inline SNMP, which is available as special feature via
subscriptions)
* Allow to specify custom host check via WATO (arbitrary command line)
+ * Implement DNS caching. This can be disabled with use_dns_cache = False
Livestatus:
* new service column staleness: indicator for outdated service checks
diff --git a/modules/check_mk.py b/modules/check_mk.py
index 1246046..7267c4e 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -211,6 +211,7 @@ agent_port = 6556
agent_ports = []
snmp_ports = [] # UDP ports used for SNMP
tcp_connect_timeout = 5.0
+use_dns_cache = True # prevent DNS by using own cache file
delay_precompile = False # delay Python compilation to Nagios
execution
restart_locking = "abort" # also possible: "wait",
None
check_submission = "file" # alternative: "pipe"
@@ -1153,25 +1154,38 @@ def lookup_ipaddress(hostname):
if hostname in g_dns_cache:
return g_dns_cache[hostname]
+ # Prepare file based fall-back DNS cache in case resolution fails
init_ip_lookup_cache()
+ cached_ip = g_ip_lookup_cache.get(hostname)
+ if cached_ip and use_dns_cache:
+ g_dns_cache[hostname] = cached_ip
+ return cached_ip
+
# Now do the actual DNS lookup
try:
ipa = socket.gethostbyname(hostname)
- # Cache the result (persistant) when resolving succeeded
- if ipa != g_ip_lookup_cache.get(hostname):
+ # Update our cached address if that has changed or was missing
+ if ipa != cached_ip:
+ if opt_verbose:
+ print "Updating DNS cache for %s: %s" % (hostname, ipa)
g_ip_lookup_cache[hostname] = ipa
write_ip_lookup_cache()
+
+ g_dns_cache[hostname] = ipa # Update in-memory-cache
+ return ipa
+
except:
- # Initialize the lookup cache when called for the first time
- if hostname in g_ip_lookup_cache:
- ipa = g_ip_lookup_cache[hostname]
+ # DNS failed. Use cached IP address if present, even if caching
+ # is disabled.
+ if cached_ip:
+ g_dns_cache[hostname] = cached_ip
+ return cached_ip
else:
g_dns_cache[hostname] = None
raise
- g_dns_cache[hostname] = ipa
- return ipa
+
def init_ip_lookup_cache():
global g_ip_lookup_cache
@@ -1184,6 +1198,20 @@ def init_ip_lookup_cache():
def write_ip_lookup_cache():
file(var_dir + '/ipaddresses.cache',
'w').write(repr(g_ip_lookup_cache))
+def do_update_dns_cache():
+ # Temporarily disable *use* of cache, we want to force an update
+ global use_dns_cache
+ use_dns_cache = False
+
+ if opt_verbose:
+ print "Updating DNS cache..."
+ for hostname in all_active_hosts() + all_active_clusters():
+ # Use intelligent logic. This prevents DNS lookups for hosts
+ # with statically configured addresses, etc.
+ lookup_ipaddress(hostname)
+
+
+
def agent_port_of(hostname):
ports = host_extra_conf(hostname, agent_ports)
if len(ports) == 0:
@@ -4305,6 +4333,7 @@ def usage():
cmk -D, --dump [H1 H2 ..] dump all or some hosts
cmk -d HOSTNAME|IPADDRESS show raw information from agent
cmk --check-inventory HOSTNAME check for items not yet checked
+ cmk --update-dns-cache update IP address lookup cache
cmk --list-hosts [G1 G2 ...] print list of hosts
cmk --list-tag TAG1 TAG2 ... list hosts having certain tags
cmk -L, --list-checks list all available check types
@@ -5270,7 +5299,7 @@ if __name__ == "__main__":
"no-cache", "update", "restart",
"reload", "dump", "fake-dns=",
"man", "nowiki", "config-check",
"backup=", "restore=",
"check-inventory=", "paths",
"cleanup-autochecks", "checks=",
- "cmc-file=", "browse-man", "list-man"
]
+ "cmc-file=", "browse-man", "list-man",
"update-dns-cache" ]
non_config_options = ['-L', '--list-checks', '-P',
'--package', '-M', '--notify',
'--man', '-V', '--version'
,'-h', '--help', '--automation']
@@ -5390,6 +5419,9 @@ if __name__ == "__main__":
elif o == '--donate':
do_donation()
done = True
+ elif o == '--update-dns-cache':
+ do_update_dns_cache()
+ done = True
elif o == '--snmpwalk':
do_snmpwalk(args)
done = True
diff --git a/web/plugins/wato/check_mk_configuration.py
b/web/plugins/wato/check_mk_configuration.py
index 68d5706..ec25642 100644
--- a/web/plugins/wato/check_mk_configuration.py
+++ b/web/plugins/wato/check_mk_configuration.py
@@ -950,6 +950,24 @@ register_configvar(group,
need_restart = True)
register_configvar(group,
+ "use_dns_cache",
+ Checkbox(
+ title = _("Use DNS lookup cache"),
+ label = _("Prevent DNS lookups by use of a cache file"),
+ help = _("When this option is enabled (which is the default), then Check_MK
tries to "
+ "prevent IP address lookups during the configuration generation.
This can speed "
+ "up this process greatly when you have a larger number of hosts.
The cache is stored "
+ "in a simple file. Note: when the cache is enabled then changes of
the IP address "
+ "of a host in your name server will not be detected immediately. If
you need an "
+ "immediate update then simply disable the cache once, activate the
changes and "
+ "enabled it again. OMD based installations automatically update the
cache once "
+ "a day."),
+ default_value = True,
+ ),
+ need_restart = True
+)
+
+register_configvar(group,
"use_inline_snmp",
Checkbox(
title = _("Use Inline SNMP (EXPERIMENTAL)"),