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