使用 Terraform(或 ARM 模板)为 Webtest 创建 Azure 警报规则? [英] Creating an Azure Alert Rule for a Webtest with Terraform (or ARM template)?
问题描述
我正在尝试为 Application Insights → 可用性 → 标准(预览)测试创建警报规则.首先,您如何创建标准测试?现在,我求助于使用 资源azurerm_resource_group_template_deployment"来创建资源.…
;请参阅
我正在尝试为 Application Insights 创建警报规则 →可用性 → 标准(预览)测试.首先,你如何创建一个标准测试?
由于 Standard
测试是预览功能,目前 terraform-azurerm-provider 中没有添加.目前只能从 ARM 模板 进行部署.目前只能从 Terraform 部署 Classic
测试.
<块引用>
但即使是 azurerm_application_insights_web_test,如何警报规则(即 azurerm_monitor_metric_alert - 对吗?)看起来像?
您不必使用 criteria/dynamic criteria
而应该使用 application_insights_web_test_location_availability_criteria
.因此, metric_name
和 metric_namespace
将不是必需的.使用您的 classic web test with alert rule 代码将如下所示:
提供者azurerm"{特征{}}数据azurerm_application_insights"例子"{名称=ansumantestapp";resource_group_name = "ansbalrg";}资源azurerm_application_insights_web_test"例子"{名称=tf-test-appinsights-webtest";位置 = data.azurerm_application_insights.example.locationresource_group_name = data.azurerm_application_insights.example.resource_group_nameapplication_insights_id = data.azurerm_application_insights.example.id种类=ping";频率 = 300超时 = 60启用 = 真geo_locations = [us-ca-sjc-azr",us-va-ash-azr"]配置=<<XML<WebTest 名称 =WebTest1";ID =ABD48585-0831-40CB-9069-682EA6BB3583"启用=真"CssProjectStructure="CssIteration="超时=0"WorkItemIds="xmlns=http://microsoft.com/schemas/VisualStudio/TeamTest/2010"描述="CredentialUserName="凭据密码="PreAuthenticate=真"代理=默认"StopOnError=错误";RecordedResultFile="ResultsLocale="><项目><请求方法=GET"Guid=a5f10126-e4cd-570d-961c-cea43999a200"版本=1.1";网址=http://microsoft.com"思考时间=0"超时=300"ParseDependentRequests=真";FollowRedirects=真"记录结果=真"缓存=假";响应时间目标=0"编码=utf-8";ExpectedHttpStatusCode=200"ExpectedResponseUrl="报告名称="IgnoreHttpStatusCode=假";/></物品></WebTest>XML}资源azurerm_monitor_action_group"主要"{名称=示例操作组";resource_group_name = data.azurerm_application_insights.example.resource_group_nameshort_name = exampleact"email_receiver {名称=发送到管理员";电子邮件地址=我的电子邮件ID";use_common_alert_schema = true}}资源azurerm_monitor_metric_alert"例子"{名称=示例-metricalert";resource_group_name = data.azurerm_application_insights.example.resource_group_name范围 = [azurerm_application_insights_web_test.example.id,data.azurerm_application_insights.example.id]描述 =PING 测试警报";application_insights_web_test_location_availability_criteria {web_test_id = azurerm_application_insights_web_test.example.idcomponent_id = data.azurerm_application_insights.example.idfailed_location_count = 2}行动 {action_group_id = azurerm_monitor_action_group.main.id}}
输出:
ARM 模板更新:
<代码>{"$schema":"https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",内容版本":1.0.0.0",参数":{appInsightsName":{类型":字符串"},certRemainingLifetimeCheck":{type":Int"},资源位置":{类型":字符串"},testCategory":{类型":字符串"},testHostname":{type":String"},testHostnameSafe":{类型":字符串"},testHttpCode":{类型":字符串"},testLocations":{类型":字符串"},应用程序名称":{类型":字符串"}},变量":{"requestUrl":"[concat('https://',parameters('testHostname'))]",testLocations":[split(parameters('testLocations'),',')]","hiddenLink": "[concat('hidden-link:',resourceId('microsoft.insights/components',parameters('appInsightsName')))]",pingTestName":[concat(参数('testHostnameSafe'),'-tls-ttl-',参数('testCategory'),'-webtest')]","pingAlertRuleName": "[concat('PingAlert-', toLower(parameters('appName')), '-', subscription().subscriptionId)]",复制":[{名称":位置",计数":[长度(变量('testLocations'))]",输入":{Id":[变量('testLocations')[copyIndex('locations')]]"}}]},资源":[{类型":Microsoft.Insights/webtests",apiVersion":2018-05-01-preview",名称":[变量('pingTestName')]",位置":[参数('resourceLocation')]",标签":{管理人":Terraform",[变量('hiddenLink')]":资源"},属性":{SyntheticMonitorId":[变量('pingTestName')]",名称":[变量('pingTestName')]",描述":[concat('检查 TLS 证书的生命周期',参数('testHostnameSafe'))]",启用":真,频率":300,超时":120,种类":标准",重试启用":真,位置":[变量('位置')]",配置":空,请求":{RequestUrl":[变量('requestUrl')]",标题":空,HttpVerb":GET",请求体":空,ParseDependentRequests":假,跟随重定向":空},验证规则":{ExpectedHttpStatusCode":[int(parameters('testHttpCode'))]",忽略HttpsStatusCode":假,内容验证":空,SSLCheck":真,SSLCertRemainingLifetimeCheck":[参数('certRemainingLifetimeCheck')]"}}},{名称":[变量('pingAlertRuleName')]",类型":Microsoft.Insights/metricAlerts",apiVersion":2018-03-01",位置":全球",取决于":[[resourceId('Microsoft.Insights/webtests', variables('pingTestName'))]"],标签":{[变量('hiddenLink')]":资源","[concat('hidden-link:', resourceId('Microsoft.Insights/webtests', variables('pingTestName')))]":"资源"},属性":{描述":网络测试警报",严重性":1,启用":真,范围":["[resourceId('Microsoft.Insights/webtests',variables('pingTestName'))]","[resourceId('microsoft.insights/components',parameters('appInsightsName'))]"],评估频率":PT1M",windowSize":PT5M",标准":{odata.type":Microsoft.Azure.Monitor.WebtestLocationAvailabilityCriteria",webTestId":[resourceId('Microsoft.Insights/webtests', variables('pingTestName'))]","componentId": "[resourceId('microsoft.insights/components',parameters('appInsightsName'))]",失败位置计数":2}}}]}
输出:
I'm trying to create an Alert Rule for an Application Insights → Availability → Standard (preview) test. First off, how do you create a standard test? For now, I resorted to creating the resource by using a resource "azurerm_resource_group_template_deployment" …
; see gist.github.com/alexs77/0b2bd07aae7fbf22bd3c145132475e7d for exactly how.
But even if it were a azurerm_application_insights_web_test
, how would an Alert Rule (ie. azurerm_monitor_metric_alert
- right?) look like? I'm confused about this part:
# …
resource "azurerm_monitor_metric_alert" "example" {
# …
criteria {
metric_name = "???"
metric_namespace = "???"
# …
}
# …
What are the correct values to use there?
I am aware about this ancient blog post Creating an Application Insights Web Test and Alert Programmatically from 2015 - 6 years ago. It makes you go to the 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.
When I go to "microsoft.insights", the "folder" alertrules
isn't there.
I'm trying to create an Alert Rule for an Application Insights → Availability → Standard (preview) test. First off, how do you create a standard test?
As Standard
Test is a preview feature , its currently not added in terraform-azurerm-provider. It can be only deployed from ARM templates as of now. Only Classic
Test can be deployed from Terraform for now.
But even if it were a azurerm_application_insights_web_test, how would an Alert Rule (ie. azurerm_monitor_metric_alert - right?) look like?
You don't have to use criteria / dynamic criteria
instead you should use application_insights_web_test_location_availability_criteria
. So, metric_name
and metric_namespace
won't be required . Using that your code for classic web test with alert rule will look something like below:
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
}
}
Output:
Update for ARM template:
{
"$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
}
}
}
]
}
Outputs:
这篇关于使用 Terraform(或 ARM 模板)为 Webtest 创建 Azure 警报规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!