Skip to content

Instantly share code, notes, and snippets.

@pistatium
Last active September 28, 2018 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pistatium/332611afa4f9533bfeaf95a1d84c3291 to your computer and use it in GitHub Desktop.
Save pistatium/332611afa4f9533bfeaf95a1d84c3291 to your computer and use it in GitHub Desktop.
ECSクラスタのCPU予約量をタスクごとに集計するやーつ
import os
import boto3
from collections import defaultdict
CLUSTER = os.environ.get('TARGET_CLUSTER')
ecs = boto3.client('ecs')
token = ''
cpus = defaultdict(int)
while True:
res = ecs.list_tasks(cluster=CLUSTER, desiredStatus='RUNNING', maxResults=100, launchType='EC2', nextToken=token)
tasks = res['taskArns']
tasks = ecs.describe_tasks(cluster=CLUSTER, tasks=tasks)
for t in tasks['tasks']:
cpus[t['taskDefinitionArn'].split('/')[-1]] += int(t['cpu'])
token = res.get('nextToken')
if not token:
break
for name, cpu in cpus.items():
print(f'{cpu} {name}')
export TARGET_CLUSTER=クラスタ名
export AWS_XXXX = ...

pip install boto3

python3 ecs_cpu_analyzer.py | sort -n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment