Last active
January 5, 2019 12:29
-
-
Save gartmeier/5c92848bf14e7345cf3f21374d2fd7c2 to your computer and use it in GitHub Desktop.
CloudWatch metric for AWS ECS container storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import subprocess | |
import boto3 | |
cw = boto3.client('cloudwatch', region_name='eu-west-1') | |
ctn_ip = subprocess.check_output('curl --silent ifconfig.co', shell=True).strip() | |
stats_lines = subprocess.check_output([ | |
'/usr/bin/docker', | |
'stats', | |
'--no-stream', | |
'--format', | |
'"{{ .Container }},{{ .Name }}"' | |
]).split('\n') | |
for stats_line in stats_lines: | |
stats = stats_line[1:-1].split(',') | |
if len(stats) != 2: | |
continue | |
ctn_id,ctn_name = stats | |
try: | |
storage_perc = int(subprocess.check_output('docker exec %s /bin/df -H / --output=pcent | tail -n 1 | cut -d ''%%'' -f 1' % ctn_id, shell=True)) | |
except subprocess.CalledProcessError: | |
continue | |
except ValueError: | |
continue | |
cw.put_metric_data( | |
Namespace='Phoenix', | |
MetricData=[ | |
{ | |
'MetricName': 'StoragePerc', | |
'Unit': 'Percent', | |
'Value': storage_perc, | |
'Dimensions': [ | |
{'Name': 'ContainerName', 'Value': ctn_name}, | |
{'Name': 'ContainerID', 'Value': ctn_id}, | |
{'Name': 'ContainerInstance', 'Value': ctn_ip} | |
] | |
} | |
] | |
) | |
#response = cw.put_metric_alarm( | |
# AlarmName='Phoenix Storage Alarm', | |
# EvaluationPeriods=1, | |
# Threshold=80, | |
# ComparisonOperator='GreaterThanOrEqualToThreshold', | |
# Namespace='Phoenix', | |
# MetricName='StoragePerc', | |
# Period=300, | |
# Statistic='Maximum' | |
#) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment