Add verbose flag and function that can be used to enhance output more precisely

- Before we only had a boolean debug flag, good for debugging errors.
   The verbose flag can be used more precisely (`-v -vvv`) to specify when
   something should be printed. This is useful for adding more output whilst avoiding
   full debug output that contains secrets.
This commit is contained in:
Markus Opolka 2024-03-18 10:52:51 +01:00
parent 4fbb0c828a
commit 0aceabfe91
2 changed files with 27 additions and 16 deletions

View File

@ -6,7 +6,6 @@ import json
import argparse import argparse
import sys import sys
import ssl import ssl
from pprint import pprint
from urllib.error import HTTPError from urllib.error import HTTPError
from urllib.error import URLError from urllib.error import URLError
@ -422,6 +421,9 @@ def parseArgs(args):
parser.add_argument('-d', '--debug', action='store_true', parser.add_argument('-d', '--debug', action='store_true',
help='debug mode') help='debug mode')
parser.add_argument('-v', '--verbose', action='count', default=0,
help='Verbose mode. Multiple -v options increase the verbosity')
parser.add_argument('-s', '--ssl', action='store_true', parser.add_argument('-s', '--ssl', action='store_true',
help='use TLS to connect to remote host') help='use TLS to connect to remote host')
parser.add_argument('-H', '--host', dest='host', parser.add_argument('-H', '--host', dest='host',
@ -517,17 +519,24 @@ def parseArgs(args):
return parser.parse_args(args) return parser.parse_args(args)
def debugPrint(debug_flag, message, pretty_flag=False): def debugPrint(debug_flag, message):
""" """
Print debug messages if -d (debug_flat ) is set. Print debug messages if -d is set.
""" """
if not debug_flag:
return
if debug_flag: print(message)
if pretty_flag:
pprint(message)
else:
print(message)
def verbosePrint(verbose_flag, when, message):
"""
Print verbose messages if -v is set.
Since -v can be used multiple times, the when parameter sets the required amount before printing
"""
if not verbose_flag:
return
if verbose_flag >= when:
print(message)
def prepare_context(args): def prepare_context(args):
""" """
@ -621,7 +630,7 @@ def main(cliargs):
if args.path: if args.path:
url += "/%s" % args.path url += "/%s" % args.path
debugPrint(args.debug, "url:%s" % url) debugPrint(args.debug, "url: %s" % url)
json_data = '' json_data = ''
try: try:
@ -644,10 +653,8 @@ def main(cliargs):
data = json.loads(json_data) data = json.loads(json_data)
except ValueError as e: except ValueError as e:
nagios.append_message(UNKNOWN_CODE, " JSON Parser error: %s" % str(e)) nagios.append_message(UNKNOWN_CODE, " JSON Parser error: %s" % str(e))
else: else:
debugPrint(args.debug, 'json:') verbosePrint(args.verbose, 1, json.dumps(data, indent=2))
debugPrint(args.debug, data, True)
# Apply rules to returned JSON data # Apply rules to returned JSON data
processor = JsonRuleProcessor(data, args) processor = JsonRuleProcessor(data, args)
nagios.append_message(WARNING_CODE, processor.checkWarning()) nagios.append_message(WARNING_CODE, processor.checkWarning())

View File

@ -9,6 +9,7 @@ import os
sys.path.append('..') sys.path.append('..')
from check_http_json import debugPrint from check_http_json import debugPrint
from check_http_json import verbosePrint
class CLITest(unittest.TestCase): class CLITest(unittest.TestCase):
@ -31,10 +32,13 @@ class CLITest(unittest.TestCase):
debugPrint(True, 'debug') debugPrint(True, 'debug')
mock_print.assert_called_once_with('debug') mock_print.assert_called_once_with('debug')
def test_debugprint_pprint(self): def test_verbose(self):
with mock.patch('check_http_json.pprint') as mock_pprint: with mock.patch('builtins.print') as mock_print:
debugPrint(True, 'debug', True) verbosePrint(0, 3, 'verbose')
mock_pprint.assert_called_once_with('debug') mock_print.assert_not_called()
verbosePrint(3, 3, 'verbose')
mock_print.assert_called_once_with('verbose')
def test_cli_without_params(self): def test_cli_without_params(self):