通过lambda python函数获取AWS RDS的当前CPU利用率

2024-06-25 05:42:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用以下代码通过lambda python获取AWS RDS的当前cpu利用率。但得到的是空值:

代码:

import json
import boto3,datetime

def lambda_handler(event, context):
    cloudwatch = boto3.client('cloudwatch')
    response = cloudwatch.get_metric_data(
    MetricDataQueries=[
    {
        'Id': 'cpu',
        'MetricStat': {
        'Metric': {
            'Namespace': 'AWS/RDS',
            'MetricName': 'CPUUtilization',
            'Dimensions': [
                    {
                        "Name": "DBInstanceIdentifier",
                        "Value": "mydb"  
                    }]
        },
        'Period': 30,
        'Stat': 'Average',
        }
    }
],
StartTime=(datetime.datetime.now() - datetime.timedelta(seconds=300)).timestamp(),
EndTime=datetime.datetime.now().timestamp()
)

print(response)

输出如下所示:

{'MetricDataResults': [{'Id': 'cpu', 'Label': 'CPUUtilization', 'Timestamps': [], 'Values': [], 'StatusCode': 'Complete'}]

获取时间戳和值的空值。感谢您的帮助


Tags: lambda代码importawsiddatetimeresponsecpu
1条回答
网友
1楼 · 发布于 2024-06-25 05:42:21

请尝试删除句点:30或将其更改为文档中建议的60的倍数:

The granularity, in seconds, of the returned data points. For metrics with regular resolution, a period can be as short as one minute (60 seconds) and must be a multiple of 60. For high-resolution metrics that are collected at intervals of less than one minute, the period can be 1, 5, 10, 30, 60, or any multiple of 60. High-resolution metrics are those metrics stored by a PutMetricData operation that includes a StorageResolution of 1 second .

此外,字典中的值区分大小写,因此请检查是否传递了正确的值

相关问题 更多 >