【问题标题】:In Azure, how can you configure an alert or notification when a SQL Server failover happened?在 Azure 中,如何在 SQL Server 故障转移发生时配置警报或通知?
【发布时间】:2019-05-16 05:58:40
【问题描述】:

在 Azure 中,如果您设置带有故障转移组的 SQL Server 并且故障转移策略设置为自动,那么当发生 SQL Server 故障转移时,如何配置警报或通知?如果无法在 Monitor 中设置,是否可以在其他地方编写脚本?

【问题讨论】:

    标签: azure azure-sql-server sql-azure-alerts


    【解决方案1】:

    找到了一种在 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
    

    【讨论】:

      【解决方案2】:

      Azure SQL 数据库仅支持以下警报指标:

      当 SQL Server 发生故障时,我们无法使用警报。你可以从这个文档中得到这个:Create alerts for Azure SQL Database and Data Warehouse using Azure portal

      希望这会有所帮助。

      【讨论】:

      • 是的,我知道这份清单。您可以在为数据库创建警报规则时设置它。我正在寻找发生故障转移时的服务器警报,而不是在单个数据库(资源)级别。谢谢。
      • 服务器警报仅支持 Azure 托管实例。而且 Azure SQL 现在没有 Server alert 之类的功能。
      • 那么,如果不是 sql 托管实例,有没有办法在 azure 中编写脚本?谢谢。
      • 据我所知,您无法在 Azure 中编写脚本。希望我的回答能帮到你。
      • 嗨@CKay,如果我的回答对您有帮助,您能接受吗?谢谢,祝你有美好的一天。
      【解决方案3】:

      感谢 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
      

      这个过程可能对其他人有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-22
        • 2018-03-15
        • 1970-01-01
        • 2021-03-25
        相关资源
        最近更新 更多