Monitor Any Linked Server From SQL 2000

, 2008-07-11 (first published: )

Hi,

DBA's often monitor Backups, Replication e.t.c manually or through some automation but there is one thing which needs to be monitored is linked server. Often there may be some issue in linked server and DBA's will not know till Application user raises issue. Also there is a straight method (sp_TestLinkedServer) to test linked servers in SQL 2005 will say pass even the Access is deleted from the path. So this script will help you to monitor Linked servers from SQL 2000 and i have posted another SQL 2005 version of this script.

Also fine below the test i have done on this script and options available to test linked servers.

Below are the tests done and their status.

The RED issues cannot be captured in SQL 2000 (No option to TRY/CATCH ???) and thus why it is good to monitor linked servers from SQL 2005.

Below are the options available to test linked server

In the above we cannot use sp_tableex since incase SQL server is not running or access denied etc then runtime error will come so only we are using sp_OA.

Any enhancements/suggestions are always welcome!

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

------------------------------------------------------------------------------------------------------------------
---	Name		: uspMonitor_LinkedServer
---	Author		: Sakthivel C
---	Release Date	: 05/03/2008
---	Revision	: O
---	Revision History:
------------------------------------------------------------------------------------------------------------------
ALTER PROCEDURE dbo.[uspMonitor_LinkedServer] 
AS
BEGIN


DECLARE @ServerName VARCHAR(255)
DECLARE @ServerType VARCHAR(50)
DECLARE @Dummy VARCHAR(100)
DECLARE @msg VARCHAR(500)
DECLARE @hr INT
DECLARE @sql INT
DECLARE @Error bigint
DECLARE @status INT ;SET @status = 0

-- Get the list of Excel and Access Linked Servers Since the Testing Method is different here
DECLARE Lnkd_srvr_Crsr CURSOR FAST_FORWARD FOR
SELECT srvname FROM master..sysservers WHERE isremote=1 AND srvproduct NOT LIKE 'SQL Server%' AND srvname NOT IN ('')

OPEN Lnkd_srvr_Crsr
FETCH NEXT FROM Lnkd_srvr_Crsr INTO @ServerName
WHILE @@FETCH_STATUS = 0
BEGIN
 -- Reading a sample Table Name to Dummy Variable from Excel sheet to ensure Connectivity
 SELECT TOP 1 @Dummy = TABLE_NAME from master.dbo.SYSREMOTE_TABLES (@ServerName)

-- Checking Status and Sending Alert Mail to Team
 IF @@ROWCOUNT = 0 BEGIN
	SET @msg= 'Error in Getting Data from linked server ' + @ServerName + ' Configured at ' + @@SERVERNAME
 
	Exec [MONITOR].master.dbo.xp_smtp_sendmail @from= 'Linked_Server_Alert@Company.com' ,@from_name= @@SERVERNAME ,
	@to='Team@Company.com', 
	@priority = 'HIGH', @subject=@msg, 
	@type= 'text/plain', @server = 'mailserver.company.com' 
	Print 'Mail Sent' + @ServerName
 END
 SET @status = 0	-- Setting the Status Variable back to 0 for Resetting Error Trapped

FETCH NEXT FROM Lnkd_srvr_Crsr INTO @ServerName
END
CLOSE Lnkd_srvr_Crsr
DEALLOCATE Lnkd_srvr_Crsr

-- Get the list of SQL Linked Servers Since the Testing Method is different here
DECLARE Lnkd_srvr_Crsr CURSOR FAST_FORWARD FOR
SELECT srvname FROM master..sysservers WHERE isremote=1 AND srvproduct LIKE 'SQL Server%' AND srvname NOT IN ('')
OPEN Lnkd_srvr_Crsr

FETCH NEXT FROM Lnkd_srvr_Crsr INTO @ServerName
WHILE @@FETCH_STATUS = 0
BEGIN

 -- Performing a login and check the status of Server(SQL) to ensure Connectivity

 EXEC @hr = sp_OACreate 'SQLDMO.SQLServer', @sql OUTPUT
 ---IF @hr<>0 EXEC sp_OAGetErrorInfo @sql -- Used while testing procedure to check error msg
 EXEC @hr = sp_OASetProperty @sql ,'LoginSecure','True'
 EXEC @hr = sp_OASetProperty @sql ,'LoginTimeout',10
 EXEC @hr = sp_OAMethod @sql,'Connect',null,@ServerName
 EXEC @hr = sp_OAGetProperty @sql ,'Status',@status OUTPUT
 EXEC @hr = sp_OAMethod @sql,'DisConnect',null
 EXEC @hr = sp_OADestroy @sql
 
 -- Checking Status and Sending Alert Mail to Team
IF @status <> 1 BEGIN
	SET @msg= 'Timeout in Getting Data from linked server ' + @ServerName + ' Configured at ' + @@SERVERNAME
	Exec [MONITOR].master.dbo.xp_smtp_sendmail @from= 'Linked_Server_Alert@Company.com' ,@from_name= @@SERVERNAME ,
	@to='Team@Company.com', 
	@priority = 'HIGH', @subject=@msg, 
	@type= 'text/plain', @server = 'mailserver.company.com' 
	Print 'Mail Sent' + @ServerName
END
print @status
SET @status = 0		-- Setting the Status Variable back to 0 for Resetting Error Trapped

 FETCH NEXT FROM Lnkd_srvr_Crsr INTO @ServerName
END
CLOSE Lnkd_srvr_Crsr
DEALLOCATE Lnkd_srvr_Crsr


END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

Rate

5 (1)

Share

Share

Rate

5 (1)

Related content

Trace-scrubbing Tools

Andrew Zanevsky shares his trace-scrubbing procedures that make it easy for you to handle large trace files and aggregate transactions by type–even when captured T-SQL code has variations.

SQL Server Profiler is a veritable treasure trove when it comes to helping DBAs optimize their T-SQL code. But, the surfeit of riches (I'm reminded of the Arabian Nights tale of Aladdin) can be overwhelming. I recently had one of those "sinking" feelings when I first tried to make sense of the enormous amount of data collected by traces on a client's servers. At this particular client, the online transactions processing system executes more than 4 million database transactions per hour. That means that even a 30-minute trace that captures "SQL Batch Completed" events results in a table with 2 million rows. Of course, it's simply impractical to process so many records without some automation, and even selecting the longest or most expensive transactions doesn't necessarily help in identifying bottlenecks. After all, short transactions can be the culprits of poor performance when executed thousands of times per minute.

2005-01-11

2,003 reads

Basics of C2 Auditing

Auditing in SQL Server is something that is becoming more and more prevelant, and will likely become more regulated over time. Dinesh Asanka brings us a new article on the basics of SQL Servers most powerful auditing configuration, C2 level auditing.

4.88 (8)

2004-11-03

28,607 reads

Monitoring Drive and Database Free Space

SQL Server will autogrow your databases as they run out of space. But the process doesn't manage space, nor does it check the free space on the drive. Allowing autogrow to grow unchecked and unmanaged will eventually use up all the free disk space and potentially crash your server. New author Mark Nash brings you his system for monitoring space usage and generating a report that eases this process.

4.4 (5)

2006-08-25 (first published: )

43,140 reads