0
4

1 回答 1

1

我正在尝试为 Application Insights → 可用性 → 标准(预览)测试创建警报规则。首先,您如何创建标准测试?

由于StandardTest 是一个预览功能,它目前没有添加到terraform-azurerm-provider中。到目前为止,它只能从ARM 模板部署。目前只能ClassicTerraform部署测试。


但即使它是 azurerm_application_insights_web_test,警报规则(即 azurerm_monitor_metric_alert - 对吗?)会是什么样子?

您不必使用,criteria / dynamic criteria而是应该使用application_insights_web_test_location_availability_criteria. 所以,metric_name并且metric_namespace不需要。使用带有警报规则的经典 Web 测试代码将如下所示:

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 模板更新:

{
    "$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
        }
      }
    }
  ]
}

输出:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

于 2021-11-23T05:59:18.710 回答