You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python SDK创建Azure告警失败,请求有效示例代码

使用Python SDK创建Azure告警时遇到经典告警规则不支持错误

问题重现

尝试通过Python SDK创建Azure VM的CPU使用率告警,执行代码时收到如下错误:

azure.core.exceptions.HttpResponseError: (BadRequest) Creating or editing classic alert rules based on this metric is no longer supported. To learn about new alert rules see https://aka.ms/create-metric-alerts
Code: BadRequest
Message: Creating or editing classic alert rules based on this metric is no longer supported. To learn about new alert rules see https://aka.ms/create-metric-alerts

原代码使用了旧的经典告警API(alert_rules.create_or_update),相关模型如RuleMetricDataSource、ThresholdRuleCondition都属于已废弃的经典告警体系。

解决方案:使用新的指标告警API

Azure现在要求使用**指标告警(Metric Alerts)**体系,对应的API是metric_alerts.create_or_update,以下是完整的正确示例代码:

from azure.mgmt.monitor import MonitorManagementClient
from azure.mgmt.monitor.models import (
    MetricAlertResource,
    MetricAlertSingleResourceMultipleMetricCriteria,
    MetricCriteria,
    EmailNotification
)
from azure.identity import DefaultAzureCredential

# 配置参数
subscription_id = 'xxxxxx-xxxxxxxxxx-xxxxxxxxx'
resource_group_name = 'example-rg'
vm_name = 'example_vm'
alert_rule_name = 'MyPyTestAlertRule'
target_email = 'abc@gmail.com'

# 初始化凭据和客户端
credentials = DefaultAzureCredential()
client = MonitorManagementClient(credentials, subscription_id)

# 目标资源ID
resource_id = (
    f"/subscriptions/{subscription_id}/"
    f"resourceGroups/{resource_group_name}/"
    f"providers/Microsoft.Compute/virtualMachines/{vm_name}"
)

# 定义告警条件:CPU使用率≥90%,5分钟窗口内平均值
metric_criteria = MetricCriteria(
    name="CPU_Criteria",
    metric_name="Percentage CPU",
    namespace="Microsoft.Compute/virtualMachines",
    operator="GreaterThanOrEqual",
    threshold=90,
    time_aggregation="Average",
)

# 组装条件集
alert_criteria = MetricAlertSingleResourceMultipleMetricCriteria(
    all_of=[metric_criteria],
    odata_type="Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria"
)

# 定义邮件通知
email_notification = EmailNotification(
    send_to_service_owners=True,
    custom_emails=[target_email]
)

# 创建告警规则
alert_rule = MetricAlertResource(
    location="global",  # 指标告警的location固定为global
    description="Testing Alert rule creation",
    severity=2,  # 告警级别,1-4,1最严重
    enabled=True,
    scopes=[resource_id],
    evaluation_frequency="PT1M",  # 评估频率,每分钟一次
    window_size="PT5M",  # 评估窗口大小
    criteria=alert_criteria,
    notifications=[email_notification]
)

# 创建或更新告警规则
result = client.metric_alerts.create_or_update(
    resource_group_name=resource_group_name,
    rule_name=alert_rule_name,
    parameters=alert_rule
)

print(f"告警规则创建成功,ID: {result.id}")

关键说明

  • API差异:替换旧的alert_rules.create_or_update为metric_alerts.create_or_update
  • 模型替换:使用MetricAlertResource替代旧的告警规则模型,用MetricCriteria定义指标条件
  • Location设置:新的指标告警规则location必须设为global
  • 依赖版本:确保安装的azure-mgmt-monitor是最新版本,可通过pip install --upgrade azure-mgmt-monitor更新

内容的提问来源于stack exchange,提问作者GoneCase123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 14:22:43