在 Azure Monitor 中对经典指标警报调用 WebhookCall a webhook with a classic metric alert in Azure Monitor

警告

本文介绍了如何使用旧式经典指标警报。This article describes how to use older classic metric alerts. Azure Monitor 现在支持较新的近实时指标警报和新的警报体验Azure Monitor now supports newer near-real time metric alerts and a new alerts experience. 经典警报已停用,尽管尚不支持新警报的资源在使用方面仍存在限制。Classic alerts are retired, though still in limited use for resources that do not yet support the new alerts.

可以使用 Webhook 将 Azure 警报通知路由到其他系统,以便进行后续处理或自定义操作。You can use webhooks to route an Azure alert notification to other systems for post-processing or custom actions. 可以针对警报使用 Webhook,以将警报路由到可以发送短信的服务,以记录 Bug、通过聊天/消息服务通知团队,或进行各种其他操作。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. 此外,还说明向 Webhook 发出的 HTTP POST 的有效负载的大致形式。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

若要添加或更新 Webhook URI,请在 Azure 门户中转到“创建/更新警报”****。To add or update the webhook URI, in the Azure portal, go to Create/Update Alerts.

“添加警报规则”窗格

还可以使用 Azure PowerShell cmdlet跨平台 CLIAzure Monitor 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.

对 webhook 进行身份验证Authenticate the webhook

Webhook 可使用基于令牌的身份验证进行身份验证。The webhook can authenticate by using token-based authorization. 保存的 Webhook URI 具有令牌 ID。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/chinaeast/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": "chinaeast",
        "resourceName": "mysite1",
        "resourceType": "microsoft.foo/sites",
        "resourceId": "/subscriptions/s1/resourceGroups/chinaeast/providers/microsoft.foo/sites/mysite1",
        "resourceRegion": "chinaeast",
        "portalLink": "https://portal.azure.cn/#resource/subscriptions/s1/resourceGroups/chinaeast/providers/microsoft.foo/sites/mysite1"
    },
    "properties": {
        "key1": "value1",
        "key2": "value2"
    }
}
字段Field 必需Mandatory 一组固定值Fixed set of values 说明Notes
状态status YY Activated, ResolvedActivated, Resolved 基于设置的条件的警报的状态。The status for the alert based on the conditions you set.
上下文context YY 警报上下文。The alert context.
timestamptimestamp YY 触发警报的时间。The time at which the alert was triggered.
idid YY 每个警报规则都有一个唯一 ID。Every alert rule has a unique ID.
namename YY 警报名称。The alert name.
descriptiondescription YY 警报的说明。A description of the alert.
conditionTypeconditionType YY “Metric”、“Event”Metric, 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.
conditioncondition 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”、“Seconds”Bytes, 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.
阈值threshold 用于指标警报For metric alerts 会激活警报的阈值。The threshold value at which the alert is activated.
windowSizewindowSize 用于指标警报For metric alerts 用于根据阈值监视警报活动的时间段。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”、“Total”Average, 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.
运算符后的表达式operator 用于指标警报For metric alerts 用于将当前指标数据与所设阈值进行比较的运算符。The operator that's used to compare the current metric data to the set threshold.
subscriptionIdsubscriptionId YY Azure 订阅 ID。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 受影响资源的资源 ID。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 Monitor REST API 设置“属性”**** 字段。You can set the properties field only by using Azure Monitor REST APIs.

后续步骤Next steps