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