Module: check_mk
Branch: master
Commit: ba4877b1b4447e7dca5c3665d2554a501b5cb030
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=ba4877b1b4447e…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Mon Sep 10 09:54:09 2018 +0200
6554 FIX Raw Edition: Improved handling of configuration errors
While creating a configuration for the Nagios core (e.g. using "cmk -U" or
during "omd update")
exceptions may be raised which are caused by configuration issues.
When using the Nagios core Check_MK does not produce a half written object file anymore.
In case
of such an error Check_MK throws away everything and keeps the old file.
You can then start the site with the old configuration and fix the configuration issue
while the
monitoring is running with the old config.
Change-Id: Ie46ea15fec29875e7d949ba1f56b16dc4801ba71
---
.werks/6554 | 19 +++++++++++++++++++
cmk_base/core_nagios.py | 33 +++++++++++++++++++++++++++++++--
omd/packages/omd/CORE.hook | 2 +-
3 files changed, 51 insertions(+), 3 deletions(-)
diff --git a/.werks/6554 b/.werks/6554
new file mode 100644
index 0000000..d772d59
--- /dev/null
+++ b/.werks/6554
@@ -0,0 +1,19 @@
+Title: Raw Edition: Improved handling of configuration errors
+Level: 2
+Component: core
+Class: fix
+Compatible: compat
+Edition: cre
+State: unknown
+Version: 1.6.0i1
+Date: 1536324895
+
+
+While creating a configuration for the Nagios core (e.g. using "cmk -U" or
during "omd update")
+exceptions may be raised which are caused by configuration issues.
+
+When using the Nagios core Check_MK does not produce a half written object file anymore.
In case
+of such an error Check_MK throws away everything and keeps the old file.
+
+You can then start the site with the old configuration and fix the configuration issue
while the
+monitoring is running with the old config.
diff --git a/cmk_base/core_nagios.py b/cmk_base/core_nagios.py
index 5bc77c9..13fa92d 100644
--- a/cmk_base/core_nagios.py
+++ b/cmk_base/core_nagios.py
@@ -30,6 +30,8 @@ import os
import subprocess
import sys
import py_compile
+import tempfile
+import errno
import cmk.paths
import cmk.tty as tty
@@ -47,8 +49,34 @@ import cmk_base.check_api_utils as check_api_utils
class NagiosCore(core_config.MonitoringCore):
def create_config(self):
- with file(cmk.paths.nagios_objects_file, "w") as out:
- create_config(out, None)
+ """Tries to create a new Check_MK object configuration file for
the Nagios core
+
+ During create_config() exceptions may be raised which are caused by configuration
issues.
+ Don't produce a half written object file. Simply throw away everything and
keep the old file.
+
+ The user can then start the site with the old configuration and fix the
configuration issue
+ while the monitoring is running.
+ """
+ tmp_path = None
+ try:
+ with tempfile.NamedTemporaryFile("w",
dir=os.path.dirname(cmk.paths.nagios_objects_file),
+ prefix=".%s.new" %
os.path.basename(cmk.paths.nagios_objects_file),
+ delete=False) as tmp:
+ tmp_path = tmp.name
+ os.chmod(tmp.name, 0660)
+ create_config(tmp, hostnames=None)
+ os.rename(tmp.name, cmk.paths.nagios_objects_file)
+
+ except Exception, e:
+ # In case an exception happens cleanup the tempfile created for writing
+ try:
+ if tmp_path:
+ os.unlink(tmp_path)
+ except IOError, e:
+ if e.errno == errno.ENOENT: # No such file or directory
+ pass
+
+ raise
def precompile(self):
console.output("Precompiling host checks...")
@@ -56,6 +84,7 @@ class NagiosCore(core_config.MonitoringCore):
console.output(tty.ok + "\n")
+
# .--Create config-------------------------------------------------------.
# | ____ _ __ _ |
# | / ___|_ __ ___ __ _| |_ ___ ___ ___ _ __ / _(_) __ _ |
diff --git a/omd/packages/omd/CORE.hook b/omd/packages/omd/CORE.hook
index cbe1345..9b488d9 100755
--- a/omd/packages/omd/CORE.hook
+++ b/omd/packages/omd/CORE.hook
@@ -67,7 +67,7 @@ case "$1" in
make_link nagios $OMD_ROOT/etc/init.d/core
# Refresh Check_MK Configuration after changing back from CMC
if [ -e $OMD_ROOT/var/check_mk/core/config ] ; then
- cmk -U >/dev/null 2>&1
+ cmk -U >/dev/null
fi
elif [ "$2" == "icinga" ]
then