Azure SQL DB (PaaS) - Alerts Setup

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Azure SQL DB (PaaS)- Alerts setup

Prerequisites

Azure MFA admin account and access to Azure Portal


Azure RM PowerShell module installed
Azure DB server contributor rights

Steps

1- Open PowerShell ISE window


2- Login to Azure with AzureRM command: Connect-AzureRmAccount
3- Copy following script to PS ISE
4- Execute script
5- Check if alert has been created on all databases properly.

NOTE: Lookup SQL server in azure portal and Fill in following details to script in ISE

Subscription name to [subscription]


Server name to [SQL server name]
Resource group to [resource group
Group email (example@company.com) to [group email address]

# Definitions

$subscription="[subscription]"
$ServerNm="[sql server name]"
$ResourceGrp="[resource group"
$Email="[group email address]"

Select-AzureRMSubscription -SubscriptionName $subscription


$Location = $(Get-AzureRmResourceGroup -Name $ResourceGrp).Location
$ResourceId = (Get-AzureRmResource -ResourceGroupName $ResourceGrp –ResourceName
"$ServerNm/$DatabaseNm").ResourceID
$ActionEmail = New-AzureRmAlertRuleEmail -CustomEmail $Email

# Loop thru databases

foreach ($DatabaseNm in $(Get-AzureRmSqlDatabase -ResourceGroupName $ResourceGrp -ServerName


$ServerNm).DatabaseName) {

#Set alert for database only if it is not master

if (!($DatabaseNm -eq "master")) {

# CPU percentage higher that 90% last 5 minutes

$MetricNm="cpu_percent"
$AlertName=$($ServerNm+"_"+$DatabaseNm+"_"+$MetricNm)
$AlertDesc="CPU percentage higher that 90% last 5 minutes"
$WindowSize= "00:05:00"
$TimeOperator="Average" # Average, Minimum, Maximum, Total, Last
$Operator="GreaterThan" # GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual
$Threshold=90
"Creating alert $AlertName for database $DatabaseNm"
Add-AzureRmMetricAlertRule -Name $AlertName -ResourceGroup $ResourceGrp -Location $Location -TargetResourceId
$ResourceId -MetricName $MetricNm -Operator $Operator -Threshold $Threshold -WindowSize $WindowSize -
TimeAggregationOperator $TimeOperator -Action $ActionEmail -Description $AlertDesc
# Data IO percentage higher that 90% last 5 minutes

$MetricNm="physical_data_read_percent"
$AlertName=$($ServerNm+"_"+$DatabaseNm+"_"+$MetricNm)
$AlertDesc="Data IO percentage higher that 90% last 5 minutes"
$WindowSize= "00:05:00"
$TimeOperator="Average" # Average, Minimum, Maximum, Total, Last
$Operator="GreaterThan" # GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual
$Threshold=90
"Creating alert $AlertName for database $DatabaseNm"
Add-AzureRmMetricAlertRule -Name $AlertName -ResourceGroup $ResourceGrp -Location $Location -TargetResourceId
$ResourceId -MetricName $MetricNm -Operator $Operator -Threshold $Threshold -WindowSize $WindowSize -
TimeAggregationOperator $TimeOperator -Action $ActionEmail -Description $AlertDesc

# Log IO percentage higher that 90% last 5 minutes

$MetricNm="log_write_percent"
$AlertName=$($ServerNm+"_"+$DatabaseNm+"_"+$MetricNm)
$AlertDesc="Log IO percentage higher that 90% last 5 minutes"
$WindowSize= "00:05:00"
$TimeOperator="Average" # Average, Minimum, Maximum, Total, Last
$Operator="GreaterThan" # GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual
$Threshold=90
"Creating alert $AlertName for database $DatabaseNm"
Add-AzureRmMetricAlertRule -Name $AlertName -ResourceGroup $ResourceGrp -Location $Location -TargetResourceId
$ResourceId -MetricName $MetricNm -Operator $Operator -Threshold $Threshold -WindowSize $WindowSize -
TimeAggregationOperator $TimeOperator -Action $ActionEmail -Description $AlertDesc

# DTU percentage higher that 90% last 5 minutes

$MetricNm="dtu_consumption_percent"
$AlertName=$($ServerNm+"_"+$DatabaseNm+"_"+$MetricNm)
$AlertDesc="DTU percentage higher that 90% last 5 minutes"
$WindowSize= "00:05:00"
$TimeOperator="Average" # Average, Minimum, Maximum, Total, Last
$Operator="GreaterThan" # GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual
$Threshold=90
"Creating alert $AlertName for database $DatabaseNm"
Add-AzureRmMetricAlertRule -Name $AlertName -ResourceGroup $ResourceGrp -Location $Location -TargetResourceId
$ResourceId -MetricName $MetricNm -Operator $Operator -Threshold $Threshold -WindowSize $WindowSize -
TimeAggregationOperator $TimeOperator -Action $ActionEmail -Description $AlertDesc

# At last one deadlock in last 10 minutes

$MetricNm="deadlock"
$AlertName=$($ServerNm+"_"+$DatabaseNm+"_"+$MetricNm)
$AlertDesc="At last one deadlock in last 10 minutes"
$WindowSize= "00:10:00"
$TimeOperator="Total" # Average, Minimum, Maximum, Total, Last
$Operator="GreaterThanOrEqual" # GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual
$Threshold=1
"Creating alert $AlertName for database $DatabaseNm"
Add-AzureRmMetricAlertRule -Name $AlertName -ResourceGroup $ResourceGrp -Location $Location -TargetResourceId
$ResourceId -MetricName $MetricNm -Operator $Operator -Threshold $Threshold -WindowSize $WindowSize -
TimeAggregationOperator $TimeOperator -Action $ActionEmail -Description $AlertDesc

# DTU percentage higher that 90% last 30 minutes

$MetricNm="storage_percent"
$AlertName=$($ServerNm+"_"+$DatabaseNm+"_"+$MetricNm)
$AlertDesc="DTU percentage higher that 90% last 30 minutes"
$WindowSize= "00:30:00"
$TimeOperator="Maximum" # Average, Minimum, Maximum, Total, Last
$Operator="GreaterThan" # GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual
$Threshold=90
"Creating alert $AlertName for database $DatabaseNm"
Add-AzureRmMetricAlertRule -Name $AlertName -ResourceGroup $ResourceGrp -Location $Location -TargetResourceId
$ResourceId -MetricName $MetricNm -Operator $Operator -Threshold $Threshold -WindowSize $WindowSize -
TimeAggregationOperator $TimeOperator -Action $ActionEmail -Description $AlertDesc

# Sessions percent higher that 90% last 5 minutes

$MetricNm="sessions_percent"
$AlertName=$($ServerNm+"_"+$DatabaseNm+"_"+$MetricNm)
$AlertDesc="Sessions percent higher that 90% last 5 minutes"
$WindowSize= "00:05:00"
$TimeOperator="Average" # Average, Minimum, Maximum, Total, Last
$Operator="GreaterThan" # GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual
$Threshold=90
"Creating alert $AlertName for database $DatabaseNm"
Add-AzureRmMetricAlertRule -Name $AlertName -ResourceGroup $ResourceGrp -Location $Location -TargetResourceId
$ResourceId -MetricName $MetricNm -Operator $Operator -Threshold $Threshold -WindowSize $WindowSize -
TimeAggregationOperator $TimeOperator -Action $ActionEmail -Description $AlertDesc
}
}

You might also like