Search code examples
azureazure-sql-serversql-azure-alerts

In Azure, how can you configure an alert or notification when a SQL Server failover happened?


In Azure, how can you configure an alert or notification when a SQL Server failover happened if you setup a SQL server with Failover groups and failover policy is set to automatic? If it can't be setup in Monitor can it be scripted elsewhere?


Solution

  • Found a way to script this in Azure using Automation Accounts > Runbook > using Powershell. A simple script like this should do it. Just need to figure out the run as account and trigger it by schedule or alert.

    function sendEmailAlert
    {
        # Send email
    }
    
    
    function checkFailover
    {
        $list = Get-AzSqlDatabaseFailoverGroup -ResourceGroupName "my-resourceGroup" -server "my-sql-server"
    
        if ( $list.ReplicationRole -ne 'Primary')
        { 
            sendEmailAlert
        }
    }
    
    checkFailover