forked from sapcc/vrops-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InventoryBuilder.py
884 lines (797 loc) · 43.2 KB
/
InventoryBuilder.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
import urllib3.exceptions
from flask import Flask
from gevent.pywsgi import WSGIServer
from threading import Thread
from tools.Vrops import Vrops
from tools.helper import yaml_read
from collections import defaultdict
import time
import json
import simplejson
import os
import sys
import logging
import requests
logger = logging.getLogger('vrops-exporter')
class InventoryBuilder:
def __init__(self, atlas_path, port, sleep, timeout):
self.atlas_path = atlas_path
self.vrops_list = None
self.port = int(port)
self.sleep = sleep
self.timeout = int(timeout)
self._user = os.environ["USER"]
self._password = os.environ["PASSWORD"]
self.vcenter_dict = dict()
self.nsxt_dict = dict()
self.vcops_dict = dict()
self.sddc_dict = dict()
self.target_tokens = dict()
self.iterated_inventory = dict()
self.amount_resources = defaultdict(dict)
self.vrops_collection_times = dict()
self.response_codes = defaultdict(dict)
self.alertdefinitions = dict()
self.successful_iteration_list = [0]
self.wsgi_address = '0.0.0.0'
if 'LOOPBACK' in os.environ:
if os.environ['LOOPBACK'] == '1':
self.wsgi_address = '127.0.0.1'
thread = Thread(target=self.run_rest_server)
thread.start()
self.query_inventory_permanent()
def run_rest_server(self):
app = Flask(__name__)
logger.info(f'serving /vrops_list on {self.port}')
@app.route('/vrops_list', methods=['GET'])
def vrops_list():
return json.dumps(self.vrops_list)
logger.info(f'serving /inventory on {self.port}')
@app.route('/<target>/vcenters/<int:iteration>', methods=['GET'])
def vcenters(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('vcenters', {}).get(target, {})
@app.route('/<target>/datacenters/<int:iteration>', methods=['GET'])
def datacenters(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('datacenters', {}).get(target, {})
@app.route('/<target>/clusters/<int:iteration>', methods=['GET'])
def clusters(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('clusters', {}).get(target, {})
@app.route('/<target>/hosts/<int:iteration>', methods=['GET'])
def hosts(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('hosts', {}).get(target, {})
@app.route('/<target>/datastores/<int:iteration>', methods=['GET'])
def datastores(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('datastores', {}).get(target, {})
@app.route('/<target>/storagepod/<int:iteration>', methods=['GET'])
def storagepod(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('storagepod', {}).get(target, {})
@app.route('/<target>/vms/<int:iteration>', methods=['GET'])
def vms(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('vms', {}).get(target, {})
@app.route('/<target>/dvs/<int:iteration>', methods=['GET'])
def distributed_virtual_switches(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('distributed_virtual_switches', {}).get(target,
{})
@app.route('/<target>/nsxt_adapter/<int:iteration>', methods=['GET'])
def nsxt_adapter(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('nsxt_adapter', {}).get(target, {})
@app.route('/<target>/nsxt_mgmt_cluster/<int:iteration>', methods=['GET'])
def nsxt_mgmt_cluster(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('nsxt_mgmt_cluster', {}).get(target, {})
@app.route('/<target>/nsxt_mgmt_nodes/<int:iteration>', methods=['GET'])
def nsxt_mgmt_nodes(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('nsxt_mgmt_nodes', {}).get(target, {})
@app.route('/<target>/nsxt_mgmt_service/<int:iteration>', methods=['GET'])
def nsxt_mgmt_service(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('nsxt_mgmt_service', {}).get(target, {})
@app.route('/<target>/nsxt_transport_nodes/<int:iteration>', methods=['GET'])
def nsxt_transport_nodes(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('nsxt_transport_nodes', {}).get(target, {})
@app.route('/<target>/nsxt_logical_switches/<int:iteration>', methods=['GET'])
def nsxt_logical_switches(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('nsxt_logical_switches', {}).get(target, {})
@app.route('/<target>/vcops_objects/<int:iteration>', methods=['GET'])
def vcops_self_monitoring_objects(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('vcops_objects', {}).get(target,
{})
@app.route('/<target>/sddc_objects/<int:iteration>', methods=['GET'])
def sddc_health_objects(target, iteration):
return self.iterated_inventory.get(str(iteration), {}).get('sddc_objects', {}).get(target, {})
@app.route('/alertdefinitions/<alert_id>', methods=['GET'])
def alert_alertdefinitions(alert_id):
return self.alertdefinitions[alert_id]
@app.route('/iteration', methods=['GET'])
def iteration():
return_iteration = self.successful_iteration_list[-1]
return str(return_iteration)
@app.route('/amount_resources', methods=['GET'])
def amount_resources():
amount_resources = self.amount_resources
return json.dumps(amount_resources)
@app.route('/collection_times', methods=['GET'])
def collection_times():
vrops_collection_times = self.vrops_collection_times
return json.dumps(vrops_collection_times)
@app.route('/api_response_codes', methods=['GET'])
def api_response_codes():
response_codes = self.response_codes
return json.dumps(response_codes)
# debugging purpose
@app.route('/iteration_store', methods=['GET'])
def iteration_store():
return_iteration = self.successful_iteration_list
return json.dumps(return_iteration)
# FIXME: this could basically be the always active token list. no active token? refresh!
@app.route('/target_tokens', methods=['GET'])
def token():
return json.dumps(self.target_tokens)
try:
if logger.level == 10:
# WSGi is logging on DEBUG Level
WSGIServer((self.wsgi_address, self.port), app).serve_forever()
else:
WSGIServer((self.wsgi_address, self.port), app, log=None).serve_forever()
except TypeError as e:
logger.error('Problem starting server, you might want to try LOOPBACK=0 or LOOPBACK=1')
logger.error(f'Current used options: {self.wsgi_address} on port {self.port}')
logger.error(f'TypeError: {e}')
def read_inventory_config(self):
return yaml_read(os.environ['INVENTORY_CONFIG'])
def get_vrops(self):
if not self.atlas_path and not self.vrops_list:
self.vrops_list = yaml_read(os.environ['INVENTORY_CONFIG']).get('vrops_targets', [])
elif self.atlas_path:
try:
response = requests.get(url=self.atlas_path)
self.response_codes['atlas'][self.atlas_path] = response.status_code
except (urllib3.exceptions.MaxRetryError, requests.exceptions.ConnectionError,
urllib3.exceptions.NewConnectionError) as err:
logger.error(f'Failed to establish a connection to: {self.atlas_path}, retrying in {self.sleep}s', err)
if not self.vrops_list:
logger.critical(f'No targets found to start InventoryBuilder, bailing out')
sys.exit(1)
logger.info(f'Continue with the old vrops_list')
return
try:
response.json()
except simplejson.errors.JSONDecodeError:
logger.error(f'Invalid json data in atlas netbox http response, Content-Type: "{response.headers.get("Content-Type")}"')
if not self.vrops_list:
logger.critical(f'No targets found to start InventoryBuilder, bailing out')
sys.exit(1)
logger.info(f'Continue with the old vrops_list')
return
netbox_json = response.json()
self.vrops_list = [target['labels']['server_name'] for target in netbox_json if target['labels']['job'] == "vrops"]
else:
return
def query_inventory_permanent(self):
# first iteration to fill is 1. while this is not ready,
# curl to /iteration would still report 0 to wait for actual data
self.iteration = 1
while True:
self.read_inventory_config()
# get vrops targets every run in case we have new targets appearing
self.get_vrops()
if len(self.successful_iteration_list) > 2:
iteration_to_be_deleted = self.successful_iteration_list.pop(0)
# initial case, since 0 is never filled in iterated_inventory
if iteration_to_be_deleted == 0:
continue
self.iterated_inventory.pop(str(iteration_to_be_deleted))
logger.debug(f'deleting iteration {iteration_to_be_deleted}')
# initialize empty inventory per iteration
self.iterated_inventory[str(self.iteration)] = dict()
logger.info(f'real run {self.iteration}')
threads = list()
for vrops in self.vrops_list:
vrops_short_name = vrops.split('.')[0]
thread = Thread(target=self.query_vrops, args=(vrops, vrops_short_name, self.iteration))
thread.start()
threads.append((thread, vrops))
timeout = self.timeout
timeout_reached = False
start_time = time.time()
current_time = start_time
joined_threads = dict()
while current_time <= (start_time + timeout):
for t in threads:
if not t[0].is_alive():
t[0].join()
if t[0] not in joined_threads:
joined_threads.setdefault(t[1], round(time.time() - start_time))
if len(joined_threads.keys()) >= len(threads):
break
time.sleep(1)
current_time = time.time()
else:
still_running = [t for t in threads if t[0].is_alive()]
for running_thread in still_running:
logger.info(f"Timeout {timeout}s reached for fetching {running_thread[1]}")
running_thread[0].join(0)
timeout_reached = True
for vrops in joined_threads:
self.vrops_collection_times[vrops] = joined_threads[vrops]
logger.info(f"Fetched {vrops} in {joined_threads[vrops]}s")
self.provide_vcenters()
self.provide_datacenters()
self.provide_clusters()
self.provide_hosts()
self.provide_datastores()
self.provide_SDRS_clusters()
self.provide_vms()
self.provide_distributed_vswitches()
self.provide_nsxt_adapter()
self.provide_nsxt_mgmt_cluster()
self.provide_nsxt_mgmt_nodes()
self.provide_nsxt_mgmt_service()
self.provide_nsxt_transport_nodes()
self.provide_nsxt_logical_switches()
self.provide_vcops_objects()
self.provide_sddc_objects()
if len(self.iterated_inventory[str(self.iteration)]['vcenters']) > 0:
self.successful_iteration_list.append(self.iteration)
else:
# immediately withdraw faulty inventory
logger.debug(f'Withdrawing current iteration: {self.iteration}')
self.iterated_inventory.pop(str(self.iteration))
self.iteration += 1
if not timeout_reached:
logger.info(f'Inventory relaxing before going to work again in {self.sleep}s')
time.sleep(int(self.sleep))
def query_vrops(self, target, vrops_short_name, iteration):
vrops = Vrops()
logger.info(f'Querying {target}')
token, self.response_codes[target]["token"] = Vrops.get_token(target=target)
if not token:
logger.warning(f'retrying connection to {target} in next iteration {self.iteration + 1}')
return False
self.target_tokens[target] = token
logger.info(f'########## Collecting resources {vrops_short_name}... ##########')
inventory_config = self.read_inventory_config()
query_specs = inventory_config.get('query_specs', {})
vcenter_adapter = self.create_vcenter_objects(vrops, target, token, query_specs)
nsxt_adapter = self.create_nsxt_objects(vrops, target, token, query_specs)
vcops_adapter = self.create_vcops_objects(vrops, target, token, inventory_config)
sddc_adapter = self.create_sddc_health_objects(vrops, target, token, inventory_config)
self.vcenter_dict[target] = vcenter_adapter
self.nsxt_dict[target] = nsxt_adapter
self.vcops_dict[target] = vcops_adapter
self.sddc_dict[target] = sddc_adapter
if iteration == 1 and not self.alertdefinitions:
self.alertdefinitions = Vrops.get_alertdefinitions(vrops, target, token)
return True
def create_vcenter_objects(self, vrops, target: str, token: str, query_specs: dict):
vcenter_adapter_list, self.response_codes[target]["vcenter"] = Vrops.get_vcenter_adapter(vrops, target, token)
if not vcenter_adapter_list:
logger.info(f'Could not get vcenter adapter!')
return False
datacenter, self.response_codes[target]["datacenters"] = \
Vrops.get_datacenter(vrops, target, token, [vc.uuid for vc in vcenter_adapter_list], query_specs=query_specs)
cluster, self.response_codes[target]["clusters"] = \
Vrops.get_cluster(vrops, target, token, [dc.uuid for dc in datacenter], query_specs=query_specs)
datastores, self.response_codes[target]["datastores"] = \
Vrops.get_datastores(vrops, target, token, [dc.uuid for dc in datacenter], query_specs=query_specs)
hosts, self.response_codes[target]["hosts"] = \
Vrops.get_hosts(vrops, target, token, [cl.uuid for cl in cluster], query_specs=query_specs)
distributed_virtual_switchs, self.response_codes[target]["distributed_virtual_switch"] = \
Vrops.get_dis_virtual_switch(vrops, target, token, [dc.uuid for dc in datacenter], query_specs=query_specs)
storagepod, self.response_codes[target]["storagepod"] = \
Vrops.get_SDRS_cluster(vrops, target, token, [dc.uuid for dc in datacenter], query_specs=query_specs)
if storagepod:
clustered_datastores, self.response_codes[target]["clustered_datastores"] = \
Vrops.get_datastores(vrops, target, token, [sc.uuid for sc in storagepod], query_specs=query_specs)
for vcenter_adapter in vcenter_adapter_list:
logger.debug(f'Collecting vCenter adapter: {vcenter_adapter.name}')
vcenter_adapter.datacenter = list()
# we need the vcenter to estimate the amount of virtual machines for the request
vms, self.response_codes[target]["vms"] = \
Vrops.get_vms(vrops, target, token, [hs.uuid for hs in hosts], vcenter_adapter.uuid, query_specs=query_specs)
for dc in datacenter:
if dc.parent == vcenter_adapter.uuid:
vcenter_adapter.datacenter.append(dc)
logger.debug(f'Collecting datacenter: {dc.name}')
for dc_object in vcenter_adapter.datacenter:
dc_object.datastores = list()
dc_object.clusters = list()
dc_object.dvss = list()
dc_object.storagepod = list()
for ds in datastores:
if ds.parent == dc_object.uuid:
dc_object.datastores.append(ds)
logger.debug(f'Collecting datastore: {ds.name}')
for sc in storagepod:
if sc.parent == dc_object.uuid:
dc_object.storagepod.append(sc)
logger.debug(f'Collecting SDRS clusters: {sc.name}')
for sc_object in dc_object.storagepod:
sc_object.datastores = list()
for ds in clustered_datastores:
if ds.parent == sc_object.uuid:
sc_object.datastores.append(ds)
logger.debug(f'Collecting clustered datastore: {ds.name}')
for cl in cluster:
if cl.parent == dc_object.uuid:
dc_object.clusters.append(cl)
logger.debug(f'Collecting cluster: {cl.name}')
for cl_object in dc_object.clusters:
cl_object.hosts = list()
for hs in hosts:
if hs.parent == cl_object.uuid:
cl_object.hosts.append(hs)
logger.debug(f'Collecting host: {hs.name}')
for hs_object in cl_object.hosts:
hs_object.vms = list()
for vm in vms:
if vm.parent == hs_object.uuid:
hs_object.vms.append(vm)
logger.debug(f'Collecting VM: {vm.name}')
for dvs in distributed_virtual_switchs:
if dvs.parent == dc_object.uuid:
dc_object.dvss.append(dvs)
logger.debug(f'Collecting distributed virtual switch: {dvs.name}')
return vcenter_adapter_list
def create_nsxt_objects(self, vrops, target: str, token: str, query_specs: dict):
nsxt_adapter_list, self.response_codes[target]["nsxt_adapter"] = Vrops.get_nsxt_adapter(vrops, target, token)
if not nsxt_adapter_list:
logger.info(f'Could not get any nsxt adapter from {target}!')
return False
nsxt_mgmt_cluster, self.response_codes[target]["nsxt_mgmt_cluster"] = \
Vrops.get_nsxt_mgmt_cluster(vrops, target, token, [a.uuid for a in nsxt_adapter_list],
query_specs=query_specs)
nsxt_mgmt_nodes, self.response_codes[target]["nsxt_mgmt_nodes"] = \
Vrops.get_nsxt_mgmt_nodes(vrops, target, token, [c.uuid for c in nsxt_mgmt_cluster],
query_specs=query_specs)
nsxt_mgmt_service, self.response_codes[target]["nsxt_mgmt_services"] = \
Vrops.get_nsxt_mgmt_service(vrops, target, token, [n.uuid for n in nsxt_mgmt_nodes],
query_specs=query_specs)
nsxt_transport_zones, self.response_codes[target]["nsxt_transport_zones"] = \
Vrops.get_nsxt_transport_zone(vrops, target, token, [c.uuid for c in nsxt_mgmt_cluster],
query_specs=query_specs)
nsxt_transport_nodes, self.response_codes[target]["nsxt_transport_nodes"] = \
Vrops.get_nsxt_transport_node(vrops, target, token, [z.uuid for z in nsxt_transport_zones],
query_specs=query_specs)
nsxt_logical_switches, self.response_codes[target]["nsxt_logical_switches"] = \
Vrops.get_nsxt_logical_switch(vrops, target, token, [c.uuid for c in nsxt_mgmt_cluster],
query_specs=query_specs)
for nsxt_adapter in nsxt_adapter_list:
logger.debug(f'Collecting NSX-T adapter: {nsxt_adapter.name}')
nsxt_adapter.mgmt_cluster = list()
for mgmt_cluster in nsxt_mgmt_cluster:
if mgmt_cluster.parent == nsxt_adapter.uuid:
nsxt_adapter.mgmt_cluster.append(mgmt_cluster)
logger.debug(f'Collecting NSX-T management cluster: {mgmt_cluster.name}')
for mgmt_cluster_object in nsxt_adapter.mgmt_cluster:
mgmt_cluster_object.mgmt_nodes = list()
mgmt_cluster_object.transport_zones = list()
mgmt_cluster_object.logical_switches = list()
for mgmt_node in nsxt_mgmt_nodes:
if mgmt_node.parent == mgmt_cluster_object.uuid:
mgmt_cluster_object.mgmt_nodes.append(mgmt_node)
logger.debug(f'Collecting NSX-T management node: {mgmt_node.name}')
for nsxt_mgmt_node in mgmt_cluster_object.mgmt_nodes:
nsxt_mgmt_node.nsxt_mgmt_services = list()
for mgmt_service_instance in nsxt_mgmt_service:
if mgmt_service_instance.parent == nsxt_mgmt_node.uuid:
nsxt_mgmt_node.nsxt_mgmt_services.append(mgmt_service_instance)
logger.debug(f'Collecting NSX-T management service: {mgmt_service_instance.name}')
for transport_zone in nsxt_transport_zones:
if transport_zone.parent == mgmt_cluster_object.uuid:
mgmt_cluster_object.transport_zones.append(transport_zone)
logger.debug(f'Collecting NSX-T transport zone: {transport_zone.name}')
for nsxt_transport_zone in mgmt_cluster_object.transport_zones:
nsxt_transport_zone.nsxt_transport_nodes = list()
for transport_node in nsxt_transport_nodes:
if transport_node.parent == nsxt_transport_zone.uuid:
nsxt_transport_zone.nsxt_transport_nodes.append(transport_node)
logger.debug(f'Collecting NSX-T transport node: {transport_node.name}')
for logical_switch in nsxt_logical_switches:
if logical_switch.parent == mgmt_cluster_object.uuid:
mgmt_cluster_object.logical_switches.append(logical_switch)
logger.debug(f'Collecting NSX-T logical switch: {logical_switch.name}')
return nsxt_adapter_list
def create_vcops_objects(self, vrops, target: str, token: str, inventory_config: dict):
vcops_adapter_instance, self.response_codes[target]["vcops_adapter"] = \
Vrops.get_vcenter_operations_adapter_intance(vrops, target, token)
vcops_adapter_instance = vcops_adapter_instance[0]
if not vcops_adapter_instance:
logger.info(f'Could not get vcops adapter!')
return False
resourcekinds = [rk for rk in inventory_config.get('resourcekinds', {}).get('vcops_resourcekinds', [])]
query_specs = inventory_config.get('query_specs', {})
vcops_objects, self.response_codes[target]["vcops_self_monitoring_objects"] = \
Vrops.get_vcops_instances(vrops, target, token, parent_uuids=[vcops_adapter_instance.uuid],
resourcekinds=resourcekinds, query_specs=query_specs)
vcops_adapter_instance.vcops_objects = list()
for vcops_object in vcops_objects:
vcops_adapter_instance.vcops_objects.append(vcops_object)
return vcops_adapter_instance
def create_sddc_health_objects(self, vrops, target: str, token: str, inventory_config: dict):
sddc_adapter_instances, self.response_codes[target]["sddc_health_adapter"] = \
Vrops.get_sddc_health_adapter_intance(vrops, target, token)
if not sddc_adapter_instances:
logger.info(f'Could not get sddc adapter!')
return False
resourcekinds = [rk for rk in inventory_config.get('resourcekinds', {}).get('sddc_resourcekinds', [])]
query_specs = inventory_config.get('query_specs', {})
sddc_objects, self.response_codes[target]["sddc_health_objects"] = \
Vrops.get_sddc_instances(vrops, target, token, parent_uuids=[s.uuid for s in sddc_adapter_instances],
resourcekinds=resourcekinds, query_specs=query_specs)
for sddc_adapter in sddc_adapter_instances:
logger.debug(f'Collecting SDDC adapter: {sddc_adapter.name}')
sddc_adapter.sddc_health_objects = list()
for sddc_object in sddc_objects:
if sddc_object.parent == sddc_adapter.uuid:
logger.debug(f'Collecting SDDC object: {sddc_object.name}')
sddc_adapter.sddc_health_objects.append(sddc_object)
return sddc_adapter_instances
def provide_vcenters(self) -> dict:
tree = dict()
for target in self.vcenter_dict:
vcenter_adapter_list = self.vcenter_dict[target]
if not vcenter_adapter_list:
continue
tree[target] = dict()
for vcenter in vcenter_adapter_list:
for dc in vcenter.datacenter:
tree[vcenter.target][vcenter.uuid] = {
'uuid': vcenter.uuid,
'name': vcenter.name,
'kind_dc_name': dc.name,
'kind_dc_uuid': dc.uuid,
'target': vcenter.target,
'token': vcenter.token,
}
self.amount_resources[target]['vcenters'] = len(tree[target])
self.iterated_inventory[str(self.iteration)]['vcenters'] = tree
return tree
def provide_datacenters(self) -> dict:
tree = dict()
for target in self.vcenter_dict:
vcenter_adapter_list = self.vcenter_dict[target]
if not vcenter_adapter_list:
continue
tree[target] = dict()
for vcenter in vcenter_adapter_list:
for dc in vcenter.datacenter:
tree[vcenter.target][dc.uuid] = {
'uuid': dc.uuid,
'name': dc.name,
'internal_name': dc.internal_name,
'parent_vcenter_uuid': vcenter.uuid,
'parent_vcenter_name': vcenter.name,
'vcenter': vcenter.name,
'target': vcenter.target,
'token': vcenter.token,
}
self.amount_resources[target]['datacenters'] = len(tree[target])
self.iterated_inventory[str(self.iteration)]['datacenters'] = tree
return tree
def provide_datastores(self) -> dict:
tree = dict()
for target in self.vcenter_dict:
vcenter_adapter_list = self.vcenter_dict[target]
if not vcenter_adapter_list:
continue
tree[target] = dict()
for vcenter in vcenter_adapter_list:
for dc in vcenter.datacenter:
for datastore in dc.datastores:
tree[vcenter.target][datastore.uuid] = {
'uuid': datastore.uuid,
'name': datastore.name,
'internal_name': datastore.internal_name,
'parent_dc_uuid': dc.uuid,
'parent_dc_name': dc.name,
'type': datastore.type,
'vcenter': vcenter.name,
'target': vcenter.target,
'token': vcenter.token,
}
for sc in dc.storagepod:
for datastore in sc.datastores:
tree[vcenter.target][datastore.uuid] = {
'uuid': datastore.uuid,
'name': datastore.name,
'internal_name': datastore.internal_name,
'parent_dc_uuid': dc.uuid,
'parent_dc_name': dc.name,
'storage_cluster_name': sc.name,
'storage_cluster_uuid': sc.uuid,
'type': datastore.type,
'vcenter': vcenter.name,
'target': vcenter.target,
'token': vcenter.token,
}
self.amount_resources[target]['datastores'] = len(tree[target])
self.iterated_inventory[str(self.iteration)]['datastores'] = tree
return tree
def provide_SDRS_clusters(self) -> dict:
tree = dict()
for target in self.vcenter_dict:
vcenter_adapter_list = self.vcenter_dict[target]
if not vcenter_adapter_list:
continue
tree[target] = dict()
for vcenter in vcenter_adapter_list:
for dc in vcenter.datacenter:
for sc in dc.storagepod:
tree[vcenter.target][sc.uuid] = {
'uuid': sc.uuid,
'name': sc.name,
'internal_name': sc.internal_name,
'parent_dc_uuid': dc.uuid,
'parent_dc_name': dc.name,
'vcenter': vcenter.name,
'target': vcenter.target,
'token': vcenter.token,
}
self.amount_resources[target]['storagepod'] = len(tree[target])
self.iterated_inventory[str(self.iteration)]['storagepod'] = tree
return tree
def provide_clusters(self) -> dict:
tree = dict()
for target in self.vcenter_dict:
vcenter_adapter_list = self.vcenter_dict[target]
if not vcenter_adapter_list:
continue
tree[target] = dict()
for vcenter in vcenter_adapter_list:
for dc in vcenter.datacenter:
for cluster in dc.clusters:
tree[vcenter.target][cluster.uuid] = {
'uuid': cluster.uuid,
'name': cluster.name,
'internal_name': cluster.internal_name,
'parent_dc_uuid': dc.uuid,
'parent_dc_name': dc.name,
'vcenter': vcenter.name,
'target': vcenter.target,
'token': vcenter.token,
}
self.amount_resources[target]['clusters'] = len(tree[target])
self.iterated_inventory[str(self.iteration)]['clusters'] = tree
return tree
def provide_hosts(self) -> dict:
tree = dict()
for target in self.vcenter_dict:
vcenter_adapter_list = self.vcenter_dict[target]
if not vcenter_adapter_list:
continue
tree[target] = dict()
for vcenter in vcenter_adapter_list:
for dc in vcenter.datacenter:
for cluster in dc.clusters:
for host in cluster.hosts:
tree[vcenter.target][host.uuid] = {
'uuid': host.uuid,
'name': host.name,
'internal_name': host.internal_name,
'parent_cluster_uuid': cluster.uuid,
'parent_cluster_name': cluster.name,
'datacenter': dc.name,
'vcenter': vcenter.name,
'target': vcenter.target,
'token': vcenter.token,
}
self.amount_resources[target]['hosts'] = len(tree[target])
self.iterated_inventory[str(self.iteration)]['hosts'] = tree
return tree
def provide_vms(self) -> dict:
tree = dict()
for target in self.vcenter_dict:
vcenter_adapter_list = self.vcenter_dict[target]
if not vcenter_adapter_list:
continue
tree[target] = dict()
for vcenter in vcenter_adapter_list:
for dc in vcenter.datacenter:
for cluster in dc.clusters:
for host in cluster.hosts:
for vm in host.vms:
tree[vcenter.target][vm.uuid] = {
'uuid': vm.uuid,
'name': vm.name,
'internal_name': vm.internal_name,
'instance_uuid': vm.instance_uuid,
'parent_host_uuid': host.uuid,
'parent_host_name': host.name,
'cluster': cluster.name,
'datacenter': dc.name,
'vcenter': vcenter.name,
'target': vcenter.target,
'token': vcenter.token,
}
self.amount_resources[target]['vms'] = len(tree[target])
self.iterated_inventory[str(self.iteration)]['vms'] = tree
return tree
def provide_distributed_vswitches(self) -> dict:
tree = dict()
for target in self.vcenter_dict:
vcenter_adapter_list = self.vcenter_dict[target]
if not vcenter_adapter_list:
continue
tree[target] = dict()
for vcenter in vcenter_adapter_list:
for dc in vcenter.datacenter:
for dvs in dc.dvss:
tree[vcenter.target][dvs.uuid] = {
'uuid': dvs.uuid,
'name': dvs.name,
'parent_dc_uuid': dc.uuid,
'parent_dc_name': dc.name,
'vcenter': vcenter.name,
'target': vcenter.target,
'token': vcenter.token,
}
self.amount_resources[target]['distributed_virtual_switches'] = len(tree[target])
self.iterated_inventory[str(self.iteration)]['distributed_virtual_switches'] = tree
return tree
def provide_nsxt_adapter(self) -> dict:
tree = dict()
for target in self.nsxt_dict:
nsxt_adapter_list = self.nsxt_dict[target]
if not nsxt_adapter_list:
continue
tree[target] = dict()
for nsxt_adapter in nsxt_adapter_list:
tree[target][nsxt_adapter.uuid] = {
'uuid': nsxt_adapter.uuid,
'name': nsxt_adapter.name,
'target': nsxt_adapter.target,
'token': nsxt_adapter.token,
}
self.amount_resources[target]['nsxt_adapter'] = len(tree[target])
self.iterated_inventory[str(self.iteration)]['nsxt_adapter'] = tree
return tree
def provide_nsxt_mgmt_cluster(self) -> dict:
tree = dict()
for target in self.nsxt_dict:
nsxt_adapter_list = self.nsxt_dict[target]
if not nsxt_adapter_list:
continue
tree[target] = dict()
for nsxt_adapter in nsxt_adapter_list:
for mgmt_cluster in nsxt_adapter.mgmt_cluster:
tree[target][mgmt_cluster.uuid] = {
'uuid': mgmt_cluster.uuid,
'name': mgmt_cluster.name,
'nsxt_adapter_name': nsxt_adapter.name,
'nsxt_adapter_uuid': nsxt_adapter.uuid,
'target': nsxt_adapter.target,
'token': nsxt_adapter.token,
}
self.amount_resources[target]['nsxt_mgmt_cluster'] = len(tree[target])
self.iterated_inventory[str(self.iteration)]['nsxt_mgmt_cluster'] = tree
return tree
def provide_nsxt_mgmt_nodes(self) -> dict:
tree = dict()
for target in self.nsxt_dict:
nsxt_adapter_list = self.nsxt_dict[target]
if not nsxt_adapter_list:
continue
tree[target] = dict()
for nsxt_adapter in nsxt_adapter_list:
for mgmt_cluster in nsxt_adapter.mgmt_cluster:
for mgmt_node in mgmt_cluster.mgmt_nodes:
tree[target][mgmt_node.uuid] = {
'uuid': mgmt_node.uuid,
'name': mgmt_node.name,
'mgmt_cluster_name': mgmt_cluster.name,
'mgmt_cluster_uuid': mgmt_cluster.uuid,
'nsxt_adapter_name': nsxt_adapter.name,
'nsxt_adapter_uuid': nsxt_adapter.uuid,
'target': nsxt_adapter.target,
'token': nsxt_adapter.token,
}
self.amount_resources[target]['nsxt_mgmt_nodes'] = len(tree[target])
self.iterated_inventory[str(self.iteration)]['nsxt_mgmt_nodes'] = tree
return tree
def provide_nsxt_mgmt_service(self) -> dict:
tree = dict()
for target in self.nsxt_dict:
nsxt_adapter_list = self.nsxt_dict[target]
if not nsxt_adapter_list:
continue
tree[target] = dict()
for nsxt_adapter in nsxt_adapter_list:
for mgmt_cluster in nsxt_adapter.mgmt_cluster:
for mgmt_node in mgmt_cluster.mgmt_nodes:
for mgmt_service in mgmt_node.nsxt_mgmt_services:
tree[nsxt_adapter.target][mgmt_service.uuid] = {
'uuid': mgmt_service.uuid,
'name': mgmt_service.name,
'nsxt_adapter_name': nsxt_adapter.name,
'nsxt_adapter_uuid': nsxt_adapter.uuid,
'mgmt_cluster_name': mgmt_cluster.name,
'mgmt_cluster_uuid': mgmt_cluster.uuid,
'mgmt_node_name': mgmt_node.name,
'mgmt_node_uuid': mgmt_node.uuid,
'target': nsxt_adapter.target,
'token': nsxt_adapter.token,
}
self.amount_resources[target]['nsxt_mgmt_service'] = len(tree[target])
self.iterated_inventory[str(self.iteration)]['nsxt_mgmt_service'] = tree
return tree
def provide_nsxt_transport_nodes(self) -> dict:
tree = dict()
for target in self.nsxt_dict:
nsxt_adapter_list = self.nsxt_dict[target]
if not nsxt_adapter_list:
continue
tree[target] = dict()
for nsxt_adapter in nsxt_adapter_list:
for mgmt_cluster in nsxt_adapter.mgmt_cluster:
for transport_zone in mgmt_cluster.transport_zones:
for transport_node in transport_zone.nsxt_transport_nodes:
tree[nsxt_adapter.target][transport_node.uuid] = {
'uuid': transport_node.uuid,
'name': transport_node.name,
'nsxt_adapter_name': nsxt_adapter.name,
'nsxt_adapter_uuid': nsxt_adapter.uuid,
'mgmt_cluster_name': mgmt_cluster.name,
'mgmt_cluster_uuid': mgmt_cluster.uuid,
'transport_zone_name': transport_zone.name,
'transport_zone_uuid': transport_zone.uuid,
'target': nsxt_adapter.target,
'token': nsxt_adapter.token,
}
self.amount_resources[target]['nsxt_transport_nodes'] = len(tree[target])
self.iterated_inventory[str(self.iteration)]['nsxt_transport_nodes'] = tree
return tree
def provide_nsxt_logical_switches(self) -> dict:
tree = dict()
for target in self.nsxt_dict:
nsxt_adapter_list = self.nsxt_dict[target]
if not nsxt_adapter_list:
continue
tree[target] = dict()
for nsxt_adapter in nsxt_adapter_list:
for mgmt_cluster in nsxt_adapter.mgmt_cluster:
for logical_switch in mgmt_cluster.logical_switches:
tree[target][logical_switch.uuid] = {
'uuid': logical_switch.uuid,
'name': logical_switch.name,
'mgmt_cluster_name': mgmt_cluster.name,
'mgmt_cluster_uuid': mgmt_cluster.uuid,
'nsxt_adapter_name': nsxt_adapter.name,
'nsxt_adapter_uuid': nsxt_adapter.uuid,
'target': nsxt_adapter.target,
'token': nsxt_adapter.token,
}
self.amount_resources[target]['nsxt_logical_switches'] = len(tree[target])
self.iterated_inventory[str(self.iteration)]['nsxt_logical_switches'] = tree
return tree
def provide_vcops_objects(self) -> dict:
tree = dict()
for target in self.vcops_dict:
vcops_adapter = self.vcops_dict[target]
if not vcops_adapter:
continue
tree[target] = dict()
for vcops_object in vcops_adapter.vcops_objects:
tree[target][vcops_object.uuid] = {
'uuid': vcops_object.uuid,
'name': vcops_object.name,
'resourcekind': vcops_object.resourcekind,
'target': target
}
self.iterated_inventory[str(self.iteration)]['vcops_objects'] = tree
return tree
def provide_sddc_objects(self) -> dict:
tree = dict()
for target in self.sddc_dict:
sddc_adapter_instances = self.sddc_dict[target]
if not sddc_adapter_instances:
continue
tree[target] = dict()
for sddc_health_adapter in sddc_adapter_instances:
for sddc_object in sddc_health_adapter.sddc_health_objects:
tree[target][sddc_object.uuid] = {
'uuid': sddc_object.uuid,
'name': sddc_object.name,
'resourcekind': sddc_object.resourcekind,
'target': target
}
self.iterated_inventory[str(self.iteration)]['sddc_objects'] = tree
return tree