【问题标题】:create Disk-Read-Throughput-IOPS cloudwatch alarm with boto3使用 boto3 创建 Disk-Read-Throughput-IOPS cloudwatch 警报
【发布时间】:2020-04-20 13:50:24
【问题描述】:

我想用 boto3 和数学表达式创建 Disk-Read-Throughput-IOPS cloudwatch 警报,但我有错误 错误 "errorMessage": "调用PutMetricAlarm操作时发生错误(ValidationError):参数MetricDataQuery Expression和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
        },
    ],
 )

【问题讨论】:

    标签: python aws-lambda boto3 amazon-cloudwatch


    【解决方案1】:
    1. 您不能在同一个 Metric 对象中包含 MetricStatExpression,您需要将它们分开。
    2. 那么如果你有多个Metric对象,正好1个可以返回数据,其余的应该有'ReturnData': False,这意味着数据将在表达式中使用但它不会在图表上产生单独的线(您只需要 1 行,即由表达式生成的那一行)。
    3. 如果您指定Metric 列表,则无法在顶层使用NamespaceMetricNameDimension 定义指标,因此您需要删除这些。

    这应该有效(就指标而言,不确定操作部分):

    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'
                },
            ],
        )
    

    【讨论】:

      猜你喜欢
      • 2019-02-06
      • 2021-07-31
      • 2021-08-07
      • 1970-01-01
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      相关资源
      最近更新 更多