-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasg.py
More file actions
53 lines (44 loc) · 1.24 KB
/
asg.py
File metadata and controls
53 lines (44 loc) · 1.24 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import boto3
import sys
from datetime import datetime, timedelta
from dateutil import tz
def get_cpu_util(instanceID,startTime,endTime):
client = boto3.client('cloudwatch')
response = client.get_metric_statistics(
Namespace='AWS/EC2',
MetricName='CPUUtilization',
Dimensions=[
{
'Name': 'InstanceId',
'Value': instanceID
},
],
StartTime=startTime,
EndTime=endTime,
Period=86400,
Statistics=[
'Average',
],
Unit='Percent'
)
for k, v in response.items():
if k == 'Datapoints':
for y in v:
return "{0:.2f}".format(y['Average'])
instanceIDs=[]
now = datetime.utcnow().date()
endTime = datetime(now.year, now.month, now.day, tzinfo=tz.tzutc())
startTime = endTime - timedelta(minutes=5)
avgs = list()
for instance in instanceIDs:
avgs.append(get_cpu_util(instance,startTime,endTime))
per = (sum([float(a) for a in avgs]) / len(avgs))
print (per)
if per >= 80:
print ("no need to modify aws asg")
else:
client = boto3.client('autoscaling')
response = client.set_desired_capacity(
AutoScalingGroupName='test-base-on-cpu',
DesiredCapacity=1,
)