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.
Before you start¶
- You are signed in to the lab directory (Getting started).
- Your lab resource group is
rg-CLOUD-LAB-GRP-N. ReplaceNwith 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¶
- Search for Virtual machines and select Create → Azure virtual machine.
-
On Basics:
Setting Value Resource group rg-CLOUD-LAB-GRP-NVirtual machine name lab-vm1Region 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 azureuserPassword the password your trainer gives you Public inbound ports None -
On Disks, set OS disk type to Standard SSD (locally-redundant storage).
- On Networking, create a new virtual network named
lab-vnetwith the defaults. Set Public IP to None and NIC network security group to None. Select Delete NIC when VM is deleted. - On Management, make sure Enable backup is not selected.
- On Monitoring, set Boot diagnostics to Disable.
- 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.
- Search for Monitor and open it. Select Alerts → Action groups → + Create.
-
On Basics:
Setting Value Resource group rg-CLOUD-LAB-GRP-NRegion Global Action group name ag-grp-NDisplay name ag-grp-N -
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.
-
Select Review + create, then Create.
Task 3: Alert on high CPU¶
- Open
lab-vm1. Under Monitoring, select Alerts → Create → Alert rule. -
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 70Check every 1 minute Lookback period 5 minutes -
On Actions, select Select action groups and choose
ag-grp-N. - 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.
- Go to Monitor → Alerts → Create → Alert rule.
- On Scope, select your resource group
rg-CLOUD-LAB-GRP-N. - On Condition, select See all signals, search for Delete Virtual Machine and select it. Select Apply.
- On Actions, choose
ag-grp-N. - 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¶
- Search for Log Analytics workspaces and select + Create.
-
On Basics:
Setting Value Resource group rg-CLOUD-LAB-GRP-NName law-grp-NRegion Southeast Asia -
Select Review + create, then Create.
-
Open the workspace. Under Settings, select Usage and estimated costs.
- Select Daily cap, choose On, set the limit to
1GB 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.
- Select Daily cap, choose On, set the limit to
-
Go to Monitor → Settings → Diagnostic settings. Find
lab-vm1and select it, then Add diagnostic setting. - Set the name to
send-to-law. Under Metrics, select AllMetrics. Under Destination details, select Send to Log Analytics workspace and chooselaw-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¶
-
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 -
Go to Monitor → Alerts. After about 5 to 8 minutes an alert named
alert-cpuappears with status Fired. - Check your email. You should get a message from Azure Monitor.
- Open the alert, select Change user response and set it to Acknowledged.
- 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¶
- Open the workspace
law-grp-Nand select Logs. Close the Queries window if it opens. -
Run each query below. Select Run after you paste it.
Which metrics are stored?
AzureMetrics | summarize count() by MetricNameThe last 20 CPU readings:
AzureMetrics | where MetricName == "Percentage CPU" | project TimeGenerated, Average, Maximum | order by TimeGenerated desc | take 20CPU as a chart:
AzureMetrics | where MetricName == "Percentage CPU" | summarize AvgCpu = avg(Average) by bin(TimeGenerated, 5m) | render timechart -
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.
- In the workspace, select Monitoring → Alerts → Create → Alert rule.
-
On Condition, select Custom log search. Paste this query:
AzureMetrics | where MetricName == "Percentage CPU" | where Average > 70 -
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. - On Actions, choose
ag-grp-N. - On Details, set Alert rule name to
alert-log-cpuand 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.
- Go to Monitor → Alerts → Alert processing rules → + Create.
- On Scope, select your resource group.
- On Rule settings, select Suppress notifications.
- On Scheduling, leave Always.
- On Details, set the name to
apr-suppressand select Review + create, then Create.
Task 10: Test the delete alert, then clean up¶
- Open
lab-vm1, select Delete, and select the OS disk and network interface so they are deleted too. Confirm. - Go to Monitor → Alerts. After a few minutes an alert named
alert-vm-deleteappears, because the VM was deleted. - Check your email. No message arrives, because
apr-suppresssuppresses the notifications. The alert is still recorded. - Delete everything else, in this order:
- The alert processing rule
apr-suppress. - The three alert rules
alert-cpu,alert-vm-deleteandalert-log-cpu. They are under Monitor → Alerts → Alert rules. - The action group
ag-grp-N. - The workspace
law-grp-N. Choose Delete the workspace permanently if offered. - The virtual network
lab-vnetand any disk that is left.
- The alert processing rule
- Check that your resource group is empty.
Check your work¶
- You got an email, and
alert-cpushowed Fired, when the CPU was busy. - The KQL chart showed the same busy period.
-
alert-vm-deletefired when you deleted the VM, and no email arrived because ofapr-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.