使用boto3创建DiskReadThroughputIOPS cloudwatch警报

2024-09-27 00:20:25 发布

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

我想用boto3和数学表达式创建磁盘读取吞吐量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
        },
    ],
 )

Tags: fromimportclientid参数错误boto3period
1条回答
网友
1楼 · 发布于 2024-09-27 00:20:25
  1. 不能将MetricStatExpression放在同一个Metric对象中,需要将它们分开。你知道吗
  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'
            },
        ],
    )

相关问题 更多 >

    热门问题