Module: check_mk
Branch: master
Commit: 377c9b3573ffcc524bb62a2fc1ff91d1d8246b83
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=377c9b3573ffcc…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Wed Sep 18 13:46:39 2013 +0200
Move some code from check_mk_base.py to check_mk.py
This makes the precompiled host checks smaller
---
modules/check_mk.py | 118 ++++++++++++++++++++++++++++++++++++++++++++++
modules/check_mk_base.py | 118 ----------------------------------------------
2 files changed, 118 insertions(+), 118 deletions(-)
diff --git a/modules/check_mk.py b/modules/check_mk.py
index d911cd9..83d4571 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -4957,6 +4957,124 @@ def ip_to_dnsname(ip):
return None
+# Reset some global variable to their original value. This
+# is needed in keepalive mode.
+# We could in fact do some positive caching in keepalive
+# mode - e.g. the counters of the hosts could be saved in memory.
+def cleanup_globals():
+ global g_agent_already_contacted
+ g_agent_already_contacted = {}
+ global g_hostname
+ g_hostname = "unknown"
+ global g_counters
+ g_counters = {}
+ global g_infocache
+ g_infocache = {}
+ global g_broken_agent_hosts
+ g_broken_agent_hosts = set([])
+ global g_broken_snmp_hosts
+ g_broken_snmp_hosts = set([])
+ global g_inactive_timerperiods
+ g_inactive_timerperiods = None
+ global g_walk_cache
+ g_walk_cache = {}
+
+
+# Diagnostic function for detecting global variables that have
+# changed during checking. This is slow and canno be used
+# in production mode.
+def copy_globals():
+ import copy
+ global_saved = {}
+ for varname, value in globals().items():
+ # Some global caches are allowed to change.
+ if varname not in [ "g_service_description",
"g_multihost_checks", "g_check_table_cache",
"g_singlehost_checks", "total_check_outout" ] \
+ and type(value).__name__ not in [ "function", "module",
"SRE_Pattern" ]:
+ global_saved[varname] = copy.copy(value)
+ return global_saved
+
+
+def do_check_keepalive():
+ global g_initial_times
+
+ def check_timeout(signum, frame):
+ raise MKCheckTimeout()
+
+ signal.signal(signal.SIGALRM, signal.SIG_IGN) # Prevent ALRM from CheckHelper.cc
+
+ global total_check_output
+ total_check_output = ""
+ if opt_debug:
+ before = copy_globals()
+
+ ipaddress_cache = {}
+
+ while True:
+ cleanup_globals()
+ hostname = sys.stdin.readline()
+ g_initial_times = os.times()
+ if not hostname:
+ break
+ hostname = hostname.strip()
+ if hostname == "*":
+ if opt_debug:
+ sys.stdout.write("Restarting myself...\n")
+ sys.stdout.flush()
+ os.execvp("cmk", sys.argv)
+ elif not hostname:
+ break
+
+ timeout = int(sys.stdin.readline())
+ try: # catch non-timeout exceptions
+ try: # catch timeouts
+ signal.signal(signal.SIGALRM, check_timeout)
+ signal.alarm(timeout)
+ if ';' in hostname:
+ hostname, ipaddress = hostname.split(";", 1)
+ elif hostname in ipaddress_cache:
+ ipaddress = ipaddress_cache[hostname]
+ else:
+ if is_cluster(hostname):
+ ipaddress = None
+ else:
+ try:
+ ipaddress = lookup_ipaddress(hostname)
+ except:
+ raise MKGeneralException("Cannot resolve hostname %s
into IP address" % hostname)
+ ipaddress_cache[hostname] = ipaddress
+
+ status = do_check(hostname, ipaddress)
+ signal.signal(signal.SIGALRM, signal.SIG_IGN) # Prevent ALRM from
CheckHelper.cc
+ signal.alarm(0)
+ except MKCheckTimeout:
+ signal.signal(signal.SIGALRM, signal.SIG_IGN) # Prevent ALRM from
CheckHelper.cc
+ status = 3
+ total_check_output = "UNKNOWN - Check_MK timed out after %d
seconds\n" % timeout
+
+ sys.stdout.write("%03d\n%08d\n%s" %
+ (status, len(total_check_output), total_check_output))
+ sys.stdout.flush()
+ total_check_output = ""
+ cleanup_globals()
+
+ # Check if all global variables are clean, but only in debug mode
+ if opt_debug:
+ after = copy_globals()
+ for varname, value in before.items():
+ if value != after[varname]:
+ sys.stderr.write("WARNING: global variable %s has changed:
%r ==> %s\n"
+ % (varname, value, repr(after[varname])[:50]))
+ new_vars = set(after.keys()).difference(set(before.keys()))
+ if (new_vars):
+ sys.stderr.write("WARNING: new variable appeared: %s" %
", ".join(new_vars))
+
+ except Exception, e:
+ if opt_debug:
+ raise
+ sys.stdout.write("UNKNOWN - %s\n3\n" % e)
+
+
+
# +----------------------------------------------------------------------+
# | ____ _ __ _ |
diff --git a/modules/check_mk_base.py b/modules/check_mk_base.py
index f715f2c..6e54c8d 100644
--- a/modules/check_mk_base.py
+++ b/modules/check_mk_base.py
@@ -949,128 +949,10 @@ def do_check(hostname, ipaddress, only_check_types = None):
sys.stdout.write(nagios_state_names[status] + " - " + output)
sys.exit(status)
-
-# Reset some global variable to their original value. This
-# is needed in keepalive mode.
-# We could in fact do some positive caching in keepalive
-# mode - e.g. the counters of the hosts could be saved in memory.
-def cleanup_globals():
- global g_agent_already_contacted
- g_agent_already_contacted = {}
- global g_hostname
- g_hostname = "unknown"
- global g_counters
- g_counters = {}
- global g_infocache
- g_infocache = {}
- global g_broken_agent_hosts
- g_broken_agent_hosts = set([])
- global g_broken_snmp_hosts
- g_broken_snmp_hosts = set([])
- global g_inactive_timerperiods
- g_inactive_timerperiods = None
- global g_walk_cache
- g_walk_cache = {}
-
-
-# Diagnostic function for detecting global variables that have
-# changed during checking. This is slow and canno be used
-# in production mode.
-def copy_globals():
- import copy
- global_saved = {}
- for varname, value in globals().items():
- # Some global caches are allowed to change.
- if varname not in [ "g_service_description",
"g_multihost_checks", "g_check_table_cache",
"g_singlehost_checks", "total_check_outout" ] \
- and type(value).__name__ not in [ "function", "module",
"SRE_Pattern" ]:
- global_saved[varname] = copy.copy(value)
- return global_saved
-
-
# Keepalive-mode for running cmk as a check helper.
class MKCheckTimeout(Exception):
pass
-def do_check_keepalive():
- global g_initial_times
-
- def check_timeout(signum, frame):
- raise MKCheckTimeout()
-
- signal.signal(signal.SIGALRM, signal.SIG_IGN) # Prevent ALRM from CheckHelper.cc
-
- global total_check_output
- total_check_output = ""
- if opt_debug:
- before = copy_globals()
-
- ipaddress_cache = {}
-
- while True:
- cleanup_globals()
- hostname = sys.stdin.readline()
- g_initial_times = os.times()
- if not hostname:
- break
- hostname = hostname.strip()
- if hostname == "*":
- if opt_debug:
- sys.stdout.write("Restarting myself...\n")
- sys.stdout.flush()
- os.execvp("cmk", sys.argv)
- elif not hostname:
- break
-
- timeout = int(sys.stdin.readline())
- try: # catch non-timeout exceptions
- try: # catch timeouts
- signal.signal(signal.SIGALRM, check_timeout)
- signal.alarm(timeout)
- if ';' in hostname:
- hostname, ipaddress = hostname.split(";", 1)
- elif hostname in ipaddress_cache:
- ipaddress = ipaddress_cache[hostname]
- else:
- if is_cluster(hostname):
- ipaddress = None
- else:
- try:
- ipaddress = lookup_ipaddress(hostname)
- except:
- raise MKGeneralException("Cannot resolve hostname %s
into IP address" % hostname)
- ipaddress_cache[hostname] = ipaddress
-
- status = do_check(hostname, ipaddress)
- signal.signal(signal.SIGALRM, signal.SIG_IGN) # Prevent ALRM from
CheckHelper.cc
- signal.alarm(0)
- except MKCheckTimeout:
- signal.signal(signal.SIGALRM, signal.SIG_IGN) # Prevent ALRM from
CheckHelper.cc
- status = 3
- total_check_output = "UNKNOWN - Check_MK timed out after %d
seconds\n" % timeout
-
- sys.stdout.write("%03d\n%08d\n%s" %
- (status, len(total_check_output), total_check_output))
- sys.stdout.flush()
- total_check_output = ""
- cleanup_globals()
-
- # Check if all global variables are clean, but only in debug mode
- if opt_debug:
- after = copy_globals()
- for varname, value in before.items():
- if value != after[varname]:
- sys.stderr.write("WARNING: global variable %s has changed:
%r ==> %s\n"
- % (varname, value, repr(after[varname])[:50]))
- new_vars = set(after.keys()).difference(set(before.keys()))
- if (new_vars):
- sys.stderr.write("WARNING: new variable appeared: %s" %
", ".join(new_vars))
-
- except Exception, e:
- if opt_debug:
- raise
- sys.stdout.write("UNKNOWN - %s\n3\n" % e)
-
-
def check_unimplemented(checkname, params, info):
return (3, 'UNKNOWN - Check not implemented')