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

xwmevbvl  于 2023-03-31  发布在  其他
关注(0)|答案(4)|浏览(105)

是否可以使用Azure CLI以编程方式创建Azure Application Insights可用性URL Ping Test?我已经查看了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测试。

9lowa7mx

9lowa7mx1#

你可以通过部署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

zysjyyx4

zysjyyx42#

这里是另一个使用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

如果你有勇气的话,你还可以创建一个所谓的标准测试,它目前正在预览中,但不需要不祥的WebTestXML字符串,对我来说效果很好。

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

你可以在我的博客文章中阅读更多关于这方面的内容,可以在here中找到

zmeyuzjn

zmeyuzjn3#

对于任何发现此讨论的人来说,现在我测试它似乎正在工作:

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所指的。

pgx2nnw8

pgx2nnw84#

没有内置的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是你想要测试的URL,请用你的值替换它们。

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

在我这边很好用。

登录门户网站:

相关问题