且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Boto获取实例的最新Cloudwatch指标数据?

更新时间:2022-12-09 18:39:20

这是夏令时/时区问题!

This is a daylight savings time / time zone issue!

您需要从Cloudwatch接收统计信息时使用UTC时间:

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']}
   )

通过一些实验,似乎还指定了多个 InstanceId 维度将仅得出最后指定实例的数据(至少在未启用详细监视的情况下)。

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).