-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2_metrics.py
66 lines (49 loc) · 1.68 KB
/
ec2_metrics.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
63
64
65
66
import boto3
from datetime import datetime, timedelta, date
from ec2_metadata import ec2_metadata
def get_ec2_metric(namespace, metric_name, custom_dimensions=None, period=300, statistic='Average', unit='Percent',
start_time_seconds=300):
client = boto3.client('cloudwatch')
if custom_dimensions is not None:
dimensions = custom_dimensions
else:
dimensions = [
{
'Name': 'InstanceId',
'Value': ec2_metadata.instance_id
},
]
response = client.get_metric_statistics(
Namespace=namespace,
MetricName=metric_name,
Dimensions=dimensions,
StartTime=datetime.utcnow() - timedelta(seconds=start_time_seconds),
EndTime=datetime.utcnow(),
Period=period,
Statistics=[
statistic,
],
Unit=unit
)
try:
result = response["Datapoints"][0][statistic]
except Exception as e:
print(e)
result = 0
print(response)
return result
def get_aws_cost():
return get_ec2_metric('AWS/Billing', 'EstimatedCharges', [{'Name': 'Currency', 'Value': 'USD'}], 60, 'Maximum',
'None', 30000)
def get_queue_depth(queue):
sqsr = boto3.resource('sqs')
queue = sqsr.get_queue_by_name(QueueName=queue)
return queue.attributes["ApproximateNumberOfMessages"]
def get_ec2_cpu():
return get_ec2_metric('AWS/EC2', 'CPUUtilization')
def get_ec2_mem():
return get_ec2_metric('CWAgent', 'mem_used_percent')
def get_ec2_network_in():
return get_ec2_metric('AWS/EC2', 'NetworkIn')
def get_ec2_network_out():
return get_ec2_metric('AWS/EC2', 'NetworkOut')