Skip to content

Lab 13: Watch, alert and query

At a glance

  • Time: about 90 minutes
  • Region: the VM in Southeast Asia 3, the workspace and log alert in Southeast Asia
  • You create: 1 Ubuntu VM, 1 action group, 3 alert rules, 1 alert processing rule, 1 Log Analytics workspace
  • Cost: a few cents in total, if you delete everything at the end

What you will learn

  • How to send a notification with an action group.
  • How to raise an alert from a metric (CPU) and from an activity log event (a VM is deleted).
  • How to send metrics to a Log Analytics workspace and ask questions about them with KQL.
  • How to create a log alert and an alert processing rule that suppresses notifications.

What is different in this environment

  • There is no VM insights and no Azure Monitor agent setup. Those need data collection rules, which aren't allowed here. Your trainer shows them in a demonstration.
  • You use email only in the action group. Don't add SMS or voice: they send real messages and cost money.
  • Your workspace has a daily cap of 1 GB and keeps data for 30 days. A longer retention is blocked with [LAB-P10-LOG-ANALYTICS].
  • The workspace and the log alert are created in Southeast Asia, because they aren't available in Southeast Asia 3. The VM stays in Southeast Asia 3.
  • Each group may have 1 activity log alert, 2 log alerts, 1 alert processing rule and 1 workspace. Extra ones are deleted automatically a few minutes later.
Diagram: the VM lab-vm1 with a metric alert, an activity log alert and a log alert, all using one action group that sends email, and a Log Analytics workspace that receives the metrics and is queried with KQL.
A VM that is watched by three alert rules, an action group that sends an email, and a workspace you query with KQL. Select the diagram to open it full size.

Before you start

  • You are signed in to the lab directory (Getting started).
  • Your lab resource group is rg-CLOUD-LAB-GRP-N. Replace N with your group number.
  • Delete the resources from Lab 12 (the vault, the storage account, the VM and the disks).
  • Have an email address you can read during the lab.

Names used in this lab

Resource Name
Virtual machine lab-vm1
Action group ag-grp-N
Metric alert rule alert-cpu
Activity log alert rule alert-vm-delete
Log Analytics workspace law-grp-N
Diagnostic setting send-to-law
Log alert rule alert-log-cpu
Alert processing rule apr-suppress

Task 1: Create a VM to watch

  1. Search for Virtual machines and select Create → Azure virtual machine.
  2. On Basics:

    Setting Value
    Resource group rg-CLOUD-LAB-GRP-N
    Virtual machine name lab-vm1
    Region Southeast Asia 3
    Availability options No infrastructure redundancy required
    Security type Standard
    Image Ubuntu Server 24.04 LTS - x64 Gen2
    Size Standard_B1s
    Authentication type Password
    Username azureuser
    Password the password your trainer gives you
    Public inbound ports None
  3. On Disks, set OS disk type to Standard SSD (locally-redundant storage).

  4. On Networking, create a new virtual network named lab-vnet with the defaults. Set Public IP to None and NIC network security group to None. Select Delete NIC when VM is deleted.
  5. On Management, make sure Enable backup is not selected.
  6. On Monitoring, set Boot diagnostics to Disable.
  7. Select Review + create, then Create. Go on with Task 2 while it deploys.

Task 2: Create an action group

An action group says who to tell when an alert fires. Several alert rules can share one.

  1. Search for Monitor and open it. Select Alerts → Action groups → + Create.
  2. On Basics:

    Setting Value
    Resource group rg-CLOUD-LAB-GRP-N
    Region Global
    Action group name ag-grp-N
    Display name ag-grp-N
  3. On Notifications, set Notification type to Email/SMS message/Push/Voice, name it email-me, and select Email only. Enter your email address and select OK.

    Email only

    Leave SMS and Voice empty. They cost money for every message.

  4. Select Review + create, then Create.

Task 3: Alert on high CPU

  1. Open lab-vm1. Under Monitoring, select Alerts → Create → Alert rule.
  2. On Condition, select See all signals, choose Percentage CPU and select Apply. Set:

    Setting Value
    Threshold Static
    Aggregation type Average
    Operator Greater than
    Threshold value 70
    Check every 1 minute
    Lookback period 5 minutes
  3. On Actions, select Select action groups and choose ag-grp-N.

  4. On Details, set Severity to 3 - Informational and Alert rule name to alert-cpu. Select Review + create, then Create.

Task 4: Alert when a VM is deleted

Some events aren't metrics. They are entries in the activity log, for example a VM was deleted.

  1. Go to Monitor → Alerts → Create → Alert rule.
  2. On Scope, select your resource group rg-CLOUD-LAB-GRP-N.
  3. On Condition, select See all signals, search for Delete Virtual Machine and select it. Select Apply.
  4. On Actions, choose ag-grp-N.
  5. On Details, set Alert rule name to alert-vm-delete. Select Review + create, then Create.

Only one

A subscription can hold only 100 activity log alerts for all groups, so each group may have 1. You test this rule at the end of the lab.

