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

求助:基于Bicep创建Azure通用CPU/内存阈值告警规则及示例代码

Azure Bicep 自动化创建通用CPU/内存告警规则

告警规则对Application Insights关联服务的适用性

这类阈值告警规则可以应用到Azure Functions、App Services,具体适配方式如下:

  • App Services:直接使用平台提供的CpuPercentage、MemoryPercentage等原生指标配置告警
  • Azure Functions:
    • 专用计划场景:可沿用虚拟机/规模集的系统指标配置告警
    • 消耗计划场景:需基于Application Insights采集的性能指标(如内存可用字节数等)转换为阈值规则

通用Bicep模板示例

以下模板支持灵活配置参数,可适配多种资源场景:

1. 参数定义(通用配置)

param alertRuleNamePrefix string = 'High-Utilization-Alert'
param targetResourceId string // 目标资源ID,如App Service、VM、Function App的资源ID
param alertDescription string = '资源利用率超过阈值时触发告警'
param severity int = 2 // 告警级别,0(最严重)-4(最轻)
param windowSize string = 'PT5M' // 评估时间段,格式为ISO 8601,如PT10M=10分钟
param evaluationFrequency string = 'PT1M' // 告警评估频率,如PT1M=每分钟评估一次
param cpuThreshold int = 80 // CPU利用率阈值(百分比)
param memoryThreshold int = 85 // 内存利用率阈值(百分比)
param actionGroupId string // 告警触发后联动的动作组ID(需提前创建)

2. CPU利用率告警规则

resource cpuAlertRule 'Microsoft.Insights/metricAlerts@2021-08-01' = {
  name: '${alertRuleNamePrefix}-CPU'
  location: 'global'
  properties: {
    description: alertDescription
    severity: severity
    enabled: true
    scopes: [
      targetResourceId
    ]
    evaluationFrequency: evaluationFrequency
    windowSize: windowSize
    criteria: {
      allOf: [
        {
          criterionType: 'StaticThresholdCriterion'
          metricName: 'CpuPercentage'
          // 根据目标资源修改命名空间:VM用Microsoft.Compute/virtualMachines,App Service用Microsoft.Web/sites
          metricNamespace: 'Microsoft.Web/sites'
          operator: 'GreaterThan'
          threshold: cpuThreshold
          timeAggregation: 'Average'
        }
      ]
    }
    actions: [
      {
        actionGroupId: actionGroupId
      }
    ]
  }
}

3. 内存利用率告警规则

resource memoryAlertRule 'Microsoft.Insights/metricAlerts@2021-08-01' = {
  name: '${alertRuleNamePrefix}-Memory'
  location: 'global'
  properties: {
    description: alertDescription
    severity: severity
    enabled: true
    scopes: [
      targetResourceId
    ]
    evaluationFrequency: evaluationFrequency
    windowSize: windowSize
    criteria: {
      allOf: [
        {
          criterionType: 'StaticThresholdCriterion'
          metricName: 'MemoryPercentage'
          // 同CPU规则,根据目标资源调整命名空间
          metricNamespace: 'Microsoft.Web/sites'
          operator: 'GreaterThan'
          threshold: memoryThreshold
          timeAggregation: 'Average'
        }
      ]
    }
    actions: [
      {
        actionGroupId: actionGroupId
      }
    ]
  }
}

模板适配说明

  • 针对不同资源类型,需修改metricNamespace参数:
    • 虚拟机/虚拟机规模集:Microsoft.Compute/virtualMachines
    • Azure Functions消耗计划:需切换为Application Insights的指标,此时metricNamespace为Microsoft.Insights/components,metricName改为performanceCounters/availableMemoryBytes,并调整阈值为字节数对应的百分比逻辑
  • 可通过修改windowSize、evaluationFrequency等参数适配不同监控精度需求

内容的提问来源于stack exchange,提问作者Jennifer M.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 14:25:38