__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)
#testargs = '--help'
#testargs = '--version'
#testargs = '-vvv'
+testing = True
OK_RESPONSE = """
{
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)
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 """
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