Exercise - Use metric alerts to alert on performance issues in your Azure environment


The shipping company you work for wants to avoid any future issues with updates to its applications on the Azure platform. To improve the alert capabilities in Azure, you've chosen to use Azure metric alerts.

In this exercise, you'll create a Linux virtual machine (VM). This VM will run an app that runs the CPU at 100 percent utilization. You'll create monitoring rules in the Azure portal and in the Azure CLI to alert you about high CPU usage.

Create the VM

This VM will run a specific configuration that stresses the CPU and generates the metric monitoring data needed to trigger an alert.

  1. Start by creating the configuration script. To create the cloud-init.txt file with the configuration for the VM, run the following command in Azure Cloud Shell:

    cat <<EOF > cloud-init.txt
    package_upgrade: true
    - stress
    - sudo stress --cpu 1
  2. To set up an Ubuntu Linux VM, run the following az vm create command. This command uses the cloud-init.txt file that you created in the previous step to configure the VM after it's created.

    az vm create \
        --resource-group <rgn>[sandbox resource group name]</rgn> \
        --name vm1 \
        --location eastUS \
        --image UbuntuLTS \
        --custom-data cloud-init.txt \

Create the metric alert using the Azure portal


Wait until the VM is successfully created before proceeding with the exercise. The VM creation process is complete when you get the completed JSON output in the Azure Cloud Shell window.

You can use either the Azure portal or the CLI to create a metric alert. In this exercise we'll cover both, starting with the Azure portal.

  1. Sign in to the Azure portal using the same account that you used to activate the sandbox.

  2. On the Azure portal menu, search for and select Monitor. On the Monitor Overview page, select Alerts.

  3. Open the + Create menu, and select Alert rule

  4. On the Select a resource pane, set the scope for your alert rule. You can filter by subscription, resource type, or resource location.

  5. In the Resource type drop-down,start to type "virtual machines", and select Virtual machines.

  6. Check the box next to vm1, then select Apply at the bottom of the pane.

    Screenshot that shows the 'Select a resource' pane, with `vm1` selected.

  7. Select Next:Condition at the bottom of the page.

  8. In the Signal name drop-down, select Percentage CPU.

  9. In the Alert logic section, enter (or confirm) the following values for each setting.

    Setting Value
    Alert logic
    Threshold Static
    Aggregation type Maximum
    Operator Greater than
    Threshold value 90
    When to evaluate
    Check every 1 minute
    Lookback period 1 minute

    Screenshot that shows the settings for metric condition logic.

  10. Select the Details tab at the top of the page. In the Alert rule details section, enter the following values for each setting.

    Setting Value
    Severity 2 - Warning
    Alert rule name Cpu90PercentAlert
    Description Virtual machine is running at or greater than 90% CPU utilization
  11. Expand the Advanced options section and confirm the following values for each setting.

    Setting Value
    Enable upon creation Yes (checked)
    Automatically resolve alerts Yes (checked)

    Screenshot that shows the completed settings for the Alert rule details section.

  12. Select Review + create to validate your input, and then select Create.

You've successfully created a metric alert rule that will trigger an alert when the CPU percentage on the VM exceeds 90 percent. The rule will check every minute and review one minute of data. It can take up to 10 minutes for a metric alert rule to become active.

Create the metric alert through the CLI

You can also set up metric alerts by using the CLI. This process can be quicker than using the portal, especially if you're planning to set up more than one alert.

Let's create a new metric alert similar to the one you set up in the Azure portal.

  1. Run the following command in Cloud Shell to obtain the resource ID of the virtual machine you previously created:

    VMID=$(az vm show \
            --resource-group <rgn>[sandbox resource group name]</rgn> \
            --name vm1 \
            --query id \
            --output tsv)
  2. Run the following command to create a new metric alert that will be triggered when the VM CPU is greater than 80 percent.

    az monitor metrics alert create \
        -n "Cpu80PercentAlert" \
        --resource-group <rgn>[sandbox resource group name]</rgn> \
        --scopes $VMID \
        --condition "max percentage CPU > 80" \
        --description "Virtual machine is running at or greater than 80% CPU utilization" \
        --evaluation-frequency 1m \
        --window-size 1m \
        --severity 3

View your metric alerts in Azure Monitor

In this exercise, you set up an Ubuntu VM and configured it to stress test the CPU. You also created a metric rule to detect when the maximum CPU percentage exceeds 80 percent and 90 percent.


It might take 10 minutes before you see the alerts show up in the Azure portal.

  1. Return to the Azure portal.

  2. On the Azure portal menu, select Monitor, and then select Alerts in the left menu pane.

    This step presents the Alert summary pane, where you can see the count of the number of alerts. If you don't see your alerts listed, wait a few minutes and select Refresh.

    Screenshot that shows the alert summary pane.' pane.

  3. You configured your metric alerts with severities of 2 and 3. Select one of the alerts to view the severity level.

  4. Select one of the alerts to show the alert details.