杭州电子商务研究院

使用 Azure 资源管理器 (ARM) 部署 Azure 日志警报

2020-10-27 08:00飞浪

介绍

Azure Monitor 捕获有关 Azure 资源的遥测、诊断、活动和指标数据。这些数据对于确定 Azure 环境的运行状况非常有用。但是,只有当数据向管理员和用户公开时,它才有用。您希望收到通知,以便尽快修复应用程序中的任何服务中断或严重错误。

在本指南中,您将学习如何编写日志查询来检测环境中的问题,以及如何部署警报规则以通知您的团队故障。本指南将使用 Azure 资源管理器 (ARM) 模板。

Azure 资源管理器模板

ARM 模板是一组 JSON 文件,您可以创建它们来定义 Azure 基础结构的不同方面,包括操作规则、警报规则和其他资源。它们很有用,因为您可以通过更改 JSON 配置中的几个项目轻松复制规则并进行大范围更改。

一种常见的情况是通过创建用于测试的暂存环境和更稳定的生产环境来复制 Azure 资源。使用 ARM 模板,您只需定义一次基础结构,即可轻松保持暂存环境和生产环境同步。

行动小组

定义基于日志的警报规则的先决条件之一是创建操作组。操作组是 Azure 警报将触发的以下零个或多个事件的组合:

  • 向用户发送电子邮件或短信
  • 触发自动化运行手册
  • 触发 Azure 函数
  • 触发逻辑应用
  • 触发 webhook
  • 触发 IT 服务管理工具(例如 ServiceNow)

Webhook 触发器提供与第三方系统最灵活的集成。您可以使用它与 Slack 和 Opsgenie 等服务集成。如果您的 Azure 环境很复杂且正常运行时间至关重要,那么使用 Webhook 操作并与Opsgenie等工具集成将为您提供最大的灵活性。Azure Monitor 可以管理遥测和活动数据的查询,而 Opsgenie 允许您创建更复杂的工作流,例如值班时间表、升级和通知。

要定义使用 ARM 向 Opsgenie 发出警报的操作组,请创建azuredeploy.json,如下所示:

      {
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "actionGroupName": {
      "type": "string"
    },
    "actionGroupShortName": {
      "type": "string"
    },
    "webhookUrl": {
      "type": "string"
    }
  },
  "resources": [
    {
      "name": "[parameters('actionGroupName')]",
      "type": "microsoft.insights/actionGroups",
      "apiVersion": "2019-06-01",
      "location": "Global",
      "tags": {
      },
      "properties": {
        "groupShortName": "[parameters('actionGroupShortName')]",
        "enabled": true,
        "webhookReceivers": [
          {
            "name": "opsgenie",
            "serviceUri": "[parameters('webhookUrl')]",
            "useCommonAlertSchema": true
          }
        ]
      }
    }
  ]
}
    

上面的 ARM 模板由两部分组成:

  • resources:具有 Opsgenie webhook 的操作组。请注意,它是一个数组,您可以添加更多。
  • 参数:允许您自定义模板。这对于创建具有不同名称的相同资源类型非常有用。您可能希望为资源添加环境前缀,例如staging-和prod-。

要部署此 ARM 模板,您可以使用 Azure CLI:

      rg=rg-alerts-pl
az group create -n $rg -l australiaeast
az group deployment create -g $rg --template-file Azuredeploy.json
    

这些命令使用 bash shell 创建资源组并将操作组部署到该资源组中。运行这些命令时,系统将提示您提供参数的值。您可以从Opsgenie 的 Azure 集成页面获取 webhookUrl 。

您还可以通过提供Microsoft 文档中描述的参数文件来提供参数。

基于日志的查询

现在您已通过操作组设置了与 Opsgenie 的集成,您可以开始创建基于日志的查询。为此,请创建一个 Application Insights 实例并针对它创建一个schedulingQuery 。

      rg=rg-alerts-pl
az extension add --name application-insights
az monitor app-insights create -a demo-insights-pl -l australiaeast -g $rg
    

