Skip to content

Instantly share code, notes, and snippets.

@nikhilo
Created October 25, 2018 21:03
Show Gist options
  • Save nikhilo/5ac654d7896a394e686240dec5c9fc66 to your computer and use it in GitHub Desktop.
Save nikhilo/5ac654d7896a394e686240dec5c9fc66 to your computer and use it in GitHub Desktop.
import boto3
import sys
client = boto3.client('ecs')
def print_resource_usage(usage):
# print usage
print "Instance\t\t\t\tCPU\tMemory\tPorts"
print "========================================================================"
for instance in usage.keys():
print ("%s\t%d\t%d\t%s" % (usage[instance]['shortName'],
usage[instance]['CPU'],
usage[instance]['Memory'],
usage[instance]['Ports']
)
)
def get_resource_usage(cluster, instances):
resource_usage = {}
response = client.describe_container_instances(cluster=cluster, containerInstances=instances)['containerInstances']
for instance in response:
instanceArn = instance['containerInstanceArn']
resource_usage[instanceArn] = {}
resource_usage[instanceArn]['shortName'] = instanceArn.split('/')[1]
resource_usage[instanceArn]['CPU'] = filter(
lambda x: x['name'] == 'CPU',
instance['remainingResources'])[0]['integerValue']
resource_usage[instanceArn]['Memory'] = filter(
lambda x: x['name'] == 'MEMORY',
instance['remainingResources'])[0]['integerValue']
ports = filter(
lambda x: x['name'] == 'PORTS',
instance['remainingResources'])[0]['stringSetValue']
resource_usage[instanceArn]['Ports'] = [ str(port) for port in ports ]
return resource_usage
def get_container_instances(cluster):
instances = client.list_container_instances(cluster=cluster)['containerInstanceArns']
return instances
def main():
usage = "Usage: ecs_list_resources.py <cluster_name>"
if len(sys.argv) < 2:
print usage
sys.exit(1)
cluster = sys.argv[1]
instances = get_container_instances(cluster)
resource_usage = get_resource_usage(cluster, instances)
print_resource_usage(resource_usage)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment