-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_utilization.py
39 lines (36 loc) · 1.39 KB
/
cpu_utilization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import functools
class CPUUtilizationGetter:
def __init__(self, start_time, end_time, period, session):
self.start_time = start_time
self.end_time = end_time
self.period = period
self.session = session
self.cloud_watch = session.client("cloudwatch")
def get_CPUUtilization(self, instance_id):
def get_metric(instance_id, statistics):
return self.cloud_watch.get_metric_statistics(
Namespace='AWS/EC2',
MetricName='CPUUtilization',
Dimensions=[
{
'Name': 'InstanceId',
'Value': instance_id
},
],
StartTime=self.start_time,
EndTime=self.end_time,
Period=self.period,
Statistics=[
statistics
]
)
average_datapoints = get_metric(instance_id,'Average')['Datapoints']
maximum_datapoints = get_metric(instance_id, 'Maximum')['Datapoints']
if (len(average_datapoints) == 0):
raise Exception(f"Failed to get metric for {instance_id}")
maximum = round(functools.reduce(lambda a, m: a if a > m['Maximum'] else m['Maximum'], maximum_datapoints, 0), 2)
average = round(functools.reduce(lambda a, m: a + m['Average'], average_datapoints, 0) / len(average_datapoints), 2)
return {
'Maximum': maximum,
'Average': average,
}