首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为带有Terraform (或ARM模板)的Webtest创建Azure警报规则?

为带有Terraform (或ARM模板)的Webtest创建Azure警报规则?
EN

Stack Overflow用户
提问于 2021-11-22 09:40:56
回答 1查看 1.3K关注 0票数 0

我正在尝试为应用程序洞察→可用性→标准(预览)测试创建一个警报规则。首先,如何创建标准测试?现在,我求助于通过使用resource "azurerm_resource_group_template_deployment" …来创建资源;有关具体方法,请参见gist.github.com/alexs77/0b2bd07aae7fbf22bd3c145132475e7d

但即使是azurerm_application_insights_web_test,警报规则(即. -对吗?)看起来像?我对这部分感到困惑:

代码语言:javascript
运行
复制
# …
resource "azurerm_monitor_metric_alert" "example" {
  # …
  criteria {
    metric_name      = "???"
    metric_namespace = "???"
    # …
  }
# …

正确的值是什么?

我知道这个古老的博客文章以编程方式创建应用程序洞察、Web测试和警报从2015年-6年前。它让你去看浅蓝资源探索者

在“资源资源管理器”中,打开您的订阅和资源组,然后打开“提供程序”Microsoft。在这里,您将看到两个文件夹,对我们今天将是重要的:网络测试和警报

当我转到"microsoft.insights“时,”文件夹“alertrules就不在了。

EN

回答 1

Stack Overflow用户

发布于 2021-11-23 05:59:18

我正在尝试为应用程序洞察→可用性→标准(预览)测试创建一个警报规则。首先,如何创建标准测试?

由于terraform-azurerm-provider.测试是预览功能,因此目前还没有在Standard中添加它到目前为止,它只能从ARM模板部署。目前只有Classic测试可以从Terraform部署。

但即使是azurerm_application_insights_web_test,警报规则(即.azurerm_monitor_metric_alert -对吗?)看起来像?

您不必使用criteria / dynamic criteria,而应该使用application_insights_web_test_location_availability_criteria。因此,不需要metric_namemetric_namespace。使用该代码,您的经典web测试代码带有警报规则,如下所示:

代码语言:javascript
运行
复制
provider "azurerm" {
  features{}
}
data "azurerm_application_insights" "example" {
  name="ansumantestapp"
  resource_group_name = "ansbalrg"
}

resource "azurerm_application_insights_web_test" "example" {
  name                    = "tf-test-appinsights-webtest"
  location                = data.azurerm_application_insights.example.location
  resource_group_name     = data.azurerm_application_insights.example.resource_group_name
  application_insights_id = data.azurerm_application_insights.example.id
  kind                    = "ping"
  frequency               = 300
  timeout                 = 60
  enabled                 = true
  geo_locations           = ["us-ca-sjc-azr", "us-va-ash-azr"]

  configuration = <<XML
<WebTest Name="WebTest1" Id="ABD48585-0831-40CB-9069-682EA6BB3583" Enabled="True" CssProjectStructure="" CssIteration="" Timeout="0" WorkItemIds="" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" Description="" CredentialUserName="" CredentialPassword="" PreAuthenticate="True" Proxy="default" StopOnError="False" RecordedResultFile="" ResultsLocale="">
  <Items>
    <Request Method="GET" Guid="a5f10126-e4cd-570d-961c-cea43999a200" Version="1.1" Url="http://microsoft.com" ThinkTime="0" Timeout="300" ParseDependentRequests="True" FollowRedirects="True" RecordResult="True" Cache="False" ResponseTimeGoal="0" Encoding="utf-8" ExpectedHttpStatusCode="200" ExpectedResponseUrl="" ReportingName="" IgnoreHttpStatusCode="False" />
  </Items>
</WebTest>
XML

}
resource "azurerm_monitor_action_group" "main" {
  name                = "example-actiongroup"
  resource_group_name = data.azurerm_application_insights.example.resource_group_name
  short_name          = "exampleact"
  email_receiver {
    name                    = "sendtoadmin"
    email_address           = "myemailid"
    use_common_alert_schema = true
  }
}

resource "azurerm_monitor_metric_alert" "example" {
  name                = "example-metricalert"
  resource_group_name = data.azurerm_application_insights.example.resource_group_name
  scopes = [azurerm_application_insights_web_test.example.id,data.azurerm_application_insights.example.id]
  description         = "PING test alert"

application_insights_web_test_location_availability_criteria {
  web_test_id = azurerm_application_insights_web_test.example.id
  component_id = data.azurerm_application_insights.example.id
  failed_location_count = 2
}

  action {
    action_group_id = azurerm_monitor_action_group.main.id
  }
}

输出:

ARM模板的更新:

代码语言:javascript
运行
复制
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "appInsightsName": {"type": "String"},
      "certRemainingLifetimeCheck": {"type": "Int"},
      "resourceLocation": {"type": "String"},
      "testCategory": {"type": "String"},
      "testHostname": {"type": "String"},
      "testHostnameSafe": {"type": "String"},
      "testHttpCode": {"type": "String"},
      "testLocations": {"type": "String"},
      "appname":{"type": "String"}
    },
    "variables": {
      "requestUrl": "[concat('https://',parameters('testHostname'))]",
      "testLocations": "[split(parameters('testLocations'),',')]",
      "hiddenLink": "[concat('hidden-link:',resourceId('microsoft.insights/components',parameters('appInsightsName')))]",
      "pingTestName":"[concat(parameters('testHostnameSafe'),'-tls-ttl-', parameters('testCategory'), '-webtest')]",
      "pingAlertRuleName": "[concat('PingAlert-', toLower(parameters('appName')), '-', subscription().subscriptionId)]",
      "copy": [
        {
          "name": "locations",
          "count": "[length(variables('testLocations'))]",
          "input":{
              "Id": "[variables('testLocations')[copyIndex('locations')]]"
          }
        }
      ]
    },
    "resources": [
      {
        "type": "Microsoft.Insights/webtests",
        "apiVersion": "2018-05-01-preview",
        "name": "[variables('pingTestName')]",
        "location":"[parameters('resourceLocation')]",
        "tags": {
          "Managed By": "Terraform",
          "[variables('hiddenLink')]": "Resource"
        },
        "properties": {
          "SyntheticMonitorId": "[variables('pingTestName')]",
          "Name": "[variables('pingTestName')]",
          "Description": "[concat('Check lifetime of TLS certificate on ', parameters('testHostnameSafe'))]",
          "Enabled": true,
          "Frequency": 300,
          "Timeout": 120,
          "Kind": "standard",
          "RetryEnabled": true,
          "Locations": "[variables('locations')]",
          "Configuration": null,
          "Request": {
            "RequestUrl": "[variables('requestUrl')]",
            "Headers": null,
            "HttpVerb": "GET",
            "RequestBody": null,
            "ParseDependentRequests": false,
            "FollowRedirects": null
          },
          "ValidationRules": {
            "ExpectedHttpStatusCode": "[int(parameters('testHttpCode'))]",
            "IgnoreHttpsStatusCode": false,
            "ContentValidation": null,
            "SSLCheck": true,
            "SSLCertRemainingLifetimeCheck": "[parameters('certRemainingLifetimeCheck')]"
          }
        }
      },
      {
      "name": "[variables('pingAlertRuleName')]",
      "type": "Microsoft.Insights/metricAlerts",
      "apiVersion": "2018-03-01",
      "location": "global",
      "dependsOn": [
        "[resourceId('Microsoft.Insights/webtests', variables('pingTestName'))]"
      ],
      "tags": {
        "[variables('hiddenLink')]": "Resource",
        "[concat('hidden-link:', resourceId('Microsoft.Insights/webtests', variables('pingTestName')))]": "Resource"
      },
      "properties": {
        "description": "Alert for web test",
        "severity": 1,
        "enabled": true,
        "scopes": [
          "[resourceId('Microsoft.Insights/webtests',variables('pingTestName'))]",
          "[resourceId('microsoft.insights/components',parameters('appInsightsName'))]"
        ],
        "evaluationFrequency": "PT1M",
        "windowSize": "PT5M",
        "criteria": {
          "odata.type": "Microsoft.Azure.Monitor.WebtestLocationAvailabilityCriteria",
          "webTestId": "[resourceId('Microsoft.Insights/webtests', variables('pingTestName'))]",
          "componentId": "[resourceId('microsoft.insights/components',parameters('appInsightsName'))]",
          "failedLocationCount": 2
        }
      }
    }
  ]
}

输出:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70063660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档