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 2014-2015 ForgeRock AS
026 */
027package org.opends.server.monitors;
028
029import static org.opends.server.util.ServerConstants.*;
030
031import java.util.Arrays;
032import java.util.LinkedList;
033import java.util.List;
034
035import org.forgerock.i18n.slf4j.LocalizedLogger;
036import org.opends.server.admin.std.server.MonitorProviderCfg;
037import org.opends.server.api.Backend;
038import org.opends.server.api.MonitorProvider;
039import org.opends.server.core.DirectoryServer;
040import org.opends.server.schema.BooleanSyntax;
041import org.opends.server.types.*;
042
043/**
044 * This class implements a monitor provider that will report generic information
045 * for an enabled Directory Server backend, including its backend ID, base DNs,
046 * writability mode, and the number of entries it contains.
047 */
048public class BackendMonitor
049       extends MonitorProvider<MonitorProviderCfg>
050{
051  /** The attribute type that will be used to report the backend ID. */
052  private AttributeType backendIDType;
053  /** The attribute type that will be used to report the set of base DNs. */
054  private AttributeType baseDNType;
055  /** The attribute type that will be used to report the number of entries. */
056  private AttributeType entryCountType;
057  /** The attribute type that will be used to report the number of entries per base DN. */
058  private AttributeType baseDNEntryCountType;
059  /** The attribute type that will be used to indicate if a backend is private. */
060  private AttributeType isPrivateType;
061  /** The attribute type that will be used to report the writability mode. */
062  private AttributeType writabilityModeType;
063
064  /** The backend with which this monitor is associated. */
065  private Backend<?> backend;
066
067  /** The name for this monitor. */
068  private String monitorName;
069  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
070
071  /**
072   * Creates a new instance of this backend monitor provider that will work with
073   * the provided backend.  Most of the initialization should be handled in the
074   * {@code initializeMonitorProvider} method.
075   *
076   * @param  backend  The backend with which this monitor is associated.
077   */
078  public BackendMonitor(Backend<?> backend)
079  {
080    this.backend = backend;
081  }
082
083  @Override
084  public void initializeMonitorProvider(MonitorProviderCfg configuration)
085  {
086    monitorName = backend.getBackendID() + " Backend";
087
088    backendIDType = DirectoryServer.getAttributeTypeOrDefault(ATTR_MONITOR_BACKEND_ID);
089    baseDNType = DirectoryServer.getAttributeTypeOrDefault(ATTR_MONITOR_BACKEND_BASE_DN);
090    entryCountType = DirectoryServer.getAttributeTypeOrDefault(ATTR_MONITOR_BACKEND_ENTRY_COUNT);
091    baseDNEntryCountType = DirectoryServer.getAttributeTypeOrDefault(ATTR_MONITOR_BASE_DN_ENTRY_COUNT);
092    isPrivateType = DirectoryServer.getAttributeTypeOrDefault(ATTR_MONITOR_BACKEND_IS_PRIVATE);
093    writabilityModeType = DirectoryServer.getAttributeTypeOrDefault(ATTR_MONITOR_BACKEND_WRITABILITY_MODE);
094  }
095
096  @Override
097  public String getMonitorInstanceName()
098  {
099    return monitorName;
100  }
101
102  /**
103   * Retrieves the objectclass that should be included in the monitor entry
104   * created from this monitor provider.
105   *
106   * @return  The objectclass that should be included in the monitor entry
107   *          created from this monitor provider.
108   */
109  @Override
110  public ObjectClass getMonitorObjectClass()
111  {
112    return DirectoryConfig.getObjectClass(OC_MONITOR_BACKEND, true);
113  }
114
115  @Override
116  public List<Attribute> getMonitorData()
117  {
118    LinkedList<Attribute> attrs = new LinkedList<>();
119
120    attrs.add(Attributes.create(backendIDType, backend.getBackendID()));
121
122    DN[] baseDNs = backend.getBaseDNs();
123
124    AttributeBuilder builder = new AttributeBuilder(baseDNType);
125    builder.addAllStrings(Arrays.asList(baseDNs));
126    attrs.add(builder.toAttribute());
127
128    attrs.add(Attributes.create(isPrivateType, BooleanSyntax
129        .createBooleanValue(backend.isPrivateBackend())));
130
131    long backendCount = backend.getEntryCount();
132    attrs.add(Attributes.create(entryCountType, String
133        .valueOf(backendCount)));
134
135    builder = new AttributeBuilder(baseDNEntryCountType);
136    if (baseDNs.length != 1)
137    {
138      for (DN dn : baseDNs)
139      {
140        long entryCount = -1;
141        try
142        {
143          entryCount = backend.getNumberOfEntriesInBaseDN(dn);
144        }
145        catch (Exception ex)
146        {
147          logger.traceException(ex);
148        }
149        builder.add(entryCount + " " + dn);
150      }
151    }
152    else
153    {
154      // This is done to avoid recalculating the number of entries
155      // using the numSubordinates method in the case where the
156      // backend has a single base DN.
157      builder.add(backendCount + " " + baseDNs[0]);
158    }
159    attrs.add(builder.toAttribute());
160
161    attrs.add(Attributes.create(writabilityModeType, String
162        .valueOf(backend.getWritabilityMode())));
163
164    return attrs;
165  }
166}
167