in BackendServices/functions/scaler.py [0:0]
def get_available_memory_and_cpu(ecs_cluster_name):
total_cpu = 0
total_memory = 0
ecs = boto3.client("ecs")
firstround = True
nextToken = None
# Use pagination to get all instances beyond the first 100
while firstround or nextToken != None:
if nextToken == None:
response = ecs.list_container_instances(
cluster=ecs_cluster_name,
)
else:
response = ecs.list_container_instances(
cluster=ecs_cluster_name, nextToken=nextToken
)
print(response)
if "nextToken" in response:
print("found next token")
nextToken = response["nextToken"]
else:
nextToken = None
container_instances = response["containerInstanceArns"]
if len(container_instances) > 0:
# Get instance id, not full arn
container_instances_id_only = []
for arn in container_instances:
container_instances_id_only.append(arn.split("/")[2])
response = ecs.describe_container_instances(
cluster=ecs_cluster_name,
containerInstances=container_instances_id_only
)
for instance in response["containerInstances"]:
for remaining_resource in instance["remainingResources"]:
if remaining_resource["name"] == "CPU":
#print("Remaining CPU: " + str(remaining_resource["integerValue"]))
total_cpu += int(remaining_resource["integerValue"])
elif remaining_resource["name"] == "MEMORY":
#print("Remaining Memoery: " + str(remaining_resource["integerValue"]))
total_memory += int(remaining_resource["integerValue"])
firstround = False
print("Subtotal: " + str(total_cpu) + "," + str(total_memory))
return total_cpu, total_memory