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
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2006-2008 Sun Microsystems, Inc.
026 */
027 package org.opends.server.monitors;
028
029
030
031 import java.util.LinkedHashSet;
032 import java.util.LinkedList;
033 import java.util.List;
034
035 import org.opends.server.admin.std.server.MonitorProviderCfg;
036 import org.opends.server.api.Backend;
037 import org.opends.server.api.MonitorProvider;
038 import org.opends.server.loggers.debug.DebugTracer;
039 import org.opends.server.schema.BooleanSyntax;
040 import org.opends.server.types.Attribute;
041 import org.opends.server.types.AttributeType;
042 import org.opends.server.types.AttributeValue;
043 import org.opends.server.types.ByteStringFactory;
044 import org.opends.server.types.DebugLogLevel;
045 import org.opends.server.types.DirectoryConfig;
046 import org.opends.server.types.DN;
047 import org.opends.server.types.ObjectClass;
048
049 import static org.opends.server.loggers.debug.DebugLogger.debugEnabled;
050 import static org.opends.server.loggers.debug.DebugLogger.getTracer;
051 import static org.opends.server.util.ServerConstants.*;
052
053
054
055 /**
056 * This class implements a monitor provider that will report generic information
057 * for an enabled Directory Server backend, including its backend ID, base DNs,
058 * writability mode, and the number of entries it contains.
059 */
060 public class BackendMonitor
061 extends MonitorProvider<MonitorProviderCfg>
062 {
063 // The attribute type that will be used to report the backend ID.
064 private AttributeType backendIDType;
065
066 // The attribute type that will be used to report the set of base DNs.
067 private AttributeType baseDNType;
068
069 // The attribute type that will be used to report the number of entries.
070 private AttributeType entryCountType;
071
072 // The attribute type that will be used to report the number of entries per
073 // base DN.
074 private AttributeType baseDNEntryCountType;
075
076 // The attribute type that will be used to indicate if a backend is private.
077 private AttributeType isPrivateType;
078
079 // The attribute type that will be used to report the writability mode.
080 private AttributeType writabilityModeType;
081
082 // The backend with which this monitor is associated.
083 private Backend backend;
084
085 // The name for this monitor.
086 private String monitorName;
087
088 /**
089 * The tracer object for the debug logger.
090 */
091 private static final DebugTracer TRACER = getTracer();
092
093 /**
094 * Creates a new instance of this backend monitor provider that will work with
095 * the provided backend. Most of the initialization should be handled in the
096 * {@code initializeMonitorProvider} method.
097 *
098 * @param backend The backend with which this monitor is associated.
099 */
100 public BackendMonitor(Backend backend)
101 {
102 super(backend.getBackendID() + " Backend");
103
104
105 this.backend = backend;
106 }
107
108
109
110 /**
111 * {@inheritDoc}
112 */
113 public void initializeMonitorProvider(MonitorProviderCfg configuration)
114 {
115 monitorName = backend.getBackendID() + " Backend";
116
117 backendIDType = DirectoryConfig.getAttributeType(ATTR_MONITOR_BACKEND_ID,
118 true);
119
120 baseDNType = DirectoryConfig.getAttributeType(ATTR_MONITOR_BACKEND_BASE_DN,
121 true);
122
123 entryCountType =
124 DirectoryConfig.getAttributeType(ATTR_MONITOR_BACKEND_ENTRY_COUNT,
125 true);
126
127 baseDNEntryCountType =
128 DirectoryConfig.getAttributeType(ATTR_MONITOR_BASE_DN_ENTRY_COUNT,
129 true);
130
131 isPrivateType =
132 DirectoryConfig.getAttributeType(ATTR_MONITOR_BACKEND_IS_PRIVATE,
133 true);
134
135 writabilityModeType =
136 DirectoryConfig.getAttributeType(ATTR_MONITOR_BACKEND_WRITABILITY_MODE,
137 true);
138 }
139
140
141
142 /**
143 * {@inheritDoc}
144 */
145 public String getMonitorInstanceName()
146 {
147 return monitorName;
148 }
149
150
151
152 /**
153 * Retrieves the objectclass that should be included in the monitor entry
154 * created from this monitor provider.
155 *
156 * @return The objectclass that should be included in the monitor entry
157 * created from this monitor provider.
158 */
159 public ObjectClass getMonitorObjectClass()
160 {
161 return DirectoryConfig.getObjectClass(OC_MONITOR_BACKEND, true);
162 }
163
164
165
166 /**
167 * {@inheritDoc}
168 */
169 public long getUpdateInterval()
170 {
171 // We don't need do anything on a periodic basis.
172 return 0;
173 }
174
175
176
177 /**
178 * {@inheritDoc}
179 */
180 public void updateMonitorData()
181 {
182 // No implementaiton is required.
183 }
184
185
186
187 /**
188 * {@inheritDoc}
189 */
190 public List<Attribute> getMonitorData()
191 {
192 LinkedList<Attribute> attrs = new LinkedList<Attribute>();
193
194 LinkedHashSet<AttributeValue> values = new LinkedHashSet<AttributeValue>();
195 values.add(new AttributeValue(backendIDType,
196 ByteStringFactory.create(backend.getBackendID())));
197 attrs.add(new Attribute(backendIDType, ATTR_MONITOR_BACKEND_ID, values));
198
199 values = new LinkedHashSet<AttributeValue>();
200 DN[] baseDNs = backend.getBaseDNs();
201 for (DN dn : baseDNs)
202 {
203 values.add(new AttributeValue(baseDNType,
204 ByteStringFactory.create(dn.toString())));
205 }
206 attrs.add(new Attribute(baseDNType, ATTR_MONITOR_BACKEND_BASE_DN, values));
207
208 values = new LinkedHashSet<AttributeValue>();
209 values.add(BooleanSyntax.createBooleanValue(backend.isPrivateBackend()));
210 attrs.add(new Attribute(isPrivateType, ATTR_MONITOR_BACKEND_IS_PRIVATE,
211 values));
212
213 values = new LinkedHashSet<AttributeValue>();
214 long backendCount = backend.getEntryCount();
215 values.add(new AttributeValue(entryCountType,
216 ByteStringFactory.create(String.valueOf(backendCount))));
217 attrs.add(new Attribute(entryCountType, ATTR_MONITOR_BACKEND_ENTRY_COUNT,
218 values));
219
220 values = new LinkedHashSet<AttributeValue>();
221 if (baseDNs.length != 1)
222 {
223 for (DN dn : baseDNs)
224 {
225 long entryCount = -1;
226 try
227 {
228 entryCount = backend.numSubordinates(dn, true) + 1;
229 }
230 catch (Exception ex)
231 {
232 if (debugEnabled())
233 {
234 TRACER.debugCaught(DebugLogLevel.ERROR, ex);
235 }
236 }
237 String s = entryCount + " " + dn.toString();
238 values.add(new AttributeValue(baseDNEntryCountType,
239 ByteStringFactory.create(s)));
240 }
241 }
242 else
243 {
244 // This is done to avoid recalculating the number of entries using the
245 // hasNumSubordinates method in the case where the backend has a single
246 // base DN.
247 String s = backendCount + " " + baseDNs[0].toString();
248 values.add(new AttributeValue(baseDNEntryCountType,
249 ByteStringFactory.create(s)));
250 }
251 attrs.add(new Attribute(baseDNEntryCountType,
252 ATTR_MONITOR_BASE_DN_ENTRY_COUNT, values));
253
254 values = new LinkedHashSet<AttributeValue>();
255 values.add(new AttributeValue(writabilityModeType,
256 ByteStringFactory.create(
257 String.valueOf(backend.getWritabilityMode()))));
258 attrs.add(new Attribute(writabilityModeType,
259 ATTR_MONITOR_BACKEND_WRITABILITY_MODE, values));
260
261 return attrs;
262 }
263 }
264