0
# Metric Alerts and Monitoring
1
2
Metric alerts monitor resource metrics and trigger actions when thresholds are exceeded. This module supports static and dynamic thresholds, multi-resource alerts, and comprehensive alert status monitoring for proactive resource management.
3
4
## Capabilities
5
6
### Metric Alert Management
7
8
Create, update, and manage metric alert rules with support for static and dynamic thresholds.
9
10
```python { .api }
11
def create_or_update(resource_group_name: str, rule_name: str, parameters: MetricAlertResource, **kwargs: Any) -> MetricAlertResource:
12
"""
13
Create or update a metric alert rule.
14
15
Parameters:
16
- resource_group_name: str - Name of the resource group
17
- rule_name: str - Name of the alert rule
18
- parameters: MetricAlertResource - Alert rule configuration
19
20
Returns:
21
MetricAlertResource - The created or updated alert rule
22
"""
23
24
def get(resource_group_name: str, rule_name: str, **kwargs: Any) -> MetricAlertResource:
25
"""
26
Retrieve an alert rule.
27
28
Parameters:
29
- resource_group_name: str - Name of the resource group
30
- rule_name: str - Name of the alert rule
31
32
Returns:
33
MetricAlertResource - The alert rule details
34
"""
35
36
def delete(resource_group_name: str, rule_name: str, **kwargs: Any) -> None:
37
"""
38
Delete an alert rule.
39
40
Parameters:
41
- resource_group_name: str - Name of the resource group
42
- rule_name: str - Name of the alert rule
43
"""
44
45
def update(resource_group_name: str, rule_name: str, parameters: MetricAlertResourcePatch, **kwargs: Any) -> MetricAlertResource:
46
"""
47
Update an alert rule.
48
49
Parameters:
50
- resource_group_name: str - Name of the resource group
51
- rule_name: str - Name of the alert rule
52
- parameters: MetricAlertResourcePatch - Properties to update
53
54
Returns:
55
MetricAlertResource - The updated alert rule
56
"""
57
```
58
59
### Metric Alert Listing
60
61
List metric alert rules within subscriptions and resource groups.
62
63
```python { .api }
64
def list_by_subscription(**kwargs: Any) -> ItemPaged[MetricAlertResource]:
65
"""
66
Retrieve alert rules for a subscription.
67
68
Returns:
69
ItemPaged[MetricAlertResource] - Paginated list of alert rules
70
"""
71
72
def list_by_resource_group(resource_group_name: str, **kwargs: Any) -> ItemPaged[MetricAlertResource]:
73
"""
74
Retrieve alert rules for a resource group.
75
76
Parameters:
77
- resource_group_name: str - Name of the resource group
78
79
Returns:
80
ItemPaged[MetricAlertResource] - Paginated list of alert rules
81
"""
82
```
83
84
### Alert Status Monitoring
85
86
Monitor the status of metric alert instances and retrieve detailed status information.
87
88
```python { .api }
89
def list(resource_group_name: str, rule_name: str, **kwargs: Any) -> MetricAlertStatusCollection:
90
"""
91
Retrieve an alert rule status collection.
92
93
Parameters:
94
- resource_group_name: str - Name of the resource group
95
- rule_name: str - Name of the alert rule
96
97
Returns:
98
MetricAlertStatusCollection - Alert status information
99
"""
100
101
def list_by_name(resource_group_name: str, rule_name: str, status_name: str, **kwargs: Any) -> MetricAlertStatusCollection:
102
"""
103
Retrieve an alert rule status by name.
104
105
Parameters:
106
- resource_group_name: str - Name of the resource group
107
- rule_name: str - Name of the alert rule
108
- status_name: str - Name of the status
109
110
Returns:
111
MetricAlertStatusCollection - Specific alert status information
112
"""
113
```
114
115
## Usage Examples
116
117
### Creating a Static Threshold Alert
118
119
```python
120
from azure.mgmt.monitor.models import (
121
MetricAlertResource, MetricAlertSingleResourceMultipleMetricCriteria,
122
MetricCriteria, MetricAlertAction
123
)
124
125
# Define alert criteria for high CPU usage
126
criteria = MetricAlertSingleResourceMultipleMetricCriteria(
127
all_of=[
128
MetricCriteria(
129
name="HighCPU",
130
metric_name="Percentage CPU",
131
metric_namespace="Microsoft.Compute/virtualMachines",
132
operator="GreaterThan",
133
threshold=80.0,
134
time_aggregation="Average"
135
)
136
]
137
)
138
139
# Define actions
140
actions = [
141
MetricAlertAction(
142
action_group_id=f"/subscriptions/{subscription_id}/resourceGroups/monitoring-rg/providers/Microsoft.Insights/actionGroups/admin-alerts"
143
)
144
]
145
146
# Create metric alert
147
alert_rule = MetricAlertResource(
148
location="global",
149
description="Alert when CPU exceeds 80%",
150
enabled=True,
151
scopes=[f"/subscriptions/{subscription_id}/resourceGroups/production-rg/providers/Microsoft.Compute/virtualMachines/web-server"],
152
evaluation_frequency="PT1M", # Evaluate every minute
153
window_size="PT5M", # 5-minute window
154
criteria=criteria,
155
actions=actions,
156
severity=2 # Warning severity
157
)
158
159
result = client.metric_alerts.create_or_update(
160
resource_group_name="monitoring-rg",
161
rule_name="high-cpu-alert",
162
parameters=alert_rule
163
)
164
```
165
166
### Creating a Dynamic Threshold Alert
167
168
```python
169
from azure.mgmt.monitor.models import (
170
MetricAlertMultipleResourceMultipleMetricCriteria,
171
DynamicMetricCriteria, DynamicThresholdFailingPeriods
172
)
173
174
# Define dynamic threshold criteria
175
failing_periods = DynamicThresholdFailingPeriods(
176
number_of_evaluation_periods=4,
177
min_failing_periods_to_alert=3
178
)
179
180
dynamic_criteria = MetricAlertMultipleResourceMultipleMetricCriteria(
181
all_of=[
182
DynamicMetricCriteria(
183
name="AbnormalRequestRate",
184
metric_name="Requests/Sec",
185
metric_namespace="Microsoft.Web/sites",
186
operator="GreaterOrLessThan",
187
time_aggregation="Average",
188
alert_sensitivity="Medium",
189
failing_periods=failing_periods,
190
ignore_data_before=datetime.utcnow() - timedelta(days=7)
191
)
192
]
193
)
194
195
# Create dynamic threshold alert
196
dynamic_alert = MetricAlertResource(
197
location="global",
198
description="Dynamic alert for abnormal request patterns",
199
enabled=True,
200
scopes=[f"/subscriptions/{subscription_id}/resourceGroups/web-apps-rg"],
201
evaluation_frequency="PT1M",
202
window_size="PT15M",
203
criteria=dynamic_criteria,
204
actions=actions,
205
severity=1 # Error severity
206
)
207
```
208
209
### Multi-Resource Alert
210
211
```python
212
# Create alert that monitors multiple resources
213
multi_resource_scopes = [
214
f"/subscriptions/{subscription_id}/resourceGroups/web-rg/providers/Microsoft.Web/sites/app1",
215
f"/subscriptions/{subscription_id}/resourceGroups/web-rg/providers/Microsoft.Web/sites/app2",
216
f"/subscriptions/{subscription_id}/resourceGroups/web-rg/providers/Microsoft.Web/sites/app3"
217
]
218
219
multi_criteria = MetricAlertMultipleResourceMultipleMetricCriteria(
220
all_of=[
221
MetricCriteria(
222
name="HighResponseTime",
223
metric_name="AverageResponseTime",
224
metric_namespace="Microsoft.Web/sites",
225
operator="GreaterThan",
226
threshold=2.0,
227
time_aggregation="Average"
228
)
229
]
230
)
231
232
multi_alert = MetricAlertResource(
233
location="global",
234
description="Multi-resource response time alert",
235
enabled=True,
236
scopes=multi_resource_scopes,
237
evaluation_frequency="PT5M",
238
window_size="PT15M",
239
criteria=multi_criteria,
240
actions=actions,
241
severity=2
242
)
243
```
244
245
### Checking Alert Status
246
247
```python
248
# Get alert status
249
status_collection = client.metric_alerts_status.list(
250
resource_group_name="monitoring-rg",
251
rule_name="high-cpu-alert"
252
)
253
254
for status_item in status_collection.value:
255
print(f"Status: {status_item.status}")
256
print(f"Timestamp: {status_item.timestamp}")
257
if status_item.dimensions:
258
print(f"Dimensions: {status_item.dimensions}")
259
```
260
261
## Types
262
263
```python { .api }
264
class MetricAlertResource:
265
"""Metric alert rule resource."""
266
location: str # Resource location (typically "global")
267
description: Optional[str] # Alert description
268
enabled: bool # Whether alert is enabled
269
scopes: List[str] # Resources to monitor (resource IDs)
270
evaluation_frequency: str # How often to evaluate (ISO 8601 duration)
271
window_size: str # Time window for evaluation (ISO 8601 duration)
272
criteria: MetricAlertCriteria # Alert criteria
273
actions: Optional[List[MetricAlertAction]] # Actions when alert fires
274
severity: int # Alert severity (0=Critical, 1=Error, 2=Warning, 3=Informational, 4=Verbose)
275
auto_mitigate: Optional[bool] # Auto-resolve when condition no longer met
276
target_resource_type: Optional[str] # Target resource type for multi-resource alerts
277
target_resource_region: Optional[str] # Target resource region for multi-resource alerts
278
279
class MetricAlertCriteria:
280
"""Base class for metric alert criteria."""
281
pass
282
283
class MetricAlertSingleResourceMultipleMetricCriteria(MetricAlertCriteria):
284
"""Criteria for single resource with multiple metrics."""
285
all_of: List[MetricCriteria] # All criteria must be met (AND logic)
286
287
class MetricAlertMultipleResourceMultipleMetricCriteria(MetricAlertCriteria):
288
"""Criteria for multiple resources with multiple metrics."""
289
all_of: List[MultiMetricCriteria] # All criteria must be met (AND logic)
290
291
class MetricCriteria:
292
"""Static threshold metric criteria."""
293
name: str # Criteria name
294
metric_name: str # Metric name to monitor
295
metric_namespace: str # Metric namespace
296
operator: ComparisonOperationType # Comparison operator
297
threshold: float # Threshold value
298
time_aggregation: AggregationType # Time aggregation method
299
dimensions: Optional[List[MetricDimension]] # Metric dimensions to filter
300
skip_metric_validation: Optional[bool] # Skip metric validation
301
302
class DynamicMetricCriteria(MultiMetricCriteria):
303
"""Dynamic threshold metric criteria."""
304
name: str # Criteria name
305
metric_name: str # Metric name to monitor
306
metric_namespace: str # Metric namespace
307
operator: DynamicThresholdOperator # Dynamic threshold operator
308
time_aggregation: AggregationType # Time aggregation method
309
alert_sensitivity: DynamicThresholdSensitivity # Sensitivity level
310
failing_periods: DynamicThresholdFailingPeriods # Failing period configuration
311
ignore_data_before: Optional[datetime] # Ignore data before this date
312
dimensions: Optional[List[MetricDimension]] # Metric dimensions to filter
313
314
class MetricAlertAction:
315
"""Action to execute when alert fires."""
316
action_group_id: str # Action group resource ID
317
webhook_properties: Optional[Dict[str, str]] # Custom webhook properties
318
319
class MetricAlertStatusCollection:
320
"""Collection of metric alert statuses."""
321
value: Optional[List[MetricAlertStatus]] # Alert status list
322
323
class MetricAlertStatus:
324
"""Metric alert status information."""
325
name: Optional[str] # Status name
326
id: Optional[str] # Status ID
327
type: Optional[str] # Status type
328
properties: Optional[MetricAlertStatusProperties] # Status properties
329
330
class MetricAlertStatusProperties:
331
"""Metric alert status properties."""
332
dimensions: Optional[Dict[str, str]] # Alert dimensions
333
status: Optional[str] # Alert status
334
timestamp: Optional[datetime] # Status timestamp
335
336
class MetricDimension:
337
"""Metric dimension filter."""
338
name: str # Dimension name
339
operator: str # Filter operator
340
values: List[str] # Dimension values
341
342
class DynamicThresholdFailingPeriods:
343
"""Dynamic threshold failing period configuration."""
344
number_of_evaluation_periods: float # Number of evaluation periods
345
min_failing_periods_to_alert: float # Minimum failing periods to trigger alert
346
347
ComparisonOperationType = Union["Equals", "NotEquals", "GreaterThan", "GreaterThanOrEqual", "LessThan", "LessThanOrEqual"]
348
DynamicThresholdOperator = Union["GreaterThan", "LessThan", "GreaterOrLessThan"]
349
DynamicThresholdSensitivity = Union["Low", "Medium", "High"]
350
AggregationType = Union["Average", "Minimum", "Maximum", "Total", "Count"]
351
```