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