使用自动缩放操作在 Azure 监视器中发送电子邮件和 webhook 警报通知Use autoscale actions to send email and webhook alert notifications in Azure Monitor

本文演示如何设置触发器,以便可以在 Azure 中基于自动缩放操作调用特定 Web URL 或发送电子邮件。This article shows you how set up triggers so that you can call specific web URLs or send emails based on autoscale actions in Azure.

WebhookWebhooks

通过 webhook 可以将 Azure 警报通知路由到其他系统以便用于后处理或自定义通知。Webhooks allow you to route the Azure alert notifications to other systems for post-processing or custom notifications. 例如,将警报路由到可以处理传入 web 请求的服务,以便使用聊天或消息服务等来发送 SMS、记录 bug、通知团队。Webhook URI 必须是有效 HTTP 或 HTTPS 终结点。For example, routing the alert to services that can handle an incoming web request to send SMS, log bugs, notify a team using chat or messaging services, etc. The webhook URI must be a valid HTTP or HTTPS endpoint.

EmailEmail

电子邮件可以发送到任何有效电子邮件地址。Email can be sent to any valid email address. 还将通知运行规则的订阅的管理员和共同管理员。Administrators and co-administrators of the subscription where the rule is running will also be notified.

云服务和应用服务Cloud Services and App Services

可以从 Azure 门户选择加入云服务和服务器场(应用服务)。You can opt-in from the Azure portal for Cloud Services and Server Farms (App Services).

  • 选择“缩放依据” 指标。Choose the scale by metric.

缩放依据

虚拟机规模集Virtual Machine scale sets

对于使用 Resource Manager(虚拟机规模集)创建的较新虚拟机,可以使用REST API、Resource Manager 模板、PowerShell 和 CLI 进行配置。For newer Virtual Machines created with Resource Manager (Virtual Machine scale sets), you can configure this using REST API, Resource Manager templates, PowerShell, and CLI. 门户界面尚不可用。A portal interface is not yet available. 使用 REST API 或资源管理器模板时,在 autoscalesettings 中包括具有以下选项的通知元素。When using the REST API or Resource Manager template, include the notifications element in your autoscalesettings with the following options.

"notifications": [
      {
        "operation": "Scale",
        "email": {
          "sendToSubscriptionAdministrator": false,
          "sendToSubscriptionCoAdministrators": false,
          "customEmails": [
              "user1@mycompany.com",
              "user2@mycompany.com"
              ]
        },
        "webhooks": [
          {
            "serviceUri": "https://foo.webhook.example.com?token=abcd1234",
            "properties": {
              "optional_key1": "optional_value1",
              "optional_key2": "optional_value2"
            }
          }
        ]
      }
    ]
字段Field 必需?Mandatory? 说明Description
operationoperation yes 值必须是“规模”value must be "Scale"
sendToSubscriptionAdministratorsendToSubscriptionAdministrator yes 值必须是“true”或“false”value must be "true" or "false"
sendToSubscriptionCoAdministratorssendToSubscriptionCoAdministrators yes 值必须是“true”或“false”value must be "true" or "false"
customEmailscustomEmails yes 值可以为 null [] 或电子邮件的字符串数组value can be null [] or string array of emails
webhookswebhooks yes 该值可以为 null 或有效的 URIvalue can be null or valid Uri
serviceUriserviceUri yes 有效的 https URIa valid https Uri
propertiesproperties yes 值必须是空的 {},也可以包含键值对value must be empty {} or can contain key-value pairs

webhook 中的身份验证Authentication in webhooks

webhook 可使用基于令牌的身份验证进行身份验证:将具有令牌 ID 的 webhook URI 保存为查询参数。The webhook can authenticate using token-based authentication, where you save the webhook URI with a token ID as a query parameter. 例如: https://mysamplealert/webcallback?tokenid=sometokenid&someparameter=somevalueFor example, https://mysamplealert/webcallback?tokenid=sometokenid&someparameter=somevalue

自动缩放通知 webhook 负载架构Autoscale notification webhook payload schema

生成自动缩放通知时,以下元数据会包含在 webhook 负载中:When the autoscale notification is generated, the following metadata is included in the webhook payload:

