As a DBA it’s handy to have a script to PING all the Link servers and get a report to examine the connectivity. Following T-SQL script will ping all the preconfigured linked servers and get the packets sent, received and lost details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
DECLARE @SQL NVARCHAR(MAX); IF OBJECT_ID('tempdb..#Ping_Results') IS NOT NULL DROP TABLE #Ping_Results CREATE TABLE #Ping_Results ( [Server_Name] NVARCHAR(128), [PING_Results] NVARCHAR(4000), ) DECLARE #servers CURSOR FOR SELECT DISTINCT [Linked_Server_Name] FROM ( SELECT SUBSTRING(name,0,CHARINDEX('\',name)) AS [Linked_Server_Name] FROM master.sys.servers WHERE name like '%\%' UNION SELECT name FROM master.sys.servers WHERE name NOT like '%\%' )LinkedServers DECLARE @LinkedServerName varchar(128) OPEN #servers FETCH #servers INTO @LinkedServerName WHILE @@FETCH_STATUS = 0 BEGIN SELECT @SQL = 'declare @results table(result varchar(4000));' SELECT @SQL = @SQL +' insert into @results' SELECT @SQL = @SQL +' EXEC master.dbo.xp_cmdshell ''ping '+@LinkedServerName+'''' SELECT @SQL = @SQL +' INSERT INTO #Ping_Results([Server_Name],[PING_Results])' SELECT @SQL = @SQL +' select '''+@LinkedServerName+''',result from @results WHERE result LIKE ''%Packets: Sent%''' PRINT @SQL EXEC(@SQL) FETCH #servers INTO @LinkedServerName END CLOSE #servers DEALLOCATE #servers SELECT [Server_Name],[PING_Results], CASE WHEN [PING_Results] LIKE '%Lost = 0 (0% loss)%' THEN 'Ping Succeeded' ELSE 'Failed' END AS [Status] FROM #Ping_Results ORDER BY [Server_Name] DROP TABLE #Ping_Results |
Leave a Comment