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