Module: check_mk
Branch: master
Commit: 0b451e1b641c488f6ee046ff0b2cb00dacf87c0b
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=0b451e1b641c48…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Sun Aug 7 18:44:58 2011 +0200
Prevent precompile wherever possible
Skip precompilation if source code (.py) is unchanged.
Delay preompilation if delay_precompile is True.
---
ChangeLog | 4 ++++
modules/check_mk.py | 41 +++++++++++++++++++++++++++++++++++++----
2 files changed, 41 insertions(+), 4 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index d8afd80..b54b9ea 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,10 @@
* FIX: sort output of cmk --list-hosts alphabetically
* Great speed up of cmk -N/-C/-U/-R, especially when number of hosts is
large.
+ * new main.mk option delay_precompile: if True, check_mk will skip Python
+ precompilation during cmk -C or cmk -R, but will do this the first
+ time the host is checked. This speeds up restarts. Default is False.
+ Nagios user needs write access in precompiled directory!
Checks & Agents:
* FIX: apc_symmetra: fix remaining runtime calculation (by factor 100)
diff --git a/modules/check_mk.py b/modules/check_mk.py
index 48fbcba..906afd6 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -212,6 +212,7 @@ NEGATE = '@negate' # negation in boolean lists
agent_port = 6556
tcp_connect_timeout = 5.0
do_rrd_update = False
+delay_precompile = False # delay Python compilation to Nagios
execution
check_submission = "pipe" # alternative: "file"
aggr_summary_hostname = "%s-s"
agent_min_version = 0 # warn, if plugin has not at least version
@@ -2079,9 +2080,24 @@ def precompile_hostcheck(hostname):
compiled_filename = precompiled_hostchecks_dir + "/" + hostname
source_filename = compiled_filename + ".py"
- output = file(source_filename, "w")
+
+ output = file(source_filename + ".new", "w")
output.write("#!/usr/bin/python\n")
output.write("# encoding: utf-8\n")
+
+ # Self-compile: replace symlink with precompiled python-code, if
+ # we are run for the first time
+ if delay_precompile:
+ output.write("""
+import os
+if os.path.islink(%(dst)r):
+ import py_compile
+ os.remove(%(dst)r)
+ py_compile.compile(%(src)r, %(dst)r, %(dst)r, True)
+ os.chmod(%(dst)r, 0755)
+
+""" % { "src" : source_filename, "dst" :
compiled_filename })
+
output.write(stripped_python_file(modules_dir + "/check_mk_base.py"))
# initialize global variables
@@ -2236,9 +2252,26 @@ no_inventory_possible = None
output.write("do_check(%r, %r)\n" % (hostname, ipaddress))
output.close()
- # compile python
- py_compile.compile(source_filename, compiled_filename, compiled_filename, True)
- os.chmod(compiled_filename, 0755)
+ # compile python (either now or delayed), but only if the source
+ # code has not changed. The Python compilation is the most costly
+ # operation here.
+ if os.path.exists(source_filename):
+ if file(source_filename).read() == file(source_filename +
".new").read():
+ if opt_verbose:
+ sys.stderr.write(" (%s is unchanged)\n" % source_filename)
+ os.remove(source_filename + ".new")
+ return
+ elif opt_verbose:
+ sys.stderr.write(" (new content)")
+
+ os.rename(source_filename + ".new", source_filename)
+ if not delay_precompile:
+ py_compile.compile(source_filename, compiled_filename, compiled_filename, True)
+ os.chmod(compiled_filename, 0755)
+ else:
+ if os.path.exists(compiled_filename) or os.path.islink(compiled_filename):
+ os.remove(compiled_filename)
+ os.symlink(hostname + ".py", compiled_filename)
if opt_verbose:
sys.stderr.write(" ==> %s.\n" % compiled_filename)