gpt4 book ai didi

azure - 使用 Terraform(或 ARM 模板)为 Webtest 创建 Azure 警报规则?

转载 作者:行者123 更新时间:2023-12-03 05:24:29 24 4
gpt4 key购买 nike

我正在尝试为 Application Insights → 可用性 → 标准(预览)测试创建警报规则。首先,如何创建标准测试?现在,我通过使用resource "azurerm_resource_group_template_deployment" …来创建资源。 ;请参阅gist.github.com/alexs77/0b2bd07aae7fbf22bd3c145132475e7d具体如何操作。

但即使它是 azurerm_application_insights_web_test ,警报规则(即 azurerm_monitor_metric_alert - 对吗?)会是什么样子?我对这部分感到困惑:

# …
resource "azurerm_monitor_metric_alert" "example" {
# …
criteria {
metric_name = "???"
metric_namespace = "???"
# …
}
# …

那里使用的正确值是什么?

我知道这篇古老的博客文章 Creating an Application Insights Web Test and Alert Programmatically从 2015 年到 6 年前。它会让您转到Azure Resource Explorer :

In Resource Explorer, open your subscription and resource group, then providers, Microsoft Insights. There you’ll see two folders that will be important to us today: webtests and alertrules.

当我转到“microsoft.insights”时,“文件夹”alertrules不存在吗。

Resource Explorer -> microsoft.insights: NO alertrules

最佳答案

I'm trying to create an Alert Rule for an Application Insights →Availability → Standard (preview) test. First off, how do you create astandard test?

由于Standard测试是预览功能,目前尚未添加到terraform-azurerm-provider中。目前只能从ARM 模板进行部署。目前只能从 Terraform 部署 Classic 测试。

<小时/>

But even if it were a azurerm_application_insights_web_test, how wouldan Alert Rule (ie. azurerm_monitor_metric_alert - right?) look like?

您不必使用条件/动态条件,而应该使用application_insights_web_test_location_availability_criteria。因此,不需要 metric_namemetric_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
}
}

输出:

enter image description here

enter image description here

enter image description here

<小时/>

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
}
}
}
]
}

输出:

enter image description here

enter image description here

enter image description here

关于azure - 使用 Terraform(或 ARM 模板)为 Webtest 创建 Azure 警报规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70063660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com