I run an agent job every 5 minutes with the following script to query the active linked servers. If a query errors out on a linked server, an email is sent to the recipient in the script with the basic information of the server down. The script runs successfully, however, I was getting early AM notifications of a server down with error 258 (timeout error) :sick:. After some inquiries the timeouts were happening during disk imaging operations and SecondCopy runs by the network administrator. I have just updated the script to exclude error 258 and wanted to share and get feedback\improvements on the script. [code="sql"]IF OBJECT_ID('tempdb..#LinkedServerPing') IS NOT NULLBEGIN DROP TABLE #LinkedServerPingEND;CREATE TABLE #LinkedServerPing ( -- If ping fails this info will be emailed Server_name SYSNAME, [STATUS] CHAR(7), [Time_Server_Polled] DATETIME, [ERROR_NUMBER] INT, [ERROR_MESSAGE] VARCHAR(MAX) ) IF EXISTS (SELECT 1 FROM master.sys.servers WHERE is_linked = 1) BEGIN DECLARE @servername SYSNAME SELECT @servername = MIN(NAME) FROM master.sys.servers WHERE is_linked = 1 RETRY: WHILE @servername IS NOT NULL BEGIN BEGIN TRY DECLARE @cmd NVARCHAR(MAX); SET @cmd = 'SELECT * FROM OPENQUERY(' + quotename(@servername) + ', ''select 1 from tempdb..sysobjects'')' EXEC master.sys.sp_executesql @cmd END TRY BEGIN CATCH IF ERROR_NUMBER() <> 258 -- Timeout Error happens early AM during image copies or SecondCopy execution BEGIN INSERT INTO #LinkedServerPing SELECT @servername AS Server_name, 'OFFLINE' AS [STATUS], GETDATE() AS [Time_Server_Polled], ERROR_NUMBER() AS [Error_Number], ERROR_MESSAGE() AS [ERROR_MESSAGE] -- Email server down alert DECLARE @xml NVARCHAR(MAX) DECLARE @body NVARCHAR(MAX) SET @xml = CAST(( SELECT [Server_name] AS 'td', '', [STATUS] AS 'td', '', [Time_Server_Polled] AS 'td', '', [ERROR_NUMBER] AS 'td', '', [ERROR_MESSAGE] AS 'td' FROM #LinkedServerPing FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(255)) SET @body = '<html><body><H3>Executed as agent job "DBA Maintenance: Ping DB Servers" on SERVERNAMEHERE</H3> <table border = 1> <center> <tr> <th> Server Name </th> <th> Status </th> <th> Time </th> <th> Error # </th> <th> Error Message </th> </tr>' SET @body = @body + @xml + '</center></table></body></html>' EXEC msdb.dbo.sp_send_dbmail @profile_name = 'SERVERNAMEHERE DB Mail', @body = @body, @body_format = 'HTML', @recipients = 'you@here.com', @subject = 'DB Server Down Alert!!!'; END IF ERROR_NUMBER() = 258 -- Timeout Error BEGIN WAITFOR DELAY '00:00:00.10' -- Wait for 10 ms GOTO RETRY -- Go to label RETRY END ELSE GOTO JUMPLOOP -- Go to label JUMPLOOP END CATCH JUMPLOOP: SELECT @servername = MIN(NAME) FROM master.sys.servers WHERE is_linked = 1 AND NAME > @servername END -- End of WHILE loopEND[/code]Steps to use:- Ensure DB mail is setup with default public profile- Review the code above and update with your email address- Review your linked servers on the instance and update to reflect the active servers you want to ping- Create SQL Server agent job and schedule
↧