Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用boto3创建磁盘读取吞吐量IOPS cloudwatch警报_Python_Aws Lambda_Boto3_Amazon Cloudwatch - Fatal编程技术网

Python 使用boto3创建磁盘读取吞吐量IOPS cloudwatch警报

Python 使用boto3创建磁盘读取吞吐量IOPS cloudwatch警报,python,aws-lambda,boto3,amazon-cloudwatch,Python,Aws Lambda,Boto3,Amazon Cloudwatch,我想用boto3和数学表达式创建磁盘读取吞吐量IOPS cloudwatch警报,但我有错误 错误 “errorMessage”:“调用PutMetricAlarm操作时发生错误(ValidationError):MetricDataQuery表达式和MetricStat参数是互斥的,您已经指定了这两个参数。” 代码 from __future__ import print_function from string import Template import json import boto3

我想用boto3和数学表达式创建磁盘读取吞吐量IOPS cloudwatch警报,但我有错误 错误 “errorMessage”:“调用PutMetricAlarm操作时发生错误(ValidationError):MetricDataQuery表达式和MetricStat参数是互斥的,您已经指定了这两个参数。”

代码

from __future__ import print_function
from string import Template
import json
import boto3

def lambda_handler(event, context):
    CW_client = boto3.client('cloudwatch', region_name='eu-west-1')

   volume_id = 'vol-01903a31c2c4d5690'
   response7 = CW_client.put_metric_alarm(
    AlarmName='Disk-Read-Throughput-IOPS',
    AlarmDescription='Disk-Read-Throughput-IOPS',
    ActionsEnabled=True,
    AlarmActions=[
        'topic',
    ],
    MetricName='VolumeReadOps',
    Namespace='AWS/EBS',
    Statistic='Sum',
    Dimensions=[
        {
            'Name': 'VolumeId',
            'Value': 'volume_id'
        },
    ],
    Period=300,
    EvaluationPeriods=3,
    DatapointsToAlarm=3,
    Threshold=600.0,
    ComparisonOperator='GreaterThanThreshold',
    Metrics=[
        {
            'Id': 'm1',
            'MetricStat': {
                'Metric': {
                    'Namespace': 'AWS/EBS',
                    'MetricName': 'VolumeReadOps',
                    'Dimensions': [
                        {
                            'Name': 'VolumeId',
                            'Value': 'volume_id'
                        },
                    ]
                },
                'Period': 300,
                'Stat': 'Sum',
            },
            'Expression': 'SUM(METRICS())/300',
            'Label': 'Expression1',
            'Period': 300
        },
    ],
 )
  • 不能将
    MetricStat
    Expression
    放在同一个
    Metric
    对象中,需要将它们分开
  • 然后,如果您有多个
    Metric
    对象,正好有一个可以返回数据,其余的应该有
    'ReturnData':False,这意味着数据将在表达式中使用,但不会在图形上产生单独的行(您只需要一行,即表达式生成的行)
  • 如果指定了
    度量
    列表,则无法在顶层使用
    命名空间
    度量名称
    维度
    定义度量,因此需要删除这些度量
  • 这应该有效(就指标而言,不确定行动部分):

    from __future__ import print_function
    from string import Template
    import json
    import boto3
    
    def lambda_handler(event, context):
        CW_client = boto3.client('cloudwatch', region_name='eu-west-1')
    
        volume_id = 'vol-01903a31c2c4d5690'
        response7 = CW_client.put_metric_alarm(
            AlarmName='Disk-Read-Throughput-IOPS',
            AlarmDescription='Disk-Read-Throughput-IOPS',
            ActionsEnabled=True,
            AlarmActions=[
                'topic',
            ],
            EvaluationPeriods=3,
            DatapointsToAlarm=3,
            Threshold=600.0,
            ComparisonOperator='GreaterThanThreshold',
            Metrics=[
                {
                    'Id': 'm1',
                    'MetricStat': {
                        'Metric': {
                            'Namespace': 'AWS/EBS',
                            'MetricName': 'VolumeReadOps',
                            'Dimensions': [
                                {
                                    'Name': 'VolumeId',
                                    'Value': 'volume_id'
                                },
                            ]
                        },
                        'Period': 300,
                        'Stat': 'Sum'
                    },
                    'Label': 'Metric1',
                    'ReturnData': False
                },
                {
                    'Id': 'm2',
                    'Expression': 'SUM(METRICS())/300',
                    'Label': 'Expression1'
                },
            ],
        )