Skip to content

Commit

Permalink
Update lxc_autoscale.py
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziosalmi authored Aug 14, 2024
1 parent 63bf92d commit c82db52
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions usr/local/bin/lxc_autoscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,16 @@ def adjust_resources(containers):
logging.info("No containers to adjust.")
return

available_cores = get_total_cores()
available_memory = get_total_memory()
total_cores = int(run_command("nproc"))
total_memory = int(run_command("free -m | awk '/Mem:/ {print $2}'"))

reserved_cores = max(1, int(total_cores * RESERVE_CPU_PERCENT / 100))
reserved_memory = RESERVE_MEMORY_MB

available_cores = total_cores - reserved_cores
available_memory = total_memory - reserved_memory

logging.info(f"Initial resources before adjustments: {available_cores} cores, {available_memory} MB memory")

for ctid, usage in containers:
cpu_usage = usage['cpu']
Expand All @@ -407,9 +415,6 @@ def adjust_resources(containers):
current_cores = usage["initial_cores"]
current_memory = usage["initial_memory"]

cores_changed = False
memory_changed = False

# Adjust CPU cores if needed
if cpu_usage > args.cpu_upper:
increment = min(
Expand All @@ -421,7 +426,6 @@ def adjust_resources(containers):
logging.info(f"Increasing cores for container {ctid} by {increment}...")
run_command(f"pct set {ctid} -cores {new_cores}")
available_cores -= increment
cores_changed = True
send_gotify_notification(
f"CPU Increased for Container {ctid}",
f"CPU cores increased to {new_cores}."
Expand All @@ -438,7 +442,6 @@ def adjust_resources(containers):
logging.info(f"Decreasing cores for container {ctid} by {decrement}...")
run_command(f"pct set {ctid} -cores {new_cores}")
available_cores += decrement
cores_changed = True
send_gotify_notification(
f"CPU Decreased for Container {ctid}",
f"CPU cores decreased to {new_cores}."
Expand All @@ -455,7 +458,6 @@ def adjust_resources(containers):
new_memory = current_memory + increment
run_command(f"pct set {ctid} -memory {new_memory}")
available_memory -= increment
memory_changed = True
send_gotify_notification(
f"Memory Increased for Container {ctid}",
f"Memory increased by {increment}MB."
Expand All @@ -472,7 +474,6 @@ def adjust_resources(containers):
new_memory = current_memory - decrease_amount
run_command(f"pct set {ctid} -memory {new_memory}")
available_memory += decrease_amount
memory_changed = True
send_gotify_notification(
f"Memory Decreased for Container {ctid}",
f"Memory decreased by {decrease_amount}MB."
Expand All @@ -484,7 +485,6 @@ def adjust_resources(containers):
logging.info(f"Reducing cores for energy efficiency during off-peak hours for container {ctid}...")
run_command(f"pct set {ctid} -cores {args.min_cores}")
available_cores += (current_cores - args.min_cores)
cores_changed = True
send_gotify_notification(
f"CPU Reduced for Container {ctid}",
f"CPU cores reduced to {args.min_cores} for energy efficiency."
Expand All @@ -493,15 +493,13 @@ def adjust_resources(containers):
logging.info(f"Reducing memory for energy efficiency during off-peak hours for container {ctid}...")
run_command(f"pct set {ctid} -memory {args.min_mem}")
available_memory += (current_memory - args.min_mem)
memory_changed = True
send_gotify_notification(
f"Memory Reduced for Container {ctid}",
f"Memory reduced to {args.min_mem}MB for energy efficiency."
)

logging.info(f"Final resources after adjustments: {available_cores} cores, {available_memory} MB memory")


def main_loop():
"""Main loop for resource allocation process."""
while running:
Expand Down

0 comments on commit c82db52

Please sign in to comment.