是否可以使用 Azure CLI 设置监控 URL Ping 测试?

问题描述 投票:0回答:5

是否可以使用 Azure CLI 以编程方式创建 Azure Application Insights 可用性 URL Ping 测试? 我已经检查了 az cli application-insights 文档(此处:https://learn.microsoft.com/en-us/cli/azure/ext/application-insights/monitor/app-insights/component?view=azure -cli-latest) 但我没有找到任何与创建 URL Ping 测试相关的信息。

azure azure-application-insights azure-cli
5个回答
2
投票

没有内置的 cli 命令来执行此操作,您的选择是使用

az rest
调用 REST API -
Web Tests - Create Or Update
直接。

首先,将如下所示的

webtest.json
文件存储到您的 powershell 执行位置,例如我的位置是PS
C:\Users\Administrator>
,我将文件存储在
C:\Users\Administrator
文件夹中。

在这个文件中,

centralus
是我的appinsight的位置,
/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/microsoft.insights/components/<appinsight-name>
是我的appinsight的资源id,
joytest1
是ping测试的名字,
joyfun2
是appinsight的名字,
https://joyfun2.azurewebsites.net 
是您要测试的网址,请将其替换为您的值。

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

然后运行下面的命令,同样替换上面的值。

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

我这边很好用。

门户查看:


1
投票

您可以通过部署 ARM 模板来完成。这是模板的示例。它创建可用性测试并设置警报。

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

以下是如何使用 Powershell Az 模块执行此操作的示例:

  $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

这里是使用 az cli 的通用 ARM 模板部署示例:https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/deploy-cli#inline-parameters


1
投票

这是另一个使用 bicep-lang 创建经典 URL Ping 测试的示例。不幸的是,没有记录 WebTest XML 的格式。

注意隐藏链接标签,它应该指向您的应用洞察资源。

var name = 'demo-url-ping-web-test'
var rg = 'rg-article'
var subscriptionId = '<your_subscription_id>'
var appInsightsName = 'appi-demo'

var id = guid('seed')
var timeout = 120
var frequency = 300
var guidId = guid('seed')
var method = 'GET'
var url = 'https://www.azureblue.io'
var expectedHttpStatusCode = 200
var version = '1.1'
var followRedirects = 'True'
var recordResults = 'True'
var cache = 'False'
var parseDependentRequests = 'False'
var ignoreHttpStatusCode = 'False'

resource urlPingWebTest 'Microsoft.Insights/webtests@2015-05-01' = {
  name: name
  location: 'westeurope'
  tags: {
    'hidden-link:/subscriptions/${subscriptionId}/resourceGroups/${rg}/providers/microsoft.insights/components/${appInsightsName}': 'Resource'
  }
  kind: 'ping'
  properties: {
    Configuration: {
      WebTest: '<WebTest xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" Name="${name}" Id="${id}" Enabled="True" CssProjectStructure="" CssIteration="" Timeout="${timeout}" WorkItemIds="" Description="" CredentialUserName="" CredentialPassword="" PreAuthenticate="True" Proxy="default" StopOnError="False" RecordedResultFile="" ResultsLocale=""> <Items> <Request Method="${method}" Guid="${guidId}" Version="${version}" Url="${url}" ThinkTime="0" Timeout="${timeout}" ParseDependentRequests="${parseDependentRequests}" FollowRedirects="${followRedirects}" RecordResult="${recordResults}" Cache="${cache}" ResponseTimeGoal="0" Encoding="utf-8" ExpectedHttpStatusCode="${expectedHttpStatusCode}" ExpectedResponseUrl="" ReportingName="" IgnoreHttpStatusCode="${ignoreHttpStatusCode}" /> </Items> </WebTest>'
    }
    Description: 'Runs a classic URL ping test'    
    Enabled: true
    Frequency: frequency
    Kind: 'ping'
    Locations: [
      {
        Id: 'emea-nl-ams-azr'
      }
      {
        Id: 'emea-ru-msa-edge'
      }
      {
        Id: 'apac-hk-hkn-azr'
      }
      {
        Id: 'latam-br-gru-edge'
      }
      {
        Id: 'emea-au-syd-edge'
      }
    ]
    Name: name
    RetryEnabled: true 
    SyntheticMonitorId: '${name}-id'
    Timeout: timeout
  }
}

您可以像这样使用 Azure CLI 部署模板:

az deployment group create --name rollout01 --resource-group rg-article --subscription "your subscription" --template-file urlpingwebtest.bicep

如果你有勇气,你也可以创建一个所谓的 Standard Test,它目前处于预览状态,但不需要不祥的 WebTest XML 字符串,对我来说效果很好。

resource standardWebTest 'Microsoft.Insights/webtests@2018-05-01-preview' = {
  name: 'demo-webtest'
  location: 'westeurope'
  tags: {
    'hidden-link:/subscriptions/ade29918-737f-497e-8808-4bffda5cc46d/resourceGroups/rg-article/providers/microsoft.insights/components/appi-demo': 'Resource'
  }
  kind: 'standard'
  properties: {
    SyntheticMonitorId: 'demo-webtest-id'
    Name: 'demo-webtest'
    Description: null
    Enabled: true
    Frequency: 300
    Timeout: 120 
    Kind: 'standard'
    RetryEnabled: true
    Locations: [
      {
        Id: 'emea-nl-ams-azr'
      }
      {
        Id: 'emea-ru-msa-edge'
      }
      {
        Id: 'apac-hk-hkn-azr'
      }
      {
        Id: 'latam-br-gru-edge'
      }
      {
        Id: 'emea-au-syd-edge'
      }
    ]
    Configuration: null
    Request: {
      RequestUrl: 'https://www.azureblue.io'
      Headers: null
      HttpVerb: 'GET'
      RequestBody: null
      ParseDependentRequests: false
      FollowRedirects: null
    }
    ValidationRules: {
      ExpectedHttpStatusCode: 200
      IgnoreHttpsStatusCode: false
      ContentValidation: null
      SSLCheck: true
      SSLCertRemainingLifetimeCheck: 7
    }
  }
}

您可以在我的博客文章中阅读更多相关信息,可以在这里找到


0
投票

为了发现此讨论的任何人的利益,现在我测试它似乎正在工作:

az monitor app-insights web-test create `
    --web-test-kind "standard" --enabled true `
    --location $location `
    --resource-group $rgName `
    --name "health-check" --defined-web-test-name "health-check" `
    --tags "hidden-link:$($appInsights.Id)=Resource" `
    --http-verb "GET" --request-url "$appBaseUrl/health" `
    --timeout 30 --frequency 300 --retry-enabled true `
    --locations Id="emea-nl-ams-azr" --locations Id="us-fl-mia-edge"

我假设这就是@ZakiMa 所指的。


0
投票

在互联网上搜索了很长时间关于如何在 Azure 上的 Application Insights 中自动化可用性测试但没有找到任何帮助我的东西之后,感谢您的评论,我能够推进我的研究并创建一个脚本来批量创建经典的 ping 测试和标准测试。我很感激你的帮助。我会在这里分享剧本,如果你能进入我的YouTube频道点赞视频并帮助我,我会很高兴

我试图在这里发布脚本,但由于某种原因,我不能。我很抱歉,因为我不会说英语。请点击我演示脚本工作的视频链接。如果您需要脚本,请在这里或在我的频道上提供您的电子邮件,我会提供它

https://youtu.be/l0pfOvyKEBM

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.