Create or update an metric alert definition.
PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Insights/metricAlerts/{ruleName}?api-version=2024-03-01-preview
URI Parameters
| Name |
In |
Required |
Type |
Description |
|
resourceGroupName
|
path |
True
|
string
minLength: 1 maxLength: 90
|
The name of the resource group. The name is case insensitive.
|
|
ruleName
|
path |
True
|
string
|
The name of the rule.
|
|
subscriptionId
|
path |
True
|
string
minLength: 1
|
The ID of the target subscription.
|
|
api-version
|
query |
True
|
string
minLength: 1
|
The API version to use for this operation.
|
Request Body
| Name |
Required |
Type |
Description |
|
location
|
True
|
string
|
The geo-location where the resource lives
|
|
properties.criteria
|
True
|
MetricAlertCriteria:
|
Defines the specific alert criteria information.
|
|
properties.enabled
|
True
|
boolean
|
The flag that indicates whether the metric alert is enabled.
|
|
properties.evaluationFrequency
|
True
|
string
(duration)
|
How often the metric alert is evaluated represented in ISO 8601 duration format.
|
|
properties.scopes
|
True
|
string[]
|
The list of resource id's that this metric alert is scoped to. You cannot change the scope of a metric rule based on logs.
|
|
properties.severity
|
True
|
integer
(int32)
|
Alert severity {0, 1, 2, 3, 4}
|
|
identity
|
Microsoft.Common.Identity
|
The identity of the resource.
|
|
properties.actionProperties
|
object
|
The properties of an action properties.
|
|
properties.actions
|
MetricAlertAction[]
|
The array of actions that are performed when the alert rule becomes active, and when an alert condition is resolved.
|
|
properties.autoMitigate
|
boolean
|
The flag that indicates whether the alert should be auto resolved or not. The default is true.
|
|
properties.customProperties
|
object
|
The properties of an alert payload.
|
|
properties.description
|
string
|
The description of the metric alert that will be included in the alert email.
|
|
properties.resolveConfiguration
|
ResolveConfiguration
|
The configuration for how the alert is resolved. Applicable for PromQLCriteria.
|
|
properties.targetResourceRegion
|
string
|
The region of the target resource(s) on which the alert is created/updated. Mandatory if the scope contains a subscription, resource group, or more than one resource.
|
|
properties.targetResourceType
|
string
|
The resource type of the target resource(s) on which the alert is created/updated. Mandatory if the scope contains a subscription, resource group, or more than one resource.
|
|
properties.windowSize
|
string
(duration)
|
The period of time (in ISO 8601 duration format) that is used to monitor alert activity based on the threshold.
|
|
tags
|
object
|
Resource tags.
|
Responses
Security
azure_auth
Azure Active Directory OAuth2 Flow.
Type:
oauth2
Flow:
implicit
Authorization URL:
https://login.microsoftonline.com/common/oauth2/authorize
Scopes
| Name |
Description |
|
user_impersonation
|
impersonate your user account
|
Examples
Create or update a dynamic alert rule for Multiple Resources
Sample request
PUT https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Insights/metricAlerts/MetricAlertOnMultipleResources?api-version=2024-03-01-preview
{
"location": "global",
"properties": {
"description": "This is the description of the rule1",
"actions": [
{
"actionGroupId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {
"key11": "value11",
"key12": "value12"
}
}
],
"autoMitigate": true,
"criteria": {
"allOf": [
{
"name": "High_CPU_80",
"alertSensitivity": "Medium",
"criterionType": "DynamicThresholdCriterion",
"dimensions": [],
"failingPeriods": {
"minFailingPeriodsToAlert": 4,
"numberOfEvaluationPeriods": 4
},
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"operator": "GreaterOrLessThan",
"timeAggregation": "Average"
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme1",
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme2"
],
"severity": 3,
"targetResourceRegion": "southcentralus",
"targetResourceType": "Microsoft.Compute/virtualMachines",
"windowSize": "PT15M"
},
"tags": {}
}
from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-monitor
# USAGE
python create_or_update_dynamic_metric_alert_multiple_resource.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = MonitorManagementClient(
credential=DefaultAzureCredential(),
subscription_id="SUBSCRIPTION_ID",
)
response = client.metric_alerts.create_or_update(
resource_group_name="gigtest",
rule_name="MetricAlertOnMultipleResources",
parameters={
"location": "global",
"properties": {
"actions": [
{
"actionGroupId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {"key11": "value11", "key12": "value12"},
}
],
"autoMitigate": True,
"criteria": {
"allOf": [
{
"alertSensitivity": "Medium",
"criterionType": "DynamicThresholdCriterion",
"dimensions": [],
"failingPeriods": {"minFailingPeriodsToAlert": 4, "numberOfEvaluationPeriods": 4},
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"name": "High_CPU_80",
"operator": "GreaterOrLessThan",
"timeAggregation": "Average",
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria",
},
"description": "This is the description of the rule1",
"enabled": True,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme1",
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme2",
],
"severity": 3,
"targetResourceRegion": "southcentralus",
"targetResourceType": "Microsoft.Compute/virtualMachines",
"windowSize": "PT15M",
},
"tags": {},
},
)
print(response)
# x-ms-original-file: 2024-03-01-preview/createOrUpdateDynamicMetricAlertMultipleResource.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
package armmonitor_test
import (
"context"
"log"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
)
// Generated from example definition: 2024-03-01-preview/createOrUpdateDynamicMetricAlertMultipleResource.json
func ExampleMetricAlertsClient_CreateOrUpdate_createOrUpdateADynamicAlertRuleForMultipleResources() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
clientFactory, err := armmonitor.NewClientFactory("00000000-0000-0000-0000-000000000000", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
res, err := clientFactory.NewMetricAlertsClient().CreateOrUpdate(ctx, "gigtest", "MetricAlertOnMultipleResources", armmonitor.MetricAlertResource{
Location: to.Ptr("global"),
Properties: &armmonitor.MetricAlertProperties{
Description: to.Ptr("This is the description of the rule1"),
Actions: []*armmonitor.MetricAlertAction{
{
ActionGroupID: to.Ptr("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
WebHookProperties: map[string]*string{
"key11": to.Ptr("value11"),
"key12": to.Ptr("value12"),
},
},
},
AutoMitigate: to.Ptr(true),
Criteria: &armmonitor.MetricAlertMultipleResourceMultipleMetricCriteria{
AllOf: []armmonitor.MultiMetricCriteriaClassification{
&armmonitor.DynamicMetricCriteria{
Name: to.Ptr("High_CPU_80"),
AlertSensitivity: to.Ptr(armmonitor.DynamicThresholdSensitivityMedium),
CriterionType: to.Ptr(armmonitor.CriterionTypeDynamicThresholdCriterion),
Dimensions: []*armmonitor.MetricDimension{},
FailingPeriods: &armmonitor.DynamicThresholdFailingPeriods{
MinFailingPeriodsToAlert: to.Ptr[float32](4),
NumberOfEvaluationPeriods: to.Ptr[float32](4),
},
MetricName: to.Ptr("Percentage CPU"),
MetricNamespace: to.Ptr("microsoft.compute/virtualmachines"),
Operator: to.Ptr(armmonitor.DynamicThresholdOperatorGreaterOrLessThan),
TimeAggregation: to.Ptr(armmonitor.AggregationTypeEnumAverage),
},
},
ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorMultipleResourceMultipleMetricCriteria),
},
Enabled: to.Ptr(true),
EvaluationFrequency: to.Ptr("PT1M"),
Scopes: []*string{
to.Ptr("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme1"),
to.Ptr("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme2"),
},
Severity: to.Ptr[int32](3),
TargetResourceRegion: to.Ptr("southcentralus"),
TargetResourceType: to.Ptr("Microsoft.Compute/virtualMachines"),
WindowSize: to.Ptr("PT15M"),
},
Tags: map[string]*string{},
}, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res = armmonitor.MetricAlertsClientCreateOrUpdateResponse{
// MetricAlertResource: armmonitor.MetricAlertResource{
// Type: to.Ptr("Microsoft.Insights/metricAlerts"),
// ID: to.Ptr("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/MetricAlertOnMultipleResources"),
// Location: to.Ptr("global"),
// Properties: &armmonitor.MetricAlertProperties{
// Description: to.Ptr("This is the description of the rule1"),
// Actions: []*armmonitor.MetricAlertAction{
// {
// ActionGroupID: to.Ptr("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
// WebHookProperties: map[string]*string{
// "key11": to.Ptr("value11"),
// "key12": to.Ptr("value12"),
// },
// },
// },
// AutoMitigate: to.Ptr(true),
// Criteria: &armmonitor.MetricAlertMultipleResourceMultipleMetricCriteria{
// AllOf: []armmonitor.MultiMetricCriteriaClassification{
// &armmonitor.DynamicMetricCriteria{
// Name: to.Ptr("High_CPU_80"),
// AlertSensitivity: to.Ptr(armmonitor.DynamicThresholdSensitivityMedium),
// CriterionType: to.Ptr(armmonitor.CriterionTypeDynamicThresholdCriterion),
// Dimensions: []*armmonitor.MetricDimension{
// },
// FailingPeriods: &armmonitor.DynamicThresholdFailingPeriods{
// MinFailingPeriodsToAlert: to.Ptr[float32](4),
// NumberOfEvaluationPeriods: to.Ptr[float32](4),
// },
// MetricName: to.Ptr("Percentage CPU"),
// MetricNamespace: to.Ptr("microsoft.compute/virtualmachines"),
// Operator: to.Ptr(armmonitor.DynamicThresholdOperatorGreaterOrLessThan),
// TimeAggregation: to.Ptr(armmonitor.AggregationTypeEnumAverage),
// },
// },
// ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorMultipleResourceMultipleMetricCriteria),
// },
// Enabled: to.Ptr(true),
// EvaluationFrequency: to.Ptr("PT1M"),
// Scopes: []*string{
// to.Ptr("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme1"),
// to.Ptr("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme2"),
// },
// Severity: to.Ptr[int32](3),
// TargetResourceRegion: to.Ptr("southcentralus"),
// TargetResourceType: to.Ptr("Microsoft.Compute/virtualMachines"),
// WindowSize: to.Ptr("PT15M"),
// },
// Tags: map[string]*string{
// "hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": to.Ptr("Resource"),
// },
// },
// }
}
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
Sample response
{
"type": "Microsoft.Insights/metricAlerts",
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/MetricAlertOnMultipleResources",
"location": "global",
"properties": {
"description": "This is the description of the rule1",
"actions": [
{
"actionGroupId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {
"key11": "value11",
"key12": "value12"
}
}
],
"autoMitigate": true,
"criteria": {
"allOf": [
{
"name": "High_CPU_80",
"alertSensitivity": "Medium",
"criterionType": "DynamicThresholdCriterion",
"dimensions": [],
"failingPeriods": {
"minFailingPeriodsToAlert": 4,
"numberOfEvaluationPeriods": 4
},
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"operator": "GreaterOrLessThan",
"timeAggregation": "Average"
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme1",
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme2"
],
"severity": 3,
"targetResourceRegion": "southcentralus",
"targetResourceType": "Microsoft.Compute/virtualMachines",
"windowSize": "PT15M"
},
"tags": {
"hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": "Resource"
}
}
Create or update a dynamic alert rule for Single Resource
Sample request
PUT https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Insights/metricAlerts/chiricutin?api-version=2024-03-01-preview
{
"location": "global",
"properties": {
"description": "This is the description of the rule1",
"actions": [
{
"actionGroupId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {
"key11": "value11",
"key12": "value12"
}
}
],
"autoMitigate": true,
"criteria": {
"allOf": [
{
"name": "High_CPU_80",
"alertSensitivity": "Medium",
"criterionType": "DynamicThresholdCriterion",
"dimensions": [],
"failingPeriods": {
"minFailingPeriodsToAlert": 4,
"numberOfEvaluationPeriods": 4
},
"ignoreDataBefore": "2019-04-04T21:00:00.000Z",
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"operator": "GreaterOrLessThan",
"timeAggregation": "Average"
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme"
],
"severity": 3,
"windowSize": "PT15M"
},
"tags": {}
}
from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-monitor
# USAGE
python create_or_update_dynamic_metric_alert_single_resource.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = MonitorManagementClient(
credential=DefaultAzureCredential(),
subscription_id="SUBSCRIPTION_ID",
)
response = client.metric_alerts.create_or_update(
resource_group_name="gigtest",
rule_name="chiricutin",
parameters={
"location": "global",
"properties": {
"actions": [
{
"actionGroupId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {"key11": "value11", "key12": "value12"},
}
],
"autoMitigate": True,
"criteria": {
"allOf": [
{
"alertSensitivity": "Medium",
"criterionType": "DynamicThresholdCriterion",
"dimensions": [],
"failingPeriods": {"minFailingPeriodsToAlert": 4, "numberOfEvaluationPeriods": 4},
"ignoreDataBefore": "2019-04-04T21:00:00.000Z",
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"name": "High_CPU_80",
"operator": "GreaterOrLessThan",
"timeAggregation": "Average",
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria",
},
"description": "This is the description of the rule1",
"enabled": True,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme"
],
"severity": 3,
"windowSize": "PT15M",
},
"tags": {},
},
)
print(response)
# x-ms-original-file: 2024-03-01-preview/createOrUpdateDynamicMetricAlertSingleResource.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
package armmonitor_test
import (
"context"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
)
// Generated from example definition: 2024-03-01-preview/createOrUpdateDynamicMetricAlertSingleResource.json
func ExampleMetricAlertsClient_CreateOrUpdate_createOrUpdateADynamicAlertRuleForSingleResource() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
clientFactory, err := armmonitor.NewClientFactory("00000000-0000-0000-0000-000000000000", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
res, err := clientFactory.NewMetricAlertsClient().CreateOrUpdate(ctx, "gigtest", "chiricutin", armmonitor.MetricAlertResource{
Location: to.Ptr("global"),
Properties: &armmonitor.MetricAlertProperties{
Description: to.Ptr("This is the description of the rule1"),
Actions: []*armmonitor.MetricAlertAction{
{
ActionGroupID: to.Ptr("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
WebHookProperties: map[string]*string{
"key11": to.Ptr("value11"),
"key12": to.Ptr("value12"),
},
},
},
AutoMitigate: to.Ptr(true),
Criteria: &armmonitor.MetricAlertMultipleResourceMultipleMetricCriteria{
AllOf: []armmonitor.MultiMetricCriteriaClassification{
&armmonitor.DynamicMetricCriteria{
Name: to.Ptr("High_CPU_80"),
AlertSensitivity: to.Ptr(armmonitor.DynamicThresholdSensitivityMedium),
CriterionType: to.Ptr(armmonitor.CriterionTypeDynamicThresholdCriterion),
Dimensions: []*armmonitor.MetricDimension{},
FailingPeriods: &armmonitor.DynamicThresholdFailingPeriods{
MinFailingPeriodsToAlert: to.Ptr[float32](4),
NumberOfEvaluationPeriods: to.Ptr[float32](4),
},
IgnoreDataBefore: to.Ptr(func() time.Time { t, _ := time.Parse(time.RFC3339Nano, "2019-04-04T21:00:00.000Z"); return t }()),
MetricName: to.Ptr("Percentage CPU"),
MetricNamespace: to.Ptr("microsoft.compute/virtualmachines"),
Operator: to.Ptr(armmonitor.DynamicThresholdOperatorGreaterOrLessThan),
TimeAggregation: to.Ptr(armmonitor.AggregationTypeEnumAverage),
},
},
ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorMultipleResourceMultipleMetricCriteria),
},
Enabled: to.Ptr(true),
EvaluationFrequency: to.Ptr("PT1M"),
Scopes: []*string{
to.Ptr("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme"),
},
Severity: to.Ptr[int32](3),
WindowSize: to.Ptr("PT15M"),
},
Tags: map[string]*string{},
}, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res = armmonitor.MetricAlertsClientCreateOrUpdateResponse{
// MetricAlertResource: armmonitor.MetricAlertResource{
// Type: to.Ptr("Microsoft.Insights/metricAlerts"),
// ID: to.Ptr("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/chiricutin"),
// Location: to.Ptr("global"),
// Properties: &armmonitor.MetricAlertProperties{
// Description: to.Ptr("This is the description of the rule1"),
// Actions: []*armmonitor.MetricAlertAction{
// {
// ActionGroupID: to.Ptr("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
// WebHookProperties: map[string]*string{
// "key11": to.Ptr("value11"),
// "key12": to.Ptr("value12"),
// },
// },
// },
// AutoMitigate: to.Ptr(true),
// Criteria: &armmonitor.MetricAlertMultipleResourceMultipleMetricCriteria{
// AllOf: []armmonitor.MultiMetricCriteriaClassification{
// &armmonitor.DynamicMetricCriteria{
// Name: to.Ptr("High_CPU_80"),
// AlertSensitivity: to.Ptr(armmonitor.DynamicThresholdSensitivityMedium),
// CriterionType: to.Ptr(armmonitor.CriterionTypeDynamicThresholdCriterion),
// Dimensions: []*armmonitor.MetricDimension{
// },
// FailingPeriods: &armmonitor.DynamicThresholdFailingPeriods{
// MinFailingPeriodsToAlert: to.Ptr[float32](4),
// NumberOfEvaluationPeriods: to.Ptr[float32](4),
// },
// IgnoreDataBefore: to.Ptr(func() time.Time { t, _ := time.Parse(time.RFC3339Nano, "2019-04-04T21:00:00.000Z"); return t}()),
// MetricName: to.Ptr("Percentage CPU"),
// MetricNamespace: to.Ptr("microsoft.compute/virtualmachines"),
// Operator: to.Ptr(armmonitor.DynamicThresholdOperatorGreaterOrLessThan),
// TimeAggregation: to.Ptr(armmonitor.AggregationTypeEnumAverage),
// },
// },
// ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorMultipleResourceMultipleMetricCriteria),
// },
// Enabled: to.Ptr(true),
// EvaluationFrequency: to.Ptr("PT1M"),
// Scopes: []*string{
// to.Ptr("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme"),
// },
// Severity: to.Ptr[int32](3),
// TargetResourceRegion: to.Ptr("southcentralus"),
// TargetResourceType: to.Ptr("Microsoft.Compute/virtualMachines"),
// WindowSize: to.Ptr("PT15M"),
// },
// Tags: map[string]*string{
// "hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": to.Ptr("Resource"),
// },
// },
// }
}
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
Sample response
{
"type": "Microsoft.Insights/metricAlerts",
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/chiricutin",
"location": "global",
"properties": {
"description": "This is the description of the rule1",
"actions": [
{
"actionGroupId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {
"key11": "value11",
"key12": "value12"
}
}
],
"autoMitigate": true,
"criteria": {
"allOf": [
{
"name": "High_CPU_80",
"alertSensitivity": "Medium",
"criterionType": "DynamicThresholdCriterion",
"dimensions": [],
"failingPeriods": {
"minFailingPeriodsToAlert": 4,
"numberOfEvaluationPeriods": 4
},
"ignoreDataBefore": "2019-04-04T21:00:00.000Z",
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"operator": "GreaterOrLessThan",
"timeAggregation": "Average"
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme"
],
"severity": 3,
"targetResourceRegion": "southcentralus",
"targetResourceType": "Microsoft.Compute/virtualMachines",
"windowSize": "PT15M"
},
"tags": {
"hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": "Resource"
}
}
Create or update a query based alert rule
Sample request
PUT https://management.azure.com/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Insights/metricAlerts/chiricutin?api-version=2024-03-01-preview
{
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {}
}
},
"location": "eastus",
"properties": {
"description": "This is the description of the rule1",
"actionProperties": {
"Email.Sujbect": "my custom email subject"
},
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"
}
],
"criteria": {
"allOf": [
{
"name": "Metric1",
"criterionType": "StaticThresholdCriterion",
"query": "avg({\"system.cpu.utilization\"}) > 90"
}
],
"failingPeriods": {
"for": "PT5M"
},
"odata.type": "Microsoft.Azure.Monitor.PromQLCriteria"
},
"customProperties": {
"key11": "value11",
"key12": "value12"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"resolveConfiguration": {
"autoResolved": true,
"timeToResolve": "PT10M"
},
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.monitor/accounts/gigwadme"
],
"severity": 3
},
"tags": {}
}
from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-monitor
# USAGE
python create_or_update_metric_alert_query.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = MonitorManagementClient(
credential=DefaultAzureCredential(),
subscription_id="SUBSCRIPTION_ID",
)
response = client.metric_alerts.create_or_update(
resource_group_name="gigtest",
rule_name="chiricutin",
parameters={
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {}
},
},
"location": "eastus",
"properties": {
"actionProperties": {"Email.Sujbect": "my custom email subject"},
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"
}
],
"criteria": {
"allOf": [
{
"criterionType": "StaticThresholdCriterion",
"name": "Metric1",
"query": 'avg({"system.cpu.utilization"}) > 90',
}
],
"failingPeriods": {"for": "PT5M"},
"odata.type": "Microsoft.Azure.Monitor.PromQLCriteria",
},
"customProperties": {"key11": "value11", "key12": "value12"},
"description": "This is the description of the rule1",
"enabled": True,
"evaluationFrequency": "PT1M",
"resolveConfiguration": {"autoResolved": True, "timeToResolve": "PT10M"},
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.monitor/accounts/gigwadme"
],
"severity": 3,
},
"tags": {},
},
)
print(response)
# x-ms-original-file: 2024-03-01-preview/createOrUpdateMetricAlertQuery.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
package armmonitor_test
import (
"context"
"log"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
)
// Generated from example definition: 2024-03-01-preview/createOrUpdateMetricAlertQuery.json
func ExampleMetricAlertsClient_CreateOrUpdate_createOrUpdateAQueryBasedAlertRule() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
clientFactory, err := armmonitor.NewClientFactory("14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
res, err := clientFactory.NewMetricAlertsClient().CreateOrUpdate(ctx, "gigtest", "chiricutin", armmonitor.MetricAlertResource{
Identity: &armmonitor.Identity{
Type: to.Ptr(armmonitor.IdentityTypeUserAssigned),
UserAssignedIdentities: map[string]*armmonitor.UserIdentityProperties{
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {},
},
},
Location: to.Ptr("eastus"),
Properties: &armmonitor.MetricAlertProperties{
Description: to.Ptr("This is the description of the rule1"),
ActionProperties: map[string]*string{
"Email.Sujbect": to.Ptr("my custom email subject"),
},
Actions: []*armmonitor.MetricAlertAction{
{
ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
},
},
Criteria: &armmonitor.PromQLCriteria{
AllOf: []armmonitor.MultiPromQLCriteriaClassification{
&armmonitor.StaticPromQLCriteria{
Name: to.Ptr("Metric1"),
CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
Query: to.Ptr("avg({\"system.cpu.utilization\"}) > 90"),
},
},
FailingPeriods: &armmonitor.QueryFailingPeriods{
For: to.Ptr("PT5M"),
},
ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorPromQLCriteria),
},
CustomProperties: map[string]*string{
"key11": to.Ptr("value11"),
"key12": to.Ptr("value12"),
},
Enabled: to.Ptr(true),
EvaluationFrequency: to.Ptr("PT1M"),
ResolveConfiguration: &armmonitor.ResolveConfiguration{
AutoResolved: to.Ptr(true),
TimeToResolve: to.Ptr("PT10M"),
},
Scopes: []*string{
to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.monitor/accounts/gigwadme"),
},
Severity: to.Ptr[int32](3),
},
Tags: map[string]*string{},
}, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res = armmonitor.MetricAlertsClientCreateOrUpdateResponse{
// MetricAlertResource: armmonitor.MetricAlertResource{
// Type: to.Ptr("Microsoft.Insights/metricAlerts"),
// ID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/chiricutin"),
// Identity: &armmonitor.Identity{
// Type: to.Ptr(armmonitor.IdentityTypeUserAssigned),
// TenantID: to.Ptr("72f988bf-86f1-41af-91ab-2d7cd011db47"),
// UserAssignedIdentities: map[string]*armmonitor.UserIdentityProperties{
// "/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": &armmonitor.UserIdentityProperties{
// ClientID: to.Ptr("778f4d04-3c60-4622-a839-5cf05866c983"),
// PrincipalID: to.Ptr("669dd76d-cde8-4dc3-b882-1de566b0c628"),
// },
// },
// },
// Location: to.Ptr("eastus"),
// Properties: &armmonitor.MetricAlertProperties{
// Description: to.Ptr("This is the description of the rule1"),
// ActionProperties: map[string]*string{
// "Email.Sujbect": to.Ptr("my custom email subject"),
// },
// Actions: []*armmonitor.MetricAlertAction{
// {
// ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
// },
// },
// Criteria: &armmonitor.PromQLCriteria{
// AllOf: []armmonitor.MultiPromQLCriteriaClassification{
// &armmonitor.StaticPromQLCriteria{
// Name: to.Ptr("Metric1"),
// CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
// Query: to.Ptr("avg({\"system.cpu.utilization\"}) > 90"),
// },
// },
// FailingPeriods: &armmonitor.QueryFailingPeriods{
// For: to.Ptr("PT2M"),
// },
// ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorPromQLCriteria),
// },
// CustomProperties: map[string]*string{
// "key11": to.Ptr("value11"),
// "key12": to.Ptr("value12"),
// },
// Enabled: to.Ptr(true),
// EvaluationFrequency: to.Ptr("PT1M"),
// ResolveConfiguration: &armmonitor.ResolveConfiguration{
// AutoResolved: to.Ptr(true),
// TimeToResolve: to.Ptr("PT10M"),
// },
// Scopes: []*string{
// to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.monitor/accounts/gigwadme"),
// },
// Severity: to.Ptr[int32](3),
// },
// Tags: map[string]*string{
// "hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": to.Ptr("Resource"),
// },
// },
// }
}
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
Sample response
{
"type": "Microsoft.Insights/metricAlerts",
"id": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/chiricutin",
"identity": {
"type": "UserAssigned",
"tenantId": "72f988bf-86f1-41af-91ab-2d7cd011db47",
"userAssignedIdentities": {
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {
"clientId": "778f4d04-3c60-4622-a839-5cf05866c983",
"principalId": "669dd76d-cde8-4dc3-b882-1de566b0c628"
}
}
},
"location": "eastus",
"properties": {
"description": "This is the description of the rule1",
"actionProperties": {
"Email.Sujbect": "my custom email subject"
},
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"
}
],
"criteria": {
"allOf": [
{
"name": "Metric1",
"criterionType": "StaticThresholdCriterion",
"query": "avg({\"system.cpu.utilization\"}) > 90"
}
],
"failingPeriods": {
"for": "PT2M"
},
"odata.type": "Microsoft.Azure.Monitor.PromQLCriteria"
},
"customProperties": {
"key11": "value11",
"key12": "value12"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"resolveConfiguration": {
"autoResolved": true,
"timeToResolve": "PT10M"
},
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.monitor/accounts/gigwadme"
],
"severity": 3
},
"tags": {
"hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": "Resource"
}
}
Create or update a query based alert rule with dynamic threshold
Sample request
PUT https://management.azure.com/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Insights/metricAlerts/chiricutin?api-version=2024-03-01-preview
{
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {}
}
},
"location": "eastus",
"properties": {
"description": "This is the description of the rule1",
"actionProperties": {
"Email.Sujbect": "my custom email subject"
},
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"
}
],
"criteria": {
"allOf": [
{
"name": "Metric1",
"alertSensitivity": "Medium",
"criterionType": "DynamicThresholdCriterion",
"ignoreDataBefore": "2019-04-04T21:00:00.000Z",
"operator": "LessThan",
"query": "avg({\"system.cpu.utilization\"})"
}
],
"failingPeriods": {
"for": "PT5M"
},
"odata.type": "Microsoft.Azure.Monitor.PromQLCriteria"
},
"customProperties": {
"key11": "value11",
"key12": "value12"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"resolveConfiguration": {
"autoResolved": true,
"timeToResolve": "PT10M"
},
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.monitor/accounts/gigwadme"
],
"severity": 3
},
"tags": {}
}
from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-monitor
# USAGE
python create_or_update_metric_alert_query_dt.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = MonitorManagementClient(
credential=DefaultAzureCredential(),
subscription_id="SUBSCRIPTION_ID",
)
response = client.metric_alerts.create_or_update(
resource_group_name="gigtest",
rule_name="chiricutin",
parameters={
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {}
},
},
"location": "eastus",
"properties": {
"actionProperties": {"Email.Sujbect": "my custom email subject"},
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"
}
],
"criteria": {
"allOf": [
{
"alertSensitivity": "Medium",
"criterionType": "DynamicThresholdCriterion",
"ignoreDataBefore": "2019-04-04T21:00:00.000Z",
"name": "Metric1",
"operator": "LessThan",
"query": 'avg({"system.cpu.utilization"})',
}
],
"failingPeriods": {"for": "PT5M"},
"odata.type": "Microsoft.Azure.Monitor.PromQLCriteria",
},
"customProperties": {"key11": "value11", "key12": "value12"},
"description": "This is the description of the rule1",
"enabled": True,
"evaluationFrequency": "PT1M",
"resolveConfiguration": {"autoResolved": True, "timeToResolve": "PT10M"},
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.monitor/accounts/gigwadme"
],
"severity": 3,
},
"tags": {},
},
)
print(response)
# x-ms-original-file: 2024-03-01-preview/createOrUpdateMetricAlertQueryDT.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
package armmonitor_test
import (
"context"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
)
// Generated from example definition: 2024-03-01-preview/createOrUpdateMetricAlertQueryDT.json
func ExampleMetricAlertsClient_CreateOrUpdate_createOrUpdateAQueryBasedAlertRuleWithDynamicThreshold() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
clientFactory, err := armmonitor.NewClientFactory("14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
res, err := clientFactory.NewMetricAlertsClient().CreateOrUpdate(ctx, "gigtest", "chiricutin", armmonitor.MetricAlertResource{
Identity: &armmonitor.Identity{
Type: to.Ptr(armmonitor.IdentityTypeUserAssigned),
UserAssignedIdentities: map[string]*armmonitor.UserIdentityProperties{
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {},
},
},
Location: to.Ptr("eastus"),
Properties: &armmonitor.MetricAlertProperties{
Description: to.Ptr("This is the description of the rule1"),
ActionProperties: map[string]*string{
"Email.Sujbect": to.Ptr("my custom email subject"),
},
Actions: []*armmonitor.MetricAlertAction{
{
ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
},
},
Criteria: &armmonitor.PromQLCriteria{
AllOf: []armmonitor.MultiPromQLCriteriaClassification{
&armmonitor.DynamicPromQLCriteria{
Name: to.Ptr("Metric1"),
AlertSensitivity: to.Ptr(armmonitor.DynamicThresholdSensitivityMedium),
CriterionType: to.Ptr(armmonitor.CriterionTypeDynamicThresholdCriterion),
IgnoreDataBefore: to.Ptr(func() time.Time { t, _ := time.Parse(time.RFC3339Nano, "2019-04-04T21:00:00.000Z"); return t }()),
Operator: to.Ptr(armmonitor.DynamicThresholdOperatorLessThan),
Query: to.Ptr("avg({\"system.cpu.utilization\"})"),
},
},
FailingPeriods: &armmonitor.QueryFailingPeriods{
For: to.Ptr("PT5M"),
},
ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorPromQLCriteria),
},
CustomProperties: map[string]*string{
"key11": to.Ptr("value11"),
"key12": to.Ptr("value12"),
},
Enabled: to.Ptr(true),
EvaluationFrequency: to.Ptr("PT1M"),
ResolveConfiguration: &armmonitor.ResolveConfiguration{
AutoResolved: to.Ptr(true),
TimeToResolve: to.Ptr("PT10M"),
},
Scopes: []*string{
to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.monitor/accounts/gigwadme"),
},
Severity: to.Ptr[int32](3),
},
Tags: map[string]*string{},
}, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res = armmonitor.MetricAlertsClientCreateOrUpdateResponse{
// MetricAlertResource: armmonitor.MetricAlertResource{
// Type: to.Ptr("Microsoft.Insights/metricAlerts"),
// ID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/chiricutin"),
// Identity: &armmonitor.Identity{
// Type: to.Ptr(armmonitor.IdentityTypeUserAssigned),
// TenantID: to.Ptr("72f988bf-86f1-41af-91ab-2d7cd011db47"),
// UserAssignedIdentities: map[string]*armmonitor.UserIdentityProperties{
// "/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": &armmonitor.UserIdentityProperties{
// ClientID: to.Ptr("778f4d04-3c60-4622-a839-5cf05866c983"),
// PrincipalID: to.Ptr("669dd76d-cde8-4dc3-b882-1de566b0c628"),
// },
// },
// },
// Location: to.Ptr("eastus"),
// Properties: &armmonitor.MetricAlertProperties{
// Description: to.Ptr("This is the description of the rule1"),
// ActionProperties: map[string]*string{
// "Email.Sujbect": to.Ptr("my custom email subject"),
// },
// Actions: []*armmonitor.MetricAlertAction{
// {
// ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
// },
// },
// Criteria: &armmonitor.PromQLCriteria{
// AllOf: []armmonitor.MultiPromQLCriteriaClassification{
// &armmonitor.DynamicPromQLCriteria{
// Name: to.Ptr("Metric1"),
// AlertSensitivity: to.Ptr(armmonitor.DynamicThresholdSensitivityMedium),
// CriterionType: to.Ptr(armmonitor.CriterionTypeDynamicThresholdCriterion),
// IgnoreDataBefore: to.Ptr(func() time.Time { t, _ := time.Parse(time.RFC3339Nano, "2019-04-04T21:00:00.000Z"); return t}()),
// Operator: to.Ptr(armmonitor.DynamicThresholdOperatorLessThan),
// Query: to.Ptr("avg({\"system.cpu.utilization\"})"),
// },
// },
// FailingPeriods: &armmonitor.QueryFailingPeriods{
// For: to.Ptr("PT2M"),
// },
// ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorPromQLCriteria),
// },
// CustomProperties: map[string]*string{
// "key11": to.Ptr("value11"),
// "key12": to.Ptr("value12"),
// },
// Enabled: to.Ptr(true),
// EvaluationFrequency: to.Ptr("PT1M"),
// ResolveConfiguration: &armmonitor.ResolveConfiguration{
// AutoResolved: to.Ptr(true),
// TimeToResolve: to.Ptr("PT10M"),
// },
// Scopes: []*string{
// to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.monitor/accounts/gigwadme"),
// },
// Severity: to.Ptr[int32](3),
// },
// Tags: map[string]*string{
// "hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": to.Ptr("Resource"),
// },
// },
// }
}
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
Sample response
{
"type": "Microsoft.Insights/metricAlerts",
"id": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/chiricutin",
"identity": {
"type": "UserAssigned",
"tenantId": "72f988bf-86f1-41af-91ab-2d7cd011db47",
"userAssignedIdentities": {
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {
"clientId": "778f4d04-3c60-4622-a839-5cf05866c983",
"principalId": "669dd76d-cde8-4dc3-b882-1de566b0c628"
}
}
},
"location": "eastus",
"properties": {
"description": "This is the description of the rule1",
"actionProperties": {
"Email.Sujbect": "my custom email subject"
},
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"
}
],
"criteria": {
"allOf": [
{
"name": "Metric1",
"alertSensitivity": "Medium",
"criterionType": "DynamicThresholdCriterion",
"ignoreDataBefore": "2019-04-04T21:00:00.000Z",
"operator": "LessThan",
"query": "avg({\"system.cpu.utilization\"})"
}
],
"failingPeriods": {
"for": "PT2M"
},
"odata.type": "Microsoft.Azure.Monitor.PromQLCriteria"
},
"customProperties": {
"key11": "value11",
"key12": "value12"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"resolveConfiguration": {
"autoResolved": true,
"timeToResolve": "PT10M"
},
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.monitor/accounts/gigwadme"
],
"severity": 3
},
"tags": {
"hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": "Resource"
}
}
Create or update a resource centric query based alert rule
Sample request
PUT https://management.azure.com/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Insights/metricAlerts/chiricutin?api-version=2024-03-01-preview
{
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {}
}
},
"location": "eastus",
"properties": {
"description": "This is the description of the rule1",
"actionProperties": {
"Email.Sujbect": "my custom email subject"
},
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"
}
],
"criteria": {
"allOf": [
{
"name": "Metric1",
"criterionType": "StaticThresholdCriterion",
"query": "avg({\"system.cpu.utilization\"}) > 90"
}
],
"failingPeriods": {
"for": "PT5M"
},
"odata.type": "Microsoft.Azure.Monitor.PromQLCriteria"
},
"customProperties": {
"key11": "value11",
"key12": "value12"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"resolveConfiguration": {
"autoResolved": true,
"timeToResolve": "PT10M"
},
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.compute/virtualMachines/myVmName"
],
"severity": 3
},
"tags": {}
}
from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-monitor
# USAGE
python create_or_update_metric_alert_query_resource_centric.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = MonitorManagementClient(
credential=DefaultAzureCredential(),
subscription_id="SUBSCRIPTION_ID",
)
response = client.metric_alerts.create_or_update(
resource_group_name="gigtest",
rule_name="chiricutin",
parameters={
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {}
},
},
"location": "eastus",
"properties": {
"actionProperties": {"Email.Sujbect": "my custom email subject"},
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"
}
],
"criteria": {
"allOf": [
{
"criterionType": "StaticThresholdCriterion",
"name": "Metric1",
"query": 'avg({"system.cpu.utilization"}) > 90',
}
],
"failingPeriods": {"for": "PT5M"},
"odata.type": "Microsoft.Azure.Monitor.PromQLCriteria",
},
"customProperties": {"key11": "value11", "key12": "value12"},
"description": "This is the description of the rule1",
"enabled": True,
"evaluationFrequency": "PT1M",
"resolveConfiguration": {"autoResolved": True, "timeToResolve": "PT10M"},
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.compute/virtualMachines/myVmName"
],
"severity": 3,
},
"tags": {},
},
)
print(response)
# x-ms-original-file: 2024-03-01-preview/createOrUpdateMetricAlertQueryResourceCentric.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
package armmonitor_test
import (
"context"
"log"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
)
// Generated from example definition: 2024-03-01-preview/createOrUpdateMetricAlertQueryResourceCentric.json
func ExampleMetricAlertsClient_CreateOrUpdate_createOrUpdateAResourceCentricQueryBasedAlertRule() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
clientFactory, err := armmonitor.NewClientFactory("14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
res, err := clientFactory.NewMetricAlertsClient().CreateOrUpdate(ctx, "gigtest", "chiricutin", armmonitor.MetricAlertResource{
Identity: &armmonitor.Identity{
Type: to.Ptr(armmonitor.IdentityTypeUserAssigned),
UserAssignedIdentities: map[string]*armmonitor.UserIdentityProperties{
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {},
},
},
Location: to.Ptr("eastus"),
Properties: &armmonitor.MetricAlertProperties{
Description: to.Ptr("This is the description of the rule1"),
ActionProperties: map[string]*string{
"Email.Sujbect": to.Ptr("my custom email subject"),
},
Actions: []*armmonitor.MetricAlertAction{
{
ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
},
},
Criteria: &armmonitor.PromQLCriteria{
AllOf: []armmonitor.MultiPromQLCriteriaClassification{
&armmonitor.StaticPromQLCriteria{
Name: to.Ptr("Metric1"),
CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
Query: to.Ptr("avg({\"system.cpu.utilization\"}) > 90"),
},
},
FailingPeriods: &armmonitor.QueryFailingPeriods{
For: to.Ptr("PT5M"),
},
ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorPromQLCriteria),
},
CustomProperties: map[string]*string{
"key11": to.Ptr("value11"),
"key12": to.Ptr("value12"),
},
Enabled: to.Ptr(true),
EvaluationFrequency: to.Ptr("PT1M"),
ResolveConfiguration: &armmonitor.ResolveConfiguration{
AutoResolved: to.Ptr(true),
TimeToResolve: to.Ptr("PT10M"),
},
Scopes: []*string{
to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.compute/virtualMachines/myVmName"),
},
Severity: to.Ptr[int32](3),
},
Tags: map[string]*string{},
}, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res = armmonitor.MetricAlertsClientCreateOrUpdateResponse{
// MetricAlertResource: armmonitor.MetricAlertResource{
// Type: to.Ptr("Microsoft.Insights/metricAlerts"),
// ID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/chiricutin"),
// Identity: &armmonitor.Identity{
// Type: to.Ptr(armmonitor.IdentityTypeUserAssigned),
// TenantID: to.Ptr("72f988bf-86f1-41af-91ab-2d7cd011db47"),
// UserAssignedIdentities: map[string]*armmonitor.UserIdentityProperties{
// "/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": &armmonitor.UserIdentityProperties{
// ClientID: to.Ptr("778f4d04-3c60-4622-a839-5cf05866c983"),
// PrincipalID: to.Ptr("669dd76d-cde8-4dc3-b882-1de566b0c628"),
// },
// },
// },
// Location: to.Ptr("eastus"),
// Properties: &armmonitor.MetricAlertProperties{
// Description: to.Ptr("This is the description of the rule1"),
// ActionProperties: map[string]*string{
// "Email.Sujbect": to.Ptr("my custom email subject"),
// },
// Actions: []*armmonitor.MetricAlertAction{
// {
// ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
// },
// },
// Criteria: &armmonitor.PromQLCriteria{
// AllOf: []armmonitor.MultiPromQLCriteriaClassification{
// &armmonitor.StaticPromQLCriteria{
// Name: to.Ptr("Metric1"),
// CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
// Query: to.Ptr("avg({\"system.cpu.utilization\"}) > 90"),
// },
// },
// FailingPeriods: &armmonitor.QueryFailingPeriods{
// For: to.Ptr("PT2M"),
// },
// ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorPromQLCriteria),
// },
// CustomProperties: map[string]*string{
// "key11": to.Ptr("value11"),
// "key12": to.Ptr("value12"),
// },
// Enabled: to.Ptr(true),
// EvaluationFrequency: to.Ptr("PT1M"),
// ResolveConfiguration: &armmonitor.ResolveConfiguration{
// AutoResolved: to.Ptr(true),
// TimeToResolve: to.Ptr("PT10M"),
// },
// Scopes: []*string{
// to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.compute/virtualMachines/myVmName"),
// },
// Severity: to.Ptr[int32](3),
// },
// Tags: map[string]*string{
// "hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": to.Ptr("Resource"),
// },
// },
// }
}
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
Sample response
{
"type": "Microsoft.Insights/metricAlerts",
"id": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/chiricutin",
"identity": {
"type": "UserAssigned",
"tenantId": "72f988bf-86f1-41af-91ab-2d7cd011db47",
"userAssignedIdentities": {
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {
"clientId": "778f4d04-3c60-4622-a839-5cf05866c983",
"principalId": "669dd76d-cde8-4dc3-b882-1de566b0c628"
}
}
},
"location": "eastus",
"properties": {
"description": "This is the description of the rule1",
"actionProperties": {
"Email.Sujbect": "my custom email subject"
},
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"
}
],
"criteria": {
"allOf": [
{
"name": "Metric1",
"criterionType": "StaticThresholdCriterion",
"query": "avg({\"system.cpu.utilization\"}) > 90"
}
],
"failingPeriods": {
"for": "PT2M"
},
"odata.type": "Microsoft.Azure.Monitor.PromQLCriteria"
},
"customProperties": {
"key11": "value11",
"key12": "value12"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"resolveConfiguration": {
"autoResolved": true,
"timeToResolve": "PT10M"
},
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/microsoft.compute/virtualMachines/myVmName"
],
"severity": 3
},
"tags": {
"hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": "Resource"
}
}
Create or update a resource centric query based alert rule for Multiple Resources
Sample request
PUT https://management.azure.com/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Insights/metricAlerts/chiricutin?api-version=2024-03-01-preview
{
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {}
}
},
"location": "eastus",
"properties": {
"description": "This is the description of the rule1",
"actionProperties": {
"Email.Sujbect": "my custom email subject"
},
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"
}
],
"criteria": {
"allOf": [
{
"name": "Metric1",
"criterionType": "StaticThresholdCriterion",
"query": "avg({\"system.cpu.utilization\"}) by (\"microsoft.resourceid\") > 90"
}
],
"failingPeriods": {
"for": "PT5M"
},
"odata.type": "Microsoft.Azure.Monitor.PromQLCriteria"
},
"customProperties": {
"key11": "value11",
"key12": "value12"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"resolveConfiguration": {
"autoResolved": true,
"timeToResolve": "PT10M"
},
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7"
],
"severity": 3
},
"tags": {}
}
from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-monitor
# USAGE
python create_or_update_metric_alert_query_multi_resource.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = MonitorManagementClient(
credential=DefaultAzureCredential(),
subscription_id="SUBSCRIPTION_ID",
)
response = client.metric_alerts.create_or_update(
resource_group_name="gigtest",
rule_name="chiricutin",
parameters={
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {}
},
},
"location": "eastus",
"properties": {
"actionProperties": {"Email.Sujbect": "my custom email subject"},
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"
}
],
"criteria": {
"allOf": [
{
"criterionType": "StaticThresholdCriterion",
"name": "Metric1",
"query": 'avg({"system.cpu.utilization"}) by ("microsoft.resourceid") > 90',
}
],
"failingPeriods": {"for": "PT5M"},
"odata.type": "Microsoft.Azure.Monitor.PromQLCriteria",
},
"customProperties": {"key11": "value11", "key12": "value12"},
"description": "This is the description of the rule1",
"enabled": True,
"evaluationFrequency": "PT1M",
"resolveConfiguration": {"autoResolved": True, "timeToResolve": "PT10M"},
"scopes": ["/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7"],
"severity": 3,
},
"tags": {},
},
)
print(response)
# x-ms-original-file: 2024-03-01-preview/createOrUpdateMetricAlertQueryMultiResource.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
package armmonitor_test
import (
"context"
"log"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
)
// Generated from example definition: 2024-03-01-preview/createOrUpdateMetricAlertQueryMultiResource.json
func ExampleMetricAlertsClient_CreateOrUpdate_createOrUpdateAResourceCentricQueryBasedAlertRuleForMultipleResources() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
clientFactory, err := armmonitor.NewClientFactory("14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
res, err := clientFactory.NewMetricAlertsClient().CreateOrUpdate(ctx, "gigtest", "chiricutin", armmonitor.MetricAlertResource{
Identity: &armmonitor.Identity{
Type: to.Ptr(armmonitor.IdentityTypeUserAssigned),
UserAssignedIdentities: map[string]*armmonitor.UserIdentityProperties{
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {},
},
},
Location: to.Ptr("eastus"),
Properties: &armmonitor.MetricAlertProperties{
Description: to.Ptr("This is the description of the rule1"),
ActionProperties: map[string]*string{
"Email.Sujbect": to.Ptr("my custom email subject"),
},
Actions: []*armmonitor.MetricAlertAction{
{
ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
},
},
Criteria: &armmonitor.PromQLCriteria{
AllOf: []armmonitor.MultiPromQLCriteriaClassification{
&armmonitor.StaticPromQLCriteria{
Name: to.Ptr("Metric1"),
CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
Query: to.Ptr("avg({\"system.cpu.utilization\"}) by (\"microsoft.resourceid\") > 90"),
},
},
FailingPeriods: &armmonitor.QueryFailingPeriods{
For: to.Ptr("PT5M"),
},
ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorPromQLCriteria),
},
CustomProperties: map[string]*string{
"key11": to.Ptr("value11"),
"key12": to.Ptr("value12"),
},
Enabled: to.Ptr(true),
EvaluationFrequency: to.Ptr("PT1M"),
ResolveConfiguration: &armmonitor.ResolveConfiguration{
AutoResolved: to.Ptr(true),
TimeToResolve: to.Ptr("PT10M"),
},
Scopes: []*string{
to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7"),
},
Severity: to.Ptr[int32](3),
},
Tags: map[string]*string{},
}, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res = armmonitor.MetricAlertsClientCreateOrUpdateResponse{
// MetricAlertResource: armmonitor.MetricAlertResource{
// Type: to.Ptr("Microsoft.Insights/metricAlerts"),
// ID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/chiricutin"),
// Identity: &armmonitor.Identity{
// Type: to.Ptr(armmonitor.IdentityTypeUserAssigned),
// TenantID: to.Ptr("72f988bf-86f1-41af-91ab-2d7cd011db47"),
// UserAssignedIdentities: map[string]*armmonitor.UserIdentityProperties{
// "/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": &armmonitor.UserIdentityProperties{
// ClientID: to.Ptr("778f4d04-3c60-4622-a839-5cf05866c983"),
// PrincipalID: to.Ptr("669dd76d-cde8-4dc3-b882-1de566b0c628"),
// },
// },
// },
// Location: to.Ptr("eastus"),
// Properties: &armmonitor.MetricAlertProperties{
// Description: to.Ptr("This is the description of the rule1"),
// ActionProperties: map[string]*string{
// "Email.Sujbect": to.Ptr("my custom email subject"),
// },
// Actions: []*armmonitor.MetricAlertAction{
// {
// ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
// },
// },
// Criteria: &armmonitor.PromQLCriteria{
// AllOf: []armmonitor.MultiPromQLCriteriaClassification{
// &armmonitor.StaticPromQLCriteria{
// Name: to.Ptr("Metric1"),
// CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
// Query: to.Ptr("avg({\"system.cpu.utilization\"}) by (\"microsoft.resourceid\") > 90"),
// },
// },
// FailingPeriods: &armmonitor.QueryFailingPeriods{
// For: to.Ptr("PT2M"),
// },
// ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorPromQLCriteria),
// },
// CustomProperties: map[string]*string{
// "key11": to.Ptr("value11"),
// "key12": to.Ptr("value12"),
// },
// Enabled: to.Ptr(true),
// EvaluationFrequency: to.Ptr("PT1M"),
// ResolveConfiguration: &armmonitor.ResolveConfiguration{
// AutoResolved: to.Ptr(true),
// TimeToResolve: to.Ptr("PT10M"),
// },
// Scopes: []*string{
// to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7"),
// },
// Severity: to.Ptr[int32](3),
// },
// Tags: map[string]*string{
// "hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": to.Ptr("Resource"),
// },
// },
// }
}
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
Sample response
{
"type": "Microsoft.Insights/metricAlerts",
"id": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/chiricutin",
"identity": {
"type": "UserAssigned",
"tenantId": "72f988bf-86f1-41af-91ab-2d7cd011db47",
"userAssignedIdentities": {
"/subscriptions/2f1a501a-6e1d-4f37-a445-462d7f8a563d/resourceGroups/AdisTest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/msi-test-euap": {
"clientId": "778f4d04-3c60-4622-a839-5cf05866c983",
"principalId": "669dd76d-cde8-4dc3-b882-1de566b0c628"
}
}
},
"location": "eastus",
"properties": {
"description": "This is the description of the rule1",
"actionProperties": {
"Email.Sujbect": "my custom email subject"
},
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"
}
],
"criteria": {
"allOf": [
{
"name": "Metric1",
"criterionType": "StaticThresholdCriterion",
"query": "avg({\"system.cpu.utilization\"}) by (\"microsoft.resourceid\") > 90"
}
],
"failingPeriods": {
"for": "PT2M"
},
"odata.type": "Microsoft.Azure.Monitor.PromQLCriteria"
},
"customProperties": {
"key11": "value11",
"key12": "value12"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"resolveConfiguration": {
"autoResolved": true,
"timeToResolve": "PT10M"
},
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7"
],
"severity": 3
},
"tags": {
"hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": "Resource"
}
}
Create or update a web test alert rule
Sample request
PUT https://management.azure.com/subscriptions/12345678-1234-1234-1234-123456789101/resourceGroups/rg-example/providers/Microsoft.Insights/metricAlerts/webtest-name-example?api-version=2024-03-01-preview
{
"location": "global",
"properties": {
"description": "Automatically created alert rule for availability test \"component-example\" a",
"actions": [],
"criteria": {
"componentId": "/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/webtest-name-example",
"failedLocationCount": 2,
"odata.type": "Microsoft.Azure.Monitor.WebtestLocationAvailabilityCriteria",
"webTestId": "/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/component-example"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/component-example",
"/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/webtest-name-example"
],
"severity": 4,
"windowSize": "PT15M"
},
"tags": {
"hidden-link:/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/webtest-name-example": "Resource",
"hidden-link:/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/component-example": "Resource"
}
}
from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-monitor
# USAGE
python create_or_update_web_test_metric_alert.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = MonitorManagementClient(
credential=DefaultAzureCredential(),
subscription_id="SUBSCRIPTION_ID",
)
response = client.metric_alerts.create_or_update(
resource_group_name="rg-example",
rule_name="webtest-name-example",
parameters={
"location": "global",
"properties": {
"actions": [],
"criteria": {
"componentId": "/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/webtest-name-example",
"failedLocationCount": 2,
"odata.type": "Microsoft.Azure.Monitor.WebtestLocationAvailabilityCriteria",
"webTestId": "/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/component-example",
},
"description": 'Automatically created alert rule for availability test "component-example" a',
"enabled": True,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/component-example",
"/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/webtest-name-example",
],
"severity": 4,
"windowSize": "PT15M",
},
"tags": {
"hidden-link:/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/webtest-name-example": "Resource",
"hidden-link:/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/component-example": "Resource",
},
},
)
print(response)
# x-ms-original-file: 2024-03-01-preview/createOrUpdateWebTestMetricAlert.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
package armmonitor_test
import (
"context"
"log"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
)
// Generated from example definition: 2024-03-01-preview/createOrUpdateWebTestMetricAlert.json
func ExampleMetricAlertsClient_CreateOrUpdate_createOrUpdateAWebTestAlertRule() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
clientFactory, err := armmonitor.NewClientFactory("12345678-1234-1234-1234-123456789101", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
res, err := clientFactory.NewMetricAlertsClient().CreateOrUpdate(ctx, "rg-example", "webtest-name-example", armmonitor.MetricAlertResource{
Location: to.Ptr("global"),
Properties: &armmonitor.MetricAlertProperties{
Description: to.Ptr("Automatically created alert rule for availability test \"component-example\" a"),
Actions: []*armmonitor.MetricAlertAction{},
Criteria: &armmonitor.WebtestLocationAvailabilityCriteria{
ComponentID: to.Ptr("/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/webtest-name-example"),
FailedLocationCount: to.Ptr[float32](2),
ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorWebtestLocationAvailabilityCriteria),
WebTestID: to.Ptr("/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/component-example"),
},
Enabled: to.Ptr(true),
EvaluationFrequency: to.Ptr("PT1M"),
Scopes: []*string{
to.Ptr("/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/component-example"),
to.Ptr("/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/webtest-name-example"),
},
Severity: to.Ptr[int32](4),
WindowSize: to.Ptr("PT15M"),
},
Tags: map[string]*string{
"hidden-link:/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/webtest-name-example": to.Ptr("Resource"),
"hidden-link:/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/component-example": to.Ptr("Resource"),
},
}, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res = armmonitor.MetricAlertsClientCreateOrUpdateResponse{
// MetricAlertResource: armmonitor.MetricAlertResource{
// Name: to.Ptr("webtest-name-example"),
// Type: to.Ptr("Microsoft.Insights/metricAlerts"),
// ID: to.Ptr("/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/metricalerts/webtest-name-example"),
// Location: to.Ptr("global"),
// Properties: &armmonitor.MetricAlertProperties{
// Description: to.Ptr("Automatically created alert rule for availability test \"webtest-name-example\" a"),
// Actions: []*armmonitor.MetricAlertAction{
// },
// Criteria: &armmonitor.WebtestLocationAvailabilityCriteria{
// ComponentID: to.Ptr("/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/component-example"),
// FailedLocationCount: to.Ptr[float32](2),
// ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorWebtestLocationAvailabilityCriteria),
// WebTestID: to.Ptr("/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/webtest-name-example"),
// },
// Enabled: to.Ptr(true),
// EvaluationFrequency: to.Ptr("PT1M"),
// Scopes: []*string{
// to.Ptr("/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/webtest-name-example"),
// to.Ptr("/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/component-example"),
// },
// Severity: to.Ptr[int32](4),
// WindowSize: to.Ptr("PT15M"),
// },
// Tags: map[string]*string{
// "hidden-link:/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/component-example": to.Ptr("Resource"),
// "hidden-link:/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/webtest-name-example": to.Ptr("Resource"),
// },
// },
// }
}
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
Sample response
{
"name": "webtest-name-example",
"type": "Microsoft.Insights/metricAlerts",
"id": "/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/metricalerts/webtest-name-example",
"location": "global",
"properties": {
"description": "Automatically created alert rule for availability test \"webtest-name-example\" a",
"actions": [],
"criteria": {
"componentId": "/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/component-example",
"failedLocationCount": 2,
"odata.type": "Microsoft.Azure.Monitor.WebtestLocationAvailabilityCriteria",
"webTestId": "/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/webtest-name-example"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/webtest-name-example",
"/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/component-example"
],
"severity": 4,
"windowSize": "PT15M"
},
"tags": {
"hidden-link:/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/component-example": "Resource",
"hidden-link:/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/webtest-name-example": "Resource"
}
}
Create or update an alert rule for Multiple Resource
Sample request
PUT https://management.azure.com/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Insights/metricAlerts/MetricAlertOnMultipleResources?api-version=2024-03-01-preview
{
"location": "global",
"properties": {
"description": "This is the description of the rule1",
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {
"key11": "value11",
"key12": "value12"
}
}
],
"autoMitigate": true,
"criteria": {
"allOf": [
{
"name": "High_CPU_80",
"criterionType": "StaticThresholdCriterion",
"dimensions": [],
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"operator": "GreaterThan",
"threshold": 80.5,
"timeAggregation": "Average"
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme1",
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme2"
],
"severity": 3,
"targetResourceRegion": "southcentralus",
"targetResourceType": "Microsoft.Compute/virtualMachines",
"windowSize": "PT15M"
},
"tags": {}
}
from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-monitor
# USAGE
python create_or_update_metric_alert_multiple_resource.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = MonitorManagementClient(
credential=DefaultAzureCredential(),
subscription_id="SUBSCRIPTION_ID",
)
response = client.metric_alerts.create_or_update(
resource_group_name="gigtest",
rule_name="MetricAlertOnMultipleResources",
parameters={
"location": "global",
"properties": {
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {"key11": "value11", "key12": "value12"},
}
],
"autoMitigate": True,
"criteria": {
"allOf": [
{
"criterionType": "StaticThresholdCriterion",
"dimensions": [],
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"name": "High_CPU_80",
"operator": "GreaterThan",
"threshold": 80.5,
"timeAggregation": "Average",
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria",
},
"description": "This is the description of the rule1",
"enabled": True,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme1",
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme2",
],
"severity": 3,
"targetResourceRegion": "southcentralus",
"targetResourceType": "Microsoft.Compute/virtualMachines",
"windowSize": "PT15M",
},
"tags": {},
},
)
print(response)
# x-ms-original-file: 2024-03-01-preview/createOrUpdateMetricAlertMultipleResource.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
package armmonitor_test
import (
"context"
"log"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
)
// Generated from example definition: 2024-03-01-preview/createOrUpdateMetricAlertMultipleResource.json
func ExampleMetricAlertsClient_CreateOrUpdate_createOrUpdateAnAlertRuleForMultipleResource() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
clientFactory, err := armmonitor.NewClientFactory("14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
res, err := clientFactory.NewMetricAlertsClient().CreateOrUpdate(ctx, "gigtest", "MetricAlertOnMultipleResources", armmonitor.MetricAlertResource{
Location: to.Ptr("global"),
Properties: &armmonitor.MetricAlertProperties{
Description: to.Ptr("This is the description of the rule1"),
Actions: []*armmonitor.MetricAlertAction{
{
ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
WebHookProperties: map[string]*string{
"key11": to.Ptr("value11"),
"key12": to.Ptr("value12"),
},
},
},
AutoMitigate: to.Ptr(true),
Criteria: &armmonitor.MetricAlertMultipleResourceMultipleMetricCriteria{
AllOf: []armmonitor.MultiMetricCriteriaClassification{
&armmonitor.MetricCriteria{
Name: to.Ptr("High_CPU_80"),
CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
Dimensions: []*armmonitor.MetricDimension{},
MetricName: to.Ptr("Percentage CPU"),
MetricNamespace: to.Ptr("microsoft.compute/virtualmachines"),
Operator: to.Ptr(armmonitor.OperatorGreaterThan),
Threshold: to.Ptr[float64](80.5),
TimeAggregation: to.Ptr(armmonitor.AggregationTypeEnumAverage),
},
},
ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorMultipleResourceMultipleMetricCriteria),
},
Enabled: to.Ptr(true),
EvaluationFrequency: to.Ptr("PT1M"),
Scopes: []*string{
to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme1"),
to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme2"),
},
Severity: to.Ptr[int32](3),
TargetResourceRegion: to.Ptr("southcentralus"),
TargetResourceType: to.Ptr("Microsoft.Compute/virtualMachines"),
WindowSize: to.Ptr("PT15M"),
},
Tags: map[string]*string{},
}, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res = armmonitor.MetricAlertsClientCreateOrUpdateResponse{
// MetricAlertResource: armmonitor.MetricAlertResource{
// Type: to.Ptr("Microsoft.Insights/metricAlerts"),
// ID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/MetricAlertOnMultipleResources"),
// Location: to.Ptr("global"),
// Properties: &armmonitor.MetricAlertProperties{
// Description: to.Ptr("This is the description of the rule1"),
// Actions: []*armmonitor.MetricAlertAction{
// {
// ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
// WebHookProperties: map[string]*string{
// "key11": to.Ptr("value11"),
// "key12": to.Ptr("value12"),
// },
// },
// },
// AutoMitigate: to.Ptr(true),
// Criteria: &armmonitor.MetricAlertMultipleResourceMultipleMetricCriteria{
// AllOf: []armmonitor.MultiMetricCriteriaClassification{
// &armmonitor.MetricCriteria{
// Name: to.Ptr("High_CPU_80"),
// CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
// Dimensions: []*armmonitor.MetricDimension{
// },
// MetricName: to.Ptr("Percentage CPU"),
// MetricNamespace: to.Ptr("microsoft.compute/virtualmachines"),
// Operator: to.Ptr(armmonitor.OperatorGreaterThan),
// Threshold: to.Ptr[float64](80.5),
// TimeAggregation: to.Ptr(armmonitor.AggregationTypeEnumAverage),
// },
// },
// ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorMultipleResourceMultipleMetricCriteria),
// },
// Enabled: to.Ptr(true),
// EvaluationFrequency: to.Ptr("PT1M"),
// Scopes: []*string{
// to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme1"),
// to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme2"),
// },
// Severity: to.Ptr[int32](3),
// TargetResourceRegion: to.Ptr("southcentralus"),
// TargetResourceType: to.Ptr("Microsoft.Compute/virtualMachines"),
// WindowSize: to.Ptr("PT15M"),
// },
// Tags: map[string]*string{
// "hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": to.Ptr("Resource"),
// },
// },
// }
}
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
Sample response
{
"type": "Microsoft.Insights/metricAlerts",
"id": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/MetricAlertOnMultipleResources",
"location": "global",
"properties": {
"description": "This is the description of the rule1",
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {
"key11": "value11",
"key12": "value12"
}
}
],
"autoMitigate": true,
"criteria": {
"allOf": [
{
"name": "High_CPU_80",
"criterionType": "StaticThresholdCriterion",
"dimensions": [],
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"operator": "GreaterThan",
"threshold": 80.5,
"timeAggregation": "Average"
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme1",
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme2"
],
"severity": 3,
"targetResourceRegion": "southcentralus",
"targetResourceType": "Microsoft.Compute/virtualMachines",
"windowSize": "PT15M"
},
"tags": {
"hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": "Resource"
}
}
Create or update an alert rule for Single Resource
Sample request
PUT https://management.azure.com/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Insights/metricAlerts/chiricutin?api-version=2024-03-01-preview
{
"location": "global",
"properties": {
"description": "This is the description of the rule1",
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {
"key11": "value11",
"key12": "value12"
}
}
],
"autoMitigate": true,
"criteria": {
"allOf": [
{
"name": "High_CPU_80",
"criterionType": "StaticThresholdCriterion",
"dimensions": [],
"metricName": "\\Processor(_Total)\\% Processor Time",
"operator": "GreaterThan",
"threshold": 80.5,
"timeAggregation": "Average"
}
],
"odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme"
],
"severity": 3,
"windowSize": "PT15M"
},
"tags": {}
}
from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-monitor
# USAGE
python create_or_update_metric_alert_single_resource.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = MonitorManagementClient(
credential=DefaultAzureCredential(),
subscription_id="SUBSCRIPTION_ID",
)
response = client.metric_alerts.create_or_update(
resource_group_name="gigtest",
rule_name="chiricutin",
parameters={
"location": "global",
"properties": {
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {"key11": "value11", "key12": "value12"},
}
],
"autoMitigate": True,
"criteria": {
"allOf": [
{
"criterionType": "StaticThresholdCriterion",
"dimensions": [],
"metricName": "\\Processor(_Total)\\% Processor Time",
"name": "High_CPU_80",
"operator": "GreaterThan",
"threshold": 80.5,
"timeAggregation": "Average",
}
],
"odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria",
},
"description": "This is the description of the rule1",
"enabled": True,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme"
],
"severity": 3,
"windowSize": "PT15M",
},
"tags": {},
},
)
print(response)
# x-ms-original-file: 2024-03-01-preview/createOrUpdateMetricAlertSingleResource.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
package armmonitor_test
import (
"context"
"log"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
)
// Generated from example definition: 2024-03-01-preview/createOrUpdateMetricAlertSingleResource.json
func ExampleMetricAlertsClient_CreateOrUpdate_createOrUpdateAnAlertRuleForSingleResource() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
clientFactory, err := armmonitor.NewClientFactory("14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
res, err := clientFactory.NewMetricAlertsClient().CreateOrUpdate(ctx, "gigtest", "chiricutin", armmonitor.MetricAlertResource{
Location: to.Ptr("global"),
Properties: &armmonitor.MetricAlertProperties{
Description: to.Ptr("This is the description of the rule1"),
Actions: []*armmonitor.MetricAlertAction{
{
ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
WebHookProperties: map[string]*string{
"key11": to.Ptr("value11"),
"key12": to.Ptr("value12"),
},
},
},
AutoMitigate: to.Ptr(true),
Criteria: &armmonitor.MetricAlertSingleResourceMultipleMetricCriteria{
AllOf: []*armmonitor.MetricCriteria{
{
Name: to.Ptr("High_CPU_80"),
CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
Dimensions: []*armmonitor.MetricDimension{},
MetricName: to.Ptr("\\Processor(_Total)\\% Processor Time"),
Operator: to.Ptr(armmonitor.OperatorGreaterThan),
Threshold: to.Ptr[float64](80.5),
TimeAggregation: to.Ptr(armmonitor.AggregationTypeEnumAverage),
},
},
ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorSingleResourceMultipleMetricCriteria),
},
Enabled: to.Ptr(true),
EvaluationFrequency: to.Ptr("PT1M"),
Scopes: []*string{
to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme"),
},
Severity: to.Ptr[int32](3),
WindowSize: to.Ptr("PT15M"),
},
Tags: map[string]*string{},
}, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res = armmonitor.MetricAlertsClientCreateOrUpdateResponse{
// MetricAlertResource: armmonitor.MetricAlertResource{
// Type: to.Ptr("Microsoft.Insights/metricAlerts"),
// ID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/chiricutin"),
// Location: to.Ptr("global"),
// Properties: &armmonitor.MetricAlertProperties{
// Description: to.Ptr("This is the description of the rule1"),
// Actions: []*armmonitor.MetricAlertAction{
// {
// ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
// WebHookProperties: map[string]*string{
// "key11": to.Ptr("value11"),
// "key12": to.Ptr("value12"),
// },
// },
// },
// AutoMitigate: to.Ptr(true),
// Criteria: &armmonitor.MetricAlertSingleResourceMultipleMetricCriteria{
// AllOf: []*armmonitor.MetricCriteria{
// {
// Name: to.Ptr("High_CPU_80"),
// CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
// Dimensions: []*armmonitor.MetricDimension{
// },
// MetricName: to.Ptr("\\Processor(_Total)\\% Processor Time"),
// Operator: to.Ptr(armmonitor.OperatorGreaterThan),
// Threshold: to.Ptr[float64](80.5),
// TimeAggregation: to.Ptr(armmonitor.AggregationTypeEnumAverage),
// },
// },
// ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorSingleResourceMultipleMetricCriteria),
// },
// Enabled: to.Ptr(true),
// EvaluationFrequency: to.Ptr("PT1M"),
// Scopes: []*string{
// to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme"),
// },
// Severity: to.Ptr[int32](3),
// WindowSize: to.Ptr("PT15M"),
// },
// Tags: map[string]*string{
// "hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": to.Ptr("Resource"),
// },
// },
// }
}
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
Sample response
{
"type": "Microsoft.Insights/metricAlerts",
"id": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/chiricutin",
"location": "global",
"properties": {
"description": "This is the description of the rule1",
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {
"key11": "value11",
"key12": "value12"
}
}
],
"autoMitigate": true,
"criteria": {
"allOf": [
{
"name": "High_CPU_80",
"criterionType": "StaticThresholdCriterion",
"dimensions": [],
"metricName": "\\Processor(_Total)\\% Processor Time",
"operator": "GreaterThan",
"threshold": 80.5,
"timeAggregation": "Average"
}
],
"odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme"
],
"severity": 3,
"windowSize": "PT15M"
},
"tags": {
"hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": "Resource"
}
}
Create or update an alert rule on Resource group(s)
Sample request
PUT https://management.azure.com/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest1/providers/Microsoft.Insights/metricAlerts/MetricAlertAtResourceGroupLevel?api-version=2024-03-01-preview
{
"location": "global",
"properties": {
"description": "This is the description of the rule1",
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {
"key11": "value11",
"key12": "value12"
}
}
],
"autoMitigate": true,
"criteria": {
"allOf": [
{
"name": "High_CPU_80",
"criterionType": "StaticThresholdCriterion",
"dimensions": [],
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"operator": "GreaterThan",
"threshold": 80.5,
"timeAggregation": "Average"
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest1",
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest2"
],
"severity": 3,
"targetResourceRegion": "southcentralus",
"targetResourceType": "Microsoft.Compute/virtualMachines",
"windowSize": "PT15M"
},
"tags": {}
}
from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-monitor
# USAGE
python create_or_update_metric_alert_resource_group.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = MonitorManagementClient(
credential=DefaultAzureCredential(),
subscription_id="SUBSCRIPTION_ID",
)
response = client.metric_alerts.create_or_update(
resource_group_name="gigtest1",
rule_name="MetricAlertAtResourceGroupLevel",
parameters={
"location": "global",
"properties": {
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {"key11": "value11", "key12": "value12"},
}
],
"autoMitigate": True,
"criteria": {
"allOf": [
{
"criterionType": "StaticThresholdCriterion",
"dimensions": [],
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"name": "High_CPU_80",
"operator": "GreaterThan",
"threshold": 80.5,
"timeAggregation": "Average",
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria",
},
"description": "This is the description of the rule1",
"enabled": True,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest1",
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest2",
],
"severity": 3,
"targetResourceRegion": "southcentralus",
"targetResourceType": "Microsoft.Compute/virtualMachines",
"windowSize": "PT15M",
},
"tags": {},
},
)
print(response)
# x-ms-original-file: 2024-03-01-preview/createOrUpdateMetricAlertResourceGroup.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
package armmonitor_test
import (
"context"
"log"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
)
// Generated from example definition: 2024-03-01-preview/createOrUpdateMetricAlertResourceGroup.json
func ExampleMetricAlertsClient_CreateOrUpdate_createOrUpdateAnAlertRuleOnResourceGroupS() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
clientFactory, err := armmonitor.NewClientFactory("14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
res, err := clientFactory.NewMetricAlertsClient().CreateOrUpdate(ctx, "gigtest1", "MetricAlertAtResourceGroupLevel", armmonitor.MetricAlertResource{
Location: to.Ptr("global"),
Properties: &armmonitor.MetricAlertProperties{
Description: to.Ptr("This is the description of the rule1"),
Actions: []*armmonitor.MetricAlertAction{
{
ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
WebHookProperties: map[string]*string{
"key11": to.Ptr("value11"),
"key12": to.Ptr("value12"),
},
},
},
AutoMitigate: to.Ptr(true),
Criteria: &armmonitor.MetricAlertMultipleResourceMultipleMetricCriteria{
AllOf: []armmonitor.MultiMetricCriteriaClassification{
&armmonitor.MetricCriteria{
Name: to.Ptr("High_CPU_80"),
CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
Dimensions: []*armmonitor.MetricDimension{},
MetricName: to.Ptr("Percentage CPU"),
MetricNamespace: to.Ptr("microsoft.compute/virtualmachines"),
Operator: to.Ptr(armmonitor.OperatorGreaterThan),
Threshold: to.Ptr[float64](80.5),
TimeAggregation: to.Ptr(armmonitor.AggregationTypeEnumAverage),
},
},
ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorMultipleResourceMultipleMetricCriteria),
},
Enabled: to.Ptr(true),
EvaluationFrequency: to.Ptr("PT1M"),
Scopes: []*string{
to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest1"),
to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest2"),
},
Severity: to.Ptr[int32](3),
TargetResourceRegion: to.Ptr("southcentralus"),
TargetResourceType: to.Ptr("Microsoft.Compute/virtualMachines"),
WindowSize: to.Ptr("PT15M"),
},
Tags: map[string]*string{},
}, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res = armmonitor.MetricAlertsClientCreateOrUpdateResponse{
// MetricAlertResource: armmonitor.MetricAlertResource{
// Type: to.Ptr("Microsoft.Insights/metricAlerts"),
// ID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest1/providers/providers/microsoft.insights/metricalerts/MetricAlertAtResourceGroupLevel"),
// Location: to.Ptr("global"),
// Properties: &armmonitor.MetricAlertProperties{
// Description: to.Ptr("This is the description of the rule1"),
// Actions: []*armmonitor.MetricAlertAction{
// {
// ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
// WebHookProperties: map[string]*string{
// "key11": to.Ptr("value11"),
// "key12": to.Ptr("value12"),
// },
// },
// },
// AutoMitigate: to.Ptr(true),
// Criteria: &armmonitor.MetricAlertMultipleResourceMultipleMetricCriteria{
// AllOf: []armmonitor.MultiMetricCriteriaClassification{
// &armmonitor.MetricCriteria{
// Name: to.Ptr("High_CPU_80"),
// CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
// Dimensions: []*armmonitor.MetricDimension{
// },
// MetricName: to.Ptr("Percentage CPU"),
// MetricNamespace: to.Ptr("microsoft.compute/virtualmachines"),
// Operator: to.Ptr(armmonitor.OperatorGreaterThan),
// Threshold: to.Ptr[float64](80.5),
// TimeAggregation: to.Ptr(armmonitor.AggregationTypeEnumAverage),
// },
// },
// ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorMultipleResourceMultipleMetricCriteria),
// },
// Enabled: to.Ptr(true),
// EvaluationFrequency: to.Ptr("PT1M"),
// Scopes: []*string{
// to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest1"),
// to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest2"),
// },
// Severity: to.Ptr[int32](3),
// TargetResourceRegion: to.Ptr("southcentralus"),
// TargetResourceType: to.Ptr("Microsoft.Compute/virtualMachines"),
// WindowSize: to.Ptr("PT15M"),
// },
// Tags: map[string]*string{
// "hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": to.Ptr("Resource"),
// },
// },
// }
}
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
Sample response
{
"type": "Microsoft.Insights/metricAlerts",
"id": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest1/providers/providers/microsoft.insights/metricalerts/MetricAlertAtResourceGroupLevel",
"location": "global",
"properties": {
"description": "This is the description of the rule1",
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {
"key11": "value11",
"key12": "value12"
}
}
],
"autoMitigate": true,
"criteria": {
"allOf": [
{
"name": "High_CPU_80",
"criterionType": "StaticThresholdCriterion",
"dimensions": [],
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"operator": "GreaterThan",
"threshold": 80.5,
"timeAggregation": "Average"
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest1",
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest2"
],
"severity": 3,
"targetResourceRegion": "southcentralus",
"targetResourceType": "Microsoft.Compute/virtualMachines",
"windowSize": "PT15M"
},
"tags": {
"hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": "Resource"
}
}
Create or update an alert rule on Subscription
Sample request
PUT https://management.azure.com/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Insights/metricAlerts/MetricAlertAtSubscriptionLevel?api-version=2024-03-01-preview
{
"location": "global",
"properties": {
"description": "This is the description of the rule1",
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {
"key11": "value11",
"key12": "value12"
}
}
],
"autoMitigate": true,
"criteria": {
"allOf": [
{
"name": "High_CPU_80",
"criterionType": "StaticThresholdCriterion",
"dimensions": [],
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"operator": "GreaterThan",
"threshold": 80.5,
"timeAggregation": "Average"
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7"
],
"severity": 3,
"targetResourceRegion": "southcentralus",
"targetResourceType": "Microsoft.Compute/virtualMachines",
"windowSize": "PT15M"
},
"tags": {}
}
from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-monitor
# USAGE
python create_or_update_metric_alert_subscription.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = MonitorManagementClient(
credential=DefaultAzureCredential(),
subscription_id="SUBSCRIPTION_ID",
)
response = client.metric_alerts.create_or_update(
resource_group_name="gigtest",
rule_name="MetricAlertAtSubscriptionLevel",
parameters={
"location": "global",
"properties": {
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {"key11": "value11", "key12": "value12"},
}
],
"autoMitigate": True,
"criteria": {
"allOf": [
{
"criterionType": "StaticThresholdCriterion",
"dimensions": [],
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"name": "High_CPU_80",
"operator": "GreaterThan",
"threshold": 80.5,
"timeAggregation": "Average",
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria",
},
"description": "This is the description of the rule1",
"enabled": True,
"evaluationFrequency": "PT1M",
"scopes": ["/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7"],
"severity": 3,
"targetResourceRegion": "southcentralus",
"targetResourceType": "Microsoft.Compute/virtualMachines",
"windowSize": "PT15M",
},
"tags": {},
},
)
print(response)
# x-ms-original-file: 2024-03-01-preview/createOrUpdateMetricAlertSubscription.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
package armmonitor_test
import (
"context"
"log"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
)
// Generated from example definition: 2024-03-01-preview/createOrUpdateMetricAlertSubscription.json
func ExampleMetricAlertsClient_CreateOrUpdate_createOrUpdateAnAlertRuleOnSubscription() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
clientFactory, err := armmonitor.NewClientFactory("14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
res, err := clientFactory.NewMetricAlertsClient().CreateOrUpdate(ctx, "gigtest", "MetricAlertAtSubscriptionLevel", armmonitor.MetricAlertResource{
Location: to.Ptr("global"),
Properties: &armmonitor.MetricAlertProperties{
Description: to.Ptr("This is the description of the rule1"),
Actions: []*armmonitor.MetricAlertAction{
{
ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
WebHookProperties: map[string]*string{
"key11": to.Ptr("value11"),
"key12": to.Ptr("value12"),
},
},
},
AutoMitigate: to.Ptr(true),
Criteria: &armmonitor.MetricAlertMultipleResourceMultipleMetricCriteria{
AllOf: []armmonitor.MultiMetricCriteriaClassification{
&armmonitor.MetricCriteria{
Name: to.Ptr("High_CPU_80"),
CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
Dimensions: []*armmonitor.MetricDimension{},
MetricName: to.Ptr("Percentage CPU"),
MetricNamespace: to.Ptr("microsoft.compute/virtualmachines"),
Operator: to.Ptr(armmonitor.OperatorGreaterThan),
Threshold: to.Ptr[float64](80.5),
TimeAggregation: to.Ptr(armmonitor.AggregationTypeEnumAverage),
},
},
ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorMultipleResourceMultipleMetricCriteria),
},
Enabled: to.Ptr(true),
EvaluationFrequency: to.Ptr("PT1M"),
Scopes: []*string{
to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7"),
},
Severity: to.Ptr[int32](3),
TargetResourceRegion: to.Ptr("southcentralus"),
TargetResourceType: to.Ptr("Microsoft.Compute/virtualMachines"),
WindowSize: to.Ptr("PT15M"),
},
Tags: map[string]*string{},
}, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res = armmonitor.MetricAlertsClientCreateOrUpdateResponse{
// MetricAlertResource: armmonitor.MetricAlertResource{
// Type: to.Ptr("Microsoft.Insights/metricAlerts"),
// ID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/MetricAlertAtSubscriptionLevel"),
// Location: to.Ptr("global"),
// Properties: &armmonitor.MetricAlertProperties{
// Description: to.Ptr("This is the description of the rule1"),
// Actions: []*armmonitor.MetricAlertAction{
// {
// ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
// WebHookProperties: map[string]*string{
// "key11": to.Ptr("value11"),
// "key12": to.Ptr("value12"),
// },
// },
// },
// AutoMitigate: to.Ptr(true),
// Criteria: &armmonitor.MetricAlertMultipleResourceMultipleMetricCriteria{
// AllOf: []armmonitor.MultiMetricCriteriaClassification{
// &armmonitor.MetricCriteria{
// Name: to.Ptr("High_CPU_80"),
// CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
// Dimensions: []*armmonitor.MetricDimension{
// },
// MetricName: to.Ptr("Percentage CPU"),
// MetricNamespace: to.Ptr("microsoft.compute/virtualmachines"),
// Operator: to.Ptr(armmonitor.OperatorGreaterThan),
// Threshold: to.Ptr[float64](80.5),
// TimeAggregation: to.Ptr(armmonitor.AggregationTypeEnumAverage),
// },
// },
// ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorMultipleResourceMultipleMetricCriteria),
// },
// Enabled: to.Ptr(true),
// EvaluationFrequency: to.Ptr("PT1M"),
// Scopes: []*string{
// to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7"),
// },
// Severity: to.Ptr[int32](3),
// TargetResourceRegion: to.Ptr("southcentralus"),
// TargetResourceType: to.Ptr("Microsoft.Compute/virtualMachines"),
// WindowSize: to.Ptr("PT15M"),
// },
// Tags: map[string]*string{
// "hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": to.Ptr("Resource"),
// },
// },
// }
}
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
Sample response
{
"type": "Microsoft.Insights/metricAlerts",
"id": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/MetricAlertAtSubscriptionLevel",
"location": "global",
"properties": {
"description": "This is the description of the rule1",
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {
"key11": "value11",
"key12": "value12"
}
}
],
"autoMitigate": true,
"criteria": {
"allOf": [
{
"name": "High_CPU_80",
"criterionType": "StaticThresholdCriterion",
"dimensions": [],
"metricName": "Percentage CPU",
"metricNamespace": "microsoft.compute/virtualmachines",
"operator": "GreaterThan",
"threshold": 80.5,
"timeAggregation": "Average"
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria"
},
"enabled": true,
"evaluationFrequency": "PT1M",
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7"
],
"severity": 3,
"targetResourceRegion": "southcentralus",
"targetResourceType": "Microsoft.Compute/virtualMachines",
"windowSize": "PT15M"
},
"tags": {
"hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": "Resource"
}
}
Create or update an alert rules with dimensions
Sample request
PUT https://management.azure.com/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Insights/metricAlerts/MetricAlertOnMultipleDimensions?api-version=2024-03-01-preview
{
"location": "global",
"properties": {
"description": "This is the description of the rule1",
"actions": [
{
"actionGroupId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {
"key11": "value11",
"key12": "value12"
}
}
],
"autoMitigate": true,
"criteria": {
"allOf": [
{
"name": "Metric1",
"criterionType": "StaticThresholdCriterion",
"dimensions": [
{
"name": "ActivityName",
"operator": "Include",
"values": [
"*"
]
},
{
"name": "StatusCode",
"operator": "Include",
"values": [
"200"
]
}
],
"metricName": "Availability",
"metricNamespace": "Microsoft.KeyVault/vaults",
"operator": "GreaterThan",
"threshold": 55,
"timeAggregation": "Average"
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria"
},
"enabled": true,
"evaluationFrequency": "PT1H",
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.KeyVault/vaults/keyVaultResource"
],
"severity": 3,
"windowSize": "P1D"
},
"tags": {}
}
from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-monitor
# USAGE
python create_or_update_metric_alert_with_dimensions.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = MonitorManagementClient(
credential=DefaultAzureCredential(),
subscription_id="SUBSCRIPTION_ID",
)
response = client.metric_alerts.create_or_update(
resource_group_name="gigtest",
rule_name="MetricAlertOnMultipleDimensions",
parameters={
"location": "global",
"properties": {
"actions": [
{
"actionGroupId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {"key11": "value11", "key12": "value12"},
}
],
"autoMitigate": True,
"criteria": {
"allOf": [
{
"criterionType": "StaticThresholdCriterion",
"dimensions": [
{"name": "ActivityName", "operator": "Include", "values": ["*"]},
{"name": "StatusCode", "operator": "Include", "values": ["200"]},
],
"metricName": "Availability",
"metricNamespace": "Microsoft.KeyVault/vaults",
"name": "Metric1",
"operator": "GreaterThan",
"threshold": 55,
"timeAggregation": "Average",
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria",
},
"description": "This is the description of the rule1",
"enabled": True,
"evaluationFrequency": "PT1H",
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.KeyVault/vaults/keyVaultResource"
],
"severity": 3,
"windowSize": "P1D",
},
"tags": {},
},
)
print(response)
# x-ms-original-file: 2024-03-01-preview/createOrUpdateMetricAlertWithDimensions.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
package armmonitor_test
import (
"context"
"log"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
)
// Generated from example definition: 2024-03-01-preview/createOrUpdateMetricAlertWithDimensions.json
func ExampleMetricAlertsClient_CreateOrUpdate_createOrUpdateAnAlertRulesWithDimensions() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
clientFactory, err := armmonitor.NewClientFactory("14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
res, err := clientFactory.NewMetricAlertsClient().CreateOrUpdate(ctx, "gigtest", "MetricAlertOnMultipleDimensions", armmonitor.MetricAlertResource{
Location: to.Ptr("global"),
Properties: &armmonitor.MetricAlertProperties{
Description: to.Ptr("This is the description of the rule1"),
Actions: []*armmonitor.MetricAlertAction{
{
ActionGroupID: to.Ptr("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
WebHookProperties: map[string]*string{
"key11": to.Ptr("value11"),
"key12": to.Ptr("value12"),
},
},
},
AutoMitigate: to.Ptr(true),
Criteria: &armmonitor.MetricAlertMultipleResourceMultipleMetricCriteria{
AllOf: []armmonitor.MultiMetricCriteriaClassification{
&armmonitor.MetricCriteria{
Name: to.Ptr("Metric1"),
CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
Dimensions: []*armmonitor.MetricDimension{
{
Name: to.Ptr("ActivityName"),
Operator: to.Ptr("Include"),
Values: []*string{
to.Ptr("*"),
},
},
{
Name: to.Ptr("StatusCode"),
Operator: to.Ptr("Include"),
Values: []*string{
to.Ptr("200"),
},
},
},
MetricName: to.Ptr("Availability"),
MetricNamespace: to.Ptr("Microsoft.KeyVault/vaults"),
Operator: to.Ptr(armmonitor.OperatorGreaterThan),
Threshold: to.Ptr[float64](55),
TimeAggregation: to.Ptr(armmonitor.AggregationTypeEnumAverage),
},
},
ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorMultipleResourceMultipleMetricCriteria),
},
Enabled: to.Ptr(true),
EvaluationFrequency: to.Ptr("PT1H"),
Scopes: []*string{
to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.KeyVault/vaults/keyVaultResource"),
},
Severity: to.Ptr[int32](3),
WindowSize: to.Ptr("P1D"),
},
Tags: map[string]*string{},
}, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res = armmonitor.MetricAlertsClientCreateOrUpdateResponse{
// MetricAlertResource: armmonitor.MetricAlertResource{
// Type: to.Ptr("Microsoft.Insights/metricAlerts"),
// ID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/MetricAlertWithDimensions"),
// Location: to.Ptr("global"),
// Properties: &armmonitor.MetricAlertProperties{
// Description: to.Ptr("This is the description of the rule1"),
// Actions: []*armmonitor.MetricAlertAction{
// {
// ActionGroupID: to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2"),
// WebHookProperties: map[string]*string{
// "key11": to.Ptr("value11"),
// "key12": to.Ptr("value12"),
// },
// },
// },
// AutoMitigate: to.Ptr(true),
// Criteria: &armmonitor.MetricAlertMultipleResourceMultipleMetricCriteria{
// AllOf: []armmonitor.MultiMetricCriteriaClassification{
// &armmonitor.MetricCriteria{
// Name: to.Ptr("Metric1"),
// CriterionType: to.Ptr(armmonitor.CriterionTypeStaticThresholdCriterion),
// Dimensions: []*armmonitor.MetricDimension{
// {
// Name: to.Ptr("ActivityName"),
// Operator: to.Ptr("Include"),
// Values: []*string{
// to.Ptr("*"),
// },
// },
// {
// Name: to.Ptr("StatusCode"),
// Operator: to.Ptr("Include"),
// Values: []*string{
// to.Ptr("200"),
// },
// },
// },
// MetricName: to.Ptr("Availability"),
// MetricNamespace: to.Ptr("Microsoft.KeyVault/vaults"),
// Operator: to.Ptr(armmonitor.OperatorGreaterThan),
// Threshold: to.Ptr[float64](55),
// TimeAggregation: to.Ptr(armmonitor.AggregationTypeEnumAverage),
// },
// },
// ODataType: to.Ptr(armmonitor.OdatatypeMicrosoftAzureMonitorMultipleResourceMultipleMetricCriteria),
// },
// Enabled: to.Ptr(true),
// EvaluationFrequency: to.Ptr("PT1H"),
// Scopes: []*string{
// to.Ptr("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.KeyVault/vaults/keyVaultResource"),
// },
// Severity: to.Ptr[int32](3),
// TargetResourceRegion: to.Ptr("southcentralus"),
// TargetResourceType: to.Ptr("Microsoft.KeyVault/vaults"),
// WindowSize: to.Ptr("P1D"),
// },
// Tags: map[string]*string{
// "hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": to.Ptr("Resource"),
// },
// },
// }
}
To use the Azure SDK library in your project, see this documentation. To provide feedback on this code sample, open a GitHub issue
Sample response
{
"type": "Microsoft.Insights/metricAlerts",
"id": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/providers/microsoft.insights/metricalerts/MetricAlertWithDimensions",
"location": "global",
"properties": {
"description": "This is the description of the rule1",
"actions": [
{
"actionGroupId": "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2",
"webHookProperties": {
"key11": "value11",
"key12": "value12"
}
}
],
"autoMitigate": true,
"criteria": {
"allOf": [
{
"name": "Metric1",
"criterionType": "StaticThresholdCriterion",
"dimensions": [
{
"name": "ActivityName",
"operator": "Include",
"values": [
"*"
]
},
{
"name": "StatusCode",
"operator": "Include",
"values": [
"200"
]
}
],
"metricName": "Availability",
"metricNamespace": "Microsoft.KeyVault/vaults",
"operator": "GreaterThan",
"threshold": 55,
"timeAggregation": "Average"
}
],
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria"
},
"enabled": true,
"evaluationFrequency": "PT1H",
"scopes": [
"/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.KeyVault/vaults/keyVaultResource"
],
"severity": 3,
"targetResourceRegion": "southcentralus",
"targetResourceType": "Microsoft.KeyVault/vaults",
"windowSize": "P1D"
},
"tags": {
"hidden-link:/subscriptions/b67f7fec-69fc-4974-9099-a26bd6ffeda3/resourceGroups/Rac46PostSwapRG/providers/Microsoft.Web/sites/leoalerttest": "Resource"
}
}
Definitions
AggregationTypeEnum
Enumeration
The criteria time aggregation types. Previously undocumented values might be returned
| Value |
Description |
|
Average
|
Average
|
|
Count
|
Count
|
|
Minimum
|
Minimum
|
|
Maximum
|
Maximum
|
|
Total
|
Total
|
createdByType
Enumeration
The type of identity that created the resource.
| Value |
Description |
|
User
|
|
Application
|
|
ManagedIdentity
|
|
Key
|
DynamicMetricCriteria
Object
Criterion for dynamic threshold.
| Name |
Type |
Description |
|
alertSensitivity
|
DynamicThresholdSensitivity
|
The extent of deviation required to trigger an alert. This will affect how tight the threshold is to the metric series pattern. Previously undocumented values might be returned
|
|
criterionType
|
string:
DynamicThresholdCriterion
|
Specifies the type of threshold criteria. Previously undocumented values might be returned
|
|
dimensions
|
MetricDimension[]
|
List of dimension conditions.
|
|
failingPeriods
|
DynamicThresholdFailingPeriods
|
The minimum number of violations required within the selected lookback time window required to raise an alert.
|
|
ignoreDataBefore
|
string
(date-time)
|
Use this option to set the date from which to start learning the metric historical data and calculate the dynamic thresholds (in ISO8601 format)
|
|
metricName
|
string
|
Name of the metric.
|
|
metricNamespace
|
string
|
Namespace of the metric.
|
|
name
|
string
|
Name of the criteria.
|
|
operator
|
DynamicThresholdOperator
|
The operator used to compare the metric value against the threshold. Previously undocumented values might be returned
|
|
skipMetricValidation
|
boolean
|
Allows creating an alert rule on a custom metric that isn't yet emitted, by causing the metric validation to be skipped.
|
|
timeAggregation
|
AggregationTypeEnum
|
The criteria time aggregation types. Previously undocumented values might be returned
|
DynamicPromQLCriteria
Object
The criterion for dynamic prom query.
| Name |
Type |
Description |
|
alertSensitivity
|
DynamicThresholdSensitivity
|
The extent of deviation required to trigger an alert. This will affect how tight the threshold is to the metric series pattern. Previously undocumented values might be returned
|
|
criterionType
|
string:
DynamicThresholdCriterion
|
Specifies the type of threshold criteria. Previously undocumented values might be returned
|
|
ignoreDataBefore
|
string
(date-time)
|
Use this option to set the date from which to start learning the metric historical data and calculate the dynamic thresholds (in ISO8601 format)
|
|
name
|
string
|
Name of the criteria.
|
|
operator
|
DynamicThresholdOperator
|
The operator used to compare the metric value against the threshold. Previously undocumented values might be returned
|
|
query
|
string
|
The query used to evaluate the alert rule
|
DynamicThresholdFailingPeriods
Object
The minimum number of violations required within the selected lookback time window required to raise an alert.
| Name |
Type |
Description |
|
minFailingPeriodsToAlert
|
number
(float)
|
The number of violations to trigger an alert. Should be smaller or equal to numberOfEvaluationPeriods.
|
|
numberOfEvaluationPeriods
|
number
(float)
|
The number of aggregated lookback points. The lookback time window is calculated based on the aggregation granularity (windowSize) and the selected number of aggregated points.
|
DynamicThresholdOperator
Enumeration
The operator used to compare the metric value against the threshold. Previously undocumented values might be returned
| Value |
Description |
|
GreaterThan
|
GreaterThan
|
|
LessThan
|
LessThan
|
|
GreaterOrLessThan
|
GreaterOrLessThan
|
DynamicThresholdSensitivity
Enumeration
The extent of deviation required to trigger an alert. This will affect how tight the threshold is to the metric series pattern. Previously undocumented values might be returned
| Value |
Description |
|
Low
|
Low
|
|
Medium
|
Medium
|
|
High
|
High
|
IdentityType
Enumeration
Type of managed service identity.
| Value |
Description |
|
SystemAssigned
|
SystemAssigned
|
|
UserAssigned
|
UserAssigned
|
|
None
|
None
|
MetricAlertAction
Object
An alert action.
| Name |
Type |
Description |
|
actionGroupId
|
string
|
The id of the action group to use.
|
|
webHookProperties
|
object
|
This field allows specifying custom properties, which would be appended to the alert payload sent as input to the webhook.
|
MetricAlertErrorResponse
Object
Describes the format of Error response.
MetricAlertMultipleResourceMultipleMetricCriteria
Object
Specifies the metric alert criteria for multiple resource that has multiple metric criteria.
| Name |
Type |
Description |
|
allOf
|
MultiMetricCriteria[]:
|
The list of multiple metric criteria for this 'all of' operation.
|
|
odata.type
|
string:
Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria
|
Specifies the type of the alert criteria. Previously undocumented values might be returned
|
MetricAlertResource
Object
The metric alert resource.
| Name |
Type |
Description |
|
id
|
string
|
Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}
|
|
identity
|
Microsoft.Common.Identity
|
The identity of the resource.
|
|
location
|
string
|
The geo-location where the resource lives
|
|
name
|
string
|
The name of the resource
|
|
properties.actionProperties
|
object
|
The properties of an action properties.
|
|
properties.actions
|
MetricAlertAction[]
|
The array of actions that are performed when the alert rule becomes active, and when an alert condition is resolved.
|
|
properties.autoMitigate
|
boolean
|
The flag that indicates whether the alert should be auto resolved or not. The default is true.
|
|
properties.criteria
|
MetricAlertCriteria:
|
Defines the specific alert criteria information.
|
|
properties.customProperties
|
object
|
The properties of an alert payload.
|
|
properties.description
|
string
|
The description of the metric alert that will be included in the alert email.
|
|
properties.enabled
|
boolean
|
The flag that indicates whether the metric alert is enabled.
|
|
properties.evaluationFrequency
|
string
(duration)
|
How often the metric alert is evaluated represented in ISO 8601 duration format.
|
|
properties.isMigrated
|
boolean
|
The value indicating whether this alert rule is migrated.
|
|
properties.lastUpdatedTime
|
string
(date-time)
|
Last time the rule was updated in ISO8601 format.
|
|
properties.resolveConfiguration
|
ResolveConfiguration
|
The configuration for how the alert is resolved. Applicable for PromQLCriteria.
|
|
properties.scopes
|
string[]
|
The list of resource id's that this metric alert is scoped to. You cannot change the scope of a metric rule based on logs.
|
|
properties.severity
|
integer
(int32)
|
Alert severity {0, 1, 2, 3, 4}
|
|
properties.targetResourceRegion
|
string
|
The region of the target resource(s) on which the alert is created/updated. Mandatory if the scope contains a subscription, resource group, or more than one resource.
|
|
properties.targetResourceType
|
string
|
The resource type of the target resource(s) on which the alert is created/updated. Mandatory if the scope contains a subscription, resource group, or more than one resource.
|
|
properties.windowSize
|
string
(duration)
|
The period of time (in ISO 8601 duration format) that is used to monitor alert activity based on the threshold.
|
|
systemData
|
systemData
|
Azure Resource Manager metadata containing createdBy and modifiedBy information.
|
|
tags
|
object
|
Resource tags.
|
|
type
|
string
|
The type of the resource. E.g. "Microsoft.Compute/virtualMachines" or "Microsoft.Storage/storageAccounts"
|
MetricAlertSingleResourceMultipleMetricCriteria
Object
Specifies the metric alert criteria for a single resource that has multiple metric criteria.
| Name |
Type |
Description |
|
allOf
|
MetricCriteria[]
|
The list of metric criteria for this 'all of' operation.
|
|
odata.type
|
string:
Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria
|
Specifies the type of the alert criteria. Previously undocumented values might be returned
|
MetricCriteria
Object
Criterion to filter metrics.
| Name |
Type |
Description |
|
criterionType
|
string:
StaticThresholdCriterion
|
Specifies the type of threshold criteria. Previously undocumented values might be returned
|
|
dimensions
|
MetricDimension[]
|
List of dimension conditions.
|
|
metricName
|
string
|
Name of the metric.
|
|
metricNamespace
|
string
|
Namespace of the metric.
|
|
name
|
string
|
Name of the criteria.
|
|
operator
|
Operator
|
The criteria operator. Previously undocumented values might be returned
|
|
skipMetricValidation
|
boolean
|
Allows creating an alert rule on a custom metric that isn't yet emitted, by causing the metric validation to be skipped.
|
|
threshold
|
number
(double)
|
The criteria threshold value that activates the alert.
|
|
timeAggregation
|
AggregationTypeEnum
|
The criteria time aggregation types. Previously undocumented values might be returned
|
MetricDimension
Object
Specifies a metric dimension.
| Name |
Type |
Description |
|
name
|
string
|
Name of the dimension.
|
|
operator
|
string
|
The dimension operator. Only 'Include' and 'Exclude' are supported
|
|
values
|
string[]
|
List of dimension values.
|
Microsoft.Common.ErrorDetail
Object
Describes details of an error response.
| Name |
Type |
Description |
|
additionalInfo
|
Microsoft.Common.ErrorDetailAdditionalInfoItem[]
|
An array of objects with 'type' and 'info' properties. The schema of 'info' is service-specific and dependent on the 'type' string.
|
|
code
|
string
|
Unlocalized string which can be used to programmatically identify the error.
|
|
message
|
string
|
Describes the error in detail and provides debugging information.
|
|
target
|
string
|
The target of the particular error (for example, the name of the property in error).
|
Microsoft.Common.ErrorDetailAdditionalInfoItem
Object
| Name |
Type |
Description |
|
info
|
The additional information specific to the type.
|
|
type
|
string
|
The type of additional information.
|
Microsoft.Common.ErrorResponseError
Object
| Name |
Type |
Description |
|
additionalInfo
|
Microsoft.Common.ErrorResponseErrorAdditionalInfoItem[]
|
An array of objects with 'type' and 'info' properties. The schema of 'info' is service-specific and dependent on the 'type' string.
|
|
code
|
string
|
Unlocalized string which can be used to programmatically identify the error.
|
|
details
|
Microsoft.Common.ErrorDetail[]
|
An array of additional nested error response info objects, as described by this contract.
|
|
message
|
string
|
Describes the error in detail and provides debugging information. If Accept-Language is set in the request, it must be localized to that language.
|
|
target
|
string
|
The target of the particular error (for example, the name of the property in error).
|
Microsoft.Common.ErrorResponseErrorAdditionalInfoItem
Object
| Name |
Type |
Description |
|
info
|
The additional information specific to the type.
|
|
type
|
string
|
The type of additional information.
|
Microsoft.Common.Identity
Object
Identity for the resource.
| Name |
Type |
Description |
|
principalId
|
string
|
The principal ID of resource identity.
|
|
tenantId
|
string
|
The tenant ID of resource.
|
|
type
|
IdentityType
|
Type of managed service identity.
|
|
userAssignedIdentities
|
<string,
Microsoft.Common.UserIdentityProperties>
|
The list of user identities associated with the resource. The user identity dictionary key references will be ARM resource ids in the form: '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identityName}'.
|
Microsoft.Common.UserIdentityProperties
Object
Properties of the user assigned identity.
| Name |
Type |
Description |
|
clientId
|
string
|
The client ID of resource identity.
|
|
principalId
|
string
|
The principal ID of resource identity.
|
Odatatype
Enumeration
Specifies the type of the alert criteria. Previously undocumented values might be returned
| Value |
Description |
|
Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria
|
Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria
|
|
Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria
|
Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria
|
|
Microsoft.Azure.Monitor.WebtestLocationAvailabilityCriteria
|
Microsoft.Azure.Monitor.WebtestLocationAvailabilityCriteria
|
|
Microsoft.Azure.Monitor.PromQLCriteria
|
Microsoft.Azure.Monitor.PromQLCriteria
|
Operator
Enumeration
The criteria operator. Previously undocumented values might be returned
| Value |
Description |
|
Equals
|
Equals
|
|
GreaterThan
|
GreaterThan
|
|
GreaterThanOrEqual
|
GreaterThanOrEqual
|
|
LessThan
|
LessThan
|
|
LessThanOrEqual
|
LessThanOrEqual
|
PromQLCriteria
Object
Specifies the PromQL criteria for the metric alert resource.
| Name |
Type |
Description |
|
allOf
|
MultiPromQLCriteria[]:
|
The list of promQL criteria. Alert will be raised when all conditions are met.
|
|
failingPeriods
|
QueryFailingPeriods
|
Configuration for failing periods in query-based alerts.
|
|
odata.type
|
string:
Microsoft.Azure.Monitor.PromQLCriteria
|
Specifies the type of the alert criteria. Previously undocumented values might be returned
|
QueryFailingPeriods
Object
Configuration for failing periods in query-based alerts.
| Name |
Type |
Description |
|
for
|
string
(duration)
|
The amount of time (in ISO 8601 duration format) alert must be active before firing.
|
ResolveConfiguration
Object
| Name |
Type |
Description |
|
autoResolved
|
boolean
|
Indicates whether the alert should be auto resolved
|
|
timeToResolve
|
string
(duration)
|
The time (in ISO 8601 duration format) after which the alert should be auto resolved
|
StaticPromQLCriteria
Object
The criterion for static prom query.
| Name |
Type |
Description |
|
criterionType
|
string:
StaticThresholdCriterion
|
Specifies the type of threshold criteria. Previously undocumented values might be returned
|
|
name
|
string
|
Name of the criteria.
|
|
query
|
string
|
The query used to evaluate the alert rule
|
systemData
Object
Metadata pertaining to creation and last modification of the resource.
| Name |
Type |
Description |
|
createdAt
|
string
(date-time)
|
The timestamp of resource creation (UTC).
|
|
createdBy
|
string
|
The identity that created the resource.
|
|
createdByType
|
createdByType
|
The type of identity that created the resource.
|
|
lastModifiedAt
|
string
(date-time)
|
The timestamp of resource last modification (UTC)
|
|
lastModifiedBy
|
string
|
The identity that last modified the resource.
|
|
lastModifiedByType
|
createdByType
|
The type of identity that last modified the resource.
|
WebtestLocationAvailabilityCriteria
Object
Specifies the metric alert rule criteria for a web test resource.
| Name |
Type |
Description |
|
componentId
|
string
|
The Application Insights resource Id.
|
|
failedLocationCount
|
number
(float)
|
The number of failed locations.
|
|
odata.type
|
string:
Microsoft.Azure.Monitor.WebtestLocationAvailabilityCriteria
|
Specifies the type of the alert criteria. Previously undocumented values might be returned
|
|
webTestId
|
string
|
The Application Insights web test Id.
|