]> git.datanom.net - check_http.git/blobdiff - check_http.py
Finish
[check_http.git] / check_http.py
index bd5fd08d9c876cefe2ab9a9298699426d5a415f2..3201dafd10ee5ca7f77661d81b95ad8b5288e7ed 100644 (file)
 __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
This page took 0.050396 seconds and 5 git commands to generate.