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

基于ARM模板实现Azure Log Analytics告警规则定时抑制需求

扩展Azure Log Analytics告警规则的触发抑制自动化能力

背景与需求

我们已通过AzureRM模板和Azure Pipelines为不同项目创建了1000+条Azure Log Analytics工作区告警规则,当前使用的ARM模板包含enabled参数,可按需启用或禁用告警。

现需扩展模板与流水线的自动化能力,支持开发者针对特定告警规则设置指定时长的触发抑制(覆盖从发现问题到解决问题的时间段),且当抑制时长结束后,告警需自动恢复至启用状态并正常触发事件。

原ARM模板代码

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "actionGroupName": {
            "type": "string"
        },           
        "query": {
            "type": "string"
        },
        "logAnalyticsWorkspaceId": {
            "type": "string"
        },
        "AlertRuleName": {
            "type": "string"
        },    
        "tags": {
            "type": "object"
        },
        "schedule": {
            "type": "object"
        },       
        "severity": {
            "type": "int"           
        },                          
        "operator": {
            "type": "string"         
        },  
        "threshold": {
            "type": "int"
        },
        "autoMitigate": {
            "type": "string",
            "defaultValue": false
        },
        "enabled": {
            "type": "string"
        },        
        "customWebhookPayload": {
            "type": "object"
        },             
        "location": {
            "defaultValue": "[resourceGroup().location]",
            "type": "string"
        }    
},    
    "resources": [
        {
            "type": "Microsoft.Insights/scheduledQueryRules",
            "name": "[parameters('AlertRuleName')]",
            "apiVersion": "2018-04-16",
            "location": "[parameters('location')]",
            "tags": "[parameters('tags')]",            
            "properties": {
                "displayName": "[parameters('AlertRuleName')]",
                "description": "[parameters('AlertRuleName')]",
                "enabled": "[parameters('enabled')]",
                "source": {
                    "query": "[parameters('query')]",
                    "dataSourceId": "[parameters('logAnalyticsWorkspaceId')]",
                    "queryType": "ResultCount"
                },
                "schedule": "[parameters('schedule')]",
                "action": {
                    "odata.type": "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.Microsoft.AppInsights.Nexus.DataContracts.Resources.ScheduledQueryRules.AlertingAction",
                    "severity": "[parameters('severity')]",
                    "aznsAction": {
                        "customWebhookPayload": "{ \"AlertRuleName\":\"#alertrulename\", \"AlertType\":\"#alerttype\", \"Severity\":\"#severity\", \"Application\":\"#{appname}#\", \"Text\":\"#alertrulename fired with #searchresultcount records. #{alertDescription}#\", \"SearchQuery\":\"#searchquery\" }",
                        "actionGroup": "[array(parameters('actionGroupName'))]"
                    },
                    "trigger": {
                        "thresholdOperator": "[parameters('operator')]",
                        "threshold": "[parameters('threshold')]"
                    }
                }
            }
        }
    ]
}

解决方案

1. 扩展ARM模板,新增抑制参数

在原模板的parameters节点中添加抑制时长参数,同时保留原有enabled参数的灵活性:

"parameters": {
    // 原有参数...
    "suppressionDurationMinutes": {
        "type": "int",
        "defaultValue": 0,
        "metadata": {
            "description": "告警触发抑制时长(分钟),0表示不启用临时抑制"
        }
    }
}

2. 配置Azure Pipelines自动化流水线

通过流水线实现“临时禁用告警-定时恢复启用”的自动化流程,支持开发者按需指定目标告警规则和抑制时长:

流水线核心步骤:

  • 参数化触发:为流水线配置两个输入参数:
    • alertRuleName:目标告警规则名称
    • suppressionMinutes:抑制时长(分钟)
  • 禁用告警规则:使用Azure CLI任务执行禁用操作:
    az monitor scheduled-query-rule update --name $(alertRuleName) --resource-group <你的资源组名称> --enabled false
    
  • 延迟等待:添加Delay任务,设置等待时长为$(suppressionMinutes)分钟
  • 恢复告警规则:再次使用Azure CLI任务恢复启用状态:
    az monitor scheduled-query-rule update --name $(alertRuleName) --resource-group <你的资源组名称> --enabled true
    

注意事项:

  • 若流水线代理可能被中断,建议改用Azure自动化账户定时作业实现恢复逻辑:
    1. 创建自动化Runbook,编写PowerShell脚本恢复告警启用状态
    2. 在禁用告警时,通过Azure CLI创建一个对应时长后的定时作业触发该Runbook
  • 确保流水线/自动化账户拥有Microsoft.Insights/scheduledQueryRules/write权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 11:28:16