準備邏輯應用程式和 Runbook 以便移轉傳統警示規則

注意

先前已宣佈,Azure 監視器中的傳統警示淘汰,不再開放給公用雲端使用者,局限使用到 2021 年 5 月 31 日為止。 Azure Government 雲端和 21Vianet 營運的 Microsoft Azure 傳統警示將於 2024 年 2 月 29 日淘汰。

若您選擇主動將傳統警示規則移轉為新的警示規則,兩個系統則有部分差異。 本文說明這些差異,以及如何準備進行變更。

API 變更

建立和管理傳統警示規則 (microsoft.insights/alertrules) 的 API 與建立和管理新計量警示 (microsoft.insights/metricalerts) 的 API 不同。 若是以程式設計方式建立和管理傳統警示規則,請更新部署指令碼以使用新的 API。

下表為傳統和新警示的程式設計介面參考:

部署指令碼類型 傳統警示 新計量警示
REST API microsoft.insights/alertrules microsoft.insights/metricalerts
Azure CLI az monitor alert az monitor metrics alert
PowerShell 參考 參考
Azure Resource Manager 範本 針對傳統警示 針對新的計量警示

通知承載變更

傳統警示規則新計量警示的通知承載格式稍有不同。 若 Webhook、邏輯應用程式或 Runbook 動作使用傳統警示規則,則須更新目標以接受新的承載格式。

使用下表從傳統格式的 Webhook 承載欄位對應至新的格式:

通知端點類型 傳統警示 新計量警示
警示是否已啟動或解決? status data.status
關於警示的內容資訊 內容 data.context
警示啟動或解決的時間戳記 context.timestamp data.context.timestamp
警示規則識別碼 context.id data.context.id
警示規則名稱 context.name data.context.name
警示規則的描述 context.description data.context.description
警示規則條件 context.condition data.context.condition
度量名稱 context.condition.metricName data.context.condition.allOf[0].metricName
時間彙總 (在評估時間範圍彙總計量) context.condition.timeAggregation context.condition.timeAggregation
評估期間 context.condition.windowSize data.context.condition.windowSize
運算子 (彙總計量值與閾值的比較) context.condition.operator data.context.condition.operator
閾值 context.condition.threshold data.context.condition.allOf[0].threshold
計量值 context.condition.metricValue data.context.condition.allOf[0].metricValue
訂用帳戶識別碼 context.subscriptionId data.context.subscriptionId
受影響資源的資源群組 context.resourceGroup data.context.resourceGroup
受影響資源的名稱 context.resourceName data.context.resourceName
受影響的資源類型 context.resourceType data.context.resourceType
受影響資源的資源識別碼 context.resourceId data.context.resourceId
入口網站資源摘要頁面的直接連結 context.portalLink data.context.portalLink
要傳遞至 Webhook 或邏輯應用程式的自訂承載欄位 properties data.properties

如您所見,承載皆很類似。 下列區段提供:

  • 修改邏輯應用程式以使用新格式的詳細資料。
  • 剖析新警示通知承載的 Runbook 範例。

修改邏輯應用程式,以接收計量警示通知

若您使用邏輯應用程式及傳統警示,則須修改邏輯應用程式程式碼,以剖析新的計量警示承載。 請遵循下列步驟:

  1. 建立新的邏輯應用程式。

  2. 使用範本「Azure 監視器 - 計量警示處理常式」。 此範本已定義 HTTP 要求觸發程式的適當結構描述。

    顯示兩個按鈕的螢幕快照:空白邏輯應用程式和 Azure 監視器 – 計量警示處理程式。

  3. 新增動作以裝載您的處理邏輯。

使用自動化 Runbook 以接收計量警示通知

下列範例提供的 PowerShell 程式碼可用於 Runbook。 此程式碼可同時剖析傳統計量警示規則與新計量警示規則的承載。

## Example PowerShell code to use in a runbook to handle parsing of both classic and new metric alerts.

[OutputType("PSAzureOperationResponse")]

param
(
    [Parameter (Mandatory=$false)]
    [object] $WebhookData
)

$ErrorActionPreference = "stop"

if ($WebhookData)
{
    # Get the data object from WebhookData.
    $WebhookBody = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)

    # Determine whether the alert triggering the runbook is a classic metric alert or a new metric alert (depends on the payload schema).
    $schemaId = $WebhookBody.schemaId
    Write-Verbose "schemaId: $schemaId" -Verbose
    if ($schemaId -eq "AzureMonitorMetricAlert") {

        # This is the new metric alert schema.
        $AlertContext = [object] ($WebhookBody.data).context
        $status = ($WebhookBody.data).status

        # Parse fields related to alert rule condition.
        $metricName = $AlertContext.condition.allOf[0].metricName
        $metricValue = $AlertContext.condition.allOf[0].metricValue
        $threshold = $AlertContext.condition.allOf[0].threshold
        $timeAggregation = $AlertContext.condition.allOf[0].timeAggregation
    }
    elseif ($schemaId -eq $null) {
        # This is the classic metric alert schema.
        $AlertContext = [object] $WebhookBody.context
        $status = $WebhookBody.status

        # Parse fields related to alert rule condition.
        $metricName = $AlertContext.condition.metricName
        $metricValue = $AlertContext.condition.metricValue
        $threshold = $AlertContext.condition.threshold
        $timeAggregation = $AlertContext.condition.timeAggregation
    }
    else {
        # The schema is neither a classic metric alert nor a new metric alert.
        Write-Error "The alert data schema - $schemaId - is not supported."
    }

    # Parse fields related to resource affected.
    $ResourceName = $AlertContext.resourceName
    $ResourceType = $AlertContext.resourceType
    $ResourceGroupName = $AlertContext.resourceGroupName
    $ResourceId = $AlertContext.resourceId
    $SubId = $AlertContext.subscriptionId

    ## Your logic to handle the alert here.
}
else {
    # Error
    Write-Error "This runbook is meant to be started from an Azure alert webhook only."
}

如需觸發警示時停止虛擬機的 Runbook 完整範例,請參閱 Azure 自動化 檔

透過 Webhook 的合作夥伴整合

大部分整合傳統警示的合作夥伴皆已透過整合來支援較新的計量警示。 已使用新計量警示的已知整合包括:

若您使用此處未列出的合作夥伴整合,請向提供者確認是否使用新的計量警示。

後續步驟