-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnarf.py
More file actions
executable file
·2053 lines (1789 loc) · 82.5 KB
/
narf.py
File metadata and controls
executable file
·2053 lines (1789 loc) · 82.5 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author: harold.gutierrez@nutanix.com
#
# Report cluster activity from arithmos
#
from __future__ import division
import sys # noqa: E402
sys.path.insert(0, '/usr/local/nutanix/bin/') # noqa: E402
import os
import signal
import uuid
import curses
import argparse
import datetime
import time
import env
from util.interfaces.interfaces import NutanixInterfaces # noqa: E402
from stats.arithmos.interface.arithmos_type_pb2 import * # noqa: E402
from stats.arithmos.interface.arithmos_interface_pb2 import (
AgentGetEntitiesArg, MasterGetEntitiesArg) # noqa: E402
from serviceability.interface.analytics.arithmos_rpc_client import (
ArithmosDataProcessing) # noqa: E402
# ~~~ Report fields definitions ~~~
# Arithmos fields are used by the report methods in the reporter classes,
# These fields needs to be mapped by the _get_*_dic() methods in reporters.
# The reason this atributes needs to be mapped is because some are
# transformed, for example "hypervisor_cpu_usage_ppm" is divided by 10000
# and transformed into "hypervisor_cpu_usage_percent".
#
# node_stat.stats.hypervisor_cpu_usage_ppm / 10000
# =
# hypervisor_cpu_usage_percent
#
# Cli fields are used by the UiCli class in the formater methods and
# describe how the information needs to be displayed on console.
# Keys in Cli fields correspond to the ones in dictionaries returned
# by the _get_*dic() methods in reporters.
#
# +--------------+
# | Arithmos | ---> hypervisor_cpu_usage_ppm ---> 89000
# +--------------+
# |
# V
# +--------------+
# | NodeReporter | ---> hypervisor_cpu_usage_percent ---> 89000 / 10000 = 8.9%
# +--------------+
# |
# V
# +--------------+> key: hypervisor_cpu_usage_percent
# | UiCli | ---> header: CPU%
# +--------------+ width: 6 (min width)
# align: > (aligned right)
# format: .2f (2 decimal float)
# |
# |
# V
# +- Console output -------
# | $ ./narf.py -n
# | 2022/01/16-02:13:04 | Node CPU% [...]
# | 2022/01/16-02:13:04 | Prolix1 8.90 [...]
# | [...]
#
#
# TODO: Move report fields definitions to a config file.
# TODO: Classes should also be splitted in modules, at least one module
# for reporters and one for Ui's needs to be created.
# TODO: Think about merging arithmo and CLI stats. This will help in a
# simple report definitaion language for the framework.
#
# ~~~ ~~~
# ========================================================================
# Definition of Nodes reports.
NODES_OVERALL_REPORT_ARITHMOS_FIELDS = (
[
"node_name", "id", "hypervisor_cpu_usage_ppm",
"hypervisor_memory_usage_ppm", "hypervisor_num_iops",
"controller_num_iops", "num_iops",
"io_bandwidth_kBps", "avg_io_latency_usecs"
]
)
NODES_OVERALL_REPORT_CLI_FIELDS = (
[
{"key": "node_name", "header": "Node",
"width": 20, "align": "<", "format": ".20"},
{"key": "hypervisor_cpu_usage_percent", "header": "CPU%",
"width": 6, "align": ">", "format": ".2f"},
{"key": "hypervisor_memory_usage_percent", "header": "MEM%",
"width": 6, "align": ">", "format": ".2f"},
{"key": "hypervisor_num_iops", "header": "hIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "controller_num_iops", "header": "cIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "num_iops", "header": "IOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "io_bandwidth_mBps",
"header": "B/W[MB]", "width": 8, "align": ">", "format": ".2f"},
{"key": "avg_io_latency_msecs",
"header": "LAT[ms]", "width": 8, "align": ">", "format": ".2f"}
]
)
NODES_IOPS_REPORT_ARITHMOS_FIELDS = (
[
"node_name", "id",
"hypervisor_cpu_usage_ppm", "hypervisor_memory_usage_ppm",
"hypervisor_num_iops",
"hypervisor_num_read_iops", "hypervisor_num_write_iops",
"controller_num_iops",
"controller_num_read_iops", "controller_num_write_iops",
"num_iops",
"num_read_iops", "num_write_iops"
]
)
NODES_IOPS_REPORT_CLI_FIELDS = (
[
{"key": "node_name", "header": "Node",
"width": 20, "align": "<", "format": ".20"},
{"key": "hypervisor_cpu_usage_percent", "header": "CPU%",
"width": 6, "align": ">", "format": ".2f"},
{"key": "hypervisor_memory_usage_percent", "header": "MEM%",
"width": 6, "align": ">", "format": ".2f"},
{"key": "hypervisor_num_iops", "header": "hIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "hypervisor_num_read_iops", "header": "hRIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "hypervisor_num_write_iops", "header": "hWIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "controller_num_iops", "header": "cIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "controller_num_read_iops", "header": "cRIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "controller_num_write_iops", "header": "cWIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "num_iops", "header": "IOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "num_read_iops", "header": "RIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "num_write_iops", "header": "WIOPS",
"width": 8, "align": ">", "format": ".2f"}
]
)
NODES_BANDWIDTH_REPORT_ARITHMOS_FIELDS = (
[
"node_name", "id",
"hypervisor_cpu_usage_ppm", "hypervisor_memory_usage_ppm",
"hypervisor_io_bandwidth_kBps",
"hypervisor_read_io_bandwidth_kBps",
"hypervisor_write_io_bandwidth_kBps",
"controller_io_bandwidth_kBps",
"controller_read_io_bandwidth_kBps",
"controller_write_io_bandwidth_kBps",
"io_bandwidth_kBps",
"read_io_bandwidth_kBps",
"write_io_bandwidth_kBps",
]
)
NODES_BANDWIDTH_REPORT_CLI_FIELDS = (
[
{"key": "node_name", "header": "Node",
"width": 20, "align": "<", "format": ".20"},
{"key": "hypervisor_cpu_usage_percent", "header": "CPU%",
"width": 6, "align": ">", "format": ".2f"},
{"key": "hypervisor_memory_usage_percent", "header": "MEM%",
"width": 6, "align": ">", "format": ".2f"},
{"key": "hypervisor_io_bandwidth_mBps",
"header": "hB/W[MB]", "width": 8, "align": ">", "format": ".2f"},
{"key": "hypervisor_read_io_bandwidth_mBps",
"header": "hRB/W[MB]", "width": 8, "align": ">", "format": ".2f"},
{"key": "hypervisor_write_io_bandwidth_mBps",
"header": "hWB/W[MB]", "width": 8, "align": ">", "format": ".2f"},
{"key": "controller_io_bandwidth_mBps",
"header": "cB/W[MB]", "width": 8, "align": ">", "format": ".2f"},
{"key": "controller_read_io_bandwidth_mBps",
"header": "cRB/W[MB]", "width": 8, "align": ">", "format": ".2f"},
{"key": "controller_write_io_bandwidth_mBps",
"header": "cWB/W[MB]", "width": 8, "align": ">", "format": ".2f"},
{"key": "io_bandwidth_mBps",
"header": "B/W[MB]", "width": 8, "align": ">", "format": ".2f"},
{"key": "read_io_bandwidth_mBps",
"header": "RB/W[MB]", "width": 8, "align": ">", "format": ".2f"},
{"key": "write_io_bandwidth_mBps",
"header": "WB/W[MB]", "width": 8, "align": ">", "format": ".2f"},
]
)
NODES_LATENCY_REPORT_ARITHMOS_FIELDS = (
[
"node_name", "id",
"hypervisor_cpu_usage_ppm", "hypervisor_memory_usage_ppm",
"hypervisor_avg_io_latency_usecs",
"hypervisor_avg_read_io_latency_usecs",
"hypervisor_avg_write_io_latency_usecs",
"controller_avg_io_latency_usecs",
"controller_avg_read_io_latency_usecs",
"controller_avg_write_io_latency_usecs",
"avg_io_latency_usecs",
"avg_read_io_latency_usecs",
"avg_write_io_latency_usecs",
]
)
NODES_LATENCY_REPORT_CLI_FIELDS = (
[
{"key": "node_name", "header": "Node",
"width": 20, "align": "<", "format": ".20"},
{"key": "hypervisor_cpu_usage_percent", "header": "CPU%",
"width": 6, "align": ">", "format": ".2f"},
{"key": "hypervisor_memory_usage_percent", "header": "MEM%",
"width": 6, "align": ">", "format": ".2f"},
{"key": "hypervisor_avg_io_latency_msecs",
"header": "hLAT[ms]", "width": 9, "align": ">", "format": ".2f"},
{"key": "hypervisor_avg_read_io_latency_msecs",
"header": "hRLAT[ms]", "width": 9, "align": ">", "format": ".2f"},
{"key": "hypervisor_avg_write_io_latency_msecs",
"header": "hWLAT[ms]", "width": 9, "align": ">", "format": ".2f"},
{"key": "controller_avg_io_latency_msecs",
"header": "cLAT[ms]", "width": 9, "align": ">", "format": ".2f"},
{"key": "controller_avg_read_io_latency_msecs",
"header": "cRLAT[ms]", "width": 9, "align": ">", "format": ".2f"},
{"key": "controller_avg_write_io_latency_msecs",
"header": "cWLAT[ms]", "width": 9, "align": ">", "format": ".2f"},
{"key": "avg_io_latency_msecs",
"header": "LAT[ms]", "width": 9, "align": ">", "format": ".2f"},
{"key": "avg_read_io_latency_msecs",
"header": "RLAT[ms]", "width": 9, "align": ">", "format": ".2f"},
{"key": "avg_write_io_latency_msecs",
"header": "WLAT[ms]", "width": 9, "align": ">", "format": ".2f"}
]
)
# ========================================================================
# Definition of VM reports.
VM_OVERALL_REPORT_ARITHMOS_FIELDS = (
[
"vm_name", "id", "node_name",
"hypervisor_cpu_usage_ppm",
"hypervisor.cpu_ready_time_ppm",
"memory_usage_ppm", "hypervisor_num_iops",
"controller_num_iops",
"controller_io_bandwidth_kBps",
"controller_avg_io_latency_usecs",
]
)
VM_OVERALL_REPORT_CLI_FIELDS = (
[
{"key": "vm_name", "header": "VM Name",
"width": 26, "align": "<", "format": ".20"},
{"key": "hypervisor_cpu_usage_percent", "header": "CPU%",
"width": 6, "align": ">", "format": ".2f"},
{"key": "hypervisor.cpu_ready_time_percent", "header": "RDY%",
"width": 6, "align": ">", "format": ".2f"},
{"key": "memory_usage_percent", "header": "MEM%",
"width": 6, "align": ">", "format": ".2f"},
{"key": "hypervisor_num_iops", "header": "hIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "controller_num_iops", "header": "cIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "controller_io_bandwidth_mBps",
"header": "cB/W[MB]", "width": 8, "align": ">", "format": ".2f"},
{"key": "controller_avg_io_latency_msecs",
"header": "cLAT[ms]", "width": 8, "align": ">", "format": ".2f"}
]
)
VM_IOPS_REPORT_ARITHMOS_FIELDS = (
[
"vm_name", "id", "node_name",
"hypervisor_cpu_usage_ppm", "memory_usage_ppm",
"hypervisor_num_iops",
"hypervisor_num_read_iops", "hypervisor_num_write_iops",
"controller_num_iops",
"controller_num_read_iops", "controller_num_write_iops",
]
)
VM_IOPS_REPORT_CLI_FIELDS = (
[
{"key": "vm_name", "header": "Node",
"width": 26, "align": "<", "format": ".20"},
{"key": "hypervisor_cpu_usage_percent", "header": "CPU%",
"width": 6, "align": ">", "format": ".2f"},
{"key": "memory_usage_percent", "header": "MEM%",
"width": 6, "align": ">", "format": ".2f"},
{"key": "hypervisor_num_iops", "header": "hIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "hypervisor_num_read_iops", "header": "hRIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "hypervisor_num_write_iops", "header": "hWIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "controller_num_iops", "header": "cIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "controller_num_read_iops", "header": "cRIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "controller_num_write_iops", "header": "cWIOPS",
"width": 8, "align": ">", "format": ".2f"},
]
)
# ========================================================================
# Definition of volume_group reports.
VG_OVERALL_REPORT_ARITHMOS_FIELDS = (
[
"volume_group_name", "id",
"num_virtual_disks",
"controller_num_iops",
"controller_num_read_iops",
"controller_num_write_iops",
"controller_io_bandwidth_kBps",
"controller_avg_io_latency_usecs"
]
)
VG_OVERALL_REPORT_CLI_FIELDS = (
[
{"key": "volume_group_name", "header": "VG name",
"width": 29, "align": "<", "format": ".29"},
{"key": "num_virtual_disks", "header": "vDiks",
"width": 8, "align": ">", "format": ".2f"},
{"key": "controller_num_iops", "header": "IOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "controller_num_read_iops", "header": "RIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "controller_num_write_iops", "header": "WIOPS",
"width": 8, "align": ">", "format": ".2f"},
{"key": "controller_io_bandwidth_mBps",
"header": "cB/W[MB]", "width": 8, "align": ">", "format": ".2f"},
{"key": "controller_avg_io_latency_msecs",
"header": "cLAT[ms]", "width": 8, "align": ">", "format": ".2f"}
]
)
# ========================================================================
class Reporter(object):
"""Reporter base """
def __init__(self):
self.interfaces = NutanixInterfaces()
self.arithmos_interface = ArithmosDataProcessing()
self.arithmos_client = self.interfaces.arithmos_client
self.FIELD_NAMES = []
def _get_live_stats(self, entity_type, sort_criteria=None,
filter_criteria=None, search_term=None,
field_name_list=None):
ret = self.arithmos_interface.MasterGetEntitiesStats(
entity_type, sort_criteria, filter_criteria, search_term,
requested_field_name_list=field_name_list)
if ret:
response = ret.response
if response.error == ArithmosErrorProto.kNoError:
return response
def _get_time_range_stat_values(self, entity_id, stat,
start, end, sampling_interval):
resp = self.arithmos_interface.MasterGetTimeRangeStats(entity_id,
self._ARITHMOS_ENTITY_PROTO, stat,
start, end,
sampling_interval)
if resp:
for res in resp.response_list:
if res.error == ArithmosErrorProto.kNoError:
return res.time_range_stat.value_list
def _get_time_range_stat_average(self, entity_id, stat,
start, end, sampling_interval=30):
values = self._get_time_range_stat_values(entity_id, stat,
start, end,
sampling_interval)
if values:
counter = 0
sum = 0
for value in values:
if value > 0:
counter += 1
sum += value
if sum > 0:
return sum / counter
return -1
def _stats_unit_conversion(self, entities_dict):
"""
Receive a list of entity dictionaries with stats and makes the name
and unit conversion. Is used to return a dictionary to UIs with the
stats in desired units and names. For example:
Arithmos stat memory_usage_ppm is changed to memory_usage_percent, and
the value is changed from parts per million to percentage.
It uses the stat names to identify the current unit value and change it
to an arbitrary desired unit.
"""
ret = []
for entity in entities_dict:
converted_entity = {}
for key in entity.keys():
new_key = ""
if "ppm" in key:
new_key = key.replace("ppm", "percent")
new_value = entity[key] / 10000
converted_entity[new_key] = new_value
elif "kBps" in key:
new_key = key.replace("kBps", "mBps")
new_value = entity[key] / 1024
converted_entity[new_key] = new_value
elif "bytes" in key:
new_key = key.replace("bytes", "Mbytes")
new_value = entity[key] / 1048576
converted_entity[new_key] = new_value
elif "usecs" in key:
new_key = key.replace("usecs", "msecs")
new_value = entity[key] / 1000
converted_entity[new_key] = new_value
else:
new_key = key
converted_entity[new_key] = entity[new_key]
# Set back to -1 if we divided in the previos statements.
# This is because arithmos returns -1 when there is no data.
if converted_entity[new_key] < 0:
converted_entity[new_key] = -1
ret.append(converted_entity)
return ret
def _get_entity_stats_from_proto(self, entity, field_list):
"""
Get an entity protobufer and return a dictionary with
desired fields.
Missing fields are populated with -1.
"""
entity_dict = {}
if hasattr(entity, "stats"):
stats = entity.stats
for tmp_stat in stats.DESCRIPTOR.fields:
if (tmp_stat.name != "common_stats" and
tmp_stat.name != "generic_stat_list"):
if tmp_stat.name in field_list:
entity_dict[tmp_stat.name] = getattr(
stats, tmp_stat.name)
if hasattr(stats, "common_stats"):
for tmp_common_stat in stats.common_stats.DESCRIPTOR.fields:
if tmp_common_stat.name in field_list:
entity_dict[tmp_common_stat.name] = getattr(
stats.common_stats, tmp_common_stat.name)
if hasattr(stats, "generic_stat_list"):
for tmp_generic_stat in stats.generic_stat_list:
entity_dict[tmp_generic_stat.stat_name] = tmp_generic_stat.stat_value
if hasattr(entity, "generic_attribute_list"):
for tmp_generic_attr in entity.generic_attribute_list:
name = None
value = None
for field, _ in tmp_generic_attr.ListFields():
if field.name == "attribute_name":
name = str(getattr(tmp_generic_attr, field.name))
elif field.name == "attribute_value_str":
value = getattr(tmp_generic_attr, str(field.name))
elif field.name == "attribute_value_int":
value = getattr(tmp_generic_attr, field.name)
elif hasattr(generic_attr, "attribute_value_str_list"):
value = getattr(tmp_generic_attr, field.name)
if name is not None and value is not None:
entity_dict[name] = value
# Method returns a dictionary with all fields in field_list,
# if there is a missing field, populate with -1.
for field in field_list:
if not field in entity_dict.keys():
entity_dict[field] = -1
return entity_dict
def _get_generic_stats_dict(self, generic_stat_list):
"""
Transform generic_stat_list returned from arithmos into a dictionary.
Arithmos returns generic_stats in the form of:
generic_stat_list {
stat_name: "memory_usage_ppm"
stat_value: 23030
}
This functions transform this into:
{ "memory_usage_ppm": 23030 }
Making it easy to handle by reporters and Ui.
"""
ret = {}
for generic_stat in generic_stat_list:
ret[generic_stat.stat_name] = generic_stat.stat_value
return ret
def _zeroed_missing_stats(self, stats, desired_stat_list):
"""
If an entity is missing an stat in the returned data from arithmos
then it's necessary to fill the missing information to fill the
fields when it's displayed by the Ui.
This function takes an actual list of stats and a list of desired
stats. If a stat in the desired list is missing from the stat list
then it add it with a value of 0 and returns a new list.
TODO: This is unnecessary, review this method for removal. If so this needs
to be done in Ui not in reporter.
"""
for desired_stat in desired_stat_list:
if desired_stat not in stats:
stats[desired_stat] = -1
return stats
def _get_generic_attribute_dict(self, generic_attribute_list):
"""
Transform generic_attribute_list returned from arithmos into a dictionary.
Arithmos returns generic_attribute in the form of:
generic_attribute_list {
attribute_name: "vm_uuid"
attribute_value_str: "00c43e1e-33db-4c91-9819-9b2e6f7b6111"
}
This functions transform this into:
{ "vm_uuid": "00c43e1e-33db-4c91-9819-9b2e6f7b6111" }
Making it easy to handle by reporters and Ui.
"""
ret = {}
for generic_attribute in generic_attribute_list:
ret[generic_attribute.attribute_name] = \
generic_attribute.attribute_value_str
return ret
def _zeroed_missing_attribute(self, attributes, desired_attribute_list):
"""
If an entity is missing an attribute in the returned data from arithmos
then it's necessary to fill the missing information to fill the
fields when it's displayed by the Ui.
This function takes an actual list of attributes and a list of desired
attributes. If a attribute in the desired list is missing from the attribute list
then it add it with a value of "-" and returns a new list.
TODO: This is unnecessary, review this method for removal. If so this needs
to be done in Ui not in reporter.
"""
for desired_attribute in desired_attribute_list:
if desired_attribute not in attributes:
attributes[desired_attribute] = "-"
return attributes
def _sort_entity_dict(self, nodes_stats_dic, sort, default_sort_field="name"):
"""
Get a list of entities dictionaries and a sort key. The sort key is
translated into a field using 'self.sort_conversion' and is
equivalent to an arithmos field. Then the list is sorted based on
this criteria.
The conversion dictionary 'self.sort_conversion' needs to be defined in the
subclasses according to their sorting criteria.
"""
if sort in self.sort_conversion.keys():
sort_by = self.sort_conversion[sort]
else:
sort_by = self.sort_conversion[default_sort_field]
# This is because the default sort field "name" is the only alphabetic,
# other fields are are numeric and needs to be sorted in reverse.
# TODO: May need to think better about this later.
if sort_by == self.sort_conversion[default_sort_field]:
return sorted(nodes_stats_dic,
key=lambda node: node[sort_by])
else:
return sorted(nodes_stats_dic,
key=lambda node: node[sort_by], reverse=True)
class ClusterReporter(Reporter):
"""Reports for Clusters"""
def __init__(self):
Reporter.__init__(self)
self._ARITHMOS_ENTITY_PROTO = ArithmosEntityProto.kCluster
self.max_cluster_name_width = 0
self.cluster = self._get_cluster_live_stats(
field_name_list=["cluster_name", "id"])
self.name = self.cluster[0].cluster_name
self.cluster_id = self.cluster[0].id
def _get_cluster_live_stats(self, sort_criteria=None, filter_criteria=None,
search_term=None, field_name_list=None):
response = self._get_live_stats(self._ARITHMOS_ENTITY_PROTO,
sort_criteria, filter_criteria,
search_term, field_name_list)
entity_list = response.entity_list.cluster
return entity_list
def overall_live_report(self, sort="name"):
field_names = ["cluster_name", "hypervisor_cpu_usage_ppm",
"hypervisor_memory_usage_ppm", "hypervisor_num_iops",
"controller_num_iops", "num_iops",
"avg_io_latency_usecs", "io_bandwidth_kBps"]
pass
class NodeReporter(Reporter):
"""Reports for Nodes"""
def __init__(self):
Reporter.__init__(self)
self._ARITHMOS_ENTITY_PROTO = ArithmosEntityProto.kNode
self.max_node_name_width = 0
self.sort_conversion = {
"name": "node_name",
"cpu": "hypervisor_cpu_usage_percent",
"mem": "hypervisor_memory_usage_percent",
"iops": "num_iops",
"bw": "io_bandwidth_mBps",
"lat": "avg_io_latency_msecs"
}
self.nodes = self._get_node_live_stats(sort_criteria="node_name",
field_name_list=["node_name", "id"])
def _get_node_live_stats(self, sort_criteria=None, filter_criteria=None,
search_term=None, field_name_list=None):
response = self._get_live_stats(self._ARITHMOS_ENTITY_PROTO,
sort_criteria, filter_criteria,
search_term, field_name_list)
entity_list = response.entity_list.node
return entity_list
def _get_live_stats_dic(self, entity_list, field_list):
"""
Get an entity_list as returned from MasterGetEntitiesStats,
parse the entities and stats to a dictinary an returns.
"""
node_stats_dic = []
for node_entity in entity_list:
node_dict = self._get_entity_stats_from_proto(
node_entity, field_list)
node_dict["id"] = node_entity.id
node_dict["node_name"] = str(node_entity.node_name)
node_stats_dic.append(node_dict)
return node_stats_dic
def _get_time_range_stats_dic(self, field_list, start, end, sampling_interval=30):
"""
Get a list of fields (stats), a time frame specified by start and end,
the desired interval and collects from arithmos the average values.
"""
nodes_stats_dic = []
for node_pivot in self.nodes:
node = {}
for field in field_list:
value = self._get_time_range_stat_average(
node_pivot.id, field, start, end,
sampling_interval
)
node[field] = value
node["node_name"] = str(node_pivot.node_name)
node["node_id"] = int(node_pivot.id)
nodes_stats_dic.append(node)
return nodes_stats_dic
def overall_live_report(self, sort="name"):
"""
Returns a sorted dictionary with nodes overall stats.
"""
entity_list = self._get_node_live_stats(
field_name_list=NODES_OVERALL_REPORT_ARITHMOS_FIELDS,
filter_criteria="")
ret = self._get_live_stats_dic(entity_list,
NODES_OVERALL_REPORT_ARITHMOS_FIELDS)
ret = self._stats_unit_conversion(ret)
return self._sort_entity_dict(ret, sort)
def overall_time_range_report(self, start, end, sort="name", nodes=[]):
"""
Returns a sorted dictionary with time range nodes overall stats.
"""
ret = self._get_time_range_stats_dic(
NODES_OVERALL_REPORT_ARITHMOS_FIELDS, start, end)
ret = self._stats_unit_conversion(ret)
return self._sort_entity_dict(ret, sort)
def iops_live_report(self, sort="name"):
"""
Returns a sorted dictionary with live nodes IOPS stats.
"""
entity_list = self._get_node_live_stats(
field_name_list=NODES_IOPS_REPORT_ARITHMOS_FIELDS,
filter_criteria="")
ret = self._get_live_stats_dic(entity_list,
NODES_IOPS_REPORT_ARITHMOS_FIELDS)
ret = self._stats_unit_conversion(ret)
return self._sort_entity_dict(ret, sort)
def iops_time_range_report(self, start, end, sort="name", nodes=[]):
"""
Returns a sorted dictionary with time range node IOPS stats.
"""
ret = self._get_time_range_stats_dic(
NODES_IOPS_REPORT_ARITHMOS_FIELDS, start, end)
ret = self._stats_unit_conversion(ret)
return self._sort_entity_dict(ret, sort)
def bw_live_report(self, sort="name"):
"""
Returns a sorted dictionary with live nodes bandwidth stats.
"""
entity_list = self._get_node_live_stats(
field_name_list=NODES_BANDWIDTH_REPORT_ARITHMOS_FIELDS,
filter_criteria="")
ret = self._get_live_stats_dic(entity_list,
NODES_BANDWIDTH_REPORT_ARITHMOS_FIELDS)
ret = self._stats_unit_conversion(ret)
return self._sort_entity_dict(ret, sort)
def bw_time_range_report(self, start, end, sort="name", nodes=[]):
"""
Returns a sorted dictionary with time range nodes bandwidth stats.
"""
ret = self._get_time_range_stats_dic(
NODES_BANDWIDTH_REPORT_ARITHMOS_FIELDS, start, end)
ret = self._stats_unit_conversion(ret)
return self._sort_entity_dict(ret, sort)
def lat_live_report(self, sort="name"):
"""
Returns a sorted dictionary with live nodes bandwidth stats.
"""
entity_list = self._get_node_live_stats(
field_name_list=NODES_LATENCY_REPORT_ARITHMOS_FIELDS,
filter_criteria="")
ret = self._get_live_stats_dic(entity_list,
NODES_LATENCY_REPORT_ARITHMOS_FIELDS)
ret = self._stats_unit_conversion(ret)
return self._sort_entity_dict(ret, sort)
def lat_time_range_report(self, start, end, sort="name", nodes=[]):
"""
Returns a sorted dictionary with time range nodes bandwidth stats.
"""
ret = self._get_time_range_stats_dic(
NODES_LATENCY_REPORT_ARITHMOS_FIELDS, start, end)
ret = self._stats_unit_conversion(ret)
return self._sort_entity_dict(ret, sort)
class VmReporter(Reporter):
"""Reports for UVMs"""
def __init__(self):
Reporter.__init__(self)
self._ARITHMOS_ENTITY_PROTO = ArithmosEntityProto.kVM
self.max_vm_name_width = 0
# The reason this conversion exists is because we want to abstract
# the actual attribute names with something more human friendly
# and easy to remember. We also want to abstract this from the
# UI classes.
self.sort_conversion = {
"name": "vm_name",
"cpu": "hypervisor_cpu_usage_percent",
"rdy": "hypervisor.cpu_ready_time_percent",
"mem": "memory_usage_percent",
"iops": "controller_num_iops",
"bw": "controller_io_bandwidth_mBps",
"lat": "controller_avg_io_latency_msecs"
}
self.sort_conversion_arithmos = {
"name": "vm_name",
"cpu": "-hypervisor_cpu_usage_ppm",
"rdy": "hypervisor.cpu_ready_time_ppm",
"mem": "-memory_usage_ppm",
"iops": "-controller_num_iops",
"bw": "-controller_io_bandwidth_kBps",
"lat": "-controller_avg_io_latency_usecs"
}
def _get_vm_live_stats(self, sort_criteria=None, filter_criteria=None,
search_term=None, field_list=None):
response = self._get_live_stats(self._ARITHMOS_ENTITY_PROTO,
sort_criteria, filter_criteria,
search_term, field_list)
entity_list = response.entity_list.vm
return entity_list
def _get_live_stats_dic(self, entity_list, field_list):
"""
Get an entity_list as returned from MasterGetEntitiesStats,
parse the entities and stats to a dictinary and returns.
"""
vm_stats_dic = []
for vm_entity in entity_list:
vm_dict = self._get_entity_stats_from_proto(vm_entity, field_list)
vm_dict["id"] = vm_entity.id
vm_dict["vm_name"] = str(vm_entity.vm_name)
vm_stats_dic.append(vm_dict)
return vm_stats_dic
def _get_time_range_stats_dic(self, entity_list, field_list,
start, end, sampling_interval=30):
"""
Get an entity_list as returned from MasterGetEntitiesStats,
parse the entities and stats to a dictinary and returns.
"""
vm_stats_dic = []
for vm_pivot in entity_list:
vm = {}
for field in field_list:
value = self._get_time_range_stat_average(
vm_pivot.id, field, start, end,
sampling_interval
)
vm[field] = value
vm["vm_name"] = str(vm_pivot.vm_name)
vm["id"] = vm_pivot.id
vm_stats_dic.append(vm)
return vm_stats_dic
def _get_arithmos_sort_field(self, sort, default_sort_field="name"):
"""
Returns the arithmos field for sort criteria. The sort key is
translated using 'self.sort_conversion_arithmos' into an arithmos field.
There is another method _sort_entity_dict() in superclass for sorting.
The reason we also need arithmos sort criteria is because there is a maximum
numbers of entities returned by arithmos, if sort_criteria is not indicated
when calling MasterGetEntitiesStats() there is a risk of missing some of the
top VMs.
"""
if sort in self.sort_conversion.keys():
sort_by_arithmos = self.sort_conversion_arithmos[sort]
else:
sort_by_arithmos = self.sort_conversion_arithmos[default_sort_field]
def _get_arithmos_filter_criteria_live(self, node_names=[], power_on=True):
"""
Currently only filter criteria for VM is nodes and power state.
Power state on is used for live reports, while power state off is used
for time range reports.
"""
filter_by = ""
if power_on:
filter_by = "power_state==on"
if node_names:
node_names_str = ",".join(["node_name==" + node_name
for node_name in node_names])
if power_on:
filter_by += ";" + node_names_str
else:
filter_by += node_names_str
return filter_by
def overall_live_report(self, sort="name", node_names=[]):
"""
Returns a sorted dictionary with VMs overall stats.
"""
sort_by_arithmos = self._get_arithmos_sort_field(sort)
filter_by = self._get_arithmos_filter_criteria_live(node_names)
entity_list = self._get_vm_live_stats(
field_list=VM_OVERALL_REPORT_ARITHMOS_FIELDS,
filter_criteria=filter_by,
sort_criteria=sort_by_arithmos)
ret = self._get_live_stats_dic(entity_list,
VM_OVERALL_REPORT_ARITHMOS_FIELDS)
ret = self._stats_unit_conversion(ret)
return self._sort_entity_dict(ret, sort)
def iops_live_report(self, sort="name", node_names=[]):
"""
Returns a sorted dictionary with VMs IOPs stats.
"""
sort_by_arithmos = self._get_arithmos_sort_field(sort)
filter_by = self._get_arithmos_filter_criteria_live(node_names)
entity_list = self._get_vm_live_stats(
field_list=VM_IOPS_REPORT_ARITHMOS_FIELDS,
filter_criteria=filter_by,
sort_criteria=sort_by_arithmos)
ret = self._get_live_stats_dic(entity_list,
VM_IOPS_REPORT_ARITHMOS_FIELDS)
ret = self._stats_unit_conversion(ret)
return self._sort_entity_dict(ret, sort)
def overall_time_range_report(self, start, end, sort="name", node_names=[]):
sort_by_arithmos = self._get_arithmos_sort_field(sort)
filter_by = self._get_arithmos_filter_criteria_live(
node_names, power_on=False)
vm_list = self._get_vm_live_stats(field_list=["vm_name", "id",
"node_name"],
filter_criteria=filter_by,
sort_criteria=sort_by_arithmos)
generic_attribute_names = ["node_name"]
ret = self._get_time_range_stats_dic(vm_list, VM_OVERALL_REPORT_ARITHMOS_FIELDS,
start, end)
ret = self._stats_unit_conversion(ret)
return self._sort_entity_dict(ret, sort)
class VgReporter(Reporter):
"""Reporter for Volume Groups"""
def __init__(self):
Reporter.__init__(self)
self._ARITHMOS_ENTITY_PROTO = ArithmosEntityProto.kVolumeGroup
# The reason this conversion exists is because we want to abstract
# the actual attribute names with something more human friendly
# and easy to remember. We also want to abstract this from the
# UI classes.
self.sort_conversion = {
"name": "volume_group_name",
"iops": "controller_num_iops",
"bw": "controller_io_bandwidth_mBps",
"lat": "controller_avg_io_latency_msecs",
"vdisks": "num_virtual_disks"
}
self.sort_conversion_arithmos = {
"name": "volume_group_name",
"iops": "-controller_num_iops",
"bw": "-controller_io_bandwidth_kBps",
"lat": "-controller_avg_io_latency_usecs",
"vdisks": "-num_virtual_disks"
}
def _get_vg_live_stats(self, sort_criteria=None, filter_criteria=None,
search_term=None, field_list=None):
response = self._get_live_stats(self._ARITHMOS_ENTITY_PROTO,
sort_criteria, filter_criteria,
search_term, field_list)
entity_list = response.entity_list.volume_group
return entity_list
def _get_live_stats_dic(self, entity_list, field_list):
"""
Get an entity_list as returned from MasterGetEntitiesStats,
parse the entities and stats to a dictinary and returns.
"""
vg_stats_dic = []
for vg_entity in entity_list:
vg_dict = self._get_entity_stats_from_proto(vg_entity, field_list)
vg_dict["id"] = vg_entity.id
vg_stats_dic.append(vg_dict)