Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the most recent Cloudwatch metric data for an instance using Boto?

I'm trying to get the most recent data for CPU utilization for an instance (actually, several instances, but just one to start with), however the following call doesn't return any data:

cw = boto.cloudwatch.connect_to_region(Region)
cw.get_metric_statistics(
    300,
    datetime.datetime.now() - datetime.timedelta(seconds=600),
    datetime.datetime.now(),
    'CPUUtilization',
    'AWS/EC2',
    'Average',
    dimensions={'InstanceId':['i-11111111']}
    # for stats across multiple instances:
    # dimensions={'InstanceId':['i-11111111', 'i-22222222', 'i-33333333']}
)

Various posts on other sites indicate that checking the region is correct, checking that the period (first argument) is a multiple of 60, and (if you don't have detailed monitoring enabled) is greater than or equal to 300. I've checked all these things and I'm still not getting any data.

like image 403
James Avatar asked May 05 '13 11:05

James


People also ask

How do I get CloudWatch metrics?

Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/ . In the navigation pane, choose Metrics, and then choose All metrics. Select a metric namespace (for example, EC2). Select a metric dimension (for example, Per-Instance Metrics).

How often are CloudWatch metrics updated?

Sometimes metrics are received by Cloudwatch at varying intervals, such as three-minute or five-minute intervals.

How do I get metrics data from AWS CloudWatch to CSV?

There is no in-built capability to export Amazon CloudWatch metrics to CSV. There are API calls available to extract metrics, but you would need to write a program to call the API, receive the metrics and store it in a suitable format.

How often is metric data sent to CloudWatch?

Many AWS services, including EC2, Kinesis, and S3, automatically send metrics to Cloudwatch in one and five minute increments. For the first 12 months of your account, you can access a limited amount of these metrics for free. After that, there is a charge.


2 Answers

This is a daylight savings time / time zone issue!

You need to use UTC time when receiving statistics from Cloudwatch:

    cw = boto.cloudwatch.connect_to_region(Region)
    cw.get_metric_statistics(
        300,
        datetime.datetime.utcnow() - datetime.timedelta(seconds=600),
        datetime.datetime.utcnow(),
        'CPUUtilization',
        'AWS/EC2',
        'Average',
        dimensions={'InstanceId':['i-11111111']}
   )

From some experimentation it also seems that specifying multiple InstanceId dimensions will result in data only for the last specified instance (at least if detailed monitoring is not enabled).

like image 51
James Avatar answered Sep 20 '22 21:09

James


I was also seeing no data returned when setting units to "Megabytes", while setting units to "Bytes" returned data.

Both are allowed in the API reference.

data = conn.get_metric_statistics(period=60,start_time=start,end_time=end,metric_name="NetworkOut",namespace="AWS/EC2",statistics="Average",unit="Megabytes",dimensions={'InstanceId':'XXXXXX'})
print "data length: %d"%len(data)
    # data length: 0


data = conn.get_metric_statistics(period=60,start_time=start,end_time=end,metric_name="NetworkOut",namespace="AWS/EC2",statistics="Average",unit="Bytes",dimensions={'InstanceId':'XXXXXX'})
print "data length: %d"%len(data)
    # data length: 59
like image 41
storm_m2138 Avatar answered Sep 19 '22 21:09

storm_m2138