10
10
)
11
11
from notification import send_notification # Import the notification function
12
12
from config import HORIZONTAL_SCALING_GROUPS , IGNORE_LXC , DEFAULTS # Import configuration constants
13
+ import paramiko
13
14
14
15
# Constants for repeated values
15
16
TIMEOUT_EXTENDED = 300 # Extended timeout for scaling operations
@@ -129,7 +130,7 @@ def scale_memory(ctid, mem_usage, mem_upper, mem_lower, current_memory, min_memo
129
130
130
131
elif mem_usage < mem_lower and current_memory > min_memory :
131
132
decrease_amount = calculate_decrement (
132
- mem_usage , mem_lower , current_memory ,
133
+ mem_usage , mem_lower , current_memory ,
133
134
int (config ['min_decrease_chunk' ] * behaviour_multiplier ), min_memory
134
135
)
135
136
if decrease_amount > 0 :
@@ -146,7 +147,7 @@ def scale_memory(ctid, mem_usage, mem_upper, mem_lower, current_memory, min_memo
146
147
def adjust_resources (containers , energy_mode ):
147
148
"""
148
149
Adjust CPU and memory resources for each container based on usage.
149
-
150
+
150
151
Args:
151
152
containers (dict): A dictionary of container resource usage data.
152
153
energy_mode (bool): Flag to indicate if energy-saving adjustments should be made during off-peak hours.
@@ -172,7 +173,7 @@ def adjust_resources(containers, energy_mode):
172
173
rounded_mem_usage = round (usage ['mem' ], 2 )
173
174
total_mem_allocated = usage ['initial_memory' ]
174
175
free_mem_percent = round (100 - ((rounded_mem_usage / total_mem_allocated ) * 100 ), 2 )
175
-
176
+
176
177
logging .info (f"Container { ctid } : CPU usage: { rounded_cpu_usage } %, Memory usage: { rounded_mem_usage } MB "
177
178
f"({ free_mem_percent } % free of { total_mem_allocated } MB total), "
178
179
f"Initial cores: { usage ['initial_cores' ]} , Initial memory: { total_mem_allocated } MB" )
@@ -219,7 +220,7 @@ def adjust_resources(containers, energy_mode):
219
220
send_notification (f"CPU Increased for Container { ctid } " , f"CPU cores increased to { new_cores } ." )
220
221
else :
221
222
logging .warning (f"Container { ctid } - Not enough available cores to increase." )
222
-
223
+
223
224
elif cpu_usage < cpu_lower and current_cores > min_cores :
224
225
decrement = calculate_decrement (cpu_usage , cpu_lower , current_cores , config ['core_min_increment' ], min_cores )
225
226
new_cores = max (min_cores , current_cores - decrement )
@@ -274,7 +275,7 @@ def manage_horizontal_scaling(containers):
274
275
total_cpu_usage = sum (containers [ctid ]['cpu' ] for ctid in group_config ['lxc_containers' ] if ctid in containers )
275
276
total_mem_usage = sum (containers [ctid ]['mem' ] for ctid in group_config ['lxc_containers' ] if ctid in containers )
276
277
num_containers = len (group_config ['lxc_containers' ])
277
-
278
+
278
279
if num_containers > 0 :
279
280
avg_cpu_usage = total_cpu_usage / num_containers
280
281
avg_mem_usage = total_mem_usage / num_containers
@@ -285,10 +286,10 @@ def manage_horizontal_scaling(containers):
285
286
logging .debug (f"Group: { group_name } | Average CPU Usage: { avg_cpu_usage } % | Average Memory Usage: { avg_mem_usage } %" )
286
287
287
288
# Check if scaling out is needed based on usage thresholds
288
- if (avg_cpu_usage > group_config ['horiz_cpu_upper_threshold' ] or
289
+ if (avg_cpu_usage > group_config ['horiz_cpu_upper_threshold' ] or
289
290
avg_mem_usage > group_config ['horiz_memory_upper_threshold' ]):
290
291
logging .debug (f"Thresholds exceeded for { group_name } . Evaluating scale-out conditions." )
291
-
292
+
292
293
# Ensure enough time has passed since the last scaling action
293
294
if current_time - last_action_time >= timedelta (seconds = group_config .get ('scale_out_grace_period' , 300 )):
294
295
scale_out (group_name , group_config )
0 commit comments