{
        "version": "1.0",
        "status": "Activated",
        "operation": "Scale In",
        "context": {
                "timestamp": "2016-03-11T07:31:04.5834118Z",
                "id": "/subscriptions/s1/resourceGroups/rg1/providers/microsoft.insights/autoscalesettings/myautoscaleSetting",
                "name": "myautoscaleSetting",
                "details": "Autoscale successfully started scale operation for resource 'MyCSRole' from capacity '3' to capacity '2'",
                "subscriptionId": "s1",
                "resourceGroupName": "rg1",
                "resourceName": "MyCSRole",
                "resourceType": "microsoft.classiccompute/domainnames/slots/roles",
                "resourceId": "/subscriptions/s1/resourceGroups/rg1/providers/microsoft.classicCompute/domainNames/myCloudService/slots/Production/roles/MyCSRole",
                "portalLink": "https://portal.azure.cn/#resource/subscriptions/s1/resourceGroups/rg1/providers/microsoft.classicCompute/domainNames/myCloudService",
                "oldCapacity": "3",
                "newCapacity": "2"
        },
        "properties": {
                "key1": "value1",
                "key2": "value2"
        }
}
字段Field 必需?Mandatory? 说明Description
状态status yes 指示生成自动缩放操作的状态The status that indicates that an autoscale action was generated
operationoperation yes 对于实例的增加,它会是“Scale Out”(横向扩展),对于实例的减少,它会是“Scale In”(横向缩减)For an increase of instances, it will be "Scale Out" and for a decrease in instances, it will be "Scale In"
上下文context yes 自动缩放操作上下文The autoscale action context
timestamptimestamp yes 触发自动缩放操作时的时间戳Time stamp when the autoscale action was triggered
idid Yes 自动缩放设置的 Resource Manager IDResource Manager ID of the autoscale setting
namename Yes 自动缩放设置的名称The name of the autoscale setting
详细信息details Yes 自动缩放服务执行的操作和实例计数的更改的说明Explanation of the action that the autoscale service took and the change in the instance count
subscriptionIdsubscriptionId Yes 所缩放的目标资源的订阅 IDSubscription ID of the target resource that is being scaled
resourceGroupNameresourceGroupName Yes 所缩放的目标资源的资源组名Resource Group name of the target resource that is being scaled
resourceNameresourceName Yes 所缩放的目标资源的名称Name of the target resource that is being scaled
resourceTyperesourceType Yes 三个支持的值是:“microsoft.classiccompute/domainnames/slots/roles” - 云服务角色、“microsoft.compute/virtualmachinescalesets” - 虚拟机规模集和“Microsoft.Web/serverfarms” - Web 应用The three supported values: "microsoft.classiccompute/domainnames/slots/roles" - Cloud Service roles, "microsoft.compute/virtualmachinescalesets" - Virtual Machine Scale Sets, and "Microsoft.Web/serverfarms" - Web App
ResourceIdresourceId Yes 所缩放的目标资源的 Resource Manager IDResource Manager ID of the target resource that is being scaled
portalLinkportalLink Yes 指向目标资源摘要页的 Azure 门户链接Azure portal link to the summary page of the target resource
oldCapacityoldCapacity Yes 自动缩放执行缩放操作时的当前(旧)实例计数The current (old) instance count when Autoscale took a scale action
newCapacitynewCapacity Yes 自动缩放将资源缩放到的新实例计数The new instance count that Autoscale scaled the resource to
propertiesproperties No 可选。Optional. <键, 值> 对集(例如字典 <String, String>)。Set of <Key, Value> pairs (for example, Dictionary <String, String>). properties 字段是可选的。The properties field is optional. 在自定义用户界面或基于逻辑应用的工作流中,可以输入可使用有效负载传递的键和值。In a custom user interface or Logic app based workflow, you can enter keys and values that can be passed using the payload. 将自定义属性传递回传出 webhook 调用的替代方法是使用 webhook URI 本身(作为查询参数)An alternate way to pass custom properties back to the outgoing webhook call is to use the webhook URI itself (as query parameters)