001/*
002 * Copyright 2008-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2008-2020 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2015-2020 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.sdk.unboundidds.monitors;
037
038
039
040import java.util.ArrayList;
041import java.util.Collections;
042import java.util.LinkedHashMap;
043import java.util.List;
044import java.util.Map;
045
046import com.unboundid.ldap.sdk.Entry;
047import com.unboundid.util.NotMutable;
048import com.unboundid.util.StaticUtils;
049import com.unboundid.util.ThreadSafety;
050import com.unboundid.util.ThreadSafetyLevel;
051
052import static com.unboundid.ldap.sdk.unboundidds.monitors.MonitorMessages.*;
053
054
055
056/**
057 * This class defines a monitor entry that provides information about the disk
058 * space usage of the Directory Server.
059 * <BR>
060 * <BLOCKQUOTE>
061 *   <B>NOTE:</B>  This class, and other classes within the
062 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
063 *   supported for use against Ping Identity, UnboundID, and
064 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
065 *   for proprietary functionality or for external specifications that are not
066 *   considered stable or mature enough to be guaranteed to work in an
067 *   interoperable way with other types of LDAP servers.
068 * </BLOCKQUOTE>
069 * <BR>
070 * The server should present at most one disk space usage monitor entry.  It
071 * can be retrieved using the
072 * {@link MonitorManager#getDiskSpaceUsageMonitorEntry} method.  The
073 * {@link DiskSpaceUsageMonitorEntry#getDiskSpaceInfo} method may be used
074 * to retrieve information about the components which may consume significant
075 * amounts of disk space, and the
076 * {@link DiskSpaceUsageMonitorEntry#getCurrentState} method may be used to
077 * obtain the current state of the server.  Alternately, this information may be
078 * accessed using the generic API.  See the {@link MonitorManager} class
079 * documentation for an example that demonstrates the use of the generic API for
080 * accessing monitor data.
081 */
082@NotMutable()
083@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
084public final class DiskSpaceUsageMonitorEntry
085       extends MonitorEntry
086{
087  /**
088   * The structural object class used in disk space usage monitor entries.
089   */
090  static final String DISK_SPACE_USAGE_MONITOR_OC =
091       "ds-disk-space-usage-monitor-entry";
092
093
094
095  /**
096   * The name of the attribute that contains information about the current disk
097   * space state for the server.
098   */
099  private static final String ATTR_CURRENT_STATE = "current-disk-space-state";
100
101
102
103  /**
104   * The prefix used for attributes that provide information about the name of
105   * a disk space consumer.
106   */
107  private static final String ATTR_PREFIX_CONSUMER_NAME =
108       "disk-space-consumer-name-";
109
110
111
112  /**
113   * The prefix used for attributes that provide information about the path of
114   * a disk space consumer.
115   */
116  private static final String ATTR_PREFIX_CONSUMER_PATH =
117       "disk-space-consumer-path-";
118
119
120
121  /**
122   * The prefix used for attributes that provide information about total bytes
123   * for a disk space consumer.
124   */
125  private static final String ATTR_PREFIX_CONSUMER_TOTAL_BYTES =
126       "disk-space-consumer-total-bytes-";
127
128
129
130  /**
131   * The prefix used for attributes that provide information about usable bytes
132   * for a disk space consumer.
133   */
134  private static final String ATTR_PREFIX_CONSUMER_USABLE_BYTES =
135       "disk-space-consumer-usable-bytes-";
136
137
138
139  /**
140   * The prefix used for attributes that provide information about usable
141   * percent for a disk space consumer.
142   */
143  private static final String ATTR_PREFIX_CONSUMER_USABLE_PERCENT =
144       "disk-space-consumer-usable-percent-";
145
146
147
148  /**
149   * The serial version UID for this serializable class.
150   */
151  private static final long serialVersionUID = -4717940564786806566L;
152
153
154
155  // The list of disk space info objects parsed from this monitor entry.
156  private final List<DiskSpaceInfo> diskSpaceInfo;
157
158  // The current disk space usage state for the server.
159  private final String currentState;
160
161
162
163  /**
164   * Creates a new disk space usage monitor entry from the provided entry.
165   *
166   * @param  entry  The entry to be parsed as a disk space usage monitor entry.
167   *                It must not be {@code null}.
168   */
169  public DiskSpaceUsageMonitorEntry(final Entry entry)
170  {
171    super(entry);
172
173    currentState = getString(ATTR_CURRENT_STATE);
174
175    int i=1;
176    final ArrayList<DiskSpaceInfo> list = new ArrayList<>(5);
177    while (true)
178    {
179      final String name = getString(ATTR_PREFIX_CONSUMER_NAME + i);
180      if (name == null)
181      {
182        break;
183      }
184
185      final String path = getString(ATTR_PREFIX_CONSUMER_PATH + i);
186      final Long totalBytes = getLong(ATTR_PREFIX_CONSUMER_TOTAL_BYTES + i);
187      final Long usableBytes = getLong(ATTR_PREFIX_CONSUMER_USABLE_BYTES + i);
188      final Long usablePercent =
189           getLong(ATTR_PREFIX_CONSUMER_USABLE_PERCENT + i);
190
191      list.add(new DiskSpaceInfo(name, path, totalBytes, usableBytes,
192                                 usablePercent));
193
194      i++;
195    }
196
197    diskSpaceInfo = Collections.unmodifiableList(list);
198  }
199
200
201
202  /**
203   * Retrieves the current disk space state for the Directory Server.  It may
204   * be one of "normal", "low space warning", "low space error", or "out of
205   * space error".
206   *
207   * @return  The current disk space state for the Directory Server, or
208   *          {@code null} if that information is not available.
209   */
210  public String getCurrentState()
211  {
212    return currentState;
213  }
214
215
216
217  /**
218   * Retrieves a list of information about the disk space consumers defined in
219   * the Directory Server.
220   *
221   * @return  A list of information about the disk space consumers defined in
222   *          the Directory Server.
223   */
224  public List<DiskSpaceInfo> getDiskSpaceInfo()
225  {
226    return diskSpaceInfo;
227  }
228
229
230
231  /**
232   * {@inheritDoc}
233   */
234  @Override()
235  public String getMonitorDisplayName()
236  {
237    return INFO_DISK_SPACE_USAGE_MONITOR_DISPNAME.get();
238  }
239
240
241
242  /**
243   * {@inheritDoc}
244   */
245  @Override()
246  public String getMonitorDescription()
247  {
248    return INFO_DISK_SPACE_USAGE_MONITOR_DESC.get();
249  }
250
251
252
253  /**
254   * {@inheritDoc}
255   */
256  @Override()
257  public Map<String,MonitorAttribute> getMonitorAttributes()
258  {
259    final LinkedHashMap<String,MonitorAttribute> attrs =
260         new LinkedHashMap<>(StaticUtils.computeMapCapacity(10));
261
262    if (currentState != null)
263    {
264      addMonitorAttribute(attrs,
265           ATTR_CURRENT_STATE,
266           INFO_DISK_SPACE_USAGE_DISPNAME_CURRENT_STATE.get(),
267           INFO_DISK_SPACE_USAGE_DESC_CURRENT_STATE.get(),
268           currentState);
269    }
270
271    if (! diskSpaceInfo.isEmpty())
272    {
273      int i=1;
274      for (final DiskSpaceInfo info : diskSpaceInfo)
275      {
276        if (info.getConsumerName() != null)
277        {
278          addMonitorAttribute(attrs,
279               ATTR_PREFIX_CONSUMER_NAME + i,
280               INFO_DISK_SPACE_USAGE_DISPNAME_DISK_SPACE_CONSUMER_PREFIX.get() +
281                    i + INFO_DISK_SPACE_USAGE_DISPNAME_NAME_SUFFIX.get(),
282               INFO_DISK_SPACE_USAGE_DESC_NAME.get(),
283               info.getConsumerName());
284        }
285
286        if (info.getPath() != null)
287        {
288          addMonitorAttribute(attrs,
289               ATTR_PREFIX_CONSUMER_PATH + i,
290               INFO_DISK_SPACE_USAGE_DISPNAME_DISK_SPACE_CONSUMER_PREFIX.get() +
291                    i + INFO_DISK_SPACE_USAGE_DISPNAME_PATH_SUFFIX.get(),
292               INFO_DISK_SPACE_USAGE_DESC_PATH.get(),
293               info.getPath());
294        }
295
296        if (info.getTotalBytes() != null)
297        {
298          addMonitorAttribute(attrs,
299               ATTR_PREFIX_CONSUMER_TOTAL_BYTES + i,
300               INFO_DISK_SPACE_USAGE_DISPNAME_DISK_SPACE_CONSUMER_PREFIX.get() +
301                    i + INFO_DISK_SPACE_USAGE_DISPNAME_TOTAL_BYTES_SUFFIX.get(),
302               INFO_DISK_SPACE_USAGE_DESC_TOTAL_BYTES.get(),
303               info.getTotalBytes());
304        }
305
306        if (info.getUsableBytes() != null)
307        {
308          addMonitorAttribute(attrs,
309               ATTR_PREFIX_CONSUMER_USABLE_BYTES + i,
310               INFO_DISK_SPACE_USAGE_DISPNAME_DISK_SPACE_CONSUMER_PREFIX.get() +
311                    i +
312                    INFO_DISK_SPACE_USAGE_DISPNAME_USABLE_BYTES_SUFFIX.get(),
313               INFO_DISK_SPACE_USAGE_DESC_USABLE_BYTES.get(),
314               info.getUsableBytes());
315        }
316
317        if (info.getUsableBytes() != null)
318        {
319          addMonitorAttribute(attrs,
320               ATTR_PREFIX_CONSUMER_USABLE_PERCENT + i,
321               INFO_DISK_SPACE_USAGE_DISPNAME_DISK_SPACE_CONSUMER_PREFIX.get() +
322                    i +
323                    INFO_DISK_SPACE_USAGE_DISPNAME_USABLE_PERCENT_SUFFIX.get(),
324               INFO_DISK_SPACE_USAGE_DESC_USABLE_PERCENT.get(),
325               info.getUsablePercent());
326        }
327
328        i++;
329      }
330    }
331
332    return Collections.unmodifiableMap(attrs);
333  }
334}