Module: check_mk
Branch: master
Commit: 71cd6884ce12baf243dab201fe273148ead3b026
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=71cd6884ce12ba…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri Oct 14 14:22:02 2016 +0200
bailing out is now handled by raising an exception
---
cmk_base/compress_history.py | 6 ++++--
lib/exceptions.py | 14 +++++++++++++-
modules/check_mk.py | 8 +++++---
modules/check_mk_base.py | 13 -------------
4 files changed, 22 insertions(+), 19 deletions(-)
diff --git a/cmk_base/compress_history.py b/cmk_base/compress_history.py
index 6f4e713..c9e1e29 100644
--- a/cmk_base/compress_history.py
+++ b/cmk_base/compress_history.py
@@ -29,12 +29,14 @@
# logfiles and then compress them. Do *not* compress compressed
# files again.
+from cmk.exceptions import MKBailOut
+
import cmk.log
logger = cmk.log.get_logger(__name__)
def do_compress_history(args):
if not args:
- bail_out("Please specify files to compress.")
+ raise MKBailOut("Please specify files to compress.")
for filename in args:
try:
@@ -43,7 +45,7 @@ def do_compress_history(args):
except Exception, e:
if cmk.debug.enabled():
raise
- bail_out(str(e))
+ raise MKBailOut("%s" % e)
def compress_history_file(input_path, output_path):
diff --git a/lib/exceptions.py b/lib/exceptions.py
index d42244c..3642b83 100644
--- a/lib/exceptions.py
+++ b/lib/exceptions.py
@@ -65,6 +65,18 @@ class MKGeneralException(MKException):
# propagate the termination up the callstack.
# This should be raised in all cases where the program termination is a
# "normal" case and no exception handling like printing a stack trace
-# should be done.
+# nor an error message should be done. The program is stopped with
+# exit code 0.
class MKTerminate(Exception):
pass
+
+
+# This is raised to print an error message and then end the program.
+# The program should catch this at top level and end exit the program
+# with exit code 3, in order to be compatible with monitoring plugin API.
+class MKBailOut(Exception):
+ def __init__(self, reason):
+ self.reason = reason
+
+ def __str__(self):
+ return self.reason
diff --git a/modules/check_mk.py b/modules/check_mk.py
index aa7334d..d6a2f71 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -46,7 +46,7 @@ import py_compile
import inspect
from cmk.regex import regex, is_regex
-from cmk.exceptions import MKGeneralException, MKTerminate
+from cmk.exceptions import MKGeneralException, MKTerminate, MKBailOut
import cmk.debug
import cmk.log
import cmk.tty as tty
@@ -4882,7 +4882,8 @@ try:
elif o in [ '-A', '--bake-agents' ]:
if 'do_bake_agents' not in globals():
- bail_out("Agent baking is not implemented in your version of
Check_MK. Sorry.")
+ raise MKBailOut("Agent baking is not implemented in your version of
Check_MK. Sorry.")
+
if args:
hostnames = parse_hostname_list(args, with_clusters = False,
with_foreign_hosts = True)
else:
@@ -4892,7 +4893,8 @@ try:
elif o == '--cap':
if 'do_cap' not in globals():
- bail_out("Agent packages are not supported by your version of
Check_MK.")
+ raise MKBailOut("Agent packages are not supported by your version of
Check_MK.")
+
do_cap(args)
done = True
diff --git a/modules/check_mk_base.py b/modules/check_mk_base.py
index 815d984..37ab13e 100644
--- a/modules/check_mk_base.py
+++ b/modules/check_mk_base.py
@@ -72,7 +72,6 @@ import cmk_base.agent_simulator
# from --debug
# - remove all remaining print commands and use sys.stdout.write instead
# or define a new output function
-# - Also create a function bail_out() for printing and error and exiting
#.
# .--Globals-------------------------------------------------------------.
@@ -114,12 +113,6 @@ def vverbose(text):
if opt_verbose >= 2:
verbose(text)
-# Output text to sys.stderr with a linefeed added. Exists
-# afterwards with and exit code of 3, in order to be
-# compatible with monitoring plugin API.
-def bail_out(reason):
- raise MKBailOut(reason)
-
def warning(reason):
stripped = reason.lstrip()
indent = reason[:len(reason) - len(stripped)]
@@ -187,12 +180,6 @@ RAISE = False
ZERO = 0.0
-class MKBailOut(Exception):
- def __init__(self, reason):
- self.reason = reason
- def __str__(self):
- return self.reason
-
class MKCounterWrapped(Exception):
def __init__(self, reason):
self.reason = reason