From c915a05c9e34da2ab92e73a8428a426cee0b204e Mon Sep 17 00:00:00 2001 From: Mike Nowak Date: Thu, 12 Dec 2019 18:41:43 +0900 Subject: [PATCH] Adding support for supressing UNKNOWN results via --no-unknown --- README.rst | 2 ++ check_docker/check_docker.py | 28 ++++++++++++++++++++++++---- tests/test_check_docker.py | 7 +++++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index 9ceb068..8287aac 100644 --- a/README.rst +++ b/README.rst @@ -117,6 +117,8 @@ check_docker Usage --restarts WARN:CRIT Container restart thresholds. --no-ok Make output terse suppressing OK messages. If all checks are OK return a single OK. + --no-unknown Make output terse suppressing UNKNOWN messages. If all + checks are UNKNOWN return a single UNKNOWN. --no-performance Suppress performance data. Reduces output when performance data is not being used. -V show program's version number and exit diff --git a/check_docker/check_docker.py b/check_docker/check_docker.py index 0440e8b..8cda6b3 100755 --- a/check_docker/check_docker.py +++ b/check_docker/check_docker.py @@ -58,6 +58,9 @@ # Reduce message to a single OK unless a checks fail. no_ok = False +# Reduce message to a single UNKNOWN unless a checks fail. +no_unknown = False + # Suppress performance data reporting no_performance = False @@ -853,6 +856,12 @@ def process_args(args): action='store_true', help='Make output terse suppressing OK messages. If all checks are OK return a single OK.') + # no-unknown + parser.add_argument('--no-unknown', + dest='no_unknown', + action='store_true', + help='Make output terse suppressing UNKNOWN messages. If all checks are UNKNOWN return a single UNKNOWN.') + # no-performance parser.add_argument('--no-performance', dest='no_performance', @@ -903,17 +912,25 @@ def socketfile_permissions_failure(parsed_args): def print_results(): - if no_ok: - # Remove all the "OK"s - filtered_messages = [message for message in messages if not message.startswith('OK: ')] + if no_ok or no_unknown: + if no_ok: + # Remove all the "OK"s + filtered_messages = [message for message in messages if not message.startswith('OK: ')] + if no_unknown: + # Remove all the "UNKNOWN"s + filtered_messages = [message for message in messages if not message.startswith('UNKNOWN: ')] if len(filtered_messages) == 0: - messages_concat = 'OK' + if no_ok: + messages_concat = 'OK' + if no_unknown: + messages_concat = 'UNKNOWN' else: messages_concat = '; '.join(filtered_messages) else: messages_concat = '; '.join(messages) + if no_performance or len(performance_data) == 0: print(messages_concat) else: @@ -935,6 +952,9 @@ def perform_checks(raw_args): global no_ok no_ok = args.no_ok + global no_unknown + no_unknown = args.no_unknown + global no_performance no_performance = args.no_ok diff --git a/tests/test_check_docker.py b/tests/test_check_docker.py index 7f27300..eafc6c0 100644 --- a/tests/test_check_docker.py +++ b/tests/test_check_docker.py @@ -45,6 +45,7 @@ def check_docker_fresh(): def check_docker(): cd.rc = -1 check_docker.no_ok = False + check_docker.no_unknown = False check_docker.no_performance = False cd.timeout = 1 cd.messages = [] @@ -720,6 +721,7 @@ def test_perform(check_docker, fs, args, called): def test_print_results(check_docker, capsys, messages, perf_data, expected): # These sometimes get set to true when using random-order plugin, for example --random-order-seed=620808 check_docker.no_ok = False + check_docker.no_unknown = False check_docker.no_performance = False check_docker.messages = messages check_docker.performance_data = perf_data @@ -728,7 +730,7 @@ def test_print_results(check_docker, capsys, messages, perf_data, expected): assert out.strip() == expected -@pytest.mark.parametrize("messages, perf_data, no_ok, no_performance, expected", ( +@pytest.mark.parametrize("messages, perf_data, no_ok, no_unknown, no_performance, expected", ( ([], [], False, False, ''), (['TEST'], [], False, False, 'TEST'), (['FOO', 'BAR'], [], False, False, 'FOO; BAR'), @@ -742,10 +744,11 @@ def test_print_results(check_docker, capsys, messages, perf_data, expected): (['OK: TEST'], ['1;2;3;4;'], False, True, 'OK: TEST'), (['OK: FOO', 'OK: BAR'], ['1;2;3;4;'], True, True, 'OK'), )) -def test_print_results_no_ok(check_docker, capsys, messages, perf_data, no_ok, no_performance, expected): +def test_print_results_no_ok(check_docker, capsys, messages, perf_data, no_ok, no_unknown, no_performance, expected): check_docker.messages = messages check_docker.performance_data = perf_data check_docker.no_ok = no_ok + check_docker.no_unknown = no_unknown check_docker.no_performance = no_performance check_docker.print_results() out, err = capsys.readouterr()