Programmatically Monitor Replication
Replication Monitor is a graphical tool that allows you to monitor a replication topology. You can access the same monitoring data programmatically by using Transact-SQL replication stored procedures or replication management objects (RMO). These objects enable you to program the following tasks:
Monitor the state of Publishers, publications, and subscriptions.
Monitor Merge Agent sessions at one or more Subscribers.
Monitor transactional commands waiting to be applied at one or more Subscribers.
Define the threshold metrics that determine when a publication requires intervention.
Monitor the status of tracer tokens.
In this topic:
Transact-SQL
Replication Management Objects (RMO)
Transact-SQL
To monitor Publishers, publications, and subscriptions from the Distributor
At the Distributor on the distribution database, execute sp_replmonitorhelppublisher. This returns monitoring information for all Publishers using this Distributor. To limit the result set to a single Publisher, specify @publisher.
At the Distributor on the distribution database, execute sp_replmonitorhelppublication. This returns monitoring information for all publications using this Distributor. To limit the result set to a single Publisher, publication, or published database, specify @publisher, @publication, or @publisher_db, respectively.
At the Distributor on the distribution database, execute sp_replmonitorhelpsubscription. This returns monitoring information for all subscriptions using this Distributor. To limit the result set to subscriptions belonging to a single Publisher, publication, or published database, specify @publisher, @publication, or @publisher_db, respectively.
To monitor transactional commands waiting to be applied at the Subscriber
- At the Distributor on the distribution database, execute sp_replmonitorsubscriptionpendingcmds. This returns monitoring information for all commands pending for all subscriptions using this Distributor. To limit the result set to commands pending for subscriptions belonging to a single Publisher, Subscriber, publication, or published database, specify @publisher, @subscriber, @publication, or @publisher_db, respectively.
To monitor merge changes waiting to be uploaded or downloaded
At the Publisher on the publication database, execute sp_showpendingchanges. This returns a result set showing information on changes that are waiting to be replicated to Subscribers. To limit the result set to changes that belong to a single publication or article, specify @publication or @article, respectively.
At a Subscriber on the subscription database, execute sp_showpendingchanges. This returns a result set showing information on changes that are waiting to be replicated to the Publisher. To limit the result set to changes that belong to a single publication or article, specify @publication or @article, respectively.
To monitor Merge Agent sessions
At the Distributor on the distribution database, execute sp_replmonitorhelpmergesession. This returns monitoring information, including Session_id, on all Merge Agent sessions for all subscriptions using this Distributor. You can also obtain Session_id by querying the MSmerge_sessions system table.
At the Distributor on the distribution database, execute sp_replmonitorhelpmergesessiondetail. Specify a Session_id value from step 1 for @session_id. This displays detailed monitor information about the session.
Repeat step 2 for each session of interest.
To monitor Merge Agent sessions for pull subscriptions from the Subscriber
At the Subscriber on the subscription database, execute sp_replmonitorhelpmergesession. For a given subscription, specify @publisher, @publication, and the name of the publication database for @publisher_db. This returns monitoring information for the last five Merge Agent sessions for this subscription. Note the value of Session_id for sessions of interest in the result set.
At the Subscriber on the subscription database, execute sp_replmonitorhelpmergesessiondetail. Specify a Session_id value from step 1 for @session_id. This displays detailed monitoring information about the session.
Repeat step 2 for each session of interest.
To view and modify the monitor threshold metrics for a publication
At the Distributor on the distribution database, execute sp_replmonitorhelppublicationthresholds. This returns the monitoring thresholds set for all publications using this Distributor. To limit the result set to monitor thresholds to publications belonging to a single Publisher or published database or to a single publication, specify @publisher, @publisher_db, or @publication, respectively. Note the value of Metric_id for any thresholds that must be changed. For more information, see Set Thresholds and Warnings in Replication Monitor.
At the Distributor on the distribution database, execute sp_replmonitorchangepublicationthreshold. Specify the following as needed:
The Metric_id value obtained in step 1 for @metric_id.
A new value for the monitor threshold metric for @value.
A value of 1 for @shouldalert for an alert to be logged when this threshold is reached, or a value of 0 if an alert is not needed.
A value of 1 for @mode to enable the monitor threshold metric or a value of 2 to disable it.
[Top]
Replication Management Objects (RMO)
To monitor a subscription to a merge publication at the Subscriber
Create a connection to the Subscriber by using the ServerConnection class.
Create an instance of the MergeSubscriberMonitor class, and set the Publisher, Publication, PublisherDB, SubscriberDB properties for the subscription, and set the ConnectionContext property to the ServerConnection created in step 1.
Call one of the following methods to return information on Merge Agent sessions for this subscription:
GetSessionsSummary() - returns an array of MergeSessionSummary objects with information on up to the last five Merge Agent sessions. Note the SessionID value for any sessions of interest.
GetSessionsSummary(Int32) - returns an array of MergeSessionSummary objects with information on Merge Agent sessions that have occurred during the past number of hours passed in as the hours parameter (up to the last five sessions). Note the SessionID value for any sessions of interest.
GetLastSessionSummary - returns a MergeSessionSummary object with information on the last Merge Agent session. Note the SessionID value for this session.
GetSessionsSummaryDataSet - returns a DataSet object with information on up to the last five Merge Agent sessions, one in each row. Note the value of the Session_id column for any sessions of interest.
GetLastSessionSummaryDataRow - returns a DataRow object with information on the last Merge Agent session. Note the value of the Session_id column for this session.
(Optional) Call RefreshSessionSummary(MergeSessionSummary%) to refresh the data for the MergeSessionSummary object passed as mss, or call RefreshSessionSummary(DataRow%) to refresh the data in the DataRow object passed as drRefresh.
Using the session ID obtained in step 3, call one of the following methods to return information on the details of a particular session:
GetSessionDetails - returns an array of MergeSessionDetail objects for the supplied sessionID.
GetSessionDetailsDataSet - returns a DataSet object with information for the specified sessionID.
To monitor replication properties for all publications at a Distributor
Create a connection to the Distributor by using the ServerConnection class.
Create an instance of the ReplicationMonitor class.
Set the ConnectionContext property to the ServerConnection created in step 1.
Call the LoadProperties method to get the properties of the object.
Execute one or more of the following methods to return replication information for all Publishers that use this Distributor.
EnumDistributionAgents - returns a DataSet object that contains information about all Distribution Agents at this Distributor.
EnumErrorRecords - returns a DataSet object that contains information about errors stored at the Distributor.
EnumLogReaderAgents - returns a DataSet object that contains information about all Log Reader Agents at the Distributor.
EnumMergeAgents - returns a DataSet object that contains information about all Merge Agents at the Distributor.
EnumMiscellaneousAgents - returns a DataSet object that contains information about all other replication agents at the Distributor.
EnumPublishers - returns a DataSet object that contains information about all Publishers at this Distributor.
EnumPublishers2 - returns a DataSet object that returns the Publishers that use this Distributor.
EnumQueueReaderAgents - returns a DataSet object that contains information about all Queue Reader Agents at the Distributor.
EnumQueueReaderAgentSessionDetails - returns a DataSet object that contains details about the specified Queue Reader Agent and session.
EnumQueueReaderAgentSessions - returns a DataSet object that contains session information about the specified Queue Reader Agent.
EnumSnapshotAgents - returns a DataSet object that contains information about all Snapshot Agents at the Distributor.
To monitor publication properties for a specific Publisher at the Distributor
Create a connection to the Distributor by using the ServerConnection class.
Get a PublisherMonitor object in one of these ways.
Create an instance of the PublisherMonitor class. Set the Name property for the Publisher, and set the ConnectionContext property to the ServerConnection created in step 1. Call the LoadProperties method to get the properties of the object. If this method returns false, either the Publisher name was defined incorrectly or the publication does not exist.
From the PublisherMonitorCollection accessed by means of the PublisherMonitors property of an existing ReplicationMonitor object.
Execute one or more of the following methods to return replication information for all publications that belong to this Publisher.
EnumDistributionAgentSessionDetails - returns a DataSet object that contains details about the specified Distribution Agent and session.
EnumDistributionAgentSessions - returns a DataSet object that contains session information about the specified Distribution Agent.
EnumErrorRecords - returns a DataSet object that contains error record information about the specified error.
EnumLogReaderAgentSessionDetails - returns a DataSet object that contains details for the specified Log Reader Agent and session.
EnumLogReaderAgentSessions - returns a DataSet object that contains session information for the specified Log Reader Agent.
EnumMergeAgentSessionDetails - returns a DataSet object that contains details about the specified Merge Agent and session.
EnumMergeAgentSessionDetails2 - returns a DataSet object that contains additional details about the specified Merge Agent and session.
EnumMergeAgentSessions - returns a DataSet object that contains session information for the specified Merge Agent.
EnumMergeAgentSessions2 - returns a DataSet object that contains additional session information for the specified Merge Agent.
EnumPublications - returns a DataSet object that contains information about all publications at this Distributor.
EnumPublications2 - returns a DataSet object that contains additional information about all publications at this Distributor.
EnumSnapshotAgentSessionDetails - returns a DataSet object that contains details about the specified Snapshot Agent and session.
EnumSnapshotAgentSessions - returns a DataSet object that contains session information for the specified Snapshot Agent.
EnumSubscriptions - returns a DataSet object that contains information about all subscriptions to publications at this Distributor.
To monitor properties for a specific publication at the Distributor
Create a connection to the Distributor by using the ServerConnection class.
Get a PublicationMonitor object in one of these ways.
Create an instance of the PublicationMonitor class. Set the DistributionDBName, PublisherName, PublicationDBName, and Name properties for the publication, and set the ConnectionContext property to the ServerConnection created in step 1. Call the LoadProperties method to get the properties of the object. If this method returns false, either the publication properties were defined incorrectly or the publication does not exist.
From the PublicationMonitorCollection accessed by means of the PublicationMonitors property of an existing PublisherMonitor object.
Execute one or more of the following methods to return information about this publication.
EnumErrorRecords - returns a DataSet object that contains error records about the specified error.
EnumLogReaderAgent - returns a DataSet object that contains information about the Log Reader Agent for this publication.
EnumMonitorThresholds - returns a DataSet object that contains information about the monitor warning thresholds set for this publication.
EnumQueueReaderAgent - returns a DataSet object that contains information about the Queue Reader Agent used by this publication.
EnumSnapshotAgent - returns a DataSet object that contains information about the Snapshot Agent for this publication.
EnumSubscriptions - returns a DataSet object that contains information about subscriptions to this publication.
EnumSubscriptions2 - returns a DataSet object that contains additional information about subscriptions to this publication based on the supplied SubscriptionResultOption.
EnumTracerTokenHistory - returns a DataSet object that contains latency information for the specified tracer token.
EnumTracerTokens - returns a DataSet object that contains information about all tracer tokens inserted into this publication.
To monitor transactional commands that are waiting to be applied at the Subscriber
Create a connection to the Distributor by using the ServerConnection class.
Get a PublicationMonitor object in one of these ways.
Create an instance of the PublicationMonitor class. Set the DistributionDBName, PublisherName, PublicationDBName, and Name properties for the publication, and set the ConnectionContext property to the ServerConnection created in step 1. Call the LoadProperties method to get the properties of the object. If this method returns false, either the publication properties were defined incorrectly or the publication does not exist.
From the PublicationMonitorCollection accessed by means of the PublicationMonitors property of an existing PublisherMonitor object.
Execute the TransPendingCommandInfo method, which returns a PendingCommandInfo object.
Use the properties of this PendingCommandInfo object to determine the estimated number of pending commands and the length of time it will take to complete the delivery of these commands.
To set the monitor warning thresholds for a publication
Create a connection to the Distributor by using the ServerConnection class.
Get a PublicationMonitor object in one of these ways.
Create an instance of the PublicationMonitor class. Set the DistributionDBName, PublisherName, PublicationDBName, and Name properties for the publication, and set the ConnectionContext property to the ServerConnection created in step 1. Call the LoadProperties method to get the properties of the object. If this method returns false, either the publication properties were defined incorrectly or the publication does not exist.
From the PublicationMonitorCollection accessed by means of the PublicationMonitors property of an existing PublisherMonitor object.
Execute the EnumMonitorThresholds method. Note the current threshold settings in the returned ArrayList of MonitorThreshold objects.
Execute the ChangeMonitorThreshold method. Pass the following parameters:
metricID - an Int32 value that represents the monitoring threshold metric from the following table:
Value
Description
1
expiration - monitors for imminent expiration of subscriptions to transactional publications.
2
latency - monitors for the performance of subscriptions to transactional publications.
4
mergeexpiration - monitors for imminent expiration of subscriptions to merge publications.
5
mergeslowrunduration - monitors the duration of merge synchronizations over low-bandwidth (dialup) connections.
6
mergefastrunduration - monitors the duration of merge synchronizations over high-bandwidth (LAN) connections.
7
mergefastrunspeed - monitors the synchronization rate of merge synchronizations over high-bandwidth (LAN) connections.
8
mergeslowrunspeed - monitors the synchronization rate of merge synchronizations over low-bandwidth (dialup) connections.
enable - Boolean value that indicates whether the metric is enabled for the publication.
thresholdValue - integer value that sets the threshold.
shouldAlert - integer that indicates whether this threshold should generate an alert.
[Top]