Module: check_mk
Branch: master
Commit: a6d1ecab3f3f08d650ea0d7d5d20303ccbebf10d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=a6d1ecab3f3f08…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Mon Nov 29 17:12:09 2010 +0100
wato: prepare setup
---
modules/check_mk.py | 53 ++++++++++++++++++++++++++++++++++++++++++++------
scripts/setup.sh | 6 +++-
web/htdocs/wato.py | 27 ++++++++++++++-----------
3 files changed, 65 insertions(+), 21 deletions(-)
diff --git a/modules/check_mk.py b/modules/check_mk.py
index f966e52..531d388 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -3094,7 +3094,7 @@ def do_update():
def do_check_nagiosconfig():
command = nagios_binary + " -v " + nagios_config_file + "
2>&1"
- sys.stderr.write("Validating Nagios configuration...")
+ sys.stdout.write("Validating Nagios configuration...")
if opt_verbose:
sys.stderr.write("Running '%s'" % command)
sys.stderr.flush()
@@ -3103,26 +3103,26 @@ def do_check_nagiosconfig():
output = process.read()
exit_status = process.close()
if not exit_status:
- sys.stderr.write(tty_ok + "\n")
+ sys.stdout.write(tty_ok + "\n")
return True
else:
- sys.stderr.write("ERROR:\n")
+ sys.stdout.write("ERROR:\n")
sys.stderr.write(output)
return False
def do_restart_nagios(only_reload):
action = only_reload and "load" or "start"
- sys.stderr.write("Re%sing Nagios..." % action)
- sys.stderr.flush()
+ sys.stdout.write("Re%sing Nagios..." % action)
+ sys.stdout.flush()
command = nagios_startscript + " re%s 2>&1" % action
process = os.popen(command, "r")
output = process.read()
if process.close():
- sys.stderr.write("ERROR:\n")
+ sys.stdout.write("ERROR:\n")
raise MKGeneralException("Cannot re%s Nagios" % action)
else:
- sys.stderr.write(tty_ok + "\n")
+ sys.stdout.write(tty_ok + "\n")
def do_reload():
do_restart(True)
@@ -3624,6 +3624,43 @@ def automation_delete_host(args):
"%s/%s.*" % (tcp_cache_dir, hostname)]:
os.system("rm -rf '%s'" % path)
+def automation_restart():
+ old_stdout = sys.stdout
+ sys.stdout = file('/dev/null', 'w')
+ try:
+ if os.path.exists(nagios_objects_file):
+ backup_path = nagios_objects_file + ".save"
+ os.rename(nagios_objects_file, backup_path)
+ else:
+ backup_path = None
+
+ try:
+ create_nagios_config(file(nagios_objects_file, "w"))
+ except Exception, e:
+ if backup_path:
+ os.rename(backup_path, nagios_objects_file)
+ raise MKAutomationError("Error creating configuration: %s" % e)
+
+ if do_check_nagiosconfig():
+ if backup_path:
+ os.remove(backup_path)
+ do_precompile_hostchecks()
+ do_restart_nagios(False)
+ else:
+ if backup_path:
+ os.rename(backup_path, nagios_objects_file)
+ else:
+ os.remove(nagios_objects_file)
+ raise MKAutomationError("Nagios configuration is invalid. Rolling
back.")
+
+ except Exception, e:
+ if backup_path and os.path.exists(backup_path):
+ os.remove(backup_path)
+ raise MKAutomationError(str(e))
+
+ sys.stdout = old_stdout
+
+
def do_automation(cmd, args):
try:
if cmd == "try-inventory":
@@ -3634,6 +3671,8 @@ def do_automation(cmd, args):
result = automation_set_autochecks(args)
elif cmd == "delete-host":
result = automation_delete_host(args)
+ elif cmd == "restart":
+ result = automation_restart()
else:
raise MKAutomationError("Automation command '%s' is not
implemented." % cmd)
diff --git a/scripts/setup.sh b/scripts/setup.sh
index db48008..e43740e 100755
--- a/scripts/setup.sh
+++ b/scripts/setup.sh
@@ -586,14 +586,16 @@ do
sed -ri 's@^export MK_LIBDIR="(.*)"@export
MK_LIBDIR="'"$agentslibdir"'"@' $agent
sed -ri 's@^export MK_CONFDIR="(.*)"@export
MK_CONFDIR="'"$agentsconfdir"'"@' $agent
done &&
- mkdir -p $DESTDIR$vardir/{autochecks,counters,precompiled,cache,logwatch,web}
&&
+ mkdir -p $DESTDIR$vardir/{autochecks,counters,precompiled,cache,logwatch,web,wato}
&&
if [ -z "$DESTDIR" ] && id "$nagiosuser" > /dev/null
2>&1 && [ $UID = 0 ] ; then
chown -R $nagiosuser $DESTDIR$vardir/{counters,cache,logwatch}
chown $nagiosuser $DESTDIR$vardir/web
fi &&
if [ -z "$DESTDIR" ] ; then
chgrp -R $wwwgroup $DESTDIR$vardir/web &&
- chmod -R g+w $DESTDIR$vardir/web
+ chmod -R g+w $DESTDIR$vardir/web &&
+ chgrp -R $wwwgroup $DESTDIR$vardir/wato &&
+ chmod -R g+w $DESTDIR$vardir/wato
fi &&
mkdir -p $DESTDIR$confdir/conf.d &&
tar xzf $SRCDIR/conf.tar.gz -C $DESTDIR$confdir &&
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 99314aa..dab6df6 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -232,15 +232,13 @@ def mode_changelog(phase):
elif phase == "action":
if html.check_transaction():
- f = os.popen("check_mk -R 2>&1 >/dev/null")
- errors = f.read()
- exitcode = f.close()
- if exitcode:
- raise MKUserError(None, errors)
- else:
- log_commit_pending() # flush logfile with pending actions
- log_audit(None, "activate-config", "Configuration
activated, monitoring server restarted")
- return None, "The new configuration has been successfully
activated."
+ try:
+ check_mk_automation("restart")
+ except Exception, e:
+ raise MKUserError(None, str(e))
+ log_commit_pending() # flush logfile with pending actions
+ log_audit(None, "activate-config", "Configuration activated,
monitoring server restarted")
+ return None, "The new configuration has been successfully activated."
else:
@@ -485,15 +483,20 @@ def parse_audit_log(what):
return []
-def check_mk_automation(command, args, indata=""):
- p = subprocess.Popen(["check_mk", "--automation", command ] +
args,
+def check_mk_automation(command, args=[], indata=""):
+ commandargs = defaults.check_mk_automation.split()
+ try:
+ p = subprocess.Popen(commandargs + [ command ] + args,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+ except Exception, e:
+ raise MKGeneralException("Cannot execute <tt>%s</tt>: %s" %
(commandargs[0], e))
p.stdin.write(repr(indata))
p.stdin.close()
outdata = p.stdout.read()
exitcode = p.wait()
if exitcode != 0:
- raise MKGeneralException(outdata)
+ raise MKGeneralException("Error running <tt>%s %s %s</tt>:
<pre>%s</pre>" %
+ (defaults.check_mk_automation, command, " ".join(args),
outdata))
return eval(outdata)