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