-
Notifications
You must be signed in to change notification settings - Fork 4
/
check_b2share.py
executable file
·287 lines (233 loc) · 10.7 KB
/
check_b2share.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python3
#
# This file is part of B2SHARE Nagios monitoring plugin.
#
# Copyright (C) 2018 Harri Hirvonsalo
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Script for checking health and availability of a B2SHARE instance."""
import argparse
import signal
import sys
from enum import IntEnum
import jsonschema
import requests
from requests.models import PreparedRequest
from requests.exceptions import HTTPError, MissingSchema
class Verbosity(IntEnum):
"""Verbosity level as described by Nagios Plugin guidelines."""
# Single line, minimal output. Summary
NONE = 0
# Single line, additional information (eg list processes that fail)
SINGLE = 1
# Multi line, configuration debug output (eg ps command used)
MULTI = 2
# Lots of detail for plugin problem diagnosis
DEBUG = 3
def handler(signum, stack):
"""Timeout handler."""
print('UNKNOWN: Timeout reached, exiting.')
sys.exit(3)
def get_dict_from_url(url, verify_tls_cert=False, verbosity=False):
"""Make HTTP GET request to given URL. Decode response body as JSON.
Returns dictionary.
Raises requests.HTTPError in case Response code is not 200 OK.
Raises ValueError in case response body cannot be decoded as JSON.
"""
if verbosity > Verbosity.MULTI:
print('Making a HTTP GET request to {}'.format(url))
r = requests.get(url, verify=verify_tls_cert)
if r.status_code != requests.codes.ok:
if verbosity > Verbosity.SINGLE:
print("Request didn't return with HTTP status code 200 OK.")
# Not 2XX, raise HTTPError in case errors (4XX-5XX codes)
r.raise_for_status()
return r.json()
def validate_url(url):
"""Validate if a string is an url.
Based on https://stackoverflow.com/a/34266413
(python-validators package was not available as rpm package in Rocky Linux 9)
"""
prepared_request = PreparedRequest()
try:
prepared_request.prepare_url(url, None)
if not prepared_request.url:
return False
except MissingSchema:
return False
return True
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='B2SHARE Nagios probe')
parser.add_argument('-u', '--url', action='store', dest='url',
required=True,
help='Base URL of B2SHARE instance to probe.')
parser.add_argument('-t', '--timeout', action='store', dest='timeout',
type=int,
help='Timeout for probe in seconds. Positive value.')
parser.add_argument('-v', '--verbose', action='count', dest='verbose',
help='Increase output verbosity.', default=0)
parser.add_argument('--verify-tls-cert',
action='store_true',
dest='verify_tls_cert',
help='Should TLS certificate of B2SHARE server \
be verified.',
default=False)
parser.add_argument('--error-if-no-records-present',
action='store_true', dest='error_if_no_records',
help='Should probe give an error if no \
records are present at the B2SHARE instance.',
default=False)
# TODO: Add version information
# parser.add_argument(--version', action='store', dest='version',
# help='version')
param = parser.parse_args()
# Set maximum verbosity level to 3
if param.verbose > 3:
param.verbose = 3
# Set verbosity level
verbosity = Verbosity(param.verbose)
# Validate parameters
if not validate_url(param.url):
raise SyntaxError(
'CRITICAL: Invalid URL syntax {0}'.format(
param.url))
if param.timeout and param.timeout < 1:
parser.error("Timeout must be higher than 0.")
base_url = param.url
timeout = param.timeout
verify_tls_cert = param.verify_tls_cert
if not verify_tls_cert:
if verbosity > Verbosity.SINGLE:
print('TLS certificate verification: OFF')
# Disable SSL/TLS warnings coming from urllib,
# i.e. trust B2SHARE server even with invalid certificate
requests.packages.urllib3.disable_warnings()
if verbosity > Verbosity.SINGLE:
print('Verbosity level: {}'.format(verbosity))
if timeout and timeout > 0:
if verbosity > Verbosity.SINGLE:
print('Timeout: {} seconds'.format(timeout))
signal.signal(signal.SIGALRM, handler)
signal.alarm(timeout)
if verbosity > Verbosity.SINGLE:
print('B2SHARE URL: {}'.format(base_url))
print('Starting B2SHARE Probe...')
print('---------------------------')
try:
search_url = base_url + "/api/records/"
if verbosity > Verbosity.SINGLE:
print('Making a search.')
search_results = get_dict_from_url(search_url, verify_tls_cert,
verbosity=verbosity)
if search_results['hits']['total'] > 0:
if verbosity > Verbosity.SINGLE:
print('Search returned some results.')
rec_with_files_url = None
for hit in search_results['hits']['hits']:
# Check if there are files in the record
if len(hit.get('files', "")) > 0:
rec_with_files_url = hit['links']['self']
break
if rec_with_files_url:
if verbosity > Verbosity.SINGLE:
print('A record containing files was found.')
rec = get_dict_from_url(rec_with_files_url, verify_tls_cert,
verbosity=verbosity)
if verbosity > Verbosity.SINGLE:
print("Fetching record's metadata schema.")
rec_md_schema_url = rec['metadata']['$schema']
rec_md_schema = get_dict_from_url(rec_md_schema_url,
verify_tls_cert,
verbosity=verbosity)
if verbosity > Verbosity.SINGLE:
print("Validating record's metadata schema.")
jsonschema.Draft4Validator.check_schema(rec_md_schema)
if verbosity > Verbosity.SINGLE:
print('Validating record against metadata schema.')
jsonschema.validate(rec['metadata'], rec_md_schema)
if verbosity > Verbosity.SINGLE:
print('Accessing file bucket of the record.')
bucket_url = rec['links']['files']
bucket = get_dict_from_url(bucket_url, verify_tls_cert,
verbosity=verbosity)
if verbosity > Verbosity.SINGLE:
print('Fetching first file of the bucket.')
file_url = bucket['contents'][0]['links']['self']
# TODO: Specify a filesize limit as arguments.
# Now this doesn't download anything.
# Just uses HTTP HEAD verb.
# NTS: Will HTTP HEAD increase download count of a file?
if verbosity > Verbosity.MULTI:
print('Making a HTTP HEAD request to {}'.format(file_url))
r = requests.head(bucket_url, verify=verify_tls_cert)
if r.status_code != requests.codes.ok:
if verbosity > Verbosity.SINGLE:
print("Request didn't return with \
HTTP status code 200 OK.")
# Not 2XX, raise HTTPError in case errors (4XX-5XX codes)
r.raise_for_status()
else:
if verbosity > Verbosity.SINGLE:
print('No records containing files were found.')
print('Fetching a record without files.')
rec_wo_files_url = (search_results['hits']
['hits']
[0]
['links']
['self'])
rec_wo_files = get_dict_from_url(rec_wo_files_url,
verify_tls_cert,
verbosity=verbosity)
rec_md_schema_url = rec_wo_files['metadata']['$schema']
rec_md_schema = get_dict_from_url(rec_md_schema_url,
verify_tls_cert,
verbosity=verbosity)
if verbosity > Verbosity.SINGLE:
print("Validating record's metadata schema.")
jsonschema.Draft4Validator.check_schema(rec_md_schema)
if verbosity > Verbosity.SINGLE:
print('Validating record against metadata schema.')
jsonschema.validate(rec_wo_files['metadata'], rec_md_schema)
else:
# No search results, i.e. no public records at the instance.
# Not necessarily an error.
if verbosity > Verbosity.SINGLE:
print('No search results returned by the query.')
if param.error_if_no_records:
raise ValueError('It seems that there are no \
records stored in this B2SHARE instance')
except SyntaxError as e:
print('CRITICAL: {}'.format(repr(e)))
sys.exit(3)
except KeyError as e:
print('CRITICAL: {}'.format(repr(e)))
sys.exit(2)
except ValueError as e:
print('CRITICAL: {}'.format(repr(e)))
sys.exit(2)
except HTTPError as e:
print('CRITICAL: {}'.format(repr(e)))
sys.exit(2)
except BaseException as e:
print('CRITICAL: {}'.format(repr(e)))
# print(sys.exc_info()[0])
sys.exit(2)
if verbosity > Verbosity.NONE:
print('---------------------------')
if rec_with_files_url:
print('OK: records, metadata schemas and files are accessible.')
else:
print('OK: records and metadata schemas are accessible.')
else:
print('OK')
sys.exit(0)