【发布时间】:2019-05-16 05:58:40
【问题描述】:
在 Azure 中,如果您设置带有故障转移组的 SQL Server 并且故障转移策略设置为自动,那么当发生 SQL Server 故障转移时,如何配置警报或通知?如果无法在 Monitor 中设置,是否可以在其他地方编写脚本?
【问题讨论】:
标签: azure azure-sql-server sql-azure-alerts
在 Azure 中,如果您设置带有故障转移组的 SQL Server 并且故障转移策略设置为自动,那么当发生 SQL Server 故障转移时,如何配置警报或通知?如果无法在 Monitor 中设置,是否可以在其他地方编写脚本?
【问题讨论】:
标签: azure azure-sql-server sql-azure-alerts
找到了一种在 Azure 中使用自动化帐户 > Runbook > 使用 Powershell 编写脚本的方法。像这样的简单脚本应该可以做到。只需要弄清楚作为帐户运行并通过计划或警报触发它。
function sendEmailAlert
{
# Send email
}
function checkFailover
{
$list = Get-AzSqlDatabaseFailoverGroup -ResourceGroupName "my-resourceGroup" -server "my-sql-server"
if ( $list.ReplicationRole -ne 'Primary')
{
sendEmailAlert
}
}
checkFailover
【讨论】:
Azure SQL 数据库仅支持以下警报指标:
当 SQL Server 发生故障时,我们无法使用警报。你可以从这个文档中得到这个:Create alerts for Azure SQL Database and Data Warehouse using Azure portal。
希望这会有所帮助。
【讨论】:
感谢 CKelly - 让我在 Azure 中应该成为标准的东西有了一个良好的开端。我创建了一个 Azure 自动化帐户,添加了 Az.Account、Az.Automation 和 Az.Sql 模块,然后在您的代码中添加了更多内容。在 Azure 中,我创建了一个 SendGrid 帐户。
#use the Azure Account Automation details to login to Azure
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
#create email alert
function sendEmailAlert
{
# Send email
$From = "<email from>"
$To = "<email of stakeholders to receive this message>"
$SMTPServer = "smtp.sendgrid.net"
$SMTPPort = "587"
$Username = "<sendgrid username>"
$Password = "<sendgridpassword>"
$subject = "<email subject>"
$body = "<text to go in email body>"
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
$smtp.Send($From, $To, $subject, $body)
}
#create failover check and send if the primary server has changed
function checkFailover
{
$list = Get-AzSqlDatabaseFailoverGroup -ResourceGroupName "<the resourcegroup>" -server "<SQl Databse server>"
if ( $list.ReplicationRole -ne 'Primary')
{
sendEmailAlert
}
}
checkFailover
这个过程可能对其他人有所帮助。
【讨论】: