Is it possible to create programmatically using the Azure CLI an Azure Application Insights availability URL Ping Test? I have checked the az cli application-insights documentation already (here: https://docs.microsoft.com/en-us/cli/azure/ext/application-insights/monitor/app-insights/component?view=azure-cli-latest) but I do not find anything related to the creation of a URL Ping Test.
2 Answers
There is no built-in cli command to do this, your option is to use az rest
call the REST API - Web Tests - Create Or Update
directly.
First, store a webtest.json
file as below to your powershell execute location e.g. my location is PS C:\Users\Administrator>
, I store the file in C:\Users\Administrator
folder.
In this file, the centralus
is the location of my appinsight, /subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/microsoft.insights/components/<appinsight-name>
is the resource id of my appinsight, joytest1
is the name of the ping test, joyfun2
is the name of the appinsight, https://joyfun2.azurewebsites.net
is the URL you want to test, please replace them with your values.
{
"location": "centralus",
"kind": "ping",
"tags": {
"hidden-link:/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/microsoft.insights/components/<appinsight-name>": "Resource"
},
"properties": {
"Name": "joytest1",
"SyntheticMonitorId": "joytest1-joyfun2",
"Configuration": {
"WebTest": "<WebTest Name=\"joytest1\" Id=\"ec14f587-a3f6-40ac-8952-75c900e1d153\" Enabled=\"True\" CssProjectStructure=\"\" CssIteration=\"\" Timeout=\"120\" 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=\"a7247e6c-29c1-2451-f4c8-78afe599253d\" Version=\"1.1\" Url=\"https://joyfun2.azurewebsites.net\" ThinkTime=\"0\" Timeout=\"120\" ParseDependentRequests=\"False\" FollowRedirects=\"True\" RecordResult=\"True\" Cache=\"False\" ResponseTimeGoal=\"0\" Encoding=\"utf-8\" ExpectedHttpStatusCode=\"200\" ExpectedResponseUrl=\"\" ReportingName=\"\" IgnoreHttpStatusCode=\"False\" /> </Items> </WebTest>"
},
"Enabled": true,
"Frequency": 900,
"Timeout": 120,
"Kind": "ping",
"RetryEnabled": true,
"Locations": [
{
"Id": "us-fl-mia-edge"
},
{
"Id": "us-tx-sn1-azr"
},
{
"Id": "emea-au-syd-edge"
}
]
}
}
Then run the command below, also replace the values like above.
az rest --method PUT --uri "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<group-name>/providers/Microsoft.Insights/webtests/joytest1-joyfun2?api-version=2015-05-01" --headers 'Content-Type=application/json' --body `@webtest.json
It works fine on my side.
Check in the portal:
You can do it via deploying ARM template. Here is an example of a template. It creates an availability test and sets up an alert.
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"webTestLocation": {
"type": "String"
},
"componentName": {
"type": "String"
},
"testName": {
"type": "String"
},
"testEndpoint": {
"type": "String"
},
"testLocations": {
"type": "Array"
},
"numLocationsToAlertOn": {
"type": "Int"
},
"alertDescription": {
"type": "String"
}
},
"resources": [
{
"type": "microsoft.insights/webtests",
"apiVersion": "2015-05-01",
"name": "[parameters('testName')]",
"location": "[parameters('webTestLocation')]",
"tags": {
"[concat('hidden-link:',resourceId('microsoft.insights/components',parameters('componentName')))]": "Resource"
},
"properties": {
"SyntheticMonitorId": "[parameters('testName')]",
"Name": "[parameters('testName')]",
"Enabled": true,
"Frequency": 300,
"Timeout": 120,
"Kind": "ping",
"RetryEnabled": false,
"Locations": "[parameters('testLocations')]",
"Configuration": {
"WebTest": "[concat('<WebTest Name=\"',parameters('testName'),'\" Id=\"00000000-0000-0000-0000-000000000000\" Enabled=\"True\" CssProjectStructure=\"\" CssIteration=\"\" Timeout=\"120\" 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=\"a86e39d1-b852-55ed-a079-23844e235d01\" Version=\"1.1\" Url=\"',parameters('testEndpoint'),'\" ThinkTime=\"0\" Timeout=\"120\" ParseDependentRequests=\"False\" FollowRedirects=\"True\" RecordResult=\"True\" Cache=\"False\" ResponseTimeGoal=\"0\" Encoding=\"utf-8\" ExpectedHttpStatusCode=\"200\" ExpectedResponseUrl=\"\" ReportingName=\"\" IgnoreHttpStatusCode=\"False\" /> </Items> </WebTest>')]"
}
}
},
{
"type": "microsoft.insights/metricalerts",
"name": "[parameters('testName')]",
"apiVersion": "2018-03-01",
"location": "global",
"tags": {
"[concat('hidden-link:',resourceId('microsoft.insights/components',parameters('componentName')))]": "Resource",
"[concat('hidden-link:',resourceId('microsoft.insights/webtests',parameters('testName')))]": "Resource"
},
"properties": {
"description": "[parameters('alertDescription')]",
"enabled": true,
"severity": 3,
"windowSize": "PT5M",
"evaluationFrequency": "PT1M",
"criteria": {
"failedLocationCount": "[parameters('numLocationsToAlertOn')]",
"webTestId": "[resourceId('microsoft.insights/webtests/',parameters('testName'))]",
"componentId": "[resourceId('microsoft.insights/components/',parameters('componentName'))]",
"odata.type": "Microsoft.Azure.Monitor.WebtestLocationAvailabilityCriteria"
},
"actions": [
{
"actionGroupId": "[resourceId('microsoft.insights/actiongroups', 'IcM')]",
"webhookProperties": {}
}
],
"scopes": [ "[resourceId('microsoft.insights/webtests/',parameters('testName'))]", "[resourceId('microsoft.insights/components/',parameters('componentName'))]" ]
},
"dependsOn": [
"[resourceId('microsoft.insights/webtests', parameters('testName'))]"
]
}
]
}
Here is an example of how to do it using Powershell Az module:
$result = New-AzResourceGroupDeployment `
-Name $stampHealthCheckScaleTestName `
-ResourceGroupName $appInsightsResourceGroupName `
-Mode Incremental `
-TemplateFile $scriptRoot\ArmTemplates\HealthCheckScaleArmTemplate.json `
-webTestLocation $appInsightsComponentLocation `
-componentName $appInsightsComponentName `
-testName $stampHealthCheckScaleTestName `
-testEndpoint "https://$($customDomainName)/healthcheckscale/status" `
-testLocations $testLocations `
-numLocationsToAlertOn $numLocationsToAlertOn `
-alertDescription $alertDescription
Here is an example of generic ARM template deployment using az cli: https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/deploy-cli#inline-parameters