]> git.datanom.net - check_http.git/commitdiff
more
authorMichael Rasmussen <mir@datanom.net>
Thu, 7 Dec 2023 00:05:46 +0000 (01:05 +0100)
committerMichael Rasmussen <mir@datanom.net>
Thu, 7 Dec 2023 00:05:46 +0000 (01:05 +0100)
Signed-off-by: Michael Rasmussen <mir@datanom.net>
.gitignore [new file with mode: 0644]
check_http.py

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..82a0a29
--- /dev/null
@@ -0,0 +1,20 @@
+.eric6project/
+.eric7project/
+.ropeproject/
+.jedi/
+.directory/
+*.pyc
+*.pyo
+*.orig
+*.bak
+*.rej
+*~
+cur/
+tmp/
+__pycache__/
+*.DS_Store
+.pytest_cache/
+venv/
+.venv/
+env/
+.env/
index ba8a4db54cfa0818d5004202fd623ec4dfed9687..bd5fd08d9c876cefe2ab9a9298699426d5a415f2 100644 (file)
 #
 ###############################################################################
 
-__author__ = 'Your Name Here'
+__author__ = 'mr.it@cbs.dk - Michael Rasmussen'
 __version__= 0.1
 
-from optparse import OptionParser, OptionGroup
-import logging as log
+try:
+    from optparse import OptionParser, OptionGroup
+    import logging as log
+    import sys
+    import urllib3 as urllib
+    import json
+except:
+    sys.exit(2)
 
 ## These will override any args passed to the script normally. Comment out after testing.
 #testargs = '--help'
 #testargs = '--version'
 #testargs = '-vvv'
 
+OK_RESPONSE = """
+{
+  "status": "UP",
+  "components": {
+    "db": {
+      "status": "UP",
+      "details": {
+        "database": "PostgreSQL",
+        "validationQuery": "isValid()"
+      }
+    },
+    "diskSpace": {
+      "status": "UP",
+      "details": {
+        "total": 2013582688256,
+        "free": 1635574571008,
+        "threshold": 10485760,
+        "path": "/home/mir/git/soasi-course-catalog-course-target-adapter/.",
+        "exists": true
+      }
+    },
+    "ping": {
+      "status": "UP"
+    }
+  }
+}"""
+DOWN_RESPONSE = """
+{
+  "status": "DOWN",
+  "components": {
+    "db": {
+      "status": "DOWN",
+      "components": {
+        "adapterDataSource": {
+          "status": "DOWN",
+          "details": {
+            "error": "org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection"
+          }
+        },
+        "es3DataSource": {
+          "status": "UP",
+          "details": {
+            "database": "Microsoft SQL Server",
+            "validationQuery": "isValid()"
+          }
+        },
+        "providersDataSource": {
+          "status": "DOWN",
+          "details": {
+            "error": "org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection"
+          }
+        }
+      }
+    },
+    "diskSpace": {
+      "status": "UP",
+      "details": {
+        "total": 1013309239296,
+        "free": 882010726400,
+        "threshold": 10485760,
+        "path": "C:\\Users\\hf.it\\Projects\\smart-integrations\\adapters\\target\\soasi-invigilation-report-target-adapter",
+        "exists": true
+      }
+    },
+    "ping": {
+      "status": "UP"
+    }
+  }
+}
+"""
 def main():
     """ Main plugin logic goes here """
 
@@ -36,8 +112,38 @@ def main():
     # log.error('error message')
     # log.critical('critical message')
     # log.fatal('fatal message')
-
-    gtfo(0)
+    options = vars(args)
+    keyword = options['keyword']
+    url = options['url']
+    agent = options['agent']
+    if keyword is None or url is None:
+        message = "Keywork: {0} url: {1}".format(keyword, url)
+        status = 3
+        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 'status' in data and data['status'] == keyword:
+                message = "UP"
+                status = 2
+            else:
+                message = "DOWN"
+                status = 0
+        except Exception as e:
+            print(e)
+            message = "DOWN"
+            status = 2
+            
+    gtfo(status, message)
 
 
 def parse_args():
@@ -52,8 +158,10 @@ def parse_args():
 
     ## CLI arguments specific to this script
     group = OptionGroup(parser,'Plugin Options')
-    group.add_option('-x', '--extra', help='Your option here',
-                     default=None)
+    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('-u', '--url', help="URL to requested resource", default=None)
     
     ## Common CLI arguments
     #parser.add_option('-c', '--critical', help='Set the critical threshold. Default: %(default)s',
This page took 0.037524 seconds and 5 git commands to generate.