Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit f36666c

Browse files
20220728 add az metric alert rule v2 (#373)
* 20220728 1509 rewrite to use AzMetricAlertRuleV2 * 20220728 1509 rewrite to use AzMetricAlertRuleV2
1 parent c446736 commit f36666c

File tree

2 files changed

+116
-17
lines changed

2 files changed

+116
-17
lines changed

sql-database/monitor-and-scale-database/monitor-and-scale-database.ps1

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1-
# Connect-AzAccount
2-
# The SubscriptionId in which to create these objects
1+
# This script requires the following
2+
# - Az.Resources
3+
# - Az.Accounts
4+
# - Az.Monitor
5+
# - Az.Sql
6+
7+
# First, run Connect-AzAccount
8+
9+
# Set the subscription in which to create these objects. This is displayed on objects in the Azure portal.
310
$SubscriptionId = ''
411
# Set the resource group name and location for your server
512
$resourceGroupName = "myResourceGroup-$(Get-Random)"
613
$location = "westus2"
714
# Set an admin login and password for your server
815
$adminSqlLogin = "SqlAdmin"
9-
$password = "ChangeYourAdminPassword1"
16+
$password = (New-Guid).Guid # Generates a randomized GUID password.
1017
# Set server name - the logical server name has to be unique in the system
1118
$serverName = "server-$(Get-Random)"
1219
# The sample database name
1320
$databaseName = "mySampleDatabase"
14-
# The ip address range that you want to allow to access your server
21+
# The ip address range that you want to allow to access your server via the firewall rule
1522
$startIp = "0.0.0.0"
1623
$endIp = "0.0.0.0"
1724

1825
# Set subscription
1926
Set-AzContext -SubscriptionId $subscriptionId
2027

21-
# Create a resource group
28+
# Create a new resource group
2229
$resourceGroup = New-AzResourceGroup -Name $resourceGroupName -Location $location
2330

24-
# Create a server with a system wide unique server name
31+
# Create a new server with a system wide unique server name
2532
$server = New-AzSqlServer -ResourceGroupName $resourceGroupName `
2633
-ServerName $serverName `
2734
-Location $location `
@@ -39,13 +46,14 @@ $database = New-AzSqlDatabase -ResourceGroupName $resourceGroupName `
3946
-RequestedServiceObjectiveName "S0" `
4047
-SampleName "AdventureWorksLT"
4148

42-
# Monitor the DTU consumption on the imported database in 5 minute intervals
49+
# Monitor the DTU consumption on the database in 5 minute intervals
4350
$MonitorParameters = @{
4451
ResourceId = "/subscriptions/$($(Get-AzContext).Subscription.Id)/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$serverName/databases/$databaseName"
4552
TimeGrain = [TimeSpan]::Parse("00:05:00")
4653
MetricNames = "dtu_consumption_percent"
4754
}
48-
(Get-AzMetric @MonitorParameters -DetailedOutput).MetricValues
55+
$metric = Get-AzMetric @monitorparameters
56+
$metric.Data
4957

5058
# Scale the database performance to Standard S1
5159
$database = Set-AzSqlDatabase -ResourceGroupName $resourceGroupName `
@@ -54,7 +62,47 @@ $database = Set-AzSqlDatabase -ResourceGroupName $resourceGroupName `
5462
-Edition "Standard" `
5563
-RequestedServiceObjectiveName "S1"
5664

57-
# Set an alert rule to automatically monitor DTU in the future
65+
66+
# Set up an Alert rule using Azure Monitor for the database
67+
# Add an Alert that fires when the pool utilization reaches 90%
68+
# Objects needed: An Action Group Receiver (in this case, an email group), an Action Group, Alert Criteria, and finally an Alert Rule.
69+
70+
# Creates an new action group receiver object with a target email address.
71+
$receiver = New-AzActionGroupReceiver `
72+
-Name "my Sample Azure Admins" `
73+
-EmailAddress "azure-admins-group@contoso.com"
74+
75+
# Creates a new or updates an existing action group.
76+
$actionGroup = Set-AzActionGroup `
77+
-Name "mysample-email-the-azure-admins" `
78+
-ShortName "AzAdminsGrp" `
79+
-ResourceGroupName $resourceGroupName `
80+
-Receiver $receiver
81+
82+
# Fetch the created AzActionGroup into an object of type Microsoft.Azure.Management.Monitor.Models.ActivityLogAlertActionGroup
83+
$actionGroupObject = New-AzActionGroup -ActionGroupId $actionGroup.Id
84+
85+
# Create a criteria for the Alert to monitor.
86+
$criteria = New-AzMetricAlertRuleV2Criteria `
87+
-MetricName "dtu_consumption_percent" `
88+
-TimeAggregation Average `
89+
-Operator GreaterThan `
90+
-Threshold 90
91+
92+
# Create the Alert rule.
93+
# Add-AzMetricAlertRuleV2 adds or updates a V2 (non-classic) metric-based alert rule.
94+
Add-AzMetricAlertRuleV2 -Name "mySample_Alert_DTU_consumption_pct" `
95+
-ResourceGroupName $resourceGroupName `
96+
-WindowSize (New-TimeSpan -Minutes 1) `
97+
-Frequency (New-TimeSpan -Minutes 1) `
98+
-TargetResourceId "/subscriptions/$($(Get-AzContext).Subscription.Id)/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$serverName/databases/$databaseName" `
99+
-Condition $criteria `
100+
-ActionGroup $actionGroupObject `
101+
-Severity 3 #Informational
102+
103+
<#
104+
# Set up an alert rule using Azure Monitor for the database
105+
# Note that Add-AzMetricAlertRule is deprecated. Use Add-AzMetricAlertRuleV2 instead.
58106
Add-AzMetricAlertRule -ResourceGroup $resourceGroupName `
59107
-Name "MySampleAlertRule" `
60108
-Location $location `
@@ -65,6 +113,7 @@ Add-AzMetricAlertRule -ResourceGroup $resourceGroupName `
65113
-WindowSize $([TimeSpan]::Parse("00:05:00")) `
66114
-TimeAggregationOperator "Average" `
67115
-Action $(New-AzAlertRuleEmail -SendToServiceOwner)
116+
#>
68117

69118
# Clean up deployment
70119
# Remove-AzResourceGroup -ResourceGroupName $resourceGroupName

sql-database/monitor-and-scale-pool/monitor-and-scale-pool.ps1

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
# Connect-AzAccount
1+
# This script requires the following
2+
# - Az.Resources
3+
# - Az.Accounts
4+
# - Az.Monitor
5+
# - Az.Sql
6+
7+
# First, run Connect-AzAccount
8+
9+
# Set the subscription in which to create these objects. This is displayed on objects in the Azure portal.
210
$SubscriptionId = ''
311
# Set the resource group name and location for your server
412
$resourceGroupName = "myResourceGroup-$(Get-Random)"
513
$location = "westus2"
6-
# Set elastic pool names
14+
# Set elastic pool name
715
$poolName = "MySamplePool"
816
# Set an admin login and password for your database
917
$adminSqlLogin = "SqlAdmin"
10-
$password = "ChangeYourAdminPassword1"
11-
# The logical server name has to be unique in the system
18+
$password = (New-Guid).Guid # Generates a randomized GUID password.
19+
# Set server name - the logical server name has to be unique in the system
1220
$serverName = "server-$(Get-Random)"
1321
# The sample database names
1422
$firstDatabaseName = "myFirstSampleDatabase"
1523
$secondDatabaseName = "mySecondSampleDatabase"
16-
# The ip address range that you want to allow to access your server
24+
# The ip address range that you want to allow to access your server via the firewall rule
1725
$startIp = "0.0.0.0"
1826
$endIp = "0.0.0.0"
1927

@@ -53,13 +61,14 @@ $secondDatabase = New-AzSqlDatabase -ResourceGroupName $resourceGroupName `
5361
-DatabaseName $secondDatabaseName `
5462
-ElasticPoolName $poolName
5563

56-
# Monitor the pool
64+
# Monitor the DTU consumption of the pool in 5 minute intervals
5765
$monitorparameters = @{
5866
ResourceId = "/subscriptions/$($(Get-AzContext).Subscription.Id)/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$serverName/elasticPools/$poolName"
5967
TimeGrain = [TimeSpan]::Parse("00:05:00")
6068
MetricNames = "dtu_consumption_percent"
6169
}
62-
(Get-AzMetric @monitorparameters -DetailedOutput).MetricValues
70+
$metric = Get-AzMetric @monitorparameters
71+
$metric.Data
6372

6473
# Scale the pool
6574
$elasticPool = Set-AzSqlElasticPool -ResourceGroupName $resourceGroupName `
@@ -70,7 +79,47 @@ $elasticPool = Set-AzSqlElasticPool -ResourceGroupName $resourceGroupName `
7079
-DatabaseDtuMin 20 `
7180
-DatabaseDtuMax 100
7281

73-
# Add an alert that fires when the pool utilization reaches 90%
82+
# Set up an Alert rule using Azure Monitor for the database
83+
# Add an Alert that fires when the pool utilization reaches 90%
84+
# Objects needed: an Action Group Receiver, an Action Group, Alert Criteria, and finally an Alert Rule.
85+
86+
# Creates an new action group receiver object with a target email address.
87+
$receiver = New-AzActionGroupReceiver `
88+
-Name "my Sample Azure Admins" `
89+
-EmailAddress "azure-admins-group@contoso.com"
90+
91+
# Creates a new or updates an existing action group.
92+
$actionGroup = Set-AzActionGroup `
93+
-Name "mysample-email-the-azure-admins" `
94+
-ShortName "AzAdminsGrp" `
95+
-ResourceGroupName $resourceGroupName `
96+
-Receiver $receiver
97+
98+
# Fetch the created AzActionGroup into an object of type Microsoft.Azure.Management.Monitor.Models.ActivityLogAlertActionGroup
99+
$actionGroupObject = New-AzActionGroup -ActionGroupId $actionGroup.Id
100+
101+
# Create a criteria for the Alert to monitor.
102+
$criteria = New-AzMetricAlertRuleV2Criteria `
103+
-MetricName "dtu_consumption_percent" `
104+
-TimeAggregation Average `
105+
-Operator GreaterThan `
106+
-Threshold 90
107+
108+
# Create the Alert rule.
109+
# Add-AzMetricAlertRuleV2 adds or updates a V2 (non-classic) metric-based alert rule.
110+
Add-AzMetricAlertRuleV2 -Name "mySample_Alert_DTU_consumption_pct" `
111+
-ResourceGroupName $resourceGroupName `
112+
-WindowSize (New-TimeSpan -Minutes 1) `
113+
-Frequency (New-TimeSpan -Minutes 1) `
114+
-TargetResourceId "/subscriptions/$($(Get-AzContext).Subscription.Id)/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$serverName/elasticPools/$poolName" `
115+
-Condition $criteria `
116+
-ActionGroup $actionGroupObject `
117+
-Severity 3 #Informational
118+
119+
<#
120+
# Set up an alert rule using Azure Monitor for the database
121+
# Add a classic alert that fires when the pool utilization reaches 90%
122+
# Note that Add-AzMetricAlertRule is deprecated. Use Add-AzMetricAlertRuleV2 instead.
74123
Add-AzMetricAlertRule -ResourceGroup $resourceGroupName `
75124
-Name "mySampleAlertRule" `
76125
-Location $location `
@@ -81,6 +130,7 @@ Add-AzMetricAlertRule -ResourceGroup $resourceGroupName `
81130
-WindowSize $([TimeSpan]::Parse("00:05:00")) `
82131
-TimeAggregationOperator "Average" `
83132
-Action $(New-AzAlertRuleEmail -SendToServiceOwner)
133+
#>
84134

85135
# Clean up deployment
86136
# Remove-AzResourceGroup -ResourceGroupName $resourceGroupName

0 commit comments

Comments
 (0)