自定义日志搜索警报电子邮件主题(预览版)

可以使用静态文本、从警报有效负载中提取的动态值或两者的组合来替代日志搜索警报电子邮件主题。

本文介绍如何使用 Azure 资源管理器模板(ARM 模板)在 Azure Monitor 中自定义日志搜索警报电子邮件主题,以便进行个性化通知。

Prerequisites

若要创建或编辑警报规则,你必须具有以下权限:

  • 对警报规则的目标资源具有读取权限。
  • 对创建的警报规则所在的资源组的写入权限。 如果从 Azure 门户中创建预警规则,则默认在目标资源所在的同一资源组中创建预警规则。
  • 对关联到警报规则的任何操作组具有读取权限(如果适用)。

使用 ARM 模板

若要创建具有自定义电子邮件主题的警报规则,请使用 资源管理器模板示例中的模板进行日志搜索警报。 请务必使用最新的 API 版本 2023-12-01。 使用 资源管理器模板样本中的模板 来创建简单日志搜索警报规则。

添加对象 actionProperties 并包括 Email.Subject 属性。 例如:

{
  "actionProperties": {
    "Email.Subject": "This is a custom email subject"
  }
}

使用动态值

从警报有效负载中提取动态值的格式为:${<path to schema field>}例如: ${data.essentials.monitorCondition}

Note

尽管为警报规则配置的操作组不使用通用架构,仍应使用通用警报架构的格式来指定有效负载中的字段。 请参阅 Azure Monitor 警报的常见警报架构

Examples

此示例创建一个电子邮件主题,其中包含受影响的资源以及它是被触发还是被解决。


{
  "actionProperties": {
    "Email.Subject": "Alert ${data.essentials.monitorCondition} on ${data.essentials.alertTargetIDs}"
  }
}

潜在结果:

  • 已在 VM1 上触发警报。
  • 告警在 VM1 上已解决。

此示例创建一个包含受影响资源错误计数的电子邮件主题:

{
  "actionProperties": {
    "Email.Subject": "${data.alertContext.condition.allOf[0].metricValue} errors found in ${data.essentials.alertTargetIDs}"
  }
}

结果:在 ContosoApp 中发现 7 个错误。

此示例显示了一个完整的资源管理器模板,该模板创建了一个日志搜索警报规则,其自定义电子邮件主题为“这是自定义电子邮件主题”。

{
  "location": "chinanorth2",
  "properties": {
    "description": "Performance rule",
    "severity": 4,
    "enabled": true,
    "evaluationFrequency": "PT5M",
    "scopes": [
      "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/scopeResourceGroup1/providers/Microsoft.Compute/virtualMachines/vm1"
    ],
    "windowSize": "PT10M",
    "criteria": {
      "allOf": [
        {
          "query": "Perf | where ObjectName == \"Processor\"",
          "timeAggregation": "Average",
          "metricMeasureColumn": "% Processor Time",
          "resourceIdColumn": "resourceId",
          "dimensions": [
            {
              "name": "ComputerIp",
              "operator": "Exclude",
              "values": [
                "192.168.1.1"
              ]
            },
            {
              "name": "OSType",
              "operator": "Include",
              "values": [
                "*"
              ]
            }
          ],
          "operator": "GreaterThan",
          "threshold": 70,
          "failingPeriods": {
            "numberOfEvaluationPeriods": 1,
            "minFailingPeriodsToAlert": 1
          }
        }
      ]
    },
    "muteActionsDuration": "",
    "actions": {
      "actionGroups": [
        "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/scopeResourceGroup1/providers/microsoft.insights/actiongroups/myactiongroup"
      ],
      "customProperties": {
        "key11": "value11",
        "key12": "value12"
      },
     "actionProperties": {
     "Email.Subject": "This is a custom email subject"
     }
    },
    "autoMitigate": true,
    "checkWorkspaceAlertsStorageConfigured": true,
    "skipQueryValidation": true
  }
}