共用方式為


快速入門:使用 ARM 範本,針對新的建議建立 Advisor 警示

本文說明如何使用 Azure Resource Manager 範本 (ARM 範本),從 Azure Advisor 設定新建議的警示。

Azure Resource Manager 範本是一個 JavaScript 物件標記法 (JSON) 檔案,會定義專案的基礎結構和設定。 範本使用宣告式語法。 您可以描述預期的部署,而不需要撰寫程式設計命令順序來建立部署。

每當 Advisor 偵測到某個資源有新的建議時,就會在 Azure 活動記錄中儲存事件。 您可以從 Advisor 使用建議特定的警示建立體驗,來設定這些事件的警示。 您可以選取訂用帳戶和選擇性的資源群組,以指定您想要收到警示的資源。

您也可以使用下列屬性來判斷建議的類型:

  • 類別
  • 影響等級
  • 建議類型

您也可以透過下列方式,設定警示觸發時所要進行的動作:

  • 選取現有的動作群組。
  • 建立新的動作群組。

若要深入了解動作群組,請參閱建立及管理動作群組

注意

Advisor 警示目前僅適用於高可用性建議、效能建議和成本建議。 不支援安全性建議。

必要條件

檢閱範本

以下範本會建立具有電子郵件目標的動作群組,並啟用目標訂用帳戶的所有服務健康情況通知。 將此範本儲存為 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 的範例),來部署範本。 將 ResourceGroupemailAddress 的範例值取代為適合您環境的值。 工作區名稱在您的 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

清除資源

如果您打算繼續進行後續的快速入門和教學課程,您可以讓這些資源留在原處。 如果不再需要這些資源,請刪除資源群組,這麼做會刪除警示規則和相關資源。 若要使用 CLI 或 PowerShell 刪除此資源群組:

az group delete --name my-resource-group