-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_check.py
62 lines (52 loc) · 2.08 KB
/
cpu_check.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import boto3
import datetime
'''Making a comment to test AWS CodeBuild. '''
print('Loading function')
now = datetime.datetime.utcnow()
def lambda_handler(event, context):
def RegionList():
regionList = []
client = boto3.client('ec2')
getRegions = client.describe_regions()
for region in getRegions['Regions']:
if region['RegionName'] not in regionList:
regionList.append(region['RegionName'])
return regionList
regions = RegionList()
def getInstances(regions):
instanceList = []
for region in regions:
client = boto3.client('ec2', region_name=region)
get_instances = client.describe_instances()
for instance in get_instances['Reservations']:
for instanceid in instance['Instances']:
if instanceid['InstanceId'] not in instanceList:
instanceList.append(instanceid['InstanceId'])
return instanceList
things = getInstances(regions)
def cw_metrics(instance_list):
client = boto3.client('cloudwatch')
s3_client = boto3.client('s3')
timer = now - datetime.timedelta(minutes=30)
for instance in instance_list:
get_cw = client.get_metric_statistics(
Namespace='AWS/EC2',
MetricName='CPUUtilization',
StartTime=timer,
EndTime=now,
Period=300,
Statistics=['Average'],
Dimensions=[
{
'Name': 'InstanceId',
'Value': instance
}
]
)
with open('/tmp/%s%s.json' % (now, instance), 'w+') as f:
f.write(str(get_cw['Datapoints']))
with open('/tmp/%s%s.json' % (now, instance), 'r') as data_f:
json_stuff = data_f.read()
s3_client.put_object(Bucket='examplebucket', Body=json_stuff,
Key='%s%s.json' % (now, instance))
cw_metrics(things)