#
###############################################################################
-__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 """
# 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():
## 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',