Python:Boto3:get_metric_statistics()仅接受关键字参数 [英] Python: Boto3: get_metric_statistics() only accepts keyword arguments

查看:103
本文介绍了Python:Boto3:get_metric_statistics()仅接受关键字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚开始将Boto3与Python结合使用,因此绝对是新手.

Just started using Boto3 with Python so definitely new at this.

我正在尝试使用一个简单的get_metric_statistics脚本返回有关实例的CPUUtilization的信息.这是我要使用的脚本:

I'm trying to use a simple get_metric_statistics script to return information about CPUUtilization for an instance. Here is the script I'm looking to use:

import boto3
import datetime

cw = boto3.client('cloudwatch')

cw.get_metric_statistics(       
        300,
        datetime.datetime.utcnow() - datetime.timedelta(seconds=600),
        datetime.datetime.utcnow(),
        'CPUUtilization',
        'AWS/EC2',
        'Average',
        {'InstanceId':'i-11111111111'},
        )

但我不断收到以下消息:

but I keep getting the following message:

Traceback (most recent call last):
  File "C:..../CloudWatch_GetMetricStatistics.py", line 13, in <module>
    {'InstanceId':'i-0c996c11414476c7c'},
  File "C:\Program Files\Python27\lib\site-packages\botocore\client.py", line 251, in _api_call
    "%s() only accepts keyword arguments." % py_operation_name)
TypeError: get_metric_statistics() only accepts keyword arguments.

我有:

  1. 看过Boto3上的文档,我相信我已经正确编写/包含了所有内容
  2. 在.aws文件夹中设置正确的区域/输出格式/安全凭据
  3. 使用put_metric_statistics搜索类似的问题,以便找出答案

我仍然对我所缺少的东西感到困惑?

I'm still stuck as to what I'm missing?

任何指导将不胜感激.

非常感谢本

推荐答案

这有效:

import boto3
import datetime

cw = boto3.client('cloudwatch')

cw.get_metric_statistics(
        Period=300,
        StartTime=datetime.datetime.utcnow() - datetime.timedelta(seconds=600),
        EndTime=datetime.datetime.utcnow(),
        MetricName='CPUUtilization',
        Namespace='AWS/EC2',
        Statistics=['Average'],
        Dimensions=[{'Name':'InstanceId', 'Value':'i-abcd1234'}]
        )

要找到正确的值,我使用 AWS命令行界面(CLI):

To find the right values, I use the AWS Command-Line Interface (CLI):

aws cloudwatch list-metrics --namespace AWS/EC2 --metric-name CPUUtilization --max-items 1

它返回诸如以下信息:

{
    "Metrics": [
        {
            "Namespace": "AWS/EC2", 
            "Dimensions": [
                {
                    "Name": "InstanceId", 
                    "Value": "i-abcd1234"
                }
            ], 
            "MetricName": "CPUUtilization"
        }
    ], 
    "NextToken": "xxx"
}

然后,您可以使用这些值来填充您的 get_metric_statistics()记录(例如 Dimensions 参数).

You can then use these values to populate your get_metric_statistics() requet (such as the Dimensions parameter).

这篇关于Python:Boto3:get_metric_statistics()仅接受关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