-
Notifications
You must be signed in to change notification settings - Fork 9
/
proxlb
executable file
·1576 lines (1251 loc) · 84.5 KB
/
proxlb
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 python3
# ProxLB
# ProxLB (re)balances VM workloads across nodes in Proxmox clusters.
# ProxLB obtains current metrics from all nodes within the cluster for
# further auto balancing by memory, disk or cpu and rebalances the VMs
# over all available nodes in a cluster by having an equal resource usage.
# Copyright (C) 2024 Florian Paul Azim Hoberg @gyptazy <gyptazy@gyptazy.ch>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import argparse
import configparser
import copy
import json
import logging
import os
try:
import proxmoxer
_imports = True
except ImportError:
_imports = False
import random
import re
import requests
import socket
import sys
import time
import urllib3
# Constants
__appname__ = "ProxLB"
__version__ = "1.0.5"
__config_version__ = 3
__author__ = "Florian Paul Azim Hoberg <gyptazy@gyptazy.com> @gyptazy"
__errors__ = False
# Classes
## Logging class
class SystemdHandler(logging.Handler):
""" Class to handle logging options. """
PREFIX = {
logging.CRITICAL: "<2> " + __appname__ + ": ",
logging.ERROR: "<3> " + __appname__ + ": ",
logging.WARNING: "<4> " + __appname__ + ": ",
logging.INFO: "<6> " + __appname__ + ": ",
logging.DEBUG: "<7> " + __appname__ + ": ",
logging.NOTSET: "<7 " + __appname__ + ": ",
}
def __init__(self, stream=sys.stdout):
self.stream = stream
logging.Handler.__init__(self)
def emit(self, record):
try:
msg = self.PREFIX[record.levelno] + self.format(record) + "\n"
self.stream.write(msg)
self.stream.flush()
except Exception:
self.handleError(record)
# Functions
def initialize_logger(log_level, update_log_verbosity=False):
""" Initialize ProxLB logging handler. """
info_prefix = 'Info: [logger]:'
root_logger = logging.getLogger()
root_logger.setLevel(log_level)
if not update_log_verbosity:
root_logger.addHandler(SystemdHandler())
logging.info(f'{info_prefix} Logger got initialized.')
else:
logging.info(f'{info_prefix} Logger verbosity got updated to: {log_level}.')
def pre_validations(config_path, proxlb_config=False):
""" Run pre-validations as sanity checks. """
info_prefix = 'Info: [pre-validations]:'
if proxlb_config:
logging.info(f'{info_prefix} Validating ProxLB config file content.')
__validate_config_content(proxlb_config)
logging.info(f'{info_prefix} ProxLB config file content validation done.')
else:
logging.info(f'{info_prefix} Validating basic configuration.')
__validate_imports()
__validate_config_file(config_path)
logging.info(f'{info_prefix} All pre-validations done.')
def post_validations():
""" Run post-validations as sanity checks. """
error_prefix = 'Error: [post-validations]:'
info_prefix = 'Info: [post-validations]:'
if __errors__:
logging.critical(f'{error_prefix} Not all post-validations succeeded. Please validate!')
else:
logging.info(f'{info_prefix} All post-validations succeeded.')
def validate_daemon(daemon, schedule):
""" Validate if ProxLB runs as a daemon. """
info_prefix = 'Info: [daemon]:'
if bool(int(daemon)):
logging.info(f'{info_prefix} Running in daemon mode. Next run in {schedule} hours.')
time.sleep(int(schedule) * 60 * 60)
else:
logging.info(f'{info_prefix} Not running in daemon mode. Quitting.')
sys.exit(0)
def __validate_imports():
""" Validate if all Python imports succeeded. """
error_prefix = 'Error: [python-imports]:'
info_prefix = 'Info: [python-imports]:'
if not _imports:
logging.critical(f'{error_prefix} Could not import all dependencies. Please install "proxmoxer".')
sys.exit(2)
else:
logging.info(f'{info_prefix} All required dependencies were imported.')
def __validate_config_file(config_path):
""" Validate if all Python imports succeeded. """
error_prefix = 'Error: [config]:'
info_prefix = 'Info: [config]:'
if not os.path.isfile(config_path):
logging.critical(f'{error_prefix} Could not find config file in: {config_path}.')
sys.exit(2)
else:
logging.info(f'{info_prefix} Configuration file loaded from: {config_path}.')
def __validate_config_content(proxlb_config):
""" Validate the user's config options. """
error_prefix = 'Error: [config]:'
info_prefix = 'Info: [config]:'
validate_bool_options = [
'proxmox_api_ssl_v',
'vm_balancing_enable',
'vm_parallel_migrations',
'storage_balancing_enable',
'storage_parallel_migrations',
'update_service',
'api',
'master_only',
'daemon'
]
for bool_val in validate_bool_options:
if type(proxlb_config.get(bool_val, None)) == bool:
logging.info(f'{info_prefix} Config option {bool_val} is in a correct format.')
else:
logging.critical(f'{error_prefix} Config option {bool_val} is incorrect: {proxlb_config.get(bool_val, None)}')
sys.exit(2)
validate_string_options = [
'vm_balancing_method',
'vm_balancing_mode',
'vm_balancing_mode_option',
'vm_balancing_type',
'storage_balancing_method',
'log_verbosity'
]
whitelist_string_options = {
'vm_balancing_method': ['memory', 'disk', 'cpu'],
'vm_balancing_mode': ['used', 'assigned'],
'vm_balancing_mode_option': ['bytes', 'percent'],
'vm_balancing_type': ['vm', 'ct', 'all'],
'storage_balancing_method': ['disk_space'],
'log_verbosity': ['DEBUG', 'INFO', 'WARNING', 'CRITICAL']
}
for string_val in validate_string_options:
if proxlb_config[string_val] in whitelist_string_options[string_val]:
logging.info(f'{info_prefix} Config option {string_val} is in a correct format.')
else:
logging.critical(f'{error_prefix} Config option {string_val} is incorrect: {proxlb_config.get(string_val, None)}')
sys.exit(2)
def initialize_args():
""" Initialize given arguments for ProxLB. """
argparser = argparse.ArgumentParser(description='ProxLB')
argparser.add_argument('-c', '--config', help='Path to config file', type=str, required=False)
argparser.add_argument('-d', '--dry-run', help='Perform a dry-run without doing any actions.', action='store_true', required=False)
argparser.add_argument('-j', '--json', help='Return a JSON of the VM movement.', action='store_true', required=False)
argparser.add_argument('-b', '--best-node', help='Returns the best next node.', action='store_true', required=False)
argparser.add_argument('-m', '--maintenance', help='Sets node to maintenance mode & moves workloads away.', type=str, required=False)
argparser.add_argument('-v', '--version', help='Returns the current ProxLB version.', action='store_true', required=False)
return argparser.parse_args()
def proxlb_output_version():
""" Print ProxLB version information on CLI. """
print(f'{__appname__} version {__version__}\nRequired config version: >= {__config_version__}')
print('ProxLB support: https://github.com/gyptazy/ProxLB\nDeveloper: gyptazy.com')
sys.exit(0)
def initialize_config_path(app_args):
""" Initialize path to ProxLB config file. """
info_prefix = 'Info: [config]:'
config_path = app_args.config
if app_args.config is None:
config_path = '/etc/proxlb/proxlb.conf'
logging.info(f'{info_prefix} No config file provided. Falling back to: {config_path}.')
else:
logging.info(f'{info_prefix} Using config file: {config_path}.')
return config_path
def initialize_config_options(config_path):
""" Read configuration from given config file for ProxLB. """
error_prefix = 'Error: [config]:'
info_prefix = 'Info: [config]:'
proxlb_config = {}
try:
config = configparser.ConfigParser()
config.read(config_path)
# Proxmox config
proxlb_config['proxmox_api_host'] = config['proxmox']['api_host']
proxlb_config['proxmox_api_user'] = config['proxmox']['api_user']
proxlb_config['proxmox_api_pass'] = config['proxmox']['api_pass']
proxlb_config['proxmox_api_ssl_v'] = config['proxmox']['verify_ssl']
proxlb_config['proxmox_api_timeout'] = config['proxmox'].get('timeout', 10)
# VM Balancing
proxlb_config['vm_balancing_enable'] = config['vm_balancing'].get('enable', 1)
proxlb_config['vm_balancing_method'] = config['vm_balancing'].get('method', 'memory')
proxlb_config['vm_balancing_mode'] = config['vm_balancing'].get('mode', 'used')
proxlb_config['vm_balancing_mode_option'] = config['vm_balancing'].get('mode_option', 'bytes')
proxlb_config['vm_balancing_type'] = config['vm_balancing'].get('type', 'vm')
proxlb_config['vm_balanciness'] = config['vm_balancing'].get('balanciness', 10)
proxlb_config['vm_parallel_migrations'] = config['vm_balancing'].get('parallel_migrations', 1)
proxlb_config['vm_maintenance_nodes'] = config['vm_balancing'].get('maintenance_nodes', '')
proxlb_config['vm_ignore_nodes'] = config['vm_balancing'].get('ignore_nodes', '')
proxlb_config['vm_ignore_vms'] = config['vm_balancing'].get('ignore_vms', '')
proxlb_config['vm_enforce_affinity_groups'] = config['vm_balancing'].get('enforce_affinity_groups', 1)
# Storage Balancing
proxlb_config['storage_balancing_enable'] = config['storage_balancing'].get('enable', 0)
proxlb_config['storage_balancing_method'] = config['storage_balancing'].get('method', 'disk_space')
proxlb_config['storage_balanciness'] = config['storage_balancing'].get('balanciness', 10)
proxlb_config['storage_parallel_migrations'] = config['storage_balancing'].get('parallel_migrations', 1)
# Update Support
proxlb_config['update_service'] = config['update_service'].get('enable', 0)
# API
proxlb_config['api'] = config['update_service'].get('enable', 0)
# Service
proxlb_config['master_only'] = config['service'].get('master_only', 0)
proxlb_config['daemon'] = config['service'].get('daemon', 1)
proxlb_config['schedule'] = config['service'].get('schedule', 24)
proxlb_config['log_verbosity'] = config['service'].get('log_verbosity', 'CRITICAL')
proxlb_config['config_version'] = config['service'].get('config_version', 2)
except configparser.NoSectionError:
logging.critical(f'{error_prefix} Could not find the required section.')
sys.exit(2)
except configparser.ParsingError:
logging.critical(f'{error_prefix} Unable to parse the config file.')
sys.exit(2)
except KeyError:
logging.critical(f'{error_prefix} Could not find the required options in config file.')
sys.exit(2)
# Normalize and update bools. Afterwards, validate minimum required config version.
proxlb_config = __update_config_parser_bools(proxlb_config)
validate_config_minimum_version(proxlb_config)
logging.info(f'{info_prefix} Configuration file loaded.')
return proxlb_config
def __update_config_parser_bools(proxlb_config):
""" Update bools in config from configparser to real bools """
info_prefix = 'Info: [config-bool-converter]:'
# Normalize and update config parser values to bools.
for section, option_value in proxlb_config.items():
if option_value in [1, '1', 'yes', 'Yes', 'true', 'True', 'enable']:
logging.info(f'{info_prefix} Converting {section} to bool: True.')
proxlb_config[section] = True
if option_value in [0, '0', 'no', 'No', 'false', 'False', 'disable']:
logging.info(f'{info_prefix} Converting {section} to bool: False.')
proxlb_config[section] = False
return proxlb_config
def validate_config_minimum_version(proxlb_config):
""" Validate the minimum required config file for ProxLB """
info_prefix = 'Info: [config-version-validator]:'
error_prefix = 'Error: [config-version-validator]:'
if int(proxlb_config['config_version']) < __config_version__:
logging.error(f'{error_prefix} ProxLB config version {proxlb_config["config_version"]} is too low. Required: {__config_version__}.')
print(f'{error_prefix} ProxLB config version {proxlb_config["config_version"]} is too low. Required: {__config_version__}.')
sys.exit(1)
else:
logging.info(f'{info_prefix} ProxLB config version {proxlb_config["config_version"]} is fine. Required: {__config_version__}.')
def api_connect(proxmox_api_host, proxmox_api_user, proxmox_api_pass, proxmox_api_ssl_v, proxmox_api_timeout):
""" Connect and authenticate to the Proxmox remote API. """
error_prefix = 'Error: [api-connection]:'
warn_prefix = 'Warning: [api-connection]:'
info_prefix = 'Info: [api-connection]:'
proxmox_api_ssl_v = bool(int(proxmox_api_ssl_v))
if not proxmox_api_ssl_v:
requests.packages.urllib3.disable_warnings()
logging.warning(f'{warn_prefix} API connection does not verify SSL certificate.')
proxmox_api_host = __api_connect_get_host(proxmox_api_host)
try:
api_object = proxmoxer.ProxmoxAPI(proxmox_api_host, user=proxmox_api_user, password=proxmox_api_pass, verify_ssl=proxmox_api_ssl_v, timeout=int(proxmox_api_timeout))
except proxmoxer.backends.https.AuthenticationError as proxmox_api_error:
logging.critical(f'{error_prefix} Provided credentials do not work: {proxmox_api_error}')
sys.exit(2)
except urllib3.exceptions.NameResolutionError:
logging.critical(f'{error_prefix} Could not resolve the given host: {proxmox_api_host}.')
sys.exit(2)
except requests.exceptions.ConnectTimeout:
logging.critical(f'{error_prefix} Connection time out to host: {proxmox_api_host}.')
sys.exit(2)
except requests.exceptions.SSLError:
logging.critical(f'{error_prefix} SSL certificate verification failed for host: {proxmox_api_host}.')
sys.exit(2)
logging.info(f'{info_prefix} API connection succeeded to host: {proxmox_api_host}.')
return api_object
def __api_connect_get_host(proxmox_api_host):
""" Validate if a list of API hosts got provided and pre-validate the hosts. """
info_prefix = 'Info: [api-connect-get-host]:'
proxmox_port = 8006
if ',' in proxmox_api_host:
logging.info(f'{info_prefix} Multiple hosts for API connection are given. Testing hosts for further usage.')
proxmox_api_host = proxmox_api_host.split(',')
# Validate all given hosts and check for responsive on Proxmox web port.
for host in proxmox_api_host:
logging.info(f'{info_prefix} Testing host {host} on port tcp/{proxmox_port}.')
reachable = __api_connect_test_ipv4_host(host, proxmox_port)
if reachable:
return host
else:
logging.info(f'{info_prefix} Using host {proxmox_api_host} on port tcp/{proxmox_port}.')
return proxmox_api_host
def __api_connect_test_ipv4_host(proxmox_api_host, port):
""" Validate if a given host on the IPv4 management address is reachable. """
error_prefix = 'Error: [api-connect-test-host]:'
info_prefix = 'Info: [api-connect-test-host]:'
proxmox_connection_timeout = 2
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(proxmox_connection_timeout)
logging.info(f'{info_prefix} Timeout for host {proxmox_api_host} is set to {proxmox_connection_timeout} seconds.')
result = sock.connect_ex((proxmox_api_host,port))
if result == 0:
sock.close()
logging.info(f'{info_prefix} Host {proxmox_api_host} is reachable on port tcp/{port}.')
return True
else:
sock.close()
logging.critical(f'{error_prefix} Host {proxmox_api_host} is unreachable on port tcp/{port}.')
return False
def __api_connect_test_ipv6_host(proxmox_api_host, port):
""" Validate if a given host on the IPv6 management address is reachable. """
error_prefix = 'Error: [api-connect-test-host]:'
info_prefix = 'Info: [api-connect-test-host]:'
proxmox_connection_timeout = 2
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
sock.settimeout(proxmox_connection_timeout)
logging.info(f'{info_prefix} Timeout for host {proxmox_api_host} is set to {proxmox_connection_timeout}.')
result = sock.connect_ex((proxmox_api_host,port))
if result == 0:
sock.close()
logging.info(f'{info_prefix} Host {proxmox_api_host} is reachable on port tcp/{port}.')
return True
else:
sock.close()
logging.critical(f'{error_prefix} Host {proxmox_api_host} is unreachable on port tcp/{port}.')
return False
def execute_rebalancing_only_by_master(api_object, master_only):
""" Validate if balancing should only be done by the cluster master. Afterwards, validate if this node is the cluster master. """
info_prefix = 'Info: [only-on-master-executor]:'
master_only = bool(int(master_only))
if bool(int(master_only)):
logging.info(f'{info_prefix} Master only rebalancing is defined. Starting validation.')
cluster_master_node = get_cluster_master(api_object)
cluster_master = validate_cluster_master(cluster_master_node)
return cluster_master, master_only
else:
logging.info(f'{info_prefix} No master only rebalancing is defined. Skipping validation.')
return False, master_only
def get_cluster_master(api_object):
""" Get the current master of the Proxmox cluster. """
error_prefix = 'Error: [cluster-master-getter]:'
info_prefix = 'Info: [cluster-master-getter]:'
try:
ha_status_object = api_object.cluster().ha().status().manager_status().get()
logging.info(f'{info_prefix} Master node: {ha_status_object.get("manager_status", None).get("master_node", None)}')
except urllib3.exceptions.NameResolutionError:
logging.critical(f'{error_prefix} Could not resolve the API.')
sys.exit(2)
except requests.exceptions.ConnectTimeout:
logging.critical(f'{error_prefix} Connection time out to API.')
sys.exit(2)
except requests.exceptions.SSLError:
logging.critical(f'{error_prefix} SSL certificate verification failed for API.')
sys.exit(2)
cluster_master = ha_status_object.get("manager_status", None).get("master_node", None)
if cluster_master:
return cluster_master
else:
logging.critical(f'{error_prefix} Could not obtain cluster master. Please check your configuration and ensure HA services in Proxmox are enabled. Stopping.')
sys.exit(2)
def validate_cluster_master(cluster_master):
""" Validate if the current execution node is the cluster master. """
info_prefix = 'Info: [cluster-master-validator]:'
node_executor_hostname = socket.gethostname()
logging.info(f'{info_prefix} Node executor hostname is: {node_executor_hostname}')
if node_executor_hostname != cluster_master:
logging.info(f'{info_prefix} {node_executor_hostname} is not the cluster master ({cluster_master}).')
return False
else:
return True
def get_node_statistics(api_object, ignore_nodes, maintenance_nodes):
""" Get statistics of cpu, memory and disk for each node in the cluster. """
info_prefix = 'Info: [node-statistics]:'
node_statistics = {}
ignore_nodes_list = ignore_nodes.split(',')
maintenance_nodes_list = maintenance_nodes.split(',')
for node in api_object.nodes.get():
if node['status'] == 'online':
node_statistics[node['node']] = {}
node_statistics[node['node']]['maintenance'] = False
node_statistics[node['node']]['ignore'] = False
node_statistics[node['node']]['cpu_total'] = node['maxcpu']
node_statistics[node['node']]['cpu_assigned'] = 0
node_statistics[node['node']]['cpu_assigned_percent'] = int((node_statistics[node['node']]['cpu_assigned']) / int(node_statistics[node['node']]['cpu_total']) * 100)
node_statistics[node['node']]['cpu_assigned_percent_last_run'] = 0
node_statistics[node['node']]['cpu_used'] = node['cpu']
node_statistics[node['node']]['cpu_free'] = (node['maxcpu']) - (node['cpu'] * node['maxcpu'])
node_statistics[node['node']]['cpu_free_percent'] = int((node_statistics[node['node']]['cpu_free']) / int(node['maxcpu']) * 100)
node_statistics[node['node']]['cpu_free_percent_last_run'] = 0
node_statistics[node['node']]['memory_total'] = node['maxmem']
node_statistics[node['node']]['memory_assigned'] = 0
node_statistics[node['node']]['memory_assigned_percent'] = int((node_statistics[node['node']]['memory_assigned']) / int(node_statistics[node['node']]['memory_total']) * 100)
node_statistics[node['node']]['memory_assigned_percent_last_run'] = 0
node_statistics[node['node']]['memory_used'] = node['mem']
node_statistics[node['node']]['memory_free'] = int(node['maxmem']) - int(node['mem'])
node_statistics[node['node']]['memory_free_percent'] = int((node_statistics[node['node']]['memory_free']) / int(node['maxmem']) * 100)
node_statistics[node['node']]['memory_free_percent_last_run'] = 0
node_statistics[node['node']]['disk_total'] = node['maxdisk']
node_statistics[node['node']]['disk_assigned'] = 0
node_statistics[node['node']]['disk_assigned_percent'] = int((node_statistics[node['node']]['disk_assigned']) / int(node_statistics[node['node']]['disk_total']) * 100)
node_statistics[node['node']]['disk_assigned_percent_last_run'] = 0
node_statistics[node['node']]['disk_used'] = node['disk']
node_statistics[node['node']]['disk_free'] = int(node['maxdisk']) - int(node['disk'])
node_statistics[node['node']]['disk_free_percent'] = int((node_statistics[node['node']]['disk_free']) / int(node['maxdisk']) * 100)
node_statistics[node['node']]['disk_free_percent_last_run'] = 0
logging.info(f'{info_prefix} Added node {node["node"]}.')
# Update node specific vars
if node['node'] in maintenance_nodes_list:
node_statistics[node['node']]['maintenance'] = True
logging.info(f'{info_prefix} Maintenance mode: {node["node"]} is set to maintenance mode.')
if node['node'] in ignore_nodes_list:
node_statistics[node['node']]['ignore'] = True
logging.info(f'{info_prefix} Ignore Node: {node["node"]} is set to be ignored.')
logging.info(f'{info_prefix} Created node statistics.')
return node_statistics
def get_vm_statistics(api_object, ignore_vms, balancing_type):
""" Get statistics of cpu, memory and disk for each vm in the cluster. """
info_prefix = 'Info: [vm-statistics]:'
warn_prefix = 'Warn: [vm-statistics]:'
vm_statistics = {}
ignore_vms_list = ignore_vms.split(',')
group_include = None
group_exclude = None
vm_ignore = None
vm_ignore_wildcard = False
_vm_details_storage_allowed = ['ide', 'nvme', 'scsi', 'virtio', 'sata', 'rootfs']
# Wildcard support: Initially validate if we need to honour
# any wildcards within the vm_ignore list.
vm_ignore_wildcard = __validate_ignore_vm_wildcard(ignore_vms)
for node in api_object.nodes.get():
# Get VM/CT objects only when the node is online and reachable.
if node['status'] == 'online':
# Add all virtual machines if type is vm or all.
if balancing_type == 'vm' or balancing_type == 'all':
for vm in api_object.nodes(node['node']).qemu.get():
# Get the VM tags from API.
vm_tags = __get_vm_tags(api_object, node, vm['vmid'], 'vm')
if vm_tags is not None:
group_include, group_exclude, vm_ignore = __get_proxlb_groups(vm_tags)
# Get wildcard match for VMs to ignore if a wildcard pattern was
# previously found. Wildcards may slow down the task when using
# many patterns in the ignore list. Therefore, run this only if
# a wildcard pattern was found. We also do not need to validate
# this if the VM is already being ignored by a defined tag.
if vm_ignore_wildcard and not vm_ignore:
vm_ignore = __check_vm_name_wildcard_pattern(vm['name'], ignore_vms_list)
if vm['status'] == 'running' and vm['name'] not in ignore_vms_list and not vm_ignore:
vm_statistics[vm['name']] = {}
vm_statistics[vm['name']]['group_include'] = group_include
vm_statistics[vm['name']]['group_exclude'] = group_exclude
vm_statistics[vm['name']]['cpu_total'] = vm['cpus']
vm_statistics[vm['name']]['cpu_used'] = vm['cpu']
vm_statistics[vm['name']]['memory_total'] = vm['maxmem']
vm_statistics[vm['name']]['memory_used'] = vm['mem']
vm_statistics[vm['name']]['disk_total'] = vm['maxdisk']
vm_statistics[vm['name']]['disk_used'] = vm['disk']
vm_statistics[vm['name']]['vmid'] = vm['vmid']
vm_statistics[vm['name']]['node_parent'] = node['node']
vm_statistics[vm['name']]['node_rebalance'] = node['node']
vm_statistics[vm['name']]['storage'] = {}
vm_statistics[vm['name']]['type'] = 'vm'
# Get disk details of the related object.
_vm_details = api_object.nodes(node['node']).qemu(vm['vmid']).config.get()
logging.info(f'{info_prefix} Getting disk information for vm {vm["name"]}.')
for vm_detail_key, vm_detail_value in _vm_details.items():
# vm_detail_key_validator = re.sub('\d+$', '', vm_detail_key)
vm_detail_key_validator = re.sub(r'\d+$', '', vm_detail_key)
if vm_detail_key_validator in _vm_details_storage_allowed:
vm_statistics[vm['name']]['storage'][vm_detail_key] = {}
match = re.match(r'([^:]+):[^/]+/(.+),iothread=\d+,size=(\d+G)', _vm_details[vm_detail_key])
# Create an efficient match group and split the strings to assign them to the storage information.
if match:
_volume = match.group(1)
_disk_name = match.group(2)
_disk_size = match.group(3)
vm_statistics[vm['name']]['storage'][vm_detail_key]['name'] = _disk_name
vm_statistics[vm['name']]['storage'][vm_detail_key]['device_name'] = vm_detail_key
vm_statistics[vm['name']]['storage'][vm_detail_key]['volume'] = _volume
vm_statistics[vm['name']]['storage'][vm_detail_key]['storage_parent'] = _volume
vm_statistics[vm['name']]['storage'][vm_detail_key]['storage_rebalance'] = _volume
vm_statistics[vm['name']]['storage'][vm_detail_key]['size'] = _disk_size[:-1]
logging.info(f'{info_prefix} Added disk for {vm["name"]}: Name {_disk_name} on volume {_volume} with size {_disk_size}.')
else:
logging.info(f'{info_prefix} No (or unsupported) disk(s) for {vm["name"]} found.')
logging.info(f'{info_prefix} Added vm {vm["name"]}.')
# Add all containers if type is ct or all.
if balancing_type == 'ct' or balancing_type == 'all':
for vm in api_object.nodes(node['node']).lxc.get():
logging.warning(f'{warn_prefix} Rebalancing on LXC containers (CT) always requires them to shut down.')
logging.warning(f'{warn_prefix} {vm["name"]} is from type CT and cannot be live migrated!')
# Get the VM tags from API.
vm_tags = __get_vm_tags(api_object, node, vm['vmid'], 'ct')
if vm_tags is not None:
group_include, group_exclude, vm_ignore = __get_proxlb_groups(vm_tags)
# Get wildcard match for VMs to ignore if a wildcard pattern was
# previously found. Wildcards may slow down the task when using
# many patterns in the ignore list. Therefore, run this only if
# a wildcard pattern was found. We also do not need to validate
# this if the VM is already being ignored by a defined tag.
if vm_ignore_wildcard and not vm_ignore:
vm_ignore = __check_vm_name_wildcard_pattern(vm['name'], ignore_vms_list)
if vm['status'] == 'running' and vm['name'] not in ignore_vms_list and not vm_ignore:
vm_statistics[vm['name']] = {}
vm_statistics[vm['name']]['group_include'] = group_include
vm_statistics[vm['name']]['group_exclude'] = group_exclude
vm_statistics[vm['name']]['cpu_total'] = vm['cpus']
vm_statistics[vm['name']]['cpu_used'] = vm['cpu']
vm_statistics[vm['name']]['memory_total'] = vm['maxmem']
vm_statistics[vm['name']]['memory_used'] = vm['mem']
vm_statistics[vm['name']]['disk_total'] = vm['maxdisk']
vm_statistics[vm['name']]['disk_used'] = vm['disk']
vm_statistics[vm['name']]['vmid'] = vm['vmid']
vm_statistics[vm['name']]['node_parent'] = node['node']
vm_statistics[vm['name']]['node_rebalance'] = node['node']
vm_statistics[vm['name']]['storage'] = {}
vm_statistics[vm['name']]['type'] = 'ct'
# Get disk details of the related object.
_vm_details = api_object.nodes(node['node']).lxc(vm['vmid']).config.get()
logging.info(f'{info_prefix} Getting disk information for vm {vm["name"]}.')
for vm_detail_key, vm_detail_value in _vm_details.items():
# vm_detail_key_validator = re.sub('\d+$', '', vm_detail_key)
vm_detail_key_validator = re.sub(r'\d+$', '', vm_detail_key)
if vm_detail_key_validator in _vm_details_storage_allowed:
vm_statistics[vm['name']]['storage'][vm_detail_key] = {}
match = re.match(r'(?P<volume>[^:]+):(?P<disk_name>[^,]+),size=(?P<disk_size>\S+)', _vm_details[vm_detail_key])
# Create an efficient match group and split the strings to assign them to the storage information.
if match:
_volume = match.group(1)
_disk_name = match.group(2)
_disk_size = match.group(3)
vm_statistics[vm['name']]['storage'][vm_detail_key]['name'] = _disk_name
vm_statistics[vm['name']]['storage'][vm_detail_key]['device_name'] = vm_detail_key
vm_statistics[vm['name']]['storage'][vm_detail_key]['volume'] = _volume
vm_statistics[vm['name']]['storage'][vm_detail_key]['storage_parent'] = _volume
vm_statistics[vm['name']]['storage'][vm_detail_key]['storage_rebalance'] = _volume
vm_statistics[vm['name']]['storage'][vm_detail_key]['size'] = _disk_size[:-1]
logging.info(f'{info_prefix} Added disk for {vm["name"]}: Name {_disk_name} on volume {_volume} with size {_disk_size}.')
else:
logging.info(f'{info_prefix} No disks for {vm["name"]} found.')
logging.info(f'{info_prefix} Added vm {vm["name"]}.')
logging.info(f'{info_prefix} Created VM statistics.')
return vm_statistics
def update_node_statistics(node_statistics, vm_statistics):
""" Update node statistics by VMs statistics. """
info_prefix = 'Info: [node-update-statistics]:'
warn_prefix = 'Warning: [node-update-statistics]:'
for vm, vm_value in vm_statistics.items():
node_statistics[vm_value['node_parent']]['cpu_assigned'] = node_statistics[vm_value['node_parent']]['cpu_assigned'] + int(vm_value['cpu_total'])
node_statistics[vm_value['node_parent']]['cpu_assigned_percent'] = (node_statistics[vm_value['node_parent']]['cpu_assigned'] / node_statistics[vm_value['node_parent']]['cpu_total']) * 100
node_statistics[vm_value['node_parent']]['memory_assigned'] = node_statistics[vm_value['node_parent']]['memory_assigned'] + int(vm_value['memory_total'])
node_statistics[vm_value['node_parent']]['memory_assigned_percent'] = (node_statistics[vm_value['node_parent']]['memory_assigned'] / node_statistics[vm_value['node_parent']]['memory_total']) * 100
node_statistics[vm_value['node_parent']]['disk_assigned'] = node_statistics[vm_value['node_parent']]['disk_assigned'] + int(vm_value['disk_total'])
node_statistics[vm_value['node_parent']]['disk_assigned_percent'] = (node_statistics[vm_value['node_parent']]['disk_assigned'] / node_statistics[vm_value['node_parent']]['disk_total']) * 100
if node_statistics[vm_value['node_parent']]['cpu_assigned_percent'] > 99:
logging.warning(f'{warn_prefix} Node {vm_value["node_parent"]} is overprovisioned for CPU by {int(node_statistics[vm_value["node_parent"]]["cpu_assigned_percent"])}%.')
if node_statistics[vm_value['node_parent']]['memory_assigned_percent'] > 99:
logging.warning(f'{warn_prefix} Node {vm_value["node_parent"]} is overprovisioned for memory by {int(node_statistics[vm_value["node_parent"]]["memory_assigned_percent"])}%.')
if node_statistics[vm_value['node_parent']]['disk_assigned_percent'] > 99:
logging.warning(f'{warn_prefix} Node {vm_value["node_parent"]} is overprovisioned for disk by {int(node_statistics[vm_value["node_parent"]]["disk_assigned_percent"])}%.')
logging.info(f'{info_prefix} Updated node resource assignments by all VMs.')
logging.debug('node_statistics')
return node_statistics
def get_storage_statistics(api_object):
""" Get statistics of all storage in the cluster. """
info_prefix = 'Info: [storage-statistics]:'
storage_whitelist = ['nfs']
storage_statistics = {}
for node in api_object.nodes.get():
for storage in api_object.nodes(node['node']).storage.get():
# Only add enabled and active storage repositories that might be suitable for further
# storage balancing.
if storage['enabled'] and storage['active'] and storage['shared'] and storage['type'] in storage_whitelist:
storage_statistics[storage['storage']] = {}
storage_statistics[storage['storage']]['name'] = storage['storage']
storage_statistics[storage['storage']]['total'] = storage['total']
storage_statistics[storage['storage']]['used'] = storage['used']
storage_statistics[storage['storage']]['used_percent'] = storage['used'] / storage['total'] * 100
storage_statistics[storage['storage']]['used_percent_last_run'] = 0
storage_statistics[storage['storage']]['free'] = storage['total'] - storage['used']
storage_statistics[storage['storage']]['free_percent'] = storage_statistics[storage['storage']]['free'] / storage['total'] * 100
storage_statistics[storage['storage']]['used_fraction'] = storage['used_fraction']
storage_statistics[storage['storage']]['type'] = storage['type']
storage_statistics[storage['storage']]['content'] = storage['content']
storage_statistics[storage['storage']]['usage_type'] = ''
# Split the Proxmox returned values to a list and validate the supported
# types of the underlying storage for further migrations.
storage_content_list = storage['content'].split(',')
usage_ct = False
usage_vm = False
if 'rootdir' in storage_content_list:
usage_ct = True
storage_statistics[storage['storage']]['usage_type'] = 'ct'
logging.info(f'{info_prefix} Storage {storage["storage"]} support CTs.')
if 'images' in storage_content_list:
usage_vm = True
storage_statistics[storage['storage']]['usage_type'] = 'vm'
logging.info(f'{info_prefix} Storage {storage["storage"]} support VMs.')
if usage_ct and usage_vm:
storage_statistics[storage['storage']]['usage_type'] = 'all'
logging.info(f'{info_prefix} Updateing storage {storage["storage"]} support to CTs and VMs.')
logging.info(f'{info_prefix} Added storage {storage["storage"]}.')
logging.info(f'{info_prefix} Created storage statistics.')
return storage_statistics
def __validate_ignore_vm_wildcard(ignore_vms):
""" Validate if a wildcard is used for ignored VMs. """
if '*' in ignore_vms:
return True
def __check_vm_name_wildcard_pattern(vm_name, ignore_vms_list):
""" Validate if the VM name is in the ignore list pattern included. """
for ignore_vm in ignore_vms_list:
if '*' in ignore_vm:
if ignore_vm[:-1] in vm_name:
return True
def __get_vm_tags(api_object, node, vmid, balancing_type):
""" Get tags for a VM/CT for a given VMID. """
info_prefix = 'Info: [api-get-vm-tags]:'
if balancing_type == 'vm':
vm_config = api_object.nodes(node['node']).qemu(vmid).config.get()
if balancing_type == 'ct':
vm_config = api_object.nodes(node['node']).lxc(vmid).config.get()
if vm_config.get("tags", None) is None:
logging.info(f'{info_prefix} Got no VM/CT tag for VM {vm_config.get("name", None)} from API.')
else:
logging.info(f'{info_prefix} Got VM/CT tag {vm_config.get("tags", None)} for VM {vm_config.get("name", None)} from API.')
return vm_config.get('tags', None)
def __get_proxlb_groups(vm_tags):
""" Get ProxLB related include and exclude groups. """
info_prefix = 'Info: [api-get-vm-include-exclude-tags]:'
group_include = None
group_exclude = None
vm_ignore = None
group_list = re.split(";", vm_tags)
for group in group_list:
if group.startswith('plb_include_'):
logging.info(f'{info_prefix} Got PLB include group.')
group_include = group
if group.startswith('plb_affinity_'):
logging.info(f'{info_prefix} Got PLB include group.')
group_include = group
if group.startswith('plb_exclude_'):
logging.info(f'{info_prefix} Got PLB exclude group.')
group_exclude = group
if group.startswith('plb_antiaffinity_'):
logging.info(f'{info_prefix} Got PLB exclude group.')
group_exclude = group
if group.startswith('plb_ignore_vm'):
logging.info(f'{info_prefix} Got PLB ignore group.')
vm_ignore = True
return group_include, group_exclude, vm_ignore
def balancing_vm_calculations(balancing_method, balancing_mode, balancing_mode_option, node_statistics, vm_statistics, balanciness, app_args, rebalance, processed_vms):
""" Calculate re-balancing of VMs on present nodes across the cluster. """
info_prefix = 'Info: [rebalancing-vm-calculator]:'
# Validate for a supported balancing method, mode and if rebalancing is required.
__validate_balancing_method(balancing_method)
__validate_balancing_mode(balancing_mode)
__validate_vm_statistics(vm_statistics)
rebalance = __validate_balanciness(balanciness, balancing_method, balancing_mode, node_statistics)
# Run rebalancing calculations.
if rebalance:
# Get most used/assigned resources of the VM and the most free or less allocated node.
resources_vm_most_used, processed_vms = __get_most_used_resources_vm(balancing_method, balancing_mode, vm_statistics, processed_vms)
resources_node_most_free = __get_most_free_resources_node(balancing_method, balancing_mode, balancing_mode_option, node_statistics)
# If most used vm is on most free node then skip it and get another one.
while resources_vm_most_used[1]['node_parent'] == resources_node_most_free[0] and len(processed_vms) < len(vm_statistics):
resources_vm_most_used, processed_vms = __get_most_used_resources_vm(balancing_method, balancing_mode, vm_statistics, processed_vms)
logging.debug(f'{info_prefix} processed {len(processed_vms)} out of {len(vm_statistics)} vms.')
# Update resource statistics for VMs and nodes.
node_statistics, vm_statistics = __update_vm_resource_statistics(resources_vm_most_used, resources_node_most_free,
vm_statistics, node_statistics, balancing_method, balancing_mode)
# Start recursion until we do not have any needs to rebalance anymore.
balancing_vm_calculations(balancing_method, balancing_mode, balancing_mode_option, node_statistics, vm_statistics, balanciness, app_args, rebalance, processed_vms)
# If only best node argument set we simply return the next best node for VM
# and CT placement on the CLI and stop ProxLB.
if app_args.best_node:
logging.info(f'{info_prefix} Only best next node for new VM & CT placement requsted.')
best_next_node = __get_most_free_resources_node(balancing_method, balancing_mode, balancing_mode_option, node_statistics)
print(best_next_node[0])
logging.info(f'{info_prefix} Best next node for VM & CT placement: {best_next_node[0]}')
sys.exit(0)
logging.info(f'{info_prefix} Balancing calculations done.')
return node_statistics, vm_statistics
def balancing_vm_maintenance(proxlb_config, app_args, node_statistics, vm_statistics):
""" Calculate re-balancing of VMs that need to be moved away from maintenance nodes. """
info_prefix = 'Info: [rebalancing-maintenance-vm-calculator]:'
maintenance_nodes_list = proxlb_config['vm_maintenance_nodes'].split(',')
nodes_present = list(node_statistics.keys())
balancing_method = proxlb_config['vm_balancing_method']
balancing_mode = proxlb_config['vm_balancing_mode']
balancing_mode_option = proxlb_config['vm_balancing_mode_option']
# Merge maintenance nodes from config and cli args.
if app_args.maintenance is not None:
logging.info(f'{info_prefix} Maintenance nodes from CLI arg and config will be merged.')
maintenance_nodes_list = maintenance_nodes_list + app_args.maintenance.split(',')
# Ensure that only existing nodes in the cluster will be used.
if len(proxlb_config['vm_maintenance_nodes']) > 1:
maintenance_nodes_list = set(maintenance_nodes_list) & set(nodes_present)
logging.info(f'{info_prefix} Maintenance mode for the following hosts defined: {maintenance_nodes_list}')
else:
logging.info(f'{info_prefix} No nodes for maintenance mode defined.')
return node_statistics, vm_statistics
for node_name in maintenance_nodes_list:
node_vms = list(filter(lambda item: item[0] if item[1]['node_parent'] == node_name else [], vm_statistics.items()))
# Update resource statistics for VMs and nodes.
for vm in node_vms:
resources_node_most_free = __get_most_free_resources_node(balancing_method, balancing_mode, balancing_mode_option, node_statistics)
node_statistics, vm_statistics = __update_vm_resource_statistics(vm, resources_node_most_free, vm_statistics, node_statistics, balancing_method, balancing_mode)
return node_statistics, vm_statistics
def __validate_balancing_method(balancing_method):
""" Validate for valid and supported balancing method. """
error_prefix = 'Error: [balancing-method-validation]:'
info_prefix = 'Info: [balancing-method-validation]:'
if balancing_method not in ['memory', 'disk', 'cpu']:
logging.error(f'{error_prefix} Invalid balancing method: {balancing_method}')
sys.exit(2)
else:
logging.info(f'{info_prefix} Valid balancing method: {balancing_method}')
def __validate_balancing_mode(balancing_mode):
""" Validate for valid and supported balancing mode. """
error_prefix = 'Error: [balancing-mode-validation]:'
info_prefix = 'Info: [balancing-mode-validation]:'
if balancing_mode not in ['used', 'assigned']:
logging.error(f'{error_prefix} Invalid balancing method: {balancing_mode}')
sys.exit(2)
else:
logging.info(f'{info_prefix} Valid balancing method: {balancing_mode}')
def __validate_vm_statistics(vm_statistics):
""" Validate for at least a single object of type CT/VM to rebalance. """
error_prefix = 'Error: [balancing-vm-stats-validation]:'
if len(vm_statistics) == 0:
logging.error(f'{error_prefix} Not a single CT/VM found in cluster.')
sys.exit(1)
def __validate_balanciness(balanciness, balancing_method, balancing_mode, node_statistics):
""" Validate for balanciness to ensure further rebalancing is needed. """
info_prefix = 'Info: [balanciness-validation]:'
node_resource_percent_list = []
node_assigned_percent_match = []
# Remap balancing mode to get the related values from nodes dict.
if balancing_mode == 'used':
node_resource_selector = 'free'
if balancing_mode == 'assigned':
node_resource_selector = 'assigned'
for node_name, node_info in node_statistics.items():
# Save information of nodes from current run to compare them in the next recursion.
if node_statistics[node_name][f'{balancing_method}_{node_resource_selector}_percent_last_run'] == node_statistics[node_name][f'{balancing_method}_{node_resource_selector}_percent']:
node_statistics[node_name][f'{balancing_method}_{node_resource_selector}_percent_match'] = True
else:
node_statistics[node_name][f'{balancing_method}_{node_resource_selector}_percent_match'] = False
# Update value to the current value of the recursion run.
node_statistics[node_name][f'{balancing_method}_{node_resource_selector}_percent_last_run'] = node_statistics[node_name][f'{balancing_method}_{node_resource_selector}_percent']
# If all node resources are unchanged, the recursion can be left.
for key, value in node_statistics.items():
node_assigned_percent_match.append(value.get(f'{balancing_method}_{node_resource_selector}_percent_match', False))
if False not in node_assigned_percent_match:
return False
# Add node information to resource list.
if not node_statistics[node_name]['maintenance']:
node_resource_percent_list.append(int(node_info[f'{balancing_method}_{node_resource_selector}_percent']))
logging.debug(f'{info_prefix} Node: {node_name} with values: {node_info}')
# Create a sorted list of the delta + balanciness between the node resources.
node_resource_percent_list_sorted = sorted(node_resource_percent_list)
node_lowest_percent = node_resource_percent_list_sorted[0]
node_highest_percent = node_resource_percent_list_sorted[-1]
# Validate if the recursion should be proceeded for further rebalancing.
if (int(node_lowest_percent) + int(balanciness)) < int(node_highest_percent):
logging.info(f'{info_prefix} Rebalancing for {balancing_method} is needed. Highest usage: {int(node_highest_percent)}% | Lowest usage: {int(node_lowest_percent)}%.')
return True
else:
logging.info(f'{info_prefix} Rebalancing for {balancing_method} is not needed. Highest usage: {int(node_highest_percent)}% | Lowest usage: {int(node_lowest_percent)}%.')
return False
def __get_most_used_resources_vm(balancing_method, balancing_mode, vm_statistics, processed_vms):
""" Get and return the most used resources of a VM by the defined balancing method. """
info_prefix = 'Info: [get-most-used-resources-vm]:'
# Remap balancing mode to get the related values from nodes dict.
if balancing_mode == 'used':
vm_resource_selector = 'used'
if balancing_mode == 'assigned':
vm_resource_selector = 'total'
vm = max(vm_statistics.items(), key=lambda item: item[1][f'{balancing_method}_{vm_resource_selector}'] if item[0] not in processed_vms else -float('inf'))
processed_vms.append(vm[0])
logging.info(f'{info_prefix} {vm}')
return vm, processed_vms
def __get_most_free_resources_node(balancing_method, balancing_mode, balancing_mode_option, node_statistics):
""" Get and return the most free resources of a node by the defined balancing method. """
info_prefix = 'Info: [get-most-free-resources-nodes]:'
# Return the node information based on the balancing mode.
if balancing_mode == 'used' and balancing_mode_option == 'bytes':