2013年11月18日月曜日

BOTOでCloudWathcのmetricsをとる

InstanceのIDを指定してCPU使用率をとる方法。

環境
python3.3
BOTO 2.9.0 py3kport
import datetime
from decimal import Decimal,ROUND_HALF_UP
from boto.ec2.cloudwatch import CloudWatchConnection,regions

for region in regions():
    cw_con = CloudWatchConnection(
        aws_access_key_id     = 'AAA',
        aws_secret_access_key = 'BBB',
        region = region,
    )
    for val in cw_con.get_metric_statistics(
            60,
            datetime.date.today() - datetime.timedelta(hours=0.1),
            datetime.datetime.today(),
            'CPUUtilization',
            'AWS/EC2',
            'Average',
            {'InstanceId': ['i-CCC']},
            'Percent',
    ):
        print(val)