Last active
April 30, 2018 05:04
-
-
Save enkeboll/f1bdfe96bcf6047760ee7a8fc60762af to your computer and use it in GitHub Desktop.
Get Redshift metrics from Cloudwatch
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
import bisect | |
import datetime | |
import operator | |
import boto3 | |
client = boto3.client('cloudwatch') | |
def get_metric(offset, metricname, threshold, oper='ge', stat='Average'): | |
if offset >= 64: | |
period, scalar = 3600, 60 | |
elif offset >= 15: | |
period, scalar = 300, 5 | |
else: | |
period, scalar = 60, 1 | |
dt = (datetime.datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0) | |
- datetime.timedelta(offset)) | |
response = client.get_metric_statistics( | |
Namespace='AWS/Redshift', | |
MetricName=metricname, | |
Dimensions=[ | |
{ | |
'Name': 'ClusterIdentifier', | |
'Value': 'sg-data-warehouse-2' | |
}, | |
], | |
StartTime=dt, | |
EndTime=dt + datetime.timedelta(1), | |
Period=period, | |
Statistics=[stat], | |
) | |
yes, no = 0, 0 | |
for datapoint in response['Datapoints']: | |
if eval("operator.{}(datapoint[stat], threshold)".format(oper)): | |
yes += 1 | |
else: | |
no += 1 | |
return [x * scalar for x in (yes, no)] | |
def get_quarter_begin(): | |
today = datetime.datetime.utcnow() | |
qbegins = [datetime.datetime(today.year, month, 1,) for month in (1,4,7,10)] | |
idx = bisect.bisect(qbegins, today) | |
return qbegins[idx-1] | |
def main(): | |
trail_days = max(7, (datetime.datetime.utcnow() - get_quarter_begin()).days) | |
cpu = [] | |
disk = [] | |
for offset in range(1, trail_days + 1): | |
cpu.append(get_metric(offset, 'CPUUtilization', 95, 'gt', 'Maximum')) | |
disk.append(get_metric(offset, 'PercentageDiskSpaceUsed', 80, 'gt', 'Minimum')) | |
msg = """ | |
CPU: {} of {} events were over the 95% threshold. | |
{:.2f}% | |
Disk Space: {} of {} events were over the 80% threshold. | |
{:.2f}% | |
""".format( | |
sum(zip(*cpu)[0]), | |
sum([y for x in zip(*cpu) for y in x]), | |
100. * float(sum(zip(*cpu)[0])) / sum([y for x in zip(*cpu) for y in x]), | |
sum(zip(*disk)[0]), | |
sum([y for x in zip(*disk) for y in x]), | |
100. * sum(zip(*disk)[0]) / sum([y for x in zip(*disk) for y in x]) | |
) | |
print msg | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment