Pre requisites :
1. Database Mail should be configured
2. Need to have a valid SMTP server to send the mail
Steps as below.
1. Configure the Database Mail
Refer Below Link
2. Create a Procedure
3. Create Table
3. Create a Job
Step 1. Execute procedure to update the Table
Step 2. EXEC msdb.dbo.sp_send_dbmail.
Concept :
One should get an alert email, whenever SQL Cluster fail over to other Node.
I have created a Table and hard coded the existing Instance Name and Active Cluster Node information in a table. There is an additional column with value as 1 for all the Active node instance and rest with value zero. So, whenever fail over happens Zero value for the respective Active node will get value as 1 and existing 1 as zero. While sending a mail, all active node instances will be selected.
Table is created on a centralized Server. All other servers will update data on the same.
USE [DBA_Admin]
GO
/****** Object: Table [dbo].[Current_AcitveCluster_Node] Script Date: 1/2/2017 7:11:46 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Current_AcitveCluster_Node](
[ActiveNode] [char](30) NULL,
[InstanceName] [char](30) NULL,
[Status] [bit] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
Procedure is created on all individual instance.
USE [DBA_Admin]
GO
/****** Object: StoredProcedure [dbo].[usp_CheckActiveNode] Script Date: 1/2/2017 7:14:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_CheckActiveNode]
AS
BEGIN
SELECT srvname InstanceName
, (CONVERT(varchar(200), (SERVERPROPERTY('ComputerNamePhysicalNetBIOS')))) NewNodeName
, GETDATE ( ) CurrentDate
into #NewData
from sys.sysservers
where srvid=0
update [LinkedServer].DBA_Admin.dbo.Current_AcitveCluster_Node
set status = 0
where instancename = (select InstanceName from #NewData)
and status = 1
update [LinkedServer].DBA_Admin.dbo.Current_AcitveCluster_Node
set status = 1
from [LinkedServer].DBA_Admin.dbo.Current_AcitveCluster_Node O
join #NewData N
on N.InstanceName = O.InstanceName
and O.activeNode = N.NewNodeName
if object_id('tempdb..#NewData') is not null
drop table #NewData
END
Job is created on all individual instance.
USE [msdb]
GO
/****** Object: Job [SQL Alert for Cluster Failover] Script Date: 1/2/2017 7:16:40 AM ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object: JobCategory [[Uncategorized (Local)]] Script Date: 1/2/2017 7:16:40 AM ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'SQL Alert for Cluster Failover',
@enabled=1,
@notify_level_eventlog=3,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'Send an alert mail whenever Failover happen on the SQL Cluster.',
@category_name=N'[Uncategorized (Local)]',
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [Update the Table] Script Date: 1/2/2017 7:16:40 AM ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Update the Table',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=3,
@on_success_step_id=0,
@on_fail_action=3,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'EXEC [dbo].[usp_CheckActiveNode]',
@database_name=N'DBA_Admin',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [Send an alert mail] Script Date: 1/2/2017 7:16:40 AM ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Send an alert mail',
@step_id=2,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'EXEC msdb.dbo.sp_send_dbmail
@profile_name = ''SQLProfile'',
@recipients=''DBTeam@XYZ.com'',
@query = ''set nocount on select InstanceName, ActiveNode from [LinkedServer].[DBA_Admin].[dbo].Current_AcitveCluster_Node_backup where status=1'',
@Body=''SQL Server Failed over to other Node. Find the Cluster Node details below.'',
@from_address= ''DBTeam@XYZ.com'',
@subject=''SQL Cluster Failover Alert'',
@execute_query_database = ''DBA_Admin''',
@database_name=N'DBA_Admin',
@flags=16
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'Starts when SQL Server Agent starts on Passive Node',
@enabled=1,
@freq_type=64,
@freq_interval=0,
@freq_subday_type=0,
@freq_subday_interval=0,
@freq_relative_interval=0,
@freq_recurrence_factor=0,
@active_start_date=20161223,
@active_end_date=99991231,
@active_start_time=0,
@active_end_time=235959,
@schedule_uid=N'6dbda10f-3d49-4da7-9bcc-1d44a7eef1a6'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO