mirror of
https://github.com/drewkerrigan/nagios-http-json.git
synced 2024-12-03 15:53:52 +01:00
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:
parent
4fbb0c828a
commit
0aceabfe91
@ -6,7 +6,6 @@ import json
|
||||
import argparse
|
||||
import sys
|
||||
import ssl
|
||||
from pprint import pprint
|
||||
from urllib.error import HTTPError
|
||||
from urllib.error import URLError
|
||||
|
||||
@ -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',
|
||||
@ -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):
|
||||
"""
|
||||
@ -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:
|
||||
@ -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())
|
||||
|
@ -9,6 +9,7 @@ import os
|
||||
sys.path.append('..')
|
||||
|
||||
from check_http_json import debugPrint
|
||||
from check_http_json import verbosePrint
|
||||
|
||||
|
||||
class CLITest(unittest.TestCase):
|
||||
@ -31,10 +32,13 @@ class CLITest(unittest.TestCase):
|
||||
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):
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user