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