設定傳統計量警示使用 Webhook 通知非 Azure 系統Have a classic metric alert notify a non-Azure system using a webhook

您可以使用 Webhook 將 Azure 警示通知路由到其他系統以進行後處理或自訂動作。You can use webhooks to route an Azure alert notification to other systems for post-processing or custom actions. 您可以針對警示使用 Webhook,以將警示路由到會傳送簡訊、記錄錯誤、透過聊天或傳訊服務通知小組,或進行其他許多動作的服務。You can use a webhook on an alert to route it to services that send SMS messages, to log bugs, to notify a team via chat or messaging services, or for various other actions.

本文說明如何針對 Azure 計量警示設定 Webhook。This article describes how to set a webhook on an Azure metric alert. 文中也會說明 HTTP POST 至 Webhook 的承載資料樣貌。It also shows you what the payload for the HTTP POST to a webhook looks like. 如需有關 Azure 活動記錄警示 (事件警示) 設定和結構描述的詳細資訊,請參閱針對 Azure 活動記錄警示呼叫 WebhookFor information about the setup and schema for an Azure activity log alert (alert on events), see Call a webhook on an Azure activity log alert.

Azure 警示會使用 HTTP POST 將警示內容以 JSON 格式傳送到您在建立警示時提供的 Webhook URI。Azure alerts use HTTP POST to send the alert contents in JSON format to a webhook URI that you provide when you create the alert. 稍後在本文中會定義結構描述。The schema is defined later in this article. 該 URI 必須是有效的 HTTP 或 HTTPS 端點。The URI must be a valid HTTP or HTTPS endpoint. 當警示啟動時,Azure 會針對每個要求張貼一個項目。Azure posts one entry per request when an alert is activated.

透過 Azure 入口網站設定 WebhookConfigure webhooks via the Azure portal

若要在 Azure 入口網站中新增或更新 Webhook URI,請移至 [建立/更新警示] 。To add or update the webhook URI, in the Azure portal, go to Create/Update Alerts.

新增警示規則窗格

您也可以使用 Azure PowerShell Cmdlet跨平台 CLIAzure 監視器 REST API (英文) 設定警示以張貼至 Webhook URI。You can also configure an alert to post to a webhook URI by using Azure PowerShell cmdlets, a cross-platform CLI, or Azure Monitor REST APIs.

驗證 WebhookAuthenticate the webhook

Webhook 可透過使用權杖型授權來驗證。The webhook can authenticate by using token-based authorization. 儲存的 Webhook URI 含有權杖識別碼。The webhook URI is saved with a token ID. 例如:https://mysamplealert/webcallback?tokenid=sometokenid&someparameter=somevalueFor example: https://mysamplealert/webcallback?tokenid=sometokenid&someparameter=somevalue

承載結構描述Payload schema

POST 作業對於所有以計量為基礎的警示會包含下列 JSON 承載和結構描述:The POST operation contains the following JSON payload and schema for all metric-based alerts:

