X-Git-Url: http://git.datanom.net/check_http.git/blobdiff_plain/04a80b594046de43ceb2b6e82e9196bb1702ac9a..465bdd35cce61393d5a8dc66d09740d762ad4fc9:/check_http.py?ds=inline diff --git a/check_http.py b/check_http.py index bd5fd08..3201daf 100644 --- a/check_http.py +++ b/check_http.py @@ -15,11 +15,11 @@ __author__ = 'mr.it@cbs.dk - Michael Rasmussen' __version__= 0.1 -try: - from optparse import OptionParser, OptionGroup +try: # RHEL8 RHEL9 + from optparse import OptionParser, OptionGroup # import logging as log import sys - import urllib3 as urllib + import urllib3 as urllib # python3-urllib3-1.24.2-5.el8.noarch.rpm python3-urllib3-1.26.5-3.el9.noarch.rpm import json except: sys.exit(2) @@ -28,6 +28,7 @@ except: #testargs = '--help' #testargs = '--version' #testargs = '-vvv' +testing = True OK_RESPONSE = """ { @@ -122,21 +123,23 @@ def main(): log.fatal(message) else: try: -# req_headers = { -# 'User-Agent': agent -# } -# http = urllib.PoolManager() -# response = http.request( -# 'GET', url, headers = req_headers -# ) -# data = response.data.decode('utf-8') - data = OK_RESPONSE.replace("\\"," ") - data = json.loads(data) + if not testing: + req_headers = { + 'User-Agent': agent + } + http = urllib.PoolManager() + response = http.request( + 'GET', url, headers = req_headers + ) + data = response.data.decode('utf-8').replace("\\"," ") + else: + data = DOWN_RESPONSE.replace("\\"," ") + data = json.loads(data) if 'status' in data and data['status'] == keyword: message = "UP" status = 2 else: - message = "DOWN" + message = gather_message(data) status = 0 except Exception as e: print(e) @@ -145,6 +148,38 @@ def main(): gtfo(status, message) +def gather_message(json_data): + """ Assemble error messages """ + msg = None + try: + if 'components' in json_data: + components = json_data['components'] + for component in components: + items = components[component] + if 'components' in items: + for item in items['components']: + if 'status' in items['components'][item] and items['components'][item]['status'].upper() == 'DOWN': + if msg is not None: + msg += "\n{0}: {1}".format(item, items['components'][item]['details']['error']) + else: + msg = "{0}: {1}".format(item, items['components'][item]['details']['error']) + else: + if 'status' in items and items['status'].upper() == 'DOWN': + if msg is not None: + if 'details' in items and 'error' in items['details']: + error = items['details']['error'] + else: + error = "No error message" + if msg is not None: + msg += "\n{0}: {1}".format(component, error) + else: + msg = "{0}: {1}".format(component, error) + else: + sys.exit(3) + except: + sys.exit(3) + return msg + def parse_args(): """ Parse command-line arguments """ @@ -161,6 +196,7 @@ def parse_args(): group.add_option('-a', '--agent', help="User agent for request. Default: Python-nagios", default="Python-nagios", type='string') group.add_option('-k', '--keyword', help="Keyword to search for in response", default=None) + group.add_option('-t', '--testing', help="Run in testing mode", default=False, action='store_true') group.add_option('-u', '--url', help="URL to requested resource", default=None) ## Common CLI arguments