-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_vmware_nsxt.py
504 lines (385 loc) · 15.9 KB
/
check_vmware_nsxt.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#!/usr/bin/env python3
# coding:utf-8
"""
Icinga check plugin for VMware NSX-T
Supported Modes:
* cluster-status - retrieves the overall NSX-T cluster status from the API
* alarms - Retrieve and display open alarms from the API
* capacity-usage - Retrieves and checks capacity indicators from the API
General API Documentation: https://code.vmware.com/apis/1083/nsx-t
---
VMware NSX® is a trademark of VMware, Inc.
Copyright (C) 2021 NETWAYS GmbH <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import sys
import argparse
import logging
import datetime
import ssl
import re
from urllib.parse import urljoin
import urllib3
import requests
from requests.auth import HTTPBasicAuth
__version__ = '0.2.0'
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
STATES = {
OK: "OK",
WARNING: "WARNING",
CRITICAL: "CRITICAL",
UNKNOWN: "UNKNOWN",
}
def fix_tls_cert_store(cafile_path):
"""
Ensure we are using the system certstore by default
See https://github.com/psf/requests/issues/2966
Inspired by https://github.com/psf/requests/issues/2966#issuecomment-614323746
"""
# Check if we got a CA file path
if not cafile_path:
return
# If CA file contains something, set as default
if os.stat(cafile_path).st_size > 0:
requests.utils.DEFAULT_CA_BUNDLE_PATH = cafile_path
requests.adapters.DEFAULT_CA_BUNDLE_PATH = cafile_path
class CriticalException(Exception):
"""
Provide an exception that will cause the check to exit critically with an error
"""
class Client:
"""
Simple API client for VMware NSX-T
"""
API_PREFIX = '/api/v1/'
def __init__(self, api, username, password, logger=None, verify=True, max_age=5):
self.api = api
self.username = username
self.password = password
self.verify = verify
self.max_age = max_age
if logger is None:
logger = logging.getLogger()
self.logger = logger
def request(self, url, method='GET'):
"""
Basic JSON request handling
Handles authentication and returns the JSON result when successful
"""
base_url = urljoin(self.api, self.API_PREFIX)
request_url = urljoin(base_url, url)
self.logger.debug("starting API %s request from: %s", method, url)
try:
response = requests.request(method, request_url, auth=HTTPBasicAuth(self.username, self.password), verify=self.verify, timeout=10)
except requests.exceptions.RequestException as req_exc:
raise CriticalException(req_exc) # pylint: disable=raise-missing-from
if response.status_code != 200:
# TODO What about 300 Redirects?
raise CriticalException('Request to %s was not successful: %s' % (request_url, response.status_code))
try:
return response.json()
except Exception as json_exc:
raise CriticalException('Could not decode API JSON: ' + str(json_exc)) # pylint: disable=raise-missing-from
def get_cluster_status(self, excludes=None):
"""
GET and build ClusterStatus
"""
return ClusterStatus(self.request('cluster/status'), excludes)
def get_alarms(self, excludes=None):
"""
GET and build Alarms
"""
status = "OPEN"
# status = "RESOLVED" # for testing
result = self.request('alarms?page_size=100&status=%s&sort_ascending=false' % status)
return Alarms(data=result['results'], excludes=excludes)
def get_capacity_usage(self, excludes=None):
"""
GET and build CapacityUsage
"""
return CapacityUsage(self.request('capacity/usage'), self.max_age, excludes)
class CheckResult:
"""
CheckResult class, stores output, perfdata and state
"""
def __init__(self):
self.state = -1
self.summary = []
self.output = []
self.perfdata = []
def build_output(self):
raise NotImplementedError("build_output not implemented in %s" % type(self))
def get_output(self):
if len(self.summary) == 0:
self.build_output()
if self.state < 0:
self.build_status()
output = ' - '.join(self.summary)
if len(self.output) > 0:
output += "\n\n" + "\n".join(self.output)
if len(self.perfdata) > 0:
output += "\n| " + " ".join(self.perfdata)
try:
state = STATES[self.state]
except KeyError:
state = "UNKNOWN"
return "[%s] " % state + output
def build_status(self):
raise NotImplementedError("build_status not implemented in %s" % type(self))
def get_status(self):
if self.state < 0:
self.build_status()
if self.state < 0:
return UNKNOWN
return self.state
def print_and_return(self):
print(self.get_output())
return self.get_status()
class ClusterStatus(CheckResult):
"""
See API Documentation: https://code.vmware.com/apis/1083/nsx-t
https://vdc-download.vmware.com/vmwb-repository/dcr-public/787988e9-6348-4b2a-8617-e6d672c690ee/a187360c-77d5-4c0c-92a8-8e07aa161a27/api_includes/method_ReadClusterStatus.html
"""
def __init__(self, data, excludes):
super().__init__()
self.data = data
self.excludes = excludes
if excludes is None:
self.excludes = []
def build_output(self):
for area in ['control_cluster_status', 'mgmt_cluster_status', 'control_cluster_status']:
self.summary.append(area + '=' + self.data[area]['status'])
nodes_online = len(self.data['mgmt_cluster_status']['online_nodes'])
self.summary.append("nodes_online=%d" % nodes_online)
self.perfdata.append("nodes_online=%d;;;0" % nodes_online)
for group in self.data['detailed_cluster_status']['groups']:
state = "OK" if self.data[area]['status'] == "STABLE" else "CRITICAL"
self.output.append('[%s] %s: %s - %d members' % (state, group['group_type'], group['group_status'], len(group['members'])))
def build_status(self):
states = []
for area in ['control_cluster_status', 'mgmt_cluster_status', 'control_cluster_status']:
states.append(OK if self.data[area]['status'] == "STABLE" else CRITICAL)
self.state = worst_state(*states)
class Alarms(CheckResult):
"""
See API Documentation: https://code.vmware.com/apis/1083/nsx-t
https://vdc-download.vmware.com/vmwb-repository/dcr-public/787988e9-6348-4b2a-8617-e6d672c690ee/a187360c-77d5-4c0c-92a8-8e07aa161a27/api_includes/method_GetAlarms.html
"""
def __init__(self, data, excludes):
super().__init__()
self.data = data
self.excludes = excludes
if excludes is None:
self.excludes = []
def _is_excluded(self, alarm):
# to exclude via --exclude
identifier = "%s %s %s %s" % (
alarm['severity'],
alarm['node_display_name'],
alarm['feature_display_name'],
alarm['event_type_display_name'])
for exclude in self.excludes:
regexp = re.compile(exclude)
if bool(regexp.search(identifier)):
return True
return False
def build_output(self):
states = {}
for alarm in self.data:
if self._is_excluded(alarm):
continue
severity = alarm['severity']
if severity in states:
states[severity] += 1
else:
states[severity] = 1
self.output.append("[%s] (%s) (%s) %s/%s - %s" % (
severity,
time_iso(alarm['_create_time']),
alarm['node_display_name'],
alarm['feature_display_name'],
alarm['event_type_display_name'],
alarm['summary'],
))
count = len(self.data)
self.summary.append("%d alarms" % count)
self.perfdata.append("alarms=%d;;;0" % count)
for state, value in states.items():
self.summary.append("%d %s" % (value, state.lower()))
self.perfdata.append("alarms.%s=%d;;;0" % (state.lower(), value))
def build_status(self):
states = []
for alarm in self.data:
if self._is_excluded(alarm):
continue
# HIGH == CRITICAL
state = WARNING if alarm['severity'] in ['MEDIUM', 'LOW'] else CRITICAL
states.append(state)
if len(states) > 0:
self.state = worst_state(*states)
else:
self.state = OK
class CapacityUsage(CheckResult):
"""
See API Documentation: https://code.vmware.com/apis/1083/nsx-t
https://vdc-download.vmware.com/vmwb-repository/dcr-public/787988e9-6348-4b2a-8617-e6d672c690ee/a187360c-77d5-4c0c-92a8-8e07aa161a27/api_includes/method_GetProtonCapacityUsage.html
"""
def __init__(self, data, max_age, excludes):
super().__init__()
self.data = data
self.max_age = max_age
self.excludes = excludes
if excludes is None:
self.excludes = []
def _is_excluded(self, usage):
# to exclude via --exclude
identifier = "%s %s" % (
usage['severity'],
usage['display_name'])
for exclude in self.excludes:
regexp = re.compile(exclude)
if bool(regexp.search(identifier)):
return True
return False
def build_output(self):
states = {}
for usage in self.data['capacity_usage']:
if self._is_excluded(usage):
continue
severity = usage['severity'] # INFO, WARNING, CRITICAL, ERROR
if severity in states:
states[severity] += 1
else:
states[severity] = 1
if severity == "INFO":
state = "OK"
elif severity == "WARNING":
state = "WARNING"
else:
state = "CRITICAL"
self.output.append("[%s] [%s] %s: %d of %d (%g%%)" % (
state,
usage['severity'],
usage['display_name'],
usage['current_usage_count'],
usage['max_supported_count'],
usage['current_usage_percentage'],
))
label = usage['usage_type'].lower()
self.perfdata.append("%s=%g%%;%d;%d;0;100" % (label, usage['current_usage_percentage'], usage['min_threshold_percentage'], usage['max_threshold_percentage']))
# Maybe we need count at some point...
# self.perfdata.append("%s_count=%d;;;0;%d" % (label, usage['current_usage_count'], usage['max_supported_count']))
for state, value in states.items():
self.summary.append("%d %s" % (value, state.lower()))
if len(states) == 0:
self.summary.append("no usages")
self.summary.append("last update: " + time_iso(self.data['meta_info']['last_updated_timestamp']))
def build_status(self):
states = []
now = datetime.datetime.now()
last_updated = build_datetime(self.data['meta_info']['last_updated_timestamp'])
if (now-last_updated).total_seconds() / 60 > self.max_age:
states.append(WARNING)
self.summary.append("last update older than %s minutes" % (self.max_age))
for usage in self.data['capacity_usage']:
if self._is_excluded(usage):
continue
severity = usage['severity'] # INFO, WARNING, CRITICAL, ERROR
if severity == "INFO":
state = OK
elif severity == "WARNING":
state = WARNING
else:
state = CRITICAL
states.append(state)
self.state = worst_state(*states)
def build_datetime(timestamp_ms):
"""
Build a datetime from the epoch including milliseconds the API returns
"""
return datetime.datetime.fromtimestamp(timestamp_ms / 1000)
def time_iso(timestamp_ms):
"""
Return a simple ISO datetime string without ms
"""
return build_datetime(timestamp_ms).strftime("%Y-%m-%d %H:%M:%S")
def worst_state(*states):
overall = -1
for state in states:
if state == CRITICAL:
overall = CRITICAL
elif state == UNKNOWN:
if overall != CRITICAL:
overall = UNKNOWN
elif state > overall:
overall = state
if overall < 0 or overall > 3:
overall = UNKNOWN
return overall
def commandline(args):
"""
Parse commandline arguments.
"""
def environ_or_required(key):
return ({'default': os.environ.get(key)} if os.environ.get(key) else {'required': True})
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--api', '-A',
**environ_or_required('CHECK_VMWARE_NSXT_API_URL'),
help='VMware NSX-T URL without any sub-path (e.g. https://vmware-nsx.local)')
parser.add_argument('--username', '-u',
**environ_or_required('CHECK_VMWARE_NSXT_API_USER'),
help='Username for Basic Auth')
parser.add_argument('--password', '-p',
**environ_or_required('CHECK_VMWARE_NSXT_API_PASSWORD'),
help='Password for Basic Auth')
parser.add_argument('--mode', '-m', choices=['cluster-status', 'alarms', 'capacity-usage'],
help='Check mode to execute. Hint: alarms will only include open alarms.', required=True)
parser.add_argument('--exclude', nargs='*', action='extend', type=str,
help="Exclude alarms or usage from the check results. Can be used multiple times and supports regular expressions.")
parser.add_argument('--max-age', '-M', type=int,
help='Max age in minutes for capacity usage updates. Defaults to 5', default=5, required=False)
parser.add_argument('--insecure',
help='Do not verify TLS certificate', action='store_true', required=False)
parser.add_argument('--version', '-V',
help='Print version', action='store_true')
return parser.parse_args(args)
def main(args):
fix_tls_cert_store(ssl.get_default_verify_paths().cafile)
if args.insecure:
urllib3.disable_warnings()
if args.version:
print(f"check_vmware_nsxt version {__version__}")
return 3
client = Client(args.api, args.username, args.password, verify=(not args.insecure), max_age=args.max_age)
if args.mode == 'cluster-status':
return client.get_cluster_status(args.exclude).print_and_return()
if args.mode == 'alarms':
return client.get_alarms(args.exclude).print_and_return()
if args.mode == 'capacity-usage':
return client.get_capacity_usage(args.exclude).print_and_return()
print("[UNKNOWN] unknown mode %s" % args.mode)
return UNKNOWN
if __package__ == '__main__' or __package__ is None: # pragma: no cover
try:
ARGS = commandline(sys.argv[1:])
sys.exit(main(ARGS))
except CriticalException as main_exc:
print("[CRITICAL] " + str(main_exc))
sys.exit(CRITICAL)
except Exception: # pylint: disable=broad-except
exception = sys.exc_info()
print("[UNKNOWN] Unexpected Python error: %s %s" % (exception[0], exception[1]))
sys.exit(UNKNOWN)