{
    "status": "Activated",
    "context": {
        "timestamp": "2015-08-14T22:26:41.9975398Z",
        "id": "/subscriptions/s1/resourceGroups/useast/providers/microsoft.insights/alertrules/ruleName1",
        "name": "ruleName1",
        "description": "some description",
        "conditionType": "Metric",
        "condition": {
            "metricName": "Requests",
            "metricUnit": "Count",
            "metricValue": "10",
            "threshold": "10",
            "windowSize": "15",
            "timeAggregation": "Average",
            "operator": "GreaterThanOrEqual"
        },
        "subscriptionId": "s1",
        "resourceGroupName": "useast",
        "resourceName": "mysite1",
        "resourceType": "microsoft.foo/sites",
        "resourceId": "/subscriptions/s1/resourceGroups/useast/providers/microsoft.foo/sites/mysite1",
        "resourceRegion": "centralus",
        "portalLink": "https://portal.azure.com/#resource/subscriptions/s1/resourceGroups/useast/providers/microsoft.foo/sites/mysite1"
    },
    "properties": {
        "key1": "value1",
        "key2": "value2"
    }
}
欄位Field MandatoryMandatory 一組固定值Fixed set of values 注意Notes
statusstatus YY Activated、ResolvedActivated, Resolved 以您設定的條件為基礎的警示狀態。The status for the alert based on the conditions you set.
contextcontext YY 警示內容。The alert context.
timestamptimestamp YY 警示觸發的時間。The time at which the alert was triggered.
idid YY 每個警示規則都有唯一的識別碼。Every alert rule has a unique ID.
namename YY 警示名稱。The alert name.
descriptiondescription YY 警示的描述。A description of the alert.
conditionTypeconditionType YY Metric、EventMetric, Event 支援兩種類型的警示:計量和事件。Two types of alerts are supported: metric and event. 以計量條件為基礎的計量警示。Metric alerts are based on a metric condition. 以活動記錄中的事件為基礎的事件警示。Event alerts are based on an event in the activity log. 使用此值來檢查警示是以計量或事件為基礎。Use this value to check whether the alert is based on a metric or on an event.
condition (條件)condition YY 要以 conditionType 值為基礎來檢查的特定欄位。The specific fields to check based on the conditionType value.
metricNamemetricName 用於計量警示For metric alerts 定義規則所監視的計量名稱。The name of the metric that defines what the rule monitors.
metricUnitmetricUnit 用於計量警示For metric alerts Bytes、BytesPerSecond、Count、CountPerSecond、Percent、SecondsBytes, BytesPerSecond, Count, CountPerSecond, Percent, Seconds 計量允許的單位。The unit allowed in the metric. 請參閱允許的值See allowed values.
metricValuemetricValue 用於計量警示For metric alerts 造成警示的計量實際值。The actual value of the metric that caused the alert.
thresholdthreshold 用於計量警示For metric alerts 會啟動警示的臨界值。The threshold value at which the alert is activated.
windowSizewindowSize 用於計量警示For metric alerts 以 threshold 為基礎用來監視警示活動的時間長度。The period of time that's used to monitor alert activity based on the threshold. 值必須介於 5 分鐘到 1 天之間。The value must be between 5 minutes and 1 day. 值必須為 ISO 8601 持續時間格式。The value must be in ISO 8601 duration format.
timeAggregationtimeAggregation 用於計量警示For metric alerts Average、Last、Maximum、Minimum、None、TotalAverage, Last, Maximum, Minimum, None, Total 收集的資料應如何隨著時間結合。How the data that's collected should be combined over time. 預設值為 Average。The default value is Average. 請參閱允許的值See allowed values.
operatoroperator 用於計量警示For metric alerts 用來比較目前計量資料與所設定臨界值的運算子。The operator that's used to compare the current metric data to the set threshold.
subscriptionIdsubscriptionId YY Azure 訂用帳戶識別碼。The Azure subscription ID.
resourceGroupNameresourceGroupName YY 受影響資源的資源群組名稱。The name of the resource group for the affected resource.
resourceNameresourceName YY 受影響資源的資源名稱。The resource name of the affected resource.
resourceTyperesourceType YY 受影響資源的資源類型。The resource type of the affected resource.
resourceIdresourceId YY 受影響資源的資源識別碼。The resource ID of the affected resource.
resourceRegionresourceRegion YY 受影響資源的區域或位置。The region or location of the affected resource.
portalLinkportalLink YY 入口網站資源摘要頁面的直接連結。A direct link to the portal resource summary page.
propertiesproperties NN 選擇性Optional 一組索引鍵/值組,具有事件的詳細資料。A set of key/value pairs that has details about the event. 例如: Dictionary<String, String>For example, Dictionary<String, String>. properties 欄位是選擇性的。The properties field is optional. 在自訂 UI 或邏輯應用程式的工作流程中,使用者可以輸入可透過承載傳遞的索引鍵/值。In a custom UI or logic app-based workflow, users can enter key/value pairs that can be passed via the payload. 另一種將自訂屬性傳回給 Webhook 的替代方式是透過 Webhook URI 本身 (做為查詢參數)。An alternate way to pass custom properties back to the webhook is via the webhook URI itself (as query parameters).

注意

您只能使用 Azure 監視器 REST API (英文) 來設定 properties欄位。You can set the properties field only by using Azure Monitor REST APIs.

後續步驟Next steps