Search code examples
azureazure-application-insightsazure-cli

Is it possible to use the Azure CLI for setting up a monitoring URL Ping test?


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://learn.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.


Solution

  • 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.

    enter image description here

    Check in the portal:

    enter image description here