From 04a80b594046de43ceb2b6e82e9196bb1702ac9a Mon Sep 17 00:00:00 2001 From: Michael Rasmussen Date: Thu, 7 Dec 2023 01:05:46 +0100 Subject: [PATCH] more Signed-off-by: Michael Rasmussen --- .gitignore | 20 +++++++++ check_http.py | 122 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..82a0a29 --- /dev/null +++ b/.gitignore @@ -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/ diff --git a/check_http.py b/check_http.py index ba8a4db..bd5fd08 100644 --- a/check_http.py +++ b/check_http.py @@ -12,17 +12,93 @@ # ############################################################################### -__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', -- 2.39.5