]>
git.datanom.net - check_http.git/blob - check_http.py
3 ###############################################################################
4 # Nagios plugin template
7 # - The RHEL boxes I work on are currently limited to Python 2.6.6, hence the
8 # use of (deprecated) optparse. If I can ever get them all updated to
9 # Python 2.7 (or better yet, 3.3), I'll switch to argparse
10 # - This template runs in 2.6-3.3. Any changes made will need to be appropriate
11 # to the Python distro you want to use
13 ###############################################################################
15 __author__
= 'mr.it@cbs.dk - Michael Rasmussen'
19 from optparse
import OptionParser
, OptionGroup
22 import urllib3
as urllib
27 ## These will override any args passed to the script normally. Comment out after testing.
29 #testargs = '--version'
39 "database": "PostgreSQL",
40 "validationQuery": "isValid()"
46 "total": 2013582688256,
47 "free": 1635574571008,
48 "threshold": 10485760,
49 "path": "/home/mir/git/soasi-course-catalog-course-target-adapter/.",
65 "adapterDataSource": {
68 "error": "org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection"
74 "database": "Microsoft SQL Server",
75 "validationQuery": "isValid()"
78 "providersDataSource": {
81 "error": "org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection"
89 "total": 1013309239296,
91 "threshold": 10485760,
92 "path": "C:\\Users\\hf.it\\Projects\\smart-integrations\\adapters\\target\\soasi-invigilation-report-target-adapter",
103 """ Main plugin logic goes here """
105 ## Parse command-line arguments
106 args
, args2
= parse_args()
108 ## Uncomment to test logging levels against verbosity settings
109 # log.debug('debug message')
110 # log.info('info message')
111 # log.warning('warning message')
112 # log.error('error message')
113 # log.critical('critical message')
114 # log.fatal('fatal message')
116 keyword
= options
['keyword']
118 agent
= options
['agent']
119 if keyword
is None or url
is None:
120 message
= "Keywork: {0} url: {1}".format(keyword
, url
)
126 # 'User-Agent': agent
128 # http = urllib.PoolManager()
129 # response = http.request(
130 # 'GET', url, headers = req_headers
132 # data = response.data.decode('utf-8')
133 data
= OK_RESPONSE
.replace("\\"," ")
134 data
= json
.loads(data
)
135 if 'status' in data
and data
['status'] == keyword
:
141 except Exception as e
:
146 gtfo(status
, message
)
150 """ Parse command-line arguments """
152 parser
= OptionParser(usage
='usage: %prog [-v|vv|vvv] [options]',
153 version
='{0}: v.{1} by {2}'.format('%prog', __version__
, __author__
))
155 ## Verbosity (want this first, so it's right after --help and --version)
156 parser
.add_option('-v', help='Set verbosity level',
157 action
='count', default
=0, dest
='v')
159 ## CLI arguments specific to this script
160 group
= OptionGroup(parser
,'Plugin Options')
161 group
.add_option('-a', '--agent', help="User agent for request. Default: Python-nagios",
162 default
="Python-nagios", type='string')
163 group
.add_option('-k', '--keyword', help="Keyword to search for in response", default
=None)
164 group
.add_option('-u', '--url', help="URL to requested resource", default
=None)
166 ## Common CLI arguments
167 #parser.add_option('-c', '--critical', help='Set the critical threshold. Default: %(default)s',
168 # default=97, type=float, dest='crit', metavar='##')
169 #parser.add_option('-w', '--warning', help='Set the warning threshold. Default: %(default)s',
170 # default=95, type=float, dest='warn', metavar='##')
172 parser
.add_option_group(group
)
174 ## Try to parse based on the testargs variable. If it doesn't exist, use args
176 args
, args2
= parser
.parse_args(testargs
.split())
178 args
, args2
= parser
.parse_args()
180 ## Set the logging level based on the -v arg
181 log
.getLogger().setLevel([log
.ERROR
, log
.WARN
, log
.INFO
, log
.DEBUG
][args
.v
])
183 log
.debug('Parsed arguments: {0}'.format(args
))
184 log
.debug('Other arguments: {0}'.format(args2
))
188 def gtfo(exitcode
, message
=''):
189 """ Exit gracefully with exitcode and (optional) message """
191 log
.debug('Exiting with status {0}. Message: {1}'.format(exitcode
, message
))
197 if __name__
== '__main__':
198 ## Initialize logging before hitting main, in case we need extra debuggability
199 log
.basicConfig(level
=log
.DEBUG
, format
='%(asctime)s - %(funcName)s - %(levelname)s - %(message)s')
This page took 0.07858 seconds and 6 git commands to generate.