How to Use Azure Monitor: Setup and Basic Usage Guide
To use
Azure Monitor, first enable monitoring on your Azure resources to collect telemetry data. Then, use Azure Monitor tools like metrics, logs, and alerts to analyze and respond to this data for maintaining your applications and infrastructure.Syntax
Azure Monitor is a service, not a single command, so its usage involves configuring resources and queries. Key parts include:
- Enable diagnostics: Turn on monitoring for resources.
- Collect metrics and logs: Data about resource health and activity.
- Query logs: Use
Kusto Query Language (KQL)to analyze data. - Create alerts: Set rules to notify on important events.
bash
az monitor metrics list --resource /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} --metric Percentage CPU --interval PT1MExample
This example shows how to list CPU usage metrics for a virtual machine using Azure CLI, which is part of using Azure Monitor to check resource health.
bash
az monitor metrics list --resource /subscriptions/12345678-1234-1234-1234-123456789abc/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM --metric Percentage CPU --interval PT5M
Output
{
"cost": 0,
"timeseries": [
{
"data": [
{"timeStamp": "2024-06-01T12:00:00Z", "average": 15.5},
{"timeStamp": "2024-06-01T12:05:00Z", "average": 20.3}
]
}
]
}
Common Pitfalls
Common mistakes when using Azure Monitor include:
- Not enabling diagnostics on resources, so no data is collected.
- Using incorrect resource IDs or metric names in queries.
- Ignoring data retention settings, causing loss of older logs.
- Setting alert thresholds too low or too high, leading to missed or excessive alerts.
bash
Wrong usage example:
az monitor metrics list --resource /wrong/resource/id --metric CPU
Right usage example:
az monitor metrics list --resource /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName} --metric Percentage CPUQuick Reference
| Feature | Description | Command / Tool |
|---|---|---|
| Enable diagnostics | Turn on monitoring for Azure resources | Azure Portal or az monitor diagnostic-settings create |
| List metrics | View performance data like CPU or memory | az monitor metrics list |
| Query logs | Analyze collected logs with queries | Azure Monitor Logs (KQL) |
| Create alerts | Set notifications for important events | az monitor metrics alert create or Azure Portal |
Key Takeaways
Enable diagnostics on your Azure resources to start collecting monitoring data.
Use Azure CLI or Azure Portal to query metrics and logs for insights.
Set alerts with appropriate thresholds to get notified about issues.
Use Kusto Query Language (KQL) to analyze logs effectively.
Check resource IDs and metric names carefully to avoid errors.