0
0
Azurecloud~5 mins

Azure Monitor overview - Commands & Configuration

Choose your learning style9 modes available
Introduction
Azure Monitor helps you see what is happening with your cloud apps and resources. It collects data and shows you useful information to keep your systems healthy and running smoothly.
When you want to track how well your website or app is performing in real time.
When you need to find out why a cloud service is slow or not working.
When you want to get alerts if something goes wrong with your cloud resources.
When you want to collect logs and metrics from different Azure services in one place.
When you want to analyze trends and usage patterns to improve your cloud setup.
Commands
This command creates a Log Analytics workspace where Azure Monitor stores collected data. You need this workspace to start monitoring your resources.
Terminal
az monitor log-analytics workspace create --resource-group example-group --workspace-name example-workspace --location eastus
Expected OutputExpected
{ "customerId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "location": "eastus", "name": "example-workspace", "resourceGroup": "example-group", "sku": { "name": "PerGB2018" }, "type": "Microsoft.OperationalInsights/workspaces" }
--resource-group - Specifies the Azure resource group to create the workspace in
--workspace-name - Names the Log Analytics workspace
--location - Sets the Azure region for the workspace
This command lists the types of metrics you can collect from a specific Azure resource, here a virtual machine. It helps you know what data you can monitor.
Terminal
az monitor metrics list-definitions --resource /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-group/providers/Microsoft.Compute/virtualMachines/example-vm
Expected OutputExpected
{ "value": [ { "name": { "value": "Percentage CPU", "localizedValue": "Percentage CPU" }, "unit": "Percent" }, { "name": { "value": "Network In", "localizedValue": "Network In" }, "unit": "Bytes" } ] }
--resource - Specifies the Azure resource to get metric definitions for
This command retrieves the current values of the 'Percentage CPU' metric for the virtual machine, updated every minute. It shows how busy the CPU is.
Terminal
az monitor metrics list --resource /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-group/providers/Microsoft.Compute/virtualMachines/example-vm --metric "Percentage CPU" --interval PT1M
Expected OutputExpected
{ "cost": 0, "timeseries": [ { "data": [ { "timeStamp": "2024-06-01T12:00:00Z", "average": 15.5 }, { "timeStamp": "2024-06-01T12:01:00Z", "average": 18.2 } ] } ] }
--metric - Specifies which metric to retrieve
--interval - Sets the time interval for metric data points
--resource - Specifies the Azure resource to get metrics from
This command creates an alert that triggers when the average CPU usage goes above 80%. Alerts help you react quickly to problems.
Terminal
az monitor alert create --name HighCPUAlert --resource-group example-group --scopes /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-group/providers/Microsoft.Compute/virtualMachines/example-vm --condition "avg Percentage CPU > 80" --description "Alert when CPU usage is over 80%" --action-group example-action-group
Expected OutputExpected
{ "name": "HighCPUAlert", "resourceGroup": "example-group", "description": "Alert when CPU usage is over 80%", "enabled": true }
--condition - Defines the rule that triggers the alert
--scopes - Specifies which resource the alert monitors
--action-group - Defines who or what gets notified when the alert fires
Key Concept

If you remember nothing else from this pattern, remember: Azure Monitor collects data from your cloud resources and helps you see and react to their health and performance.

Common Mistakes
Trying to get metrics without creating a Log Analytics workspace first
Azure Monitor needs a workspace to store and query monitoring data
Always create a Log Analytics workspace before collecting or querying metrics
Using incorrect resource IDs in commands
Commands fail if the resource path is wrong or does not exist
Copy the full resource ID exactly from the Azure portal or CLI
Setting alert conditions with wrong syntax
Alerts won't create or won't trigger if the condition syntax is invalid
Use the correct condition format like "avg Percentage CPU > 80"
Summary
Create a Log Analytics workspace to store monitoring data.
List available metrics for your Azure resources to know what you can monitor.
Retrieve current metric values to check resource performance.
Set alerts to get notified when important metrics cross thresholds.