Show RDS Metrics for multiple instances

柔情痞子 提交于 2020-05-14 20:22:44

问题


I would like to pull RDS Cloudwatch Metrics using Boto for multiple databases at once.

So far I have only been able to get metrics for only one instance at a time using an approach like this:

botoRDS = boto.connect_cloudwatch(aws_access_key_id=Key, aws_secret_access_key=OtherKey)

instanceStats = botoRDS.get_metric_statistics(period=60, 
                start_time=self.startTime,
                end_time=self.endTime, 
                namespace="AWS/RDS", 
                metric_name='CPUUtilization', 
                statistics=["Average"],
                dimensions={'DBInstanceIdentifier':['DB1','DB2']})

This is what I get:

[
{
    u'Timestamp': datetime.datetime(2034,1,14,21,2),
    u'Average': 45.1,
    u'Unit': u'Percent'
}]

What I would like to be able to return is the average for both the database seperately. Something like this:

[
{
    u'Timestamp': datetime.datetime(2034,1,14,21,2),
    u'DBInstanceID':'DB1',
    u'Average': 33.02,
    u'Unit': u'Percent'
},
{
   u'Timestamp': datetime.datetime(2034,1,14,21,2),
   u'DBInstanceID':'DB2',
    u'Average': 45.1,
    u'Unit': u'Percent'
}

]

Is it possible to form the dimension specified to get results like this. I would really like to not have to pull data for every DB.


回答1:


I don't think there is any way to do what you are asking. You can specify a single DBInstanceIndentifier as the dimension and get specific data for that DBInstance or you can specify multiple DBInstanceIdentifiers and get metric data aggregated across those dimensions but I don't think there is any way to request multiple, separate dimensions in a single API call. I think you have to make a call for each specific dimension you are interested in.




回答2:


with Boto3, the Dimensions needs works as below:

response = cloudwatch.get_metric_data(
    MetricDataQueries=[
        {
            'Id': 'cpu_1',
            'MetricStat': {
            'Metric': {
                'Namespace': 'AWS/RDS',
                'MetricName': 'CPUUtilization',
                'Dimensions': [
                        {
                            "Name": "DBInstanceIdentifier",
                            "Value": "DB1"  
                        }]
            },
            'Period': period,
            'Stat': 'Maximum',
            }
        },
        {
            'Id': 'cpu_2',
            'MetricStat': {
            'Metric': {
                'Namespace': 'AWS/RDS',
                'MetricName': 'CPUUtilization',
                'Dimensions': [
                        {
                            "Name": "DBInstanceIdentifier",
                            "Value": "DB2"  
                        }]
            },
            'Period': period,
            'Stat': 'Maximum',
            }
        }
    ],
    StartTime=(datetime.now() - timedelta(seconds=300 * 3)).timestamp(),
    EndTime=datetime.now().timestamp()
)


来源:https://stackoverflow.com/questions/22029039/show-rds-metrics-for-multiple-instances

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!