Skip to content

Commit

Permalink
Add verbose flag and function that can be used to enhance output more…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
martialblog committed Apr 9, 2024
1 parent 4fbb0c8 commit 0aceabf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
31 changes: 19 additions & 12 deletions check_http_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import argparse
import sys
import ssl
from pprint import pprint
from urllib.error import HTTPError
from urllib.error import URLError

Expand Down Expand Up @@ -422,6 +421,9 @@ def parseArgs(args):

parser.add_argument('-d', '--debug', action='store_true',
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',
help='use TLS to connect to remote host')
parser.add_argument('-H', '--host', dest='host',
Expand Down Expand Up @@ -517,17 +519,24 @@ def parseArgs(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:
if pretty_flag:
pprint(message)
else:
print(message)
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):
"""
Expand Down Expand Up @@ -621,7 +630,7 @@ def main(cliargs):
if args.path:
url += "/%s" % args.path

debugPrint(args.debug, "url:%s" % url)
debugPrint(args.debug, "url: %s" % url)
json_data = ''

try:
Expand All @@ -644,10 +653,8 @@ def main(cliargs):
data = json.loads(json_data)
except ValueError as e:
nagios.append_message(UNKNOWN_CODE, " JSON Parser error: %s" % str(e))

else:
debugPrint(args.debug, 'json:')
debugPrint(args.debug, data, True)
verbosePrint(args.verbose, 1, json.dumps(data, indent=2))
# Apply rules to returned JSON data
processor = JsonRuleProcessor(data, args)
nagios.append_message(WARNING_CODE, processor.checkWarning())
Expand Down
12 changes: 8 additions & 4 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
sys.path.append('..')

from check_http_json import debugPrint
from check_http_json import verbosePrint


class CLITest(unittest.TestCase):
Expand All @@ -31,10 +32,13 @@ def test_debugprint(self):
debugPrint(True, 'debug')
mock_print.assert_called_once_with('debug')

def test_debugprint_pprint(self):
with mock.patch('check_http_json.pprint') as mock_pprint:
debugPrint(True, 'debug', True)
mock_pprint.assert_called_once_with('debug')
def test_verbose(self):
with mock.patch('builtins.print') as mock_print:
verbosePrint(0, 3, 'verbose')
mock_print.assert_not_called()

verbosePrint(3, 3, 'verbose')
mock_print.assert_called_once_with('verbose')

def test_cli_without_params(self):

Expand Down

0 comments on commit 0aceabf

Please sign in to comment.