Created
November 20, 2018 00:52
-
-
Save mweiden/035979ee28a000e48cc655058c6e1a73 to your computer and use it in GitHub Desktop.
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/env python3 | |
""" | |
requires python 3.7 | |
""" | |
import argparse | |
import re | |
import sys | |
import datetime | |
import boto3 | |
UUID_REGEX = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" | |
cloudwatch = boto3.client('cloudwatch') | |
def uuid_type(s): | |
if not re.compile(UUID_REGEX).match(s): | |
raise argparse.ArgumentTypeError(f"Does not match r\"{UUID_REGEX}\"") | |
return s | |
if __name__ == '__main__': | |
name = sys.argv[0].split('/')[-1] | |
end_date = datetime.datetime.utcnow() | |
start_date = end_date - datetime.timedelta(days=7) | |
cli = argparse.ArgumentParser( | |
description=f"`{name}` determines the availability of your service based on a route53 health check." | |
) | |
cli.add_argument( | |
"-s", | |
"--start-date", | |
dest="start_date", | |
help="The Start Date - format YYYY-MM-DD", | |
default=start_date, | |
type=datetime.datetime.fromisoformat | |
) | |
cli.add_argument( | |
"-e", | |
"--end-date", | |
dest="end_date", | |
help="The End Date format YYYY-MM-DD (Inclusive)", | |
default=end_date, | |
type=datetime.datetime.fromisoformat | |
) | |
cli.add_argument( | |
"health_check_id", | |
type=uuid_type, | |
help="The UUID of your application's health check" | |
) | |
options = cli.parse_args(sys.argv[1:]) | |
period = int((options.end_date - options.start_date).total_seconds()) | |
print(f"start date:\t{options.start_date.isoformat()}") | |
print(f"end date:\t{options.end_date.isoformat()}") | |
print(f"period:\t\t{period}s") | |
data = cloudwatch.get_metric_data( | |
MetricDataQueries=[ | |
{ | |
'Id': "sum", | |
'MetricStat': { | |
'Metric': { | |
'Namespace': 'AWS/Route53', | |
'MetricName': 'HealthCheckPercentageHealthy', | |
'Dimensions': [ | |
{ | |
'Name': 'HealthCheckId', | |
'Value': options.health_check_id | |
}, | |
] | |
}, | |
'Period': period, | |
'Stat': 'Sum', | |
}, | |
'ReturnData': True | |
}, | |
{ | |
'Id': "sample_count", | |
'MetricStat': { | |
'Metric': { | |
'Namespace': 'AWS/Route53', | |
'MetricName': 'HealthCheckPercentageHealthy', | |
'Dimensions': [ | |
{ | |
'Name': 'HealthCheckId', | |
'Value': options.health_check_id | |
}, | |
] | |
}, | |
'Period': period, | |
'Stat': 'SampleCount', | |
}, | |
'ReturnData': True | |
}, | |
], | |
StartTime=options.start_date, | |
EndTime=options.end_date | |
)['MetricDataResults'] | |
metric_sum = next(ele for ele in data if ele['Id'] == 'sum')['Values'][0] | |
metric_sample_count = next(ele for ele in data if ele['Id'] == 'sample_count')['Values'][0] | |
print(f"availability:\t{metric_sum / metric_sample_count}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment