-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathperfdata.py
1499 lines (1211 loc) · 70.3 KB
/
perfdata.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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#plotly libraries
import plotly
from plotly.graph_objs import Scatter, Layout
import plotly.graph_objs as go
# vCenter libraries
from pyVim import connect
from pyVim.connect import SmartConnect, Disconnect
import datetime
from pyVmomi import vim
from datetime import timedelta, datetime
import ssl
#s = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
#s.verify_mode = ssl.CERT_NONE
s = ssl._create_unverified_context()
class PerfData:
def __init__(self):
self.c = SmartConnect(host="", user="", pwd="", sslContext=s)
self.content = self.c.RetrieveContent()
# Get all the performance counters
self.perf_dict = {}
self.perfList = self.content.perfManager.perfCounter
for counter in self.perfList:
counter_full = "{}.{}.{}".format(counter.groupInfo.key, counter.nameInfo.key, counter.rollupType)
self.perf_dict[counter_full] = counter.key
# Get metric values
def BuildQuery(self, content, CounterId, interval, instance, entity, startTime, endTime):
perfManager = content.perfManager
metricId = vim.PerformanceManager.MetricId(counterId=CounterId, instance=instance)
query = vim.PerformanceManager.QuerySpec(intervalId=interval,
entity=entity,
metricId=[metricId],
startTime=startTime,
endTime=endTime)
perfResults = perfManager.QueryPerf(querySpec=[query])
return perfResults
# Get real-time metric values
def BuildQuery_RealTime(self, content, CounterId, interval, instance, entity):
perfManager = content.perfManager
metricId = vim.PerformanceManager.MetricId(counterId=CounterId, instance=instance)
query = vim.PerformanceManager.QuerySpec(intervalId=interval,
entity=entity,
metricId=[metricId])
perfResults = perfManager.QueryPerf(querySpec=[query])
return perfResults
def StatCheck(self, perf_dict, counter_name):
counter_key = perf_dict[counter_name]
return counter_key
# Function to get metric values when there is only one instance in perfResults
def get_values(self, perfResults):
values = perfResults[0].value[0].value
times = []
for val in perfResults[0].sampleInfo:
times.append(val.timestamp)
return times, values
# Function to get instances (LUNs) and their metric values
def get_instance_values(self, perfResults):
lun_instances = {}
values = perfResults[0].value
for val_instances in values:
lun_instances[val_instances.id.instance] = val_instances.value
times = []
for val in perfResults[0].sampleInfo:
times.append(val.timestamp)
return times, lun_instances
# Function to get instances (LUNs) and their metric values
def get_network_values(self, perfResults):
network_usage_values = []
values = perfResults[0].value
for val_instances in values:
if val_instances.id.instance == '':
network_usage_values = val_instances.value
times = []
for val in perfResults[0].sampleInfo:
times.append(val.timestamp)
return times, network_usage_values
def get_metric(self, identifier, element, metric, interval, get_max=False):
# identifier - it's the name of the object to get the metric from. There is a specific case where identifier is the object itself
# element - a number to define if it's a host, vm, cluster
# metric - the metric to get the information from
# interval - interval where to get the metric from
identifier = identifier
search_index = self.content.searchIndex
if element == 1:
# Look for VM
object = search_index.FindByUuid(uuid=identifier, vmSearch=True, instanceUuid=True)
elif element == 2:
# Look for ESXi host
object = search_index.FindByDnsName(dnsName=identifier, vmSearch=False)
elif element == 3:
# look for cluster
datacenters = self.content.rootFolder.childEntity
for datacenter in datacenters: # Iterate through DataCenters
clusters = datacenter.hostFolder.childEntity
for cluster in clusters:
if cluster.name == identifier:
object = cluster
elif element == 4:
# the object was passed in the argument of the method
object = identifier
# INTERVALS
# ONE DAY
end_time = datetime.today() - timedelta(days=1)
sampling_period = 300
if interval == '2':
# ONE WEEK - 5 minutes sampling period
end_time = datetime.today() - timedelta(weeks=1)
sampling_period = 1800
elif interval == '3':
# ONE MONTH - 2 hours sampling period
end_time = datetime.today() - timedelta(weeks=4)
sampling_period = 7200
elif interval == '4':
# ONE YEAR or Historic Interval - 1 day sampling period
end_time = datetime.today() - timedelta(days=365)
sampling_period = 86400
metric_object = self.BuildQuery(self.content, (self.StatCheck(self.perf_dict, metric)), sampling_period, "", object,
end_time, datetime.today())
(metric_times, metric_values) = self.get_values(metric_object)
# GET MAX VALUE OF MEMORY
if get_max == True:
if element == 1:
# get max value for VM
max_value = object.summary.config.memorySizeMB
elif element == 2:
# get max value for ESXi host
max_value = object.systemResources.config.memoryAllocation.limit
else:
# get max value for cluster
max_value = object.summary.effectiveMemory
return metric_times, metric_values, max_value
return metric_times, metric_values
def get_graph(self, identifier, element, metric, interval, title, conversion):
if conversion == 3:
# Get max value also in this case
get_max = True
time_values, metric_values, max_value = self.get_metric(identifier, element, metric, interval, get_max)
max_value = round(max_value / 1024)
else:
time_values, metric_values = self.get_metric(identifier, element, metric, interval)
if conversion == 1:
# The metric in % needs to be divided by 100
metric_values = [x / 100 for x in metric_values]
elif conversion == 2:
# Convert metric from KBps to Mbps
metric_values = [(x * 8) / 1024 for x in metric_values]
elif conversion == 3:
# Convert metric from KB to GB
metric_values = [((x / 1024) / 1024) for x in metric_values]
data = [
go.Scatter(
x=time_values,
y=metric_values
)
]
if conversion == 1:
layout = go.Layout(
margin=dict(t=20, l=40, r=40, b=40),
showlegend=False,
height=300,
xaxis=dict(
title="Date",
autorange=True
),
yaxis=dict(
title="Percentage",
rangemode='tozero',
range=[0, 100]
)
)
elif conversion == 2:
layout = go.Layout(
margin=dict(t=20, l=40, r=40, b=40),
showlegend=False,
height=300,
xaxis=dict(
title="Date",
autorange=True
),
yaxis=dict(
title="Mbps",
autorange=True,
rangemode='tozero'
)
)
elif conversion == 3:
layout = go.Layout(
margin=dict(t=20, l=40, r=40, b=40),
showlegend=False,
height=300,
xaxis=dict(
title="Date",
autorange=True
),
yaxis=dict(
title='GB',
range=[0, max_value]
)
)
else:
layout = go.Layout(
margin=dict(t=20, l=40, r=40, b=40),
showlegend=False,
height=300,
xaxis=dict(
title="Date",
autorange=True
),
yaxis=dict(
title="KBps",
autorange=True,
rangemode='tozero'
)
)
fig = go.Figure(data=data, layout=layout)
graph = plotly.offline.plot(fig, auto_open=False, output_type='div', show_link=False)
return graph
def get_graph_cluster_network_disk(self, cluster_name, interval):
total_network_usage = []
network_metric_times = []
total_disk_usage = []
disk_metric_times = []
# INTERVALS
# ONE DAY
end_time = datetime.today() - timedelta(days=1)
sampling_period = 300
if interval == '2':
# ONE WEEK - 5 minutes sampling period
end_time = datetime.today() - timedelta(weeks=1)
sampling_period = 1800
elif interval == '3':
# ONE MONTH - 2 hours sampling period
end_time = datetime.today() - timedelta(weeks=4)
sampling_period = 7200
elif interval == '4':
# ONE YEAR or Historic Interval - 1 day sampling period
end_time = datetime.today() - timedelta(days=365)
sampling_period = 86400
datacenters = self.content.rootFolder.childEntity
for datacenter in datacenters: # Iterate through DataCenters
clusters = datacenter.hostFolder.childEntity
for cluster in clusters: # Iterate through the clusters in DC
if cluster.name == cluster_name:
hosts = cluster.host
for host in hosts: # Iterate through Hosts in the cluster
# GET Network Usage [KBps]
metric_object_network = self.BuildQuery(self.content, (self.StatCheck(self.perf_dict, 'net.usage.average')), sampling_period, "", host,
end_time, datetime.today())
(network_metric_times, host_network_values) = self.get_values(metric_object_network)
if total_network_usage:
# It is not empty
temp_list = [(x + y) for x, y in zip(total_network_usage, host_network_values)]
total_network_usage = list(temp_list)
else:
total_network_usage = list(host_network_values)
# GET Disk [KBps]
metric_object = self.BuildQuery(self.content, (self.StatCheck(self.perf_dict, 'disk.usage.average')), sampling_period, "", host,
end_time, datetime.today())
(disk_metric_times, host_disk_values) = self.get_values(metric_object)
if total_disk_usage:
# It is not empty
temp_list = [(x + y) for x, y in zip(total_disk_usage, host_disk_values)]
total_disk_usage = list(temp_list)
else:
total_disk_usage = list(host_disk_values)
network_data = [
go.Scatter(
x=network_metric_times,
y=total_network_usage
)
]
disk_data = [
go.Scatter(
x=disk_metric_times,
y=total_disk_usage
)
]
layout = go.Layout(
margin=dict(t=20, l=40, r=40, b=40),
showlegend=False,
height=300,
xaxis=dict(
title="Date",
autorange=True
),
yaxis=dict(
title="KBps",
autorange=True,
rangemode='tozero'
)
)
fig_network = go.Figure(data=network_data, layout=layout)
graph_network = plotly.offline.plot(fig_network, auto_open=False, output_type='div', show_link=False)
fig_disk = go.Figure(data=disk_data, layout=layout)
graph_disk= plotly.offline.plot(fig_disk, auto_open=False, output_type='div', show_link=False)
return graph_network, graph_disk
# Function to get values for doughnut graphs (Top memory, cpu and storage)
def get_pie_graph(self, metric, total_value, consumed_value):
if metric == 'MEMORY':
labels = ['Consumed Memory', 'Free Memory']
elif metric == 'CPU':
labels = ['Consumed CPU', 'Free CPU']
elif metric == 'STORAGE':
labels = ['Used Storage', 'Free Storage']
values = [consumed_value, (total_value - consumed_value)]
fig = {
'data': [
{
'labels': labels,
'values': values,
'type': 'pie',
'name': 'Memory'
}
],
'layout': {'showlegend': False, 'width': 300, 'height': 300}
}
graph = plotly.offline.plot(fig, auto_open=False, output_type='div')
return graph
# Function to get values and graph metrics of all clusters together
def get_graph_cumulative(self, metric, interval, convert, title):
datacenters = self.content.rootFolder.childEntity
y_cluster_values = []
time_x = []
max_len_time_x = 0
for datacenter in datacenters: # Iterate through DataCenters
clusters = datacenter.hostFolder.childEntity
for cluster in clusters:
values = []
metric_time = []
# Get Storage Usage in the cluster
if title == 'Storage Usage':
datastores = cluster.datastore
for datastore in datastores:
(metric_time_1, metric_values) = self.get_metric(datastore, 4, metric, interval)
if values:
# It is not empty
temp_list = [(x + y) for x, y in zip(values, metric_values)]
values = list(temp_list)
else:
values = list(metric_values)
metric_time = list(metric_time_1)
# Convert values from KB to GB
values = [round(((x / 1024) / 1024), 2) for x in values]
else:
(metric_time, metric_values) = self.get_metric(cluster, 4, metric, interval)
# convert from KB to GB
if convert == 1:
values = [round(((x / 1024) / 1024), 2) for x in metric_values]
# convert from MHz to GHz
if convert == 2:
values = [round((x / 1000), 2) for x in metric_values]
cluster_values = {'name': cluster.name, 'values': values}
y_cluster_values.append(cluster_values.copy())
# keep only the largest time_x (metric_time)
len_time = len(metric_time)
if len_time > max_len_time_x:
max_len_time_x = len_time
time_x = metric_time
y_accumulative = []
count = 0
data_graph = []
colors = ['rgb(255, 211, 154)', 'rgb(128, 195, 210)', 'rgb(255, 253, 186)', 'rgb(187, 233, 252)',
'rgb(184, 247, 212)', 'rgb(229, 182, 251)', 'rgb(200, 82, 51)', 'rgb(130, 140, 50)', 'rgb(232, 12, 21)']
for y_value in y_cluster_values:
y_values = y_value['values']
if count == 0:
y_accumulative = y_values
count += 1
else:
y_temp = y_accumulative
# Check size of the list - make both of the same size (largest one)
len_accumulative = len(y_temp)
len_values = len(y_values)
if len_accumulative < len_values:
diff_len = len_values - len_accumulative
list_zeros = [0] * diff_len
y_temp = list_zeros + y_temp
else:
diff_len = len_accumulative - len_values
list_zeros = [0] * diff_len
y_values = list_zeros + y_values
y_accumulative = [y0 + y1 for y0, y1 in zip(y_values, y_temp)]
# Make original values strings and add % for hover text
# This is to show the real value when mouse over in the graph
if convert == 1:
y0_txt = [str(y0) + 'GB' for y0 in y_values]
y_title = 'GB'
if convert == 2:
y0_txt = [str(y0) + 'GHz' for y0 in y_values]
y_title = 'GHz'
if title == 'Storage Usage':
y0_txt = [str(y0) + 'GB' for y0 in y_values]
y_title = 'GB'
trace = go.Scatter(
x=time_x,
y=y_accumulative,
text=y0_txt,
hoverinfo='x+text',
mode='lines',
name=y_value['name'],
line=dict(width=0.5,
color=colors[count - 1]),
fill='tonexty'
)
data_graph.append(trace)
count += 1
layout = go.Layout(
margin=dict(t=20, b=40),
height=400,
xaxis=dict(
title="Date",
autorange=True
),
yaxis=dict(
title=y_title,
autorange=True,
rangemode='tozero'
)
)
fig = go.Figure(data=data_graph, layout=layout)
graph = plotly.offline.plot(fig, auto_open=False, output_type='div', show_link=False)
return graph
# Get clusters, hosts and VMs to create menu
def get_elements(self):
datacenters = self.content.rootFolder.childEntity
list_datacenters = []
for datacenter in datacenters: # Iterate through DataCenters
clusters = datacenter.hostFolder.childEntity
list_clusters = []
for cluster in clusters: # Iterate through the clusters in DC
hosts = cluster.host
list_hosts = []
list_vm = []
for host in hosts: # Iterate through Hosts in the cluster
vms = host.vm
for vm in vms:
try:
dict_vm = {'name': vm.name, 'uuid': vm.config.instanceUuid}
list_vm.append(dict_vm.copy())
except:
pass
list_hosts.append(host.name)
list_vms_sorted = sorted(list_vm, key=lambda i: i['name'])
list_hosts.sort()
dict_cluster = {'name': cluster.name, 'hosts': list_hosts, 'vms': list_vms_sorted}
list_clusters.append(dict_cluster.copy())
dict_datacenter = {datacenter.name: list_clusters}
list_datacenters.append(dict_datacenter.copy())
return list_datacenters
def get_vms(self):
datacenters = self.content.rootFolder.childEntity
vm_list = []
for datacenter in datacenters: # Iterate through DataCenters
clusters = datacenter.hostFolder.childEntity
for cluster in clusters: # Iterate through the clusters in DC
hosts = cluster.host
for host in hosts: # Iterate through Hosts in the cluster
vms = host.vm
for vm in vms:
vm_list.append(vm.name)
return vm_list
# Function to get VMs that are using more resources in the Cluster - CPU, Memory
def get_top_vms_cluster(self, cluster_name, resource):
datacenters = self.content.rootFolder.childEntity
vms_top_name = ['']*5
vms_top_val = [0, 0, 0, 0, 0]
for datacenter in datacenters: # Iterate through DataCenters
clusters = datacenter.hostFolder.childEntity
for cluster in clusters: # Iterate through the clusters in DC
# print ('Cluster: ' + cluster.name)
if cluster.name == cluster_name:
hosts = cluster.host
for host in hosts: # Iterate through Hosts in the cluster
# print ('Host: ' + host.name)
vms = host.vm
for vm in vms:
# GET value of consumed memory [MB] or CPU [MHz] at that moment
if resource == 'MEMORY':
value = vm.summary.quickStats.hostMemoryUsage
else:
value = vm.summary.quickStats.overallCpuUsage
for index, val in enumerate(vms_top_val):
if value > val:
# Insert Value
temp_mem = vms_top_val[index:4]
if resource == 'MEMORY':
# Convert Memory from MB to GB
value = value
else:
# Convert Consumed CPU from MHz to GHz
value = value
vms_top_val[index] = value
vms_top_val = vms_top_val[0:(index + 1)] + temp_mem
# NAMES
temp_name = vms_top_name[index:4]
vms_top_name[index] = vm.name
new_vms_top_name = vms_top_name[0:(index+1)] + temp_name
vms_top_name = new_vms_top_name
break
# Remove not initialized elements from list
top_names = list(filter(lambda a: a != '', vms_top_name))
top_vms = {'name': top_names, 'values': vms_top_val}
return top_vms
# Function to get Hosts that are using more resources in the Cluster - CPU, Memory
def get_top_hosts_cluster(self, cluster_name, resource):
datacenters = self.content.rootFolder.childEntity
hosts_top_name = ['']*5
hosts_top_val = [0, 0, 0, 0, 0]
for datacenter in datacenters: # Iterate through DataCenters
clusters = datacenter.hostFolder.childEntity
for cluster in clusters: # Iterate through the clusters in DC
# print ('Cluster: ' + cluster.name)
if cluster.name == cluster_name:
hosts = cluster.host
for host in hosts: # Iterate through Hosts in the cluster
# GET value of consumed memory [MB] or CPU at that moment
if resource == 'MEMORY':
mem_consumed_val = host.summary.quickStats.overallMemoryUsage
else:
mem_consumed_val = host.summary.quickStats.overallCpuUsage
for index, mem_val in enumerate(hosts_top_val):
if mem_consumed_val > mem_val:
# MEMORY
temp_mem = hosts_top_val[index:4]
hosts_top_val[index] = mem_consumed_val
hosts_top_val = hosts_top_val[0:(index + 1)] + temp_mem
# NAMES
temp_name = hosts_top_name[index:4]
hosts_top_name[index] = host.name
new_hosts_top_name = hosts_top_name[0:(index+1)] + temp_name
hosts_top_name = new_hosts_top_name
break
# Remove not initialized elements from list
top_names = list(filter(lambda a: a != '', hosts_top_name))
top_hosts = {'name': top_names, 'values': hosts_top_val}
return top_hosts
# Function to get Memory, CPU and Storage Capacity in the Datacenter
def get_usage_metrics_datacenter(self):
num_clusters = 0
num_hosts = 0
num_vms = 0
num_datastores = 0
total_memory = 0
total_memory_cluster = 0
total_cpu = 0
total_cpu_cluster = 0
total_consumed_mem = 0
total_consumed_mem_cluster = 0
total_consumed_cpu = 0
total_consumed_cpu_cluster = 0
total_storage = 0
total_storage_cluster = 0
free_storage = 0
free_storage_cluster = 0
total_used_network_cluster = 0
cluster_names = []
cluster_mem_values = []
cluster_cpu_values = []
cluster_storage_values = []
cluster_network_values = []
#Dict
clusters_percents = {}
max_total_values = []
counter_hosts = 0
datacenters = self.content.rootFolder.childEntity
for datacenter in datacenters: # Iterate through DataCenters
clusters = datacenter.hostFolder.childEntity
for cluster in clusters: # Iterate through the clusters in DC
num_clusters += 1
num_hosts += cluster.summary.numHosts
num_vms += cluster.summary.usageSummary.totalVmCount
# GET Total Memory [bytes]
total_memory_cluster = cluster.summary.totalMemory
# Get Total CPU [MHz]
total_cpu_cluster = cluster.summary.totalCpu
# GET Total Consumed Memory and CPU in the cluster
hosts = cluster.host
for host in hosts: # Iterate through Hosts in the cluster
# GET value of consumed memory [MB] and CPU [MHz] at that moment
total_consumed_mem_cluster += host.summary.quickStats.overallMemoryUsage
total_consumed_cpu_cluster += host.summary.quickStats.overallCpuUsage
# Get Network Usage
# ONE DAY
end_time = datetime.today() - timedelta(days=1)
sampling_period = 300
metric_object = self.BuildQuery(self.content, (self.StatCheck(self.perf_dict, 'net.usage.average')), sampling_period, "*", host,
end_time, datetime.today())
(times, network_usage_values) = self.get_values(metric_object)
# Get the average for network usage
counter = 0
total = 0
for val in network_usage_values:
counter += 1
total = total + val
network_usage_values_average = total / counter
total_used_network_cluster += network_usage_values_average
# GET MAX Latency values (Only query one host - supposing same datastores connected to all hosts)
if counter_hosts == 0:
metric = self.BuildQuery_RealTime(self.content, (self.StatCheck(self.perf_dict, 'disk.maxTotalLatency.latest')), 20, "*", host)
(times, max_total_values) = self.get_values(metric)
counter_hosts = 1
counter_hosts = 0
# Get list of Datastores in the Cluster
datastores = cluster.datastore
num_datastores += len(datastores)
for datastore in datastores: # Iterate through Datastores in the cluster
# Get Total Storage [Bytes] and Free Storage [Bytes] and add them to total cluster metric
total_storage_cluster += datastore.summary.capacity
free_storage_cluster += datastore.summary.freeSpace
# Get values for Pie Charts graphs
pie_consumed_memory = round(total_consumed_mem_cluster / 1024, 2) # GB
pie_consumed_cpu = round(total_consumed_cpu_cluster / 1000, 3) # GHz
temp_consumed_storage = total_storage_cluster - free_storage_cluster
pie_consumed_storage = round((((temp_consumed_storage / 1024) / 1024) / 1024), 2) # GB
pie_consumed_network = round(total_used_network_cluster) # KBps
cluster_names.append(cluster.name)
cluster_mem_values.append({'cluster_name': cluster.name, 'value': pie_consumed_memory})
cluster_cpu_values.append({'cluster_name': cluster.name, 'value': pie_consumed_cpu})
cluster_storage_values.append({'cluster_name': cluster.name, 'value': pie_consumed_storage})
cluster_network_values.append({'cluster_name': cluster.name, 'value': pie_consumed_network})
# End Pie Charts values
# Get values for Line Charts
percent_cpu_used_cluster = round((total_consumed_cpu_cluster / total_cpu_cluster) * 100)
# Convert Total Memory in the cluster from bytes to MB
total_memory_cluster_mb = round((total_memory_cluster / 1024) / 1024, 2)
percent_memory_consumed_cluster = round((total_consumed_mem_cluster / total_memory_cluster_mb) * 100)
percent_used_storage_cluster = round((temp_consumed_storage / total_storage_cluster) * 100)
# Get the average for Max_Total_Latency
counter = 0
total = 0
for val in max_total_values:
counter += 1
total += val
max_latency_average = total / counter
# The total disk average latency should be below 20ms.
# In this case I am using 25 as the maximum for the line graph
if max_latency_average <= 25:
max_latency_average_percent = round((max_latency_average / 25) * 100)
else:
max_latency_average_percent = 100
value_percent = {'cpu': percent_cpu_used_cluster, 'memory': percent_memory_consumed_cluster,
'storage': percent_used_storage_cluster, 'max_average_latency_percent': max_latency_average_percent,
'max_latency_value': round(max_latency_average)}
clusters_percents[cluster.name] = value_percent
# END Line Charts values
total_memory += total_memory_cluster
total_cpu += total_cpu_cluster
total_consumed_mem += total_consumed_mem_cluster
total_consumed_cpu += total_consumed_cpu_cluster
total_storage = total_storage + total_storage_cluster
free_storage = free_storage + free_storage_cluster
# Reset cluster variables
total_consumed_mem_cluster = 0
total_consumed_cpu_cluster = 0
total_storage_cluster = 0
free_storage_cluster = 0
total_used_network_cluster = 0
# Calculate CPU % before doing conversions
percent_consumed_cpu = round((total_consumed_cpu / total_cpu) * 100)
# Convert Consumed Memory from MB to GB
total_consumed_mem = round(total_consumed_mem / 1024, 2)
# Convert Total CPU from MHz to GHz
total_cpu = round(total_cpu / 1000, 2)
# Convert Total Memory from bytes to GB
total_memory = round(((total_memory / 1024) / 1024) / 1024, 2)
# Calculate free memory
free_memory = round(total_memory - total_consumed_mem, 2)
# Convert Consumed CPU from MHz to GHz
total_consumed_cpu = round(total_consumed_cpu / 1000, 3)
# Calculate free cpu
free_cpu = round(total_cpu - total_consumed_cpu, 2)
# Convert Total and Free Storage from Bytes to TB
total_storage = round(((((total_storage / 1024) / 1024) / 1024) / 1024), 2)
free_storage = round(((((free_storage / 1024) / 1024) / 1024) / 1024), 2)
# Calculate Memory %
percent_consumed_mem = round((total_consumed_mem / total_memory) * 100)
used_storage = round(total_storage - free_storage, 2)
percent_used_storage = round((used_storage / total_storage) * 100)
# SORT PIE VALUES - TOP 5
cluster_mem_values_sorted = sorted(cluster_mem_values, key=lambda i: i['value'], reverse=True)
cluster_cpu_values_sorted = sorted(cluster_cpu_values, key=lambda i: i['value'], reverse=True)
cluster_storage_values_sorted = sorted(cluster_storage_values, key=lambda i: i['value'], reverse=True)
cluster_network_values_sorted = sorted(cluster_network_values, key=lambda i: i['value'], reverse=True)
return_value = {'total_memory': total_memory, 'total_consumed_mem': total_consumed_mem,
'free_memory': free_memory, 'percent_consumed_mem': percent_consumed_mem,
'total_cpu': total_cpu, 'total_consumed_cpu': total_consumed_cpu,
'free_cpu': free_cpu, 'percent_consumed_cpu': percent_consumed_cpu,
'total_storage': total_storage, 'free_storage': free_storage,
'used_storage': used_storage, 'percent_used_storage': percent_used_storage,
'cluster_names': cluster_names, 'cluster_mem_values': cluster_mem_values_sorted[:5],
'cluster_cpu_values': cluster_cpu_values_sorted[:5], 'cluster_storage_values': cluster_storage_values_sorted[:5],
'cluster_network_values': cluster_network_values_sorted[:5], 'clusters_percents': clusters_percents,
'num_clusters': num_clusters, 'num_hosts': num_hosts, 'num_vms': num_vms, 'num_datastores': num_datastores}
return return_value
# Function to get Memory, CPU and Storage Capacity in the Cluster, also RealTime graph values
def get_usage_metrics_cluster(self, object_name):
total_memory = 0
total_cpu = 0
total_consumed_mem = 0
total_consumed_cpu = 0
total_active_memory = []
active_memory_time = []
total_ballooned_memory = []
total_swapused_memory = []
total_cpu_usage_percent = []
total_cpu_readiness_percent = []
total_network_transmitted_rate = []
total_network_received_rate = []
total_disks_read_rate = []
total_disks_write_rate = []
total_storage = 0
free_storage = 0
total_power = 0
counter_hosts = 0
lun_instances = {}
times = []
datastores_latency = {}
datastores_lun_name = {}
datastores_usage_dict = {}
datacenters = self.content.rootFolder.childEntity
for datacenter in datacenters: # Iterate through DataCenters
# print ('Data Center: ' + datacenter.name)
clusters = datacenter.hostFolder.childEntity
for cluster in clusters: # Iterate through the clusters in DC
# print ('Cluster: ' + cluster.name)
if cluster.name == object_name:
num_hosts = cluster.summary.numHosts
num_vms = cluster.summary.usageSummary.totalVmCount
# Check if HA and DRS are enabled
if cluster.configuration.dasConfig.enabled:
ha = 'ON'
else:
ha = 'OFF'
if cluster.configuration.drsConfig.enabled:
drs = 'ON'
else:
drs = 'OFF'
# GET Total Memory [bytes]
total_memory = cluster.summary.totalMemory
# Get Total CPU [MHz]
total_cpu = cluster.summary.totalCpu
# GET Total Consumed Memory and CPU in the cluster
hosts = cluster.host
for host in hosts: # Iterate through Hosts in the cluster
# Get power
metric_object = self.BuildQuery_RealTime(self.content, self.perf_dict['power.power.average'],
20, "*", host)
values = metric_object[0].value[0].value
power_average = sum(values) / len(values)
total_power += power_average
# GET value of consumed memory [MB] and CPU [MHz] at that moment
total_consumed_mem = total_consumed_mem + host.summary.quickStats.overallMemoryUsage
total_consumed_cpu = total_consumed_cpu + host.summary.quickStats.overallCpuUsage
# GET ACTIVE MEMORY
metric_active_memory = self.BuildQuery_RealTime(self.content, (self.StatCheck(self.perf_dict, 'mem.active.average')), 20, "", host)
(memory_time, active_memory_values) = self.get_values(metric_active_memory)
if total_active_memory:
# It is not empty
temp_list = [(x + y) for x, y in zip(total_active_memory, active_memory_values)]
total_active_memory = list(temp_list)
else:
total_active_memory = list(active_memory_values)
active_memory_time = list(memory_time)
# GET BALLOONED MEMORY
metric_balloned_memory = self.BuildQuery_RealTime(self.content, (self.StatCheck(self.perf_dict, 'mem.vmmemctl.average')), 20, "", host)
(memory_time, ballooned_memory_values) = self.get_values(metric_balloned_memory)
if total_ballooned_memory:
# It is not empty
temp_list = [(x + y) for x, y in zip(total_ballooned_memory, ballooned_memory_values)]
total_ballooned_memory = list(temp_list)
else:
total_ballooned_memory = list(ballooned_memory_values)
# GET SWAPPED MEMORY
metric_swapped_memory = self.BuildQuery_RealTime(self.content, (self.StatCheck(self.perf_dict, 'mem.swapused.average')), 20, "", host)
(memory_time, swapused_memory_values) = self.get_values(metric_swapped_memory)
if total_swapused_memory:
# It is not empty
temp_list = [(x + y) for x, y in zip(total_swapused_memory, swapused_memory_values)]
total_swapused_memory = list(temp_list)
else:
total_swapused_memory = list(swapused_memory_values)
# GET CPU Usage (%)
metric_cpu_usage = self.BuildQuery_RealTime(self.content, (self.StatCheck(self.perf_dict, 'cpu.usage.average')), 20, "", host)
(cpu_time, cpu_usage_values) = self.get_values(metric_cpu_usage)
# The metric in % needs to be divided by 100
cpu_usage_values_percent = [x / 100 for x in cpu_usage_values]
if total_cpu_usage_percent:
# It is not empty
temp_list = [(x + y) for x, y in zip(total_cpu_usage_percent, cpu_usage_values_percent)]
total_cpu_usage_percent = list(temp_list)
else:
total_cpu_usage_percent = list(cpu_usage_values_percent)
# GET CPU Readiness (%)
metric_cpu_readiness = self.BuildQuery_RealTime(self.content, (self.StatCheck(self.perf_dict, 'cpu.readiness.average')), 20, "", host)
(cpu_time, cpu_readiness_values) = self.get_values(metric_cpu_readiness)
# The metric in % needs to be divided by 100
cpu_readiness_values_percent = [x / 100 for x in cpu_readiness_values]
if total_cpu_readiness_percent:
# It is not empty
temp_list = [(x + y) for x, y in zip(total_cpu_readiness_percent, cpu_readiness_values_percent)]
total_cpu_readiness_percent = list(temp_list)
else:
total_cpu_readiness_percent = list(cpu_readiness_values_percent)
# Get Network transmitted rate [KBps]
network_transmitted_rate = self.BuildQuery_RealTime(self.content, (self.StatCheck(self.perf_dict, 'net.transmitted.average')), 20, "", host)
(network_time, network_transmitted_rate_values) = self.get_values(network_transmitted_rate)
if total_network_transmitted_rate:
# It is not empty
temp_list = [(x + y) for x, y in zip(total_network_transmitted_rate, network_transmitted_rate_values)]
total_network_transmitted_rate = list(temp_list)
else:
total_network_transmitted_rate = list(network_transmitted_rate_values)
# Get Network Received rate [KBps]
network_received_rate = self.BuildQuery_RealTime(self.content, (self.StatCheck(self.perf_dict, 'net.received.average')), 20, "", host)
(network_time, network_received_rate_values) = self.get_values(network_received_rate)
if total_network_received_rate:
# It is not empty
temp_list = [(x + y) for x, y in zip(total_network_received_rate, network_received_rate_values)]
total_network_received_rate = list(temp_list)
else:
total_network_received_rate = list(network_received_rate_values)
# GET LUNs Latency Values (Only query one host - supposing same datastores connected to all hosts)
# GET READ RATE (Rate at which data is read from each LUN on the hos)
# GET WRITE RATE (Rate at which data is written to each LUN on the host)
if counter_hosts == 0:
metric = self.BuildQuery_RealTime(self.content, (self.StatCheck(self.perf_dict, 'disk.totalLatency.average')), 20, "*", host)
(times, lun_instances) = self.get_instance_values(metric)
# Look for the datastore name for the specific Disk name (naa.24324...)