diff --git a/usr/local/bin/lxc_autoscale.py b/usr/local/bin/lxc_autoscale.py index 9d74aa3..a68201f 100644 --- a/usr/local/bin/lxc_autoscale.py +++ b/usr/local/bin/lxc_autoscale.py @@ -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'] @@ -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( @@ -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}." @@ -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}." @@ -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." @@ -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." @@ -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." @@ -493,7 +493,6 @@ 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." @@ -501,7 +500,6 @@ def adjust_resources(containers): 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: