001/*
002 * Copyright 2014-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.Collections;
026import java.util.Date;
027import java.util.LinkedHashMap;
028import java.util.Map;
029
030import com.unboundid.ldap.sdk.Entry;
031import com.unboundid.util.NotMutable;
032import com.unboundid.util.StaticUtils;
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 recent
042 * CPU and memory utilization of the underlying system.
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 */
054@NotMutable()
055@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
056public final class HostSystemRecentCPUAndMemoryMonitorEntry
057       extends MonitorEntry
058{
059  /**
060   * The structural object class used in host system recent CPU and memory
061   * monitor entries.
062   */
063  static final String HOST_SYSTEM_RECENT_CPU_AND_MEMORY_MONITOR_OC =
064       "ds-host-system-cpu-memory-monitor-entry";
065
066
067
068  /**
069   * The name of the attribute that contains the recent CPU idle percentage.
070   */
071  private static final String ATTR_RECENT_CPU_IDLE = "recent-cpu-idle";
072
073
074
075  /**
076   * The name of the attribute that contains the recent CPU I/O wait percentage.
077   */
078  private static final String ATTR_RECENT_CPU_IOWAIT = "recent-cpu-iowait";
079
080
081
082  /**
083   * The name of the attribute that contains the recent CPU system percentage.
084   */
085  private static final String ATTR_RECENT_CPU_SYSTEM = "recent-cpu-system";
086
087
088
089  /**
090   * The name of the attribute that contains the recent CPU total busy
091   * percentage.
092   */
093  private static final String ATTR_RECENT_TOTAL_CPU_BUSY = "recent-cpu-used";
094
095
096
097  /**
098   * The name of the attribute that contains the recent CPU user percentage.
099   */
100  private static final String ATTR_RECENT_CPU_USER = "recent-cpu-user";
101
102
103
104  /**
105   * The name of the attribute that contains the recent amount of free system
106   * memory, in gigabytes.
107   */
108  private static final String ATTR_RECENT_MEMORY_FREE_GB =
109       "recent-memory-free-gb";
110
111
112
113  /**
114   * The name of the attribute that contains the recent percent of system memory
115   * that is currently free.
116   */
117  private static final String ATTR_RECENT_MEMORY_FREE_PCT =
118       "recent-memory-pct-free";
119
120
121
122  /**
123   * The name of the attribute that contains the time the information was
124   * last updated.
125   */
126  private static final String ATTR_TIMESTAMP = "timestamp";
127
128
129
130  /**
131   * The name of the attribute that contains the total amount of system memory,
132   * in gigabytes.
133   */
134  private static final String ATTR_TOTAL_MEMORY_GB = "total-memory-gb";
135
136
137
138  /**
139   * The serial version UID for this serializable class.
140   */
141  private static final long serialVersionUID = -4408434740529394905L;
142
143
144
145  // The time the CPU and memory usage information was last updated.
146  private final Date timestamp;
147
148  // The recent CPU idle percent.
149  private final Double recentCPUIdle;
150
151  // The recent CPU I/O wait percent.
152  private final Double recentCPUIOWait;
153
154  // The recent CPU system percent.
155  private final Double recentCPUSystem;
156
157  // The recent CPU total percent busy.
158  private final Double recentCPUTotalBusy;
159
160  // The recent CPU user percent.
161  private final Double recentCPUUser;
162
163  // The recent free memory, in gigabytes.
164  private final Double recentMemoryFreeGB;
165
166  // The recent free memory percent..
167  private final Double recentMemoryPercentFree;
168
169  // The total amount of system memory, in gigabytes.
170  private final Double totalMemoryGB;
171
172
173
174  /**
175   * Creates a new host system recent CPU and memory monitor entry from the
176   * provided entry.
177   *
178   * @param  entry  The entry to be parsed as a host system recent CPU and
179   *                memory monitor entry.  It must not be {@code null}.
180   */
181  public HostSystemRecentCPUAndMemoryMonitorEntry(final Entry entry)
182  {
183    super(entry);
184
185    timestamp = getDate(ATTR_TIMESTAMP);
186    recentCPUIdle = getDouble(ATTR_RECENT_CPU_IDLE);
187    recentCPUIOWait = getDouble(ATTR_RECENT_CPU_IOWAIT);
188    recentCPUSystem = getDouble(ATTR_RECENT_CPU_SYSTEM);
189    recentCPUUser = getDouble(ATTR_RECENT_CPU_USER);
190    recentCPUTotalBusy = getDouble(ATTR_RECENT_TOTAL_CPU_BUSY);
191    recentMemoryFreeGB = getDouble(ATTR_RECENT_MEMORY_FREE_GB);
192    recentMemoryPercentFree = getDouble(ATTR_RECENT_MEMORY_FREE_PCT);
193    totalMemoryGB = getDouble(ATTR_TOTAL_MEMORY_GB);
194  }
195
196
197
198  /**
199   * Retrieves the time that the CPU and memory utilization data was last
200   * updated, if available.
201   *
202   * @return  The time that the CPU and system memory utilization data was
203   *          last updated, or {@code null} if it was not included in the
204   *          monitor entry.
205   */
206  public Date getUpdateTime()
207  {
208    return timestamp;
209  }
210
211
212
213  /**
214   * Retrieves the total percentage of recent CPU time spent in user, system, or
215   * I/O wait states, if available.
216   *
217   * @return  The total percentage of recent CPU time spent in user, system, or
218   *          I/O wait states, or {@code null} if it was not included in the
219   *          monitor entry.
220   */
221  public Double getRecentCPUTotalBusyPercent()
222  {
223    return recentCPUTotalBusy;
224  }
225
226
227
228  /**
229   * Retrieves the percentage of recent CPU time spent in the user state, if
230   * available.
231   *
232   * @return  The percentage of recent CPU time spent in the user state, or
233   *          {@code null} if it was not included in the monitor entry.
234   */
235  public Double getRecentCPUUserPercent()
236  {
237    return recentCPUUser;
238  }
239
240
241
242  /**
243   * Retrieves the percentage of recent CPU time spent in the system state, if
244   * available.
245   *
246   * @return  The percentage of recent CPU time spent in the system state, or
247   *          {@code null} if it was not included in the monitor entry.
248   */
249  public Double getRecentCPUSystemPercent()
250  {
251    return recentCPUSystem;
252  }
253
254
255
256  /**
257   * Retrieves the percentage of recent CPU time spent in the I/O wait state, if
258   * available.
259   *
260   * @return  The percentage of recent CPU time spent in the I/O wait state, or
261   *          {@code null} if it was not included in the monitor entry.
262   */
263  public Double getRecentCPUIOWaitPercent()
264  {
265    return recentCPUIOWait;
266  }
267
268
269
270  /**
271   * Retrieves the percentage of recent CPU idle time, if available.
272   *
273   * @return  The percentage of recent CPU idle time, or {@code null} if it was
274   *          not included in the monitor entry.
275   */
276  public Double getRecentCPUIdlePercent()
277  {
278    return recentCPUIdle;
279  }
280
281
282
283  /**
284   * Retrieves the total amount of system memory in gigabytes, if available.
285   *
286   * @return  The total amount of system memory in gigabytes, or {@code null} if
287   *          it was not included in the monitor entry.
288   */
289  public Double getTotalSystemMemoryGB()
290  {
291    return totalMemoryGB;
292  }
293
294
295
296  /**
297   * Retrieves the recent amount of free system memory in gigabytes, if
298   * available.
299   *
300   * @return  The recent amount of free system memory in gigabytes, or
301   *          {@code null} if it was not included in the monitor entry.
302   */
303  public Double getRecentSystemMemoryFreeGB()
304  {
305    return recentMemoryFreeGB;
306  }
307
308
309
310  /**
311   * Retrieves the recent percentage of free system memory, if available.
312   *
313   * @return  The recent percentage of free system memory, or {@code null} if it
314   *          was not included in the monitor entry.
315   */
316  public Double getRecentSystemMemoryPercentFree()
317  {
318    return recentMemoryPercentFree;
319  }
320
321
322
323  /**
324   * {@inheritDoc}
325   */
326  @Override()
327  public String getMonitorDisplayName()
328  {
329    return INFO_CPU_MEM_MONITOR_DISPNAME.get();
330  }
331
332
333
334  /**
335   * {@inheritDoc}
336   */
337  @Override()
338  public String getMonitorDescription()
339  {
340    return INFO_CPU_MEM_MONITOR_DESC.get();
341  }
342
343
344
345  /**
346   * {@inheritDoc}
347   */
348  @Override()
349  public Map<String,MonitorAttribute> getMonitorAttributes()
350  {
351    final LinkedHashMap<String,MonitorAttribute> attrs =
352         new LinkedHashMap<>(StaticUtils.computeMapCapacity(9));
353
354    if (timestamp != null)
355    {
356      addMonitorAttribute(attrs,
357           ATTR_TIMESTAMP,
358           INFO_CPU_MEM_DISPNAME_TIMESTAMP.get(),
359           INFO_CPU_MEM_DESC_TIMESTAMP.get(),
360           timestamp);
361    }
362
363    if (recentCPUTotalBusy != null)
364    {
365      addMonitorAttribute(attrs,
366           ATTR_RECENT_TOTAL_CPU_BUSY,
367           INFO_CPU_MEM_DISPNAME_RECENT_CPU_TOTAL_BUSY.get(),
368           INFO_CPU_MEM_DESC_RECENT_CPU_TOTAL_BUSY.get(),
369           recentCPUTotalBusy);
370    }
371
372    if (recentCPUUser != null)
373    {
374      addMonitorAttribute(attrs,
375           ATTR_RECENT_CPU_USER,
376           INFO_CPU_MEM_DISPNAME_RECENT_CPU_USER.get(),
377           INFO_CPU_MEM_DESC_RECENT_CPU_USER.get(),
378           recentCPUUser);
379    }
380
381    if (recentCPUSystem != null)
382    {
383      addMonitorAttribute(attrs,
384           ATTR_RECENT_CPU_SYSTEM,
385           INFO_CPU_MEM_DISPNAME_RECENT_CPU_SYSTEM.get(),
386           INFO_CPU_MEM_DESC_RECENT_CPU_SYSTEM.get(),
387           recentCPUSystem);
388    }
389
390    if (recentCPUIOWait != null)
391    {
392      addMonitorAttribute(attrs,
393           ATTR_RECENT_CPU_IOWAIT,
394           INFO_CPU_MEM_DISPNAME_RECENT_CPU_IOWAIT.get(),
395           INFO_CPU_MEM_DESC_RECENT_CPU_IOWAIT.get(),
396           recentCPUIOWait);
397    }
398
399    if (recentCPUIdle != null)
400    {
401      addMonitorAttribute(attrs,
402           ATTR_RECENT_CPU_IDLE,
403           INFO_CPU_MEM_DISPNAME_RECENT_CPU_IDLE.get(),
404           INFO_CPU_MEM_DESC_RECENT_CPU_IDLE.get(),
405           recentCPUIdle);
406    }
407
408    if (totalMemoryGB != null)
409    {
410      addMonitorAttribute(attrs,
411           ATTR_TOTAL_MEMORY_GB,
412           INFO_CPU_MEM_DISPNAME_TOTAL_MEM.get(),
413           INFO_CPU_MEM_DESC_TOTAL_MEM.get(),
414           totalMemoryGB);
415    }
416
417    if (recentMemoryFreeGB != null)
418    {
419      addMonitorAttribute(attrs,
420           ATTR_RECENT_MEMORY_FREE_GB,
421           INFO_CPU_MEM_DISPNAME_FREE_MEM_GB.get(),
422           INFO_CPU_MEM_DESC_FREE_MEM_GB.get(),
423           recentMemoryFreeGB);
424    }
425
426    if (recentMemoryPercentFree != null)
427    {
428      addMonitorAttribute(attrs,
429           ATTR_RECENT_MEMORY_FREE_PCT,
430           INFO_CPU_MEM_DISPNAME_FREE_MEM_PCT.get(),
431           INFO_CPU_MEM_DESC_FREE_MEM_PCT.get(),
432           recentMemoryPercentFree);
433    }
434
435    return Collections.unmodifiableMap(attrs);
436  }
437}