你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 ARM 模板为创建针对新建议的 Azure 顾问警报

本文介绍如何使用 Azure 资源管理器模板(ARM 模板)针对 Azure 顾问给出的新建议设置警报。

Azure 资源管理器模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。 模板使用声明性语法。 你可以在不编写用于创建部署的编程命令序列的情况下,描述预期部署。

当 Azure 顾问检测到针对某项资源的新建议时,将在 Azure 活动日志中存储一个事件。 可以使用特定于建议的警报创建体验,为来自 Azure 顾问的这些事件设置警报。 可以选择订阅和资源组(可选)来指定想要接收其警报的资源。

还可以使用以下属性来确定建议类型:

  • 类别
  • 影响级别
  • 建议类型

还可通过以下方式配置触发警报时将发生的操作:

  • 选择现有操作组
  • 创建新的操作组

若要了解有关操作组的详细信息,请参阅创建和管理操作组

注意

顾问警报目前仅适用于高可用性、性能和成本建议。 不支持安全建议。

先决条件

查看模板

以下模板将创建以电子邮件为目标的操作组,并为目标订阅启用所有服务运行状况通知。 将此模板保存为 CreateAdvisorAlert.json。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "actionGroups_name": {
      "defaultValue": "advisorAlert",
      "type": "string"
    },
    "activityLogAlerts_name": {
      "defaultValue": "AdvisorAlertsTest",
      "type": "string"
    },
    "emailAddress": {
      "defaultValue": "<email address>",
      "type": "string"
    }
  },
  "variables": {
    "alertScope": "[concat('/','subscriptions','/',subscription().subscriptionId)]"
  },
  "resources": [
    {
      "comments": "Action Group",
      "type": "microsoft.insights/actionGroups",
      "apiVersion": "2019-06-01",
      "name": "[parameters('actionGroups_name')]",
      "location": "Global",
      "scale": null,
      "dependsOn": [],
      "tags": {},
      "properties": {
        "groupShortName": "[parameters('actionGroups_name')]",
        "enabled": true,
        "emailReceivers": [
          {
            "name": "[parameters('actionGroups_name')]",
            "emailAddress": "[parameters('emailAddress')]"
          }
        ],
        "smsReceivers": [],
        "webhookReceivers": []
      }
    },
    {
      "comments": "Azure Advisor Activity Log Alert",
      "type": "microsoft.insights/activityLogAlerts",
      "apiVersion": "2017-04-01",
      "name": "[parameters('activityLogAlerts_name')]",
      "location": "Global",
      "scale": null,
      "tags": {},
      "properties": {
        "scopes": [
          "[variables('alertScope')]"
        ],
        "condition": {
          "allOf": [
            {
              "field": "category",
              "equals": "Recommendation"
            },
            {
              "field": "properties.recommendationCategory",
              "equals": "Cost"
            },
            {
              "field": "properties.recommendationImpact",
              "equals": "Medium"
            },
            {
              "field": "operationName",
              "equals": "Microsoft.Advisor/recommendations/available/action"
            }
          ]
        },
        "actions": {
          "actionGroups": [
            {
              "actionGroupId": "[resourceId('microsoft.insights/actionGroups', parameters('actionGroups_name'))]",
              "webhookProperties": {}
            }
          ]
        },
        "enabled": true,
        "description": ""
      },
      "dependsOn": [
        "[resourceId('microsoft.insights/actionGroups', parameters('actionGroups_name'))]"
      ]
    }
  ]
}

该模板定义了两项资源:

部署模板

使用部署 ARM 模板的任何标准方法来部署模板,如以下使用 CLI 和 PowerShell 的示例。 将“资源组”和“emailAddress”的示例值替换为环境的相应值。 工作区名称在所有 Azure 订阅中必须唯一。

az login
az deployment group create --name CreateAdvisorAlert --resource-group my-resource-group --template-file CreateAdvisorAlert.json --parameters emailAddress='user@contoso.com'

验证部署

验证是否已使用以下命令之一创建工作区。 将“资源组”的示例值替换为上面使用的值。

az monitor activity-log alert show --resource-group my-resource-group --name AdvisorAlertsTest

清理资源

如果打算继续使用后续的快速入门和教程,则可能需要保留这些资源。 如果不再需要资源组,可以将其删除,这将删除警报规则和相关的资源。 使用 Azure CLI 或 Azure PowerShell 删除资源组

az group delete --name my-resource-group

后续步骤