Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
可以在日志搜索警报规则中使用 Webhook 操作来调用单个 HTTP POST 请求。 本文介绍将操作组配置为使用 Webhook 时可用的属性。 被调用的服务应支持 Webhook,并知道将如何使用其接收的有效负载。
建议使用通用警报架构进行 Webhook 集成。 通用警报架构的优点是可以跨 Azure Monitor 中的所有警报服务提供单个可扩展且统一的警报有效负载。
对于定义了自定义 JSON 有效负载的日志搜索警报规则,启用此通用警报架构会将有效负载架构还原为此处所述架构。 如果要定义自定义 JSON 有效负载,Webhook 将无法使用通用警报架构。
对于启用了常见架构的警报,大小上限为 256KB/警报。 更大的警报不会包括搜索结果。 如果不包括搜索结果,可使用 LinkToFilteredSearchResultsAPI
或 LinkToSearchResultsAPI
通过 Log Analytics API 访问查询结果。
示例有效负载包括有效负载是标准有效负载时以及是自定义有效负载时的示例。
以下示例负载用于基于资源日志的日志搜索警报时的标准 Webhook:
{
"schemaId": "azureMonitorCommonAlertSchema",
"data": {
"essentials": {
"alertId": "/subscriptions/12345a-1234b-123c-123d-12345678e/providers/Microsoft.AlertsManagement/alerts/12345a-1234b-123c-123d-12345678e",
"alertRule": "AcmeRule",
"severity": "Sev4",
"signalType": "Log",
"monitorCondition": "Fired",
"monitoringService": "Log Alerts V2",
"alertTargetIDs": [
"/subscriptions/12345a-1234b-123c-123d-12345678e/resourcegroups/ai-engineering/providers/microsoft.compute/virtualmachines/testvm"
],
"originAlertId": "123c123d-1a23-1bf3-ba1d-dd1234ff5a67",
"firedDateTime": "2020-07-09T14:04:49.99645Z",
"description": "log alert rule V2",
"essentialsVersion": "1.0",
"alertContextVersion": "1.0"
},
"alertContext": {
"properties": {
"name1": "value1",
"name2": "value2"
},
"conditionType": "LogQueryCriteria",
"condition": {
"windowSize": "PT10M",
"allOf": [
{
"searchQuery": "Heartbeat",
"metricMeasureColumn": "CounterValue",
"targetResourceTypes": "['Microsoft.Compute/virtualMachines']",
"operator": "LowerThan",
"threshold": "1",
"timeAggregation": "Count",
"dimensions": [
{
"name": "ResourceId",
"value": "/subscriptions/12345a-1234b-123c-123d-12345678e/resourceGroups/TEST/providers/Microsoft.Compute/virtualMachines/testvm"
}
],
"metricValue": 0.0,
"failingPeriods": {
"numberOfEvaluationPeriods": 1,
"minFailingPeriodsToAlert": 1
},
"linkToSearchResultsUI": "https://portal.azure.cn#@12f345bf-12f3-12af-12ab-1d2cd345db67/blade/Microsoft_Azure_Monitoring_Logs/LogsBlade/source/Alerts.EmailLinks/scope/%7B%22resources%22%3A%5B%7B%22resourceId%22%3A%22%2Fsubscriptions%2F12345a-1234b-123c-123d-12345678e%2FresourceGroups%2FTEST%2Fproviders%2FMicrosoft.Compute%2FvirtualMachines%2Ftestvm%22%7D%5D%7D/q/eJzzSE0sKklKTSypUSjPSC1KVQjJzE11T81LLUosSU1RSEotKU9NzdNIAfJKgDIaRgZGBroG5roGliGGxlYmJlbGJnoGEKCpp4dDmSmKMk0A/prettify/1/timespan/2020-07-07T13%3a54%3a34.0000000Z%2f2020-07-09T13%3a54%3a34.0000000Z",
"linkToFilteredSearchResultsUI": "https://portal.azure.cn#@12f345bf-12f3-12af-12ab-1d2cd345db67/blade/Microsoft_Azure_Monitoring_Logs/LogsBlade/source/Alerts.EmailLinks/scope/%7B%22resources%22%3A%5B%7B%22resourceId%22%3A%22%2Fsubscriptions%2F12345a-1234b-123c-123d-12345678e%2FresourceGroups%2FTEST%2Fproviders%2FMicrosoft.Compute%2FvirtualMachines%2Ftestvm%22%7D%5D%7D/q/eJzzSE0sKklKTSypUSjPSC1KVQjJzE11T81LLUosSU1RSEotKU9NzdNIAfJKgDIaRgZGBroG5roGliGGxlYmJlbGJnoGEKCpp4dDmSmKMk0A/prettify/1/timespan/2020-07-07T13%3a54%3a34.0000000Z%2f2020-07-09T13%3a54%3a34.0000000Z",
"linkToSearchResultsAPI": "https://api.loganalytics.io/v1/subscriptions/12345a-1234b-123c-123d-12345678e/resourceGroups/TEST/providers/Microsoft.Compute/virtualMachines/testvm/query?query=Heartbeat%7C%20where%20TimeGenerated%20between%28datetime%282020-07-09T13%3A44%3A34.0000000%29..datetime%282020-07-09T13%3A54%3A34.0000000%29%29×pan=2020-07-07T13%3a54%3a34.0000000Z%2f2020-07-09T13%3a54%3a34.0000000Z",
"linkToFilteredSearchResultsAPI": "https://api.loganalytics.io/v1/subscriptions/12345a-1234b-123c-123d-12345678e/resourceGroups/TEST/providers/Microsoft.Compute/virtualMachines/testvm/query?query=Heartbeat%7C%20where%20TimeGenerated%20between%28datetime%282020-07-09T13%3A44%3A34.0000000%29..datetime%282020-07-09T13%3A54%3A34.0000000%29%29×pan=2020-07-07T13%3a54%3a34.0000000Z%2f2020-07-09T13%3a54%3a34.0000000Z"
}
],
"windowStartTime": "2020-07-07T13:54:34Z",
"windowEndTime": "2020-07-09T13:54:34Z"
}
}
}
}
以下示例有效负载适用于基于 Log Analytics 的警报使用的标准 Webhook 操作:
{
"SubscriptionId": "12345a-1234b-123c-123d-12345678e",
"AlertRuleName": "AcmeRule",
"SearchQuery": "Perf | where ObjectName == \"Processor\" and CounterName == \"% Processor Time\" | summarize AggregatedValue = avg(CounterValue) by bin(TimeGenerated, 5m), Computer",
"SearchIntervalStartTimeUtc": "2018-03-26T08:10:40Z",
"SearchIntervalEndtimeUtc": "2018-03-26T09:10:40Z",
"AlertThresholdOperator": "Greater Than",
"AlertThresholdValue": 0,
"ResultCount": 2,
"SearchIntervalInSeconds": 3600,
"LinkToSearchResults": "https://portal.azure.cn/#Analyticsblade/search/index?_timeInterval.intervalEnd=2018-03-26T09%3a10%3a40.0000000Z&_timeInterval.intervalDuration=3600&q=Usage",
"LinkToFilteredSearchResultsUI": "https://portal.azure.cn/#Analyticsblade/search/index?_timeInterval.intervalEnd=2018-03-26T09%3a10%3a40.0000000Z&_timeInterval.intervalDuration=3600&q=Usage",
"LinkToSearchResultsAPI": "https://api.loganalytics.io/v1/workspaces/workspaceID/query?query=Heartbeat×pan=2020-05-07T18%3a11%3a51.0000000Z%2f2020-05-07T18%3a16%3a51.0000000Z",
"LinkToFilteredSearchResultsAPI": "https://api.loganalytics.io/v1/workspaces/workspaceID/query?query=Heartbeat×pan=2020-05-07T18%3a11%3a51.0000000Z%2f2020-05-07T18%3a16%3a51.0000000Z",
"Description": "log alert rule",
"Severity": "Warning",
"AffectedConfigurationItems": [
"INC-Gen2Alert"
],
"Dimensions": [
{
"name": "Computer",
"value": "INC-Gen2Alert"
}
],
"SearchResult": {
"tables": [
{
"name": "PrimaryResult",
"columns": [
{
"name": "$table",
"type": "string"
},
{
"name": "Computer",
"type": "string"
},
{
"name": "TimeGenerated",
"type": "datetime"
}
],
"rows": [
[
"Fabrikam",
"33446677a",
"2018-02-02T15:03:12.18Z"
],
[
"Contoso",
"33445566b",
"2018-02-02T15:16:53.932Z"
]
]
}
]
},
"WorkspaceId": "12345a-1234b-123c-123d-12345678e",
"AlertType": "Metric measurement"
}
以下示例有效负载是在标准 Webhook 基于 Application Insights 资源用于日志搜索警报时使用的:
{
"schemaId": "Microsoft.Insights/LogAlert",
"data": {
"SubscriptionId": "12345a-1234b-123c-123d-12345678e",
"AlertRuleName": "AcmeRule",
"SearchQuery": "requests | where resultCode == \"500\" | summarize AggregatedValue = Count by bin(Timestamp, 5m), IP",
"SearchIntervalStartTimeUtc": "2018-03-26T08:10:40Z",
"SearchIntervalEndtimeUtc": "2018-03-26T09:10:40Z",
"AlertThresholdOperator": "Greater Than",
"AlertThresholdValue": 0,
"ResultCount": 2,
"SearchIntervalInSeconds": 3600,
"LinkToSearchResults": "https://portal.azure.cn/AnalyticsBlade/subscriptions/12345a-1234b-123c-123d-12345678e/?query=search+*+&timeInterval.intervalEnd=2018-03-26T09%3a10%3a40.0000000Z&_timeInterval.intervalDuration=3600&q=Usage",
"LinkToFilteredSearchResultsUI": "https://portal.azure.cn/AnalyticsBlade/subscriptions/12345a-1234b-123c-123d-12345678e/?query=search+*+&timeInterval.intervalEnd=2018-03-26T09%3a10%3a40.0000000Z&_timeInterval.intervalDuration=3600&q=Usage",
"LinkToSearchResultsAPI": "https://api.applicationinsights.io/v1/apps/0MyAppId0/metrics/requests/count",
"LinkToFilteredSearchResultsAPI": "https://api.applicationinsights.io/v1/apps/0MyAppId0/metrics/requests/count",
"Description": null,
"Severity": "3",
"Dimensions": [
{
"name": "IP",
"value": "1.1.1.1"
}
],
"SearchResult": {
"tables": [
{
"name": "PrimaryResult",
"columns": [
{
"name": "$table",
"type": "string"
},
{
"name": "Id",
"type": "string"
},
{
"name": "Timestamp",
"type": "datetime"
}
],
"rows": [
[
"Fabrikam",
"33446677a",
"2018-02-02T15:03:12.18Z"
],
[
"Contoso",
"33445566b",
"2018-02-02T15:16:53.932Z"
]
]
}
]
},
"ApplicationId": "00001111-aaaa-2222-bbbb-3333cccc4444",
"AlertType": "Metric measurement"
}
}
Nota
从 API 版本 2021-08-01
开始,不支持基于 JSON 的自定义 Webhook。
下表列出了默认 Webhook 操作属性及其自定义 JSON 参数名称。
参数 | 变量 | 说明 |
---|---|---|
AlertRuleName |
#alertrulename | 警报规则的名称。 |
Severity |
#severity | 为触发的日志搜索警报设置严重性。 |
AlertThresholdOperator |
#thresholdoperator | 警报规则的阈值运算符。 |
AlertThresholdValue |
#thresholdvalue | 警报规则的阈值。 |
LinkToSearchResults |
#linktosearchresults | 指向 Analytics 门户的链接,该门户会从创建警报的查询返回记录。 |
LinkToSearchResultsAPI |
#linktosearchresultsapi | 指向 Analytics API 的链接,该 API 会从创建警报的查询返回记录。 |
LinkToFilteredSearchResultsUI |
#linktofilteredsearchresultsui | 指向 Analytics 门户的链接,该门户返回按创建警报的维度值组合筛选的查询中的记录。 |
LinkToFilteredSearchResultsAPI |
#linktofilteredsearchresultsapi | 指向 Analytics API 的链接,该 API 返回按创建警报的维度值组合筛选的查询中的记录。 |
ResultCount |
#searchresultcount | 搜索结果中的记录数。 |
Search Interval End time |
#searchintervalendtimeutc | 查询结束时间 (UTC),格式为 mm/dd/yyyy HH:mm:ss AM/PM。 |
Search Interval |
#searchinterval | 警报规则的时间范围,格式为 HH:mm:ss。 |
Search Interval StartTime |
#searchintervalstarttimeutc | 查询开始时间 (UTC),格式为 mm/dd/yyyy HH:mm:ss AM/PM。 |
SearchQuery |
#searchquery | 警报规则所使用的日志搜索查询。 |
SearchResults |
"IncludeSearchResults": true | 查询以 JSON 表形式返回的记录,仅限于前 1,000 条记录。 在自定义 JSON Webhook 定义中添加 "IncludeSearchResults": true 作为顶级属性。 |
Dimensions |
"IncludeDimensions": true | 将该警报作为 JSON 部分触发的维度值组合。 在自定义 JSON Webhook 定义中添加 "IncludeDimensions": true 作为顶级属性。 |
Alert Type |
#alerttype | 配置为指标度量或结果数的日志搜索警报规则的类型。 |
WorkspaceID |
#workspaceid | Log Analytics 工作区的 ID。 |
Application ID |
#applicationid | Application Insights 应用的 ID。 |
Subscription ID |
#subscriptionid | 使用的 Azure 订阅的 ID。 |
通过使用这些参数,可以使用包括 Webhook 的自定义 JSON 有效负载来获取自定义 JSON 有效负载。 还可以生成更多属性。
例如,可以指定以下自定义负载,其中包含名为 text
的单一参数。 此 Webhook 调用的服务需要此参数:
{
"text":"#alertrulename fired with #searchresultcount over threshold of #thresholdvalue."
}
此示例有效负载会在发送到 Webhook 时解析为类似以下示例的内容:
{
"text":"My Alert Rule fired with 18 records over threshold of 10 ."
}
必须在 JSON 附件中指定自定义 Webhook 中的变量。 例如,在 Webhook 示例中引用 #searchresultcount
将根据警报结果生成输出。
若要包含搜索结果,在自定义 JSON 中将 IncludeSearchResults 添加为顶级属性。 搜索结果以 JSON 结构的形式包含,因此无法在自定义的字段中引用结果。
Nota
“包含 Webhook 的自定义 JSON 有效负载”选项旁的“查看 Webhook”按钮可显示提供内容的预览。 它不包含实际数据,但代表要使用的 JSON 架构。
例如,要创建仅包含警报名称和搜索结果的自定义有效负载,可使用以下配置:
{
"alertname":"#alertrulename",
"IncludeSearchResults":true
}
下面是用于任何日志搜索警报的自定义 Webhook 操作的示例有效负载:
{
"alertname":"AcmeRule","IncludeSearchResults":true,
"SearchResults":
{
"tables":[
{"name":"PrimaryResult","columns":
[
{"name":"$table","type":"string"},
{"name":"Id","type":"string"},
{"name":"TimeGenerated","type":"datetime"}
],
"rows":
[
["Fabrikam","33446677a","2018-02-02T15:03:12.18Z"],
["Contoso","33445566b","2018-02-02T15:16:53.932Z"]
]
}
]
}
}
- 了解 Azure Monitor 警报。
- 创建和管理 Azure 中的操作组。
- 了解有关日志查询的详细信息。