然后,使用schedulingquery资源和一些额外参数扩展您之前创建的 ARM 模板文件以对其进行自定义。

      {
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "actionGroupName": {
      "type": "string",
      "defaultValue": "demo-ag-group"
    },
    "actionGroupShortName": {
      "type": "string",
      "defaultValue": "aaa"
    },
    "webhookUrl": {
      "type": "string",
      "defaultValue": "https://api.opsgenie.com/v1/json/azure?apiKey=xxxxxxxxxxxxxxxxxxxxxxxxx"
    },
    "alertRules": {
      "type": "array",
      "defaultValue": [{
        "alertRuleName": "demo-ar-exceptions",
        "alertDescription": "new exception occured in demo-insights-pl",
        "resourceName": "demo-insights-pl",
        "resourceGroup": "rg-alerts-pl",
        "query": "exceptions | union (traces | where (severityLevel > 2))"
      }]
    }
  },
  "resources": [
    {
      "name": "[parameters('actionGroupName')]",
      "type": "microsoft.insights/actionGroups",
      "apiVersion": "2019-06-01",
      "location": "Global",
      "tags": {
      },
      "properties": {
        "groupShortName": "[parameters('actionGroupShortName')]",
        "enabled": true,
        "webhookReceivers": [
          {
            "name": "opsgenie",
            "serviceUri": "[parameters('webhookUrl')]",
            "useCommonAlertSchema": true
          }
        ]
      }
    },
    {
      "name": "[parameters('alertRules')[copyIndex()].alertRuleName]",
      "type": "microsoft.insights/scheduledqueryrules",
      "location": "[resourceGroup().location]",
      "apiVersion": "2018-04-16",
      "dependsOn": [
        "[resourceId('microsoft.insights/actionGroups', parameters('actionGroupName'))]"
      ],
      "tags": {
      },
      "copy": {
        "name": "alertscopy",
        "count": "[length(parameters('alertRules'))]"
      },
      "properties": {
        "description": "[parameters('alertRules')[copyIndex()].alertDescription]",
        "enabled": "true",
        "source": {
          "query": "[parameters('alertRules')[copyIndex()].query]",
          "authorizedResources": [
          ],
          "dataSourceId": "[resourceId(parameters('alertRules')[copyIndex()].resourceGroup, 'microsoft.insights/components', parameters('alertRules')[copyIndex()].resourceName)]",
          "queryType": "ResultCount"
        },
        "schedule": {
          "frequencyInMinutes": 5,
          "timeWindowInMinutes": 5
        },
        "action": {
          "severity": "3",
          "aznsAction": {
            "actionGroup": [
              "[resourceId('microsoft.insights/actionGroups', parameters('actionGroupName'))]"
            ]
          },
          "throttlingInMin": 60,
          "throttleConsecutiveWindowCount": 0,
          "trigger": {
            "thresholdOperator": "GreaterThan",
            "threshold": 1
          },
          "odata.type": "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.Microsoft.AppInsights.Nexus.DataContracts.Resources.ScheduledQueryRules.AlertingAction"
        }
      }
    }
  ]
}
    

这段代码有几个有趣的方面:

  • 您添加了alertRules参数,它是一个数组。这允许您创建连接到同一操作组的多个警报规则。
  • schedulingQuery使用copyIndex()函数根据alertRules数组创建多个资源。这样可以轻松添加、修改或删除警报规则。
  • 警报规则的不同方面均可配置。例如,您可以将警报配置为仅在观察到 5 个以上异常时触发,或将警报配置为每 24 小时仅触发一次,以防止向您的警报系统发送垃圾邮件。
  • 为简洁起见,已将defaultValue添加到参数中。您可能希望省略此操作并将参数保存在单独的文件中。

结论

警报是一种强大的工具,可让您随时了解系统状态的变化。要了解有关如何设置和最佳配置 Opsgenie 集成的更多信息,请阅读使用 OpsGenie 在 Slack 上进行事件管理的最佳实践。

曝光31677浏览1682