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.core.DirectoryServer;
037import org.opends.server.replication.service.ReplicationDomain.ImportExportContext;
038import org.opends.server.types.Attribute;
039import org.opends.server.types.AttributeBuilder;
040import org.opends.server.types.AttributeType;
041import org.opends.server.types.Attributes;
042
043/**
044 * Class used to generate monitoring information for the replication.
045 */
046public class ReplicationMonitor extends MonitorProvider<MonitorProviderCfg>
047{
048  private final ReplicationDomain domain;
049
050  /**
051   * Create a new replication monitor.
052   * @param domain the plugin which created the monitor
053   */
054  ReplicationMonitor(ReplicationDomain domain)
055  {
056    this.domain = domain;
057  }
058
059  /** {@inheritDoc} */
060  @Override
061  public void initializeMonitorProvider(MonitorProviderCfg configuration)
062  {
063    // no implementation needed.
064  }
065
066  /**
067   * Retrieves the name of this monitor provider.  It should be unique among all
068   * monitor providers, including all instances of the same monitor provider.
069   *
070   * @return  The name of this monitor provider.
071   */
072  @Override
073  public String getMonitorInstanceName()
074  {
075    return "Directory server DS(" + domain.getServerId() + ") "
076        + domain.getLocalUrl()
077        + ",cn=" + domain.getBaseDN().toString().replace(',', '_').replace('=', '_')
078        + ",cn=Replication";
079  }
080
081  /**
082   * Retrieves a set of attributes containing monitor data that should be
083   * returned to the client if the corresponding monitor entry is requested.
084   *
085   * @return  A set of attributes containing monitor data that should be
086   *          returned to the client if the corresponding monitor entry is
087   *          requested.
088   */
089  @Override
090  public List<Attribute> getMonitorData()
091  {
092    List<Attribute> attributes = new ArrayList<>();
093
094    attributes.add(Attributes.create("domain-name", String.valueOf(domain.getBaseDN())));
095    attributes.add(Attributes.create("connected-to", domain.getReplicationServer()));
096    addMonitorData(attributes, "lost-connections", domain.getNumLostConnections());
097    addMonitorData(attributes, "received-updates", domain.getNumRcvdUpdates());
098    addMonitorData(attributes, "sent-updates", domain.getNumSentUpdates());
099
100    // get number of changes replayed
101    addMonitorData(attributes, "replayed-updates", domain.getNumProcessedUpdates());
102
103    addMonitorData(attributes, "server-id", domain.getServerId());
104
105    // get window information
106    addMonitorData(attributes, "max-rcv-window", domain.getMaxRcvWindow());
107    addMonitorData(attributes, "current-rcv-window", domain.getCurrentRcvWindow());
108    addMonitorData(attributes, "max-send-window", domain.getMaxSendWindow());
109    addMonitorData(attributes, "current-send-window", domain.getCurrentSendWindow());
110
111    // get the Server State
112    final String ATTR_SERVER_STATE = "server-state";
113    AttributeType type = DirectoryServer.getDefaultAttributeType(ATTR_SERVER_STATE);
114    AttributeBuilder builder = new AttributeBuilder(type, ATTR_SERVER_STATE);
115    builder.addAllStrings(domain.getServerState().toStringSet());
116    attributes.add(builder.toAttribute());
117
118    attributes.add(Attributes.create("ssl-encryption", String.valueOf(domain.isSessionEncrypted())));
119    attributes.add(Attributes.create("generation-id", String.valueOf(domain.getGenerationID())));
120
121    // Add import/export monitoring attributes
122    final ImportExportContext ieContext = domain.getImportExportContext();
123    if (ieContext != null)
124    {
125      addMonitorData(attributes, "total-update", ieContext.importInProgress() ? "import" : "export");
126      addMonitorData(attributes, "total-update-entry-count", ieContext.getTotalEntryCount());
127      addMonitorData(attributes, "total-update-entry-left", ieContext.getLeftEntryCount());
128    }
129
130
131    // Add the concrete Domain attributes
132    attributes.addAll(domain.getAdditionalMonitoring());
133
134    /*
135     * Add assured replication related monitoring fields
136     * (see domain.getXXX() method comment for field meaning)
137     */
138    addMonitorData(attributes, "assured-sr-sent-updates", domain.getAssuredSrSentUpdates());
139    addMonitorData(attributes, "assured-sr-acknowledged-updates", domain.getAssuredSrAcknowledgedUpdates());
140    addMonitorData(attributes, "assured-sr-not-acknowledged-updates", domain.getAssuredSrNotAcknowledgedUpdates());
141    addMonitorData(attributes, "assured-sr-timeout-updates", domain.getAssuredSrTimeoutUpdates());
142    addMonitorData(attributes, "assured-sr-wrong-status-updates", domain.getAssuredSrWrongStatusUpdates());
143    addMonitorData(attributes, "assured-sr-replay-error-updates", domain.getAssuredSrReplayErrorUpdates());
144    addMonitorData(attributes, "assured-sr-server-not-acknowledged-updates", domain
145        .getAssuredSrServerNotAcknowledgedUpdates());
146    addMonitorData(attributes, "assured-sr-received-updates", domain.getAssuredSrReceivedUpdates());
147    addMonitorData(attributes, "assured-sr-received-updates-acked", domain.getAssuredSrReceivedUpdatesAcked());
148    addMonitorData(attributes, "assured-sr-received-updates-not-acked", domain.getAssuredSrReceivedUpdatesNotAcked());
149    addMonitorData(attributes, "assured-sd-sent-updates", domain.getAssuredSdSentUpdates());
150    addMonitorData(attributes, "assured-sd-acknowledged-updates", domain.getAssuredSdAcknowledgedUpdates());
151    addMonitorData(attributes, "assured-sd-timeout-updates", domain.getAssuredSdTimeoutUpdates());
152    addMonitorData(attributes, "assured-sd-server-timeout-updates", domain.getAssuredSdServerTimeoutUpdates());
153
154    // Status related monitoring fields
155    addMonitorData(attributes, "last-status-change-date", domain.getLastStatusChangeDate().toString());
156
157    addMonitorData(attributes, "status", domain.getStatus().toString());
158
159    return attributes;
160  }
161
162  private void addMonitorData(List<Attribute> attributes, String attrType,
163      Map<Integer, Integer> serverIdToNb)
164  {
165    if (!serverIdToNb.isEmpty())
166    {
167      AttributeType type = DirectoryServer.getDefaultAttributeType(attrType);
168      final AttributeBuilder builder = new AttributeBuilder(type, attrType);
169      for (Entry<Integer, Integer> entry : serverIdToNb.entrySet())
170      {
171        final Integer serverId = entry.getKey();
172        final Integer nb = entry.getValue();
173        builder.add(serverId + ":" + nb);
174      }
175      attributes.add(builder.toAttribute());
176    }
177  }
178
179  /**
180   * Add an attribute with an integer value to the list of monitoring
181   * attributes.
182   *
183   * @param attributes the list of monitoring attributes
184   * @param name the name of the attribute to add.
185   * @param value The integer value of he attribute to add.
186   */
187  public static void addMonitorData(List<Attribute> attributes, String name, int value)
188  {
189    addMonitorData(attributes, name, String.valueOf(value));
190  }
191
192  /**
193   * Add an attribute with an integer value to the list of monitoring
194   * attributes.
195   *
196   * @param attributes the list of monitoring attributes
197   * @param name the name of the attribute to add.
198   * @param value The integer value of he attribute to add.
199   */
200  private static void addMonitorData(List<Attribute> attributes, String name, long value)
201  {
202    addMonitorData(attributes, name, String.valueOf(value));
203  }
204
205  /**
206   * Add an attribute with an integer value to the list of monitoring
207   * attributes.
208   *
209   * @param attributes the list of monitoring attributes
210   * @param name the name of the attribute to add.
211   * @param value The String value of he attribute to add.
212   */
213  private static void addMonitorData(List<Attribute> attributes, String name, String value)
214  {
215    AttributeType type = DirectoryServer.getDefaultAttributeType(name);
216    attributes.add(Attributes.create(type, value));
217  }
218}