001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2010 Sun Microsystems, Inc.
025 *      Portions copyright 2013-2015 ForgeRock AS.
026 */
027package org.opends.server.replication.service;
028
029import java.util.ArrayList;
030import java.util.List;
031import java.util.Map;
032import java.util.Map.Entry;
033
034import org.opends.server.admin.std.server.MonitorProviderCfg;
035import org.opends.server.api.MonitorProvider;
036import org.opends.server.replication.service.ReplicationDomain.ImportExportContext;
037import org.opends.server.types.Attribute;
038import org.opends.server.types.AttributeBuilder;
039import org.opends.server.types.Attributes;
040
041/**
042 * Class used to generate monitoring information for the replication.
043 */
044public class ReplicationMonitor extends MonitorProvider<MonitorProviderCfg>
045{
046  private final ReplicationDomain domain;
047
048  /**
049   * Create a new replication monitor.
050   * @param domain the plugin which created the monitor
051   */
052  ReplicationMonitor(ReplicationDomain domain)
053  {
054    this.domain = domain;
055  }
056
057  /** {@inheritDoc} */
058  @Override
059  public void initializeMonitorProvider(MonitorProviderCfg configuration)
060  {
061    // no implementation needed.
062  }
063
064  /**
065   * Retrieves the name of this monitor provider.  It should be unique among all
066   * monitor providers, including all instances of the same monitor provider.
067   *
068   * @return  The name of this monitor provider.
069   */
070  @Override
071  public String getMonitorInstanceName()
072  {
073    return "Directory server DS(" + domain.getServerId() + ") "
074        + domain.getLocalUrl()
075        + ",cn=" + domain.getBaseDN().toString().replace(',', '_').replace('=', '_')
076        + ",cn=Replication";
077  }
078
079  /**
080   * Retrieves a set of attributes containing monitor data that should be
081   * returned to the client if the corresponding monitor entry is requested.
082   *
083   * @return  A set of attributes containing monitor data that should be
084   *          returned to the client if the corresponding monitor entry is
085   *          requested.
086   */
087  @Override
088  public List<Attribute> getMonitorData()
089  {
090    List<Attribute> attributes = new ArrayList<>();
091
092    addMonitorData(attributes, "domain-name", domain.getBaseDN());
093    addMonitorData(attributes, "connected-to", domain.getReplicationServer());
094    addMonitorData(attributes, "lost-connections", domain.getNumLostConnections());
095    addMonitorData(attributes, "received-updates", domain.getNumRcvdUpdates());
096    addMonitorData(attributes, "sent-updates", domain.getNumSentUpdates());
097
098    // get number of changes replayed
099    addMonitorData(attributes, "replayed-updates", domain.getNumProcessedUpdates());
100
101    addMonitorData(attributes, "server-id", domain.getServerId());
102
103    // get window information
104    addMonitorData(attributes, "max-rcv-window", domain.getMaxRcvWindow());
105    addMonitorData(attributes, "current-rcv-window", domain.getCurrentRcvWindow());
106    addMonitorData(attributes, "max-send-window", domain.getMaxSendWindow());
107    addMonitorData(attributes, "current-send-window", domain.getCurrentSendWindow());
108
109    // get the Server State
110    final String ATTR_SERVER_STATE = "server-state";
111    AttributeBuilder builder = new AttributeBuilder(ATTR_SERVER_STATE);
112    builder.addAllStrings(domain.getServerState().toStringSet());
113    attributes.add(builder.toAttribute());
114
115    addMonitorData(attributes, "ssl-encryption", domain.isSessionEncrypted());
116    addMonitorData(attributes, "generation-id", domain.getGenerationID());
117
118    // Add import/export monitoring attributes
119    final ImportExportContext ieContext = domain.getImportExportContext();
120    if (ieContext != null)
121    {
122      addMonitorData(attributes, "total-update", ieContext.importInProgress() ? "import" : "export");
123      addMonitorData(attributes, "total-update-entry-count", ieContext.getTotalEntryCount());
124      addMonitorData(attributes, "total-update-entry-left", ieContext.getLeftEntryCount());
125    }
126
127
128    // Add the concrete Domain attributes
129    attributes.addAll(domain.getAdditionalMonitoring());
130
131    /*
132     * Add assured replication related monitoring fields
133     * (see domain.getXXX() method comment for field meaning)
134     */
135    addMonitorData(attributes, "assured-sr-sent-updates", domain.getAssuredSrSentUpdates());
136    addMonitorData(attributes, "assured-sr-acknowledged-updates", domain.getAssuredSrAcknowledgedUpdates());
137    addMonitorData(attributes, "assured-sr-not-acknowledged-updates", domain.getAssuredSrNotAcknowledgedUpdates());
138    addMonitorData(attributes, "assured-sr-timeout-updates", domain.getAssuredSrTimeoutUpdates());
139    addMonitorData(attributes, "assured-sr-wrong-status-updates", domain.getAssuredSrWrongStatusUpdates());
140    addMonitorData(attributes, "assured-sr-replay-error-updates", domain.getAssuredSrReplayErrorUpdates());
141    addMonitorData(attributes, "assured-sr-server-not-acknowledged-updates", domain
142        .getAssuredSrServerNotAcknowledgedUpdates());
143    addMonitorData(attributes, "assured-sr-received-updates", domain.getAssuredSrReceivedUpdates());
144    addMonitorData(attributes, "assured-sr-received-updates-acked", domain.getAssuredSrReceivedUpdatesAcked());
145    addMonitorData(attributes, "assured-sr-received-updates-not-acked", domain.getAssuredSrReceivedUpdatesNotAcked());
146    addMonitorData(attributes, "assured-sd-sent-updates", domain.getAssuredSdSentUpdates());
147    addMonitorData(attributes, "assured-sd-acknowledged-updates", domain.getAssuredSdAcknowledgedUpdates());
148    addMonitorData(attributes, "assured-sd-timeout-updates", domain.getAssuredSdTimeoutUpdates());
149    addMonitorData(attributes, "assured-sd-server-timeout-updates", domain.getAssuredSdServerTimeoutUpdates());
150
151    // Status related monitoring fields
152    addMonitorData(attributes, "last-status-change-date", domain.getLastStatusChangeDate());
153
154    addMonitorData(attributes, "status", domain.getStatus());
155
156    return attributes;
157  }
158
159  private void addMonitorData(List<Attribute> attributes, String attrName,
160      Map<Integer, Integer> serverIdToNb)
161  {
162    if (!serverIdToNb.isEmpty())
163    {
164      final AttributeBuilder builder = new AttributeBuilder(attrName);
165      for (Entry<Integer, Integer> entry : serverIdToNb.entrySet())
166      {
167        final Integer serverId = entry.getKey();
168        final Integer nb = entry.getValue();
169        builder.add(serverId + ":" + nb);
170      }
171      attributes.add(builder.toAttribute());
172    }
173  }
174
175  /**
176   * Adds an attribute with a value to the list of monitoring attributes.
177   *
178   * @param attributes the list of monitoring attributes
179   * @param attrName the name of the attribute to add.
180   * @param value The value of he attribute to add.
181   */
182  public static void addMonitorData(List<Attribute> attributes, String attrName, Object value)
183  {
184    attributes.add(Attributes.create(attrName, String.valueOf(value)));
185  }
186}