Task 5: Create a workspace and send metrics to it

  1. Search for Log Analytics workspaces and select + Create.
  2. On Basics:

    Setting Value
    Resource group rg-CLOUD-LAB-GRP-N
    Name law-grp-N
    Region Southeast Asia
  3. Select Review + create, then Create.

  4. Open the workspace. Under Settings, select Usage and estimated costs.

    • Select Daily cap, choose On, set the limit to 1 GB and select OK.
    • Select Data retention and check that it is 30 days.

    Set the cap now

    A workspace without a daily cap can run up a bill if something sends a lot of data. The cap stops the intake for the rest of the day when it is reached.

  5. Go to Monitor → Settings → Diagnostic settings. Find lab-vm1 and select it, then Add diagnostic setting.

  6. Set the name to send-to-law. Under Metrics, select AllMetrics. Under Destination details, select Send to Log Analytics workspace and choose law-grp-N. Select Save.

Data from a new diagnostic setting takes 5 to 15 minutes to show up. Go on to Task 6 now.

Task 6: Make the CPU busy and watch the alert

  1. Open lab-vm1 → Run command → RunShellScript. Run this script. It uses all of the CPU for 10 minutes and then stops by itself:

    nohup timeout 600 sh -c 'while true; do :; done' > /dev/null 2>&1 &
    echo started
    
  2. Go to Monitor → Alerts. After about 5 to 8 minutes an alert named alert-cpu appears with status Fired.

  3. Check your email. You should get a message from Azure Monitor.
  4. Open the alert, select Change user response and set it to Acknowledged.
  5. After the load stops, the alert resolves by itself. Check that Monitor condition becomes Resolved.

Think about it

Why does the alert look at an average over 5 minutes and not at a single reading? What would happen if you used 1 minute?

Task 7: Ask questions with KQL

  1. Open the workspace law-grp-N and select Logs. Close the Queries window if it opens.
  2. Run each query below. Select Run after you paste it.

    Which metrics are stored?

    AzureMetrics
    | summarize count() by MetricName
    

    The last 20 CPU readings:

    AzureMetrics
    | where MetricName == "Percentage CPU"
    | project TimeGenerated, Average, Maximum
    | order by TimeGenerated desc
    | take 20
    

    CPU as a chart:

    AzureMetrics
    | where MetricName == "Percentage CPU"
    | summarize AvgCpu = avg(Average) by bin(TimeGenerated, 5m)
    | render timechart
    
  3. Check that the chart shows the busy period you made in Task 6.

No rows yet?

Wait a few more minutes and run the query again. If the table AzureMetrics doesn't exist after 20 minutes, check that the diagnostic setting send-to-law is saved.

Task 8: Create a log alert

A log alert runs a KQL query on a schedule and fires when it finds rows.

  1. In the workspace, select Monitoring → Alerts → Create → Alert rule.
  2. On Condition, select Custom log search. Paste this query:

    AzureMetrics
    | where MetricName == "Percentage CPU"
    | where Average > 70
    
  3. Set Measurement to Table rows, Aggregation type to Count, Operator to Greater than and Threshold value to 0. Set Check every to 15 minutes and Lookback period to 15 minutes.

  4. On Actions, choose ag-grp-N.
  5. On Details, set Alert rule name to alert-log-cpu and the Region to Southeast Asia. Select Review + create, then Create.

Think about it

The metric alert checked every minute, and this one checks every 15. When would you pay for the extra checks, and when is a slower check enough?

Task 9: Suppress notifications

An alert processing rule changes what happens to alerts that are already firing, without changing the alert rules. A common use is planned maintenance.

  1. Go to Monitor → Alerts → Alert processing rules → + Create.
  2. On Scope, select your resource group.
  3. On Rule settings, select Suppress notifications.
  4. On Scheduling, leave Always.
  5. On Details, set the name to apr-suppress and select Review + create, then Create.

Task 10: Test the delete alert, then clean up

  1. Open lab-vm1, select Delete, and select the OS disk and network interface so they are deleted too. Confirm.
  2. Go to Monitor → Alerts. After a few minutes an alert named alert-vm-delete appears, because the VM was deleted.
  3. Check your email. No message arrives, because apr-suppress suppresses the notifications. The alert is still recorded.
  4. Delete everything else, in this order:
    1. The alert processing rule apr-suppress.
    2. The three alert rules alert-cpu, alert-vm-delete and alert-log-cpu. They are under Monitor → Alerts → Alert rules.
    3. The action group ag-grp-N.
    4. The workspace law-grp-N. Choose Delete the workspace permanently if offered.
    5. The virtual network lab-vnet and any disk that is left.
  5. Check that your resource group is empty.

Check your work

  • You got an email, and alert-cpu showed Fired, when the CPU was busy.
  • The KQL chart showed the same busy period.
  • alert-vm-delete fired when you deleted the VM, and no email arrived because of apr-suppress.
  • Your resource group is empty again.

Key points

  • An alert rule says when, and an action group says who is told. One action group can serve many rules.
  • Metric alerts react fast to numbers such as CPU. Activity log alerts react to events, such as a delete. Log alerts react to what a query finds, and run less often.
  • A workspace stores data you can query with KQL. It bills by the amount of data, so set a daily cap and keep retention short.
  • An alert processing rule can switch off notifications for a period without touching the alert rules.