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.Collections;
026    import java.util.Date;
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 general information about
048     * the state of the Directory Server.  The general monitor entry is the
049     * top-level monitor entry that is generated by the monitor backend and is the
050     * parent of all monitor entries generated by the registered monitor providers.
051     * Information that may be included in the general monitor entry includes:
052     * <UL>
053     *   <LI>The number of connections currently established to the server.</LI>
054     *   <LI>The maximum number of connections that have been established at any one
055     *       time.</LI>
056     *   <LI>The total number of connections established to the server since
057     *       startup.</LI>
058     *   <LI>The time that the directory server was started.</LI>
059     *   <LI>The current time on the server.</LI>
060     *   <LI>The length of time in milliseconds that the server has been
061     *       online.</LI>
062     *   <LI>A user-friendly string that describes the length of time that the
063     *       server has been online.</LI>
064     *   <LI>The name of the directory server product.</LI>
065     *   <LI>The name of the vendor that provides the directory server.</LI>
066     *   <LI>The server version string.</LI>
067     *   <LI>The DNs of the configuration entries for any third-party extensions
068     *       loaded in the server.</LI>
069     * </UL>
070     * The server should present at most one general monitor entry.  It can be
071     * retrieved using the {@link MonitorManager#getGeneralMonitorEntry} method.
072     * This entry provides specific methods for accessing information about the
073     * server (e.g., the
074     * {@link GeneralMonitorEntry#getCurrentConnections} method can be used
075     * to retrieve the number of connections currently established).  Alternately,
076     * this information may be accessed using the generic API.  See the
077     * {@link MonitorManager} class documentation for an example that demonstrates
078     * the use of the generic API for accessing monitor data.
079     */
080    @NotMutable()
081    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
082    public final class GeneralMonitorEntry
083           extends MonitorEntry
084    {
085      /**
086       * The structural object class used in general monitor entries.
087       */
088      static final String GENERAL_MONITOR_OC = "ds-general-monitor-entry";
089    
090    
091    
092      /**
093       * The name of the attribute that contains the number of connections currently
094       * established to the server.
095       */
096      private static final String ATTR_CURRENT_CONNECTIONS = "currentConnections";
097    
098    
099    
100      /**
101       * The name of the attribute that contains the Directory Server's current
102       * time.
103       */
104      private static final String ATTR_CURRENT_TIME = "currentTime";
105    
106    
107    
108      /**
109       * The name of the attribute that contains the names of any alert types that
110       * have caused the server to be classified as "degraded".
111       */
112      private static final String ATTR_DEGRADED_ALERT_TYPE =
113           "degraded-alert-type";
114    
115    
116    
117      /**
118       * The name of the attribute that contains the server instance name.
119       */
120      private static final String ATTR_INSTANCE_NAME = "instanceName";
121    
122    
123    
124      /**
125       * The name of the attribute that contains the maximum number of concurrent
126       * client connections established since startup.
127       */
128      private static final String ATTR_MAX_CONNECTIONS = "maxConnections";
129    
130    
131    
132      /**
133       * The name of the attribute that contains the Directory Server product name.
134       */
135      private static final String ATTR_PRODUCT_NAME = "productName";
136    
137    
138    
139      /**
140       * The name of the attribute that contains the Directory Server start time.
141       */
142      private static final String ATTR_START_TIME = "startTime";
143    
144    
145    
146      /**
147       * The name of the attribute that contains the Directory Server startup ID.
148       */
149      private static final String ATTR_STARTUP_ID = "startupID";
150    
151    
152    
153      /**
154       * The name of the attribute that contains the Directory Server startup UUID.
155       */
156      private static final String ATTR_STARTUP_UUID = "startupUUID";
157    
158    
159    
160      /**
161       * The name of the attribute that holds the DNs of the configuration entries
162       * for any third-party extensions loaded in the server.
163       */
164      private static final String ATTR_THIRD_PARTY_EXTENSION_DN =
165           "thirdPartyExtensionDN";
166    
167    
168    
169      /**
170       * The name of the attribute that contains the total number of connections
171       * that have been established since startup.
172       */
173      private static final String ATTR_TOTAL_CONNECTIONS = "totalConnections";
174    
175    
176    
177      /**
178       * The name of the attribute that contains the Directory Server's uptime.
179       */
180      private static final String ATTR_UP_TIME = "upTime";
181    
182    
183    
184      /**
185       * The name of the attribute that contains the Directory Server vendor name.
186       */
187      private static final String ATTR_VENDOR_NAME = "productVendor";
188    
189    
190    
191      /**
192       * The name of the attribute that contains the Directory Server version
193       * string.
194       */
195      private static final String ATTR_VERSION = "productVersion";
196    
197    
198    
199      /**
200       * The name of the attribute that contains the names of any alert types that
201       * have caused the server to be classified as "unavailable".
202       */
203      private static final String ATTR_UNAVAILABLE_ALERT_TYPE =
204           "unavailable-alert-type";
205    
206    
207    
208      /**
209       * The serial version UID for this serializable class.
210       */
211      private static final long serialVersionUID = 4262569940859462743L;
212    
213    
214    
215      // The server's current time.
216      private final Date currentTime;
217    
218      // The server's start time.
219      private final Date startTime;
220    
221      // The names of the alert types that have caused the server to be classified
222      // as "degraded".
223      private final List<String> degradedAlertTypes;
224    
225      // The DNs of the config entries for any third-party extensions loaded in the
226      // server.
227      private final List<String> thirdPartyExtensionDNs;
228    
229      // The names of the alert types that have caused the server to be classified
230      // as "unavailable".
231      private final List<String> unavailableAlertTypes;
232    
233      // The number connections currently established.
234      private final Long currentConnections;
235    
236      // The maximum number connections established at any time since startup.
237      private final Long maxConnections;
238    
239      // The total number of connections that have been established since startup.
240      private final Long totalConnections;
241    
242      // The Directory Server instance name.
243      private final String instanceName;
244    
245      // The Directory Server product name.
246      private final String productName;
247    
248      // The Directory Server startup ID.
249      private final String startupID;
250    
251      // The Directory Server startup UUID.
252      private final String startupUUID;
253    
254      // The string representation of the uptime.
255      private final String uptime;
256    
257      // The Directory Server vendor name.
258      private final String vendorName;
259    
260      // The Directory Server version string.
261      private final String versionString;
262    
263    
264    
265      /**
266       * Creates a new general monitor entry from the provided entry.
267       *
268       * @param  entry  The entry to be parsed as a general monitor entry.  It must
269       *                not be {@code null}.
270       */
271      public GeneralMonitorEntry(final Entry entry)
272      {
273        super(entry);
274    
275        currentConnections     = getLong(ATTR_CURRENT_CONNECTIONS);
276        currentTime            = getDate(ATTR_CURRENT_TIME);
277        maxConnections         = getLong(ATTR_MAX_CONNECTIONS);
278        productName            = getString(ATTR_PRODUCT_NAME);
279        startTime              = getDate(ATTR_START_TIME);
280        instanceName           = getString(ATTR_INSTANCE_NAME);
281        startupID              = getString(ATTR_STARTUP_ID);
282        startupUUID            = getString(ATTR_STARTUP_UUID);
283        totalConnections       = getLong(ATTR_TOTAL_CONNECTIONS);
284        uptime                 = getString(ATTR_UP_TIME);
285        vendorName             = getString(ATTR_VENDOR_NAME);
286        versionString          = getString(ATTR_VERSION);
287        degradedAlertTypes     = getStrings(ATTR_DEGRADED_ALERT_TYPE);
288        unavailableAlertTypes  = getStrings(ATTR_UNAVAILABLE_ALERT_TYPE);
289        thirdPartyExtensionDNs = getStrings(ATTR_THIRD_PARTY_EXTENSION_DN);
290      }
291    
292    
293    
294      /**
295       * Retrieves the number of connections currently established.
296       *
297       * @return  The number of connections currently established, or {@code null}
298       *          if it was not included in the monitor entry.
299       */
300      public Long getCurrentConnections()
301      {
302        return currentConnections;
303      }
304    
305    
306    
307      /**
308       * Retrieves the maximum number of concurrent connections established at any
309       * time since startup.
310       *
311       * @return  The maximum number of concurrent connections established at any
312       *          time since startup, or {@code null} if it was not included in the
313       *          monitor entry.
314       */
315      public Long getMaxConnections()
316      {
317        return maxConnections;
318      }
319    
320    
321    
322      /**
323       * Retrieves the total number of connections established since startup.
324       *
325       * @return  The total number of connections established since startup, or
326       *          {@code null} if it was not included in the monitor entry.
327       */
328      public Long getTotalConnections()
329      {
330        return totalConnections;
331      }
332    
333    
334    
335      /**
336       * Retrieves the current time as reported by the Directory Server.
337       *
338       * @return  The current time as reported by the Directory Server, or
339       *          {@code null} if it was not included in the monitor entry.
340       */
341      public Date getCurrentTime()
342      {
343        return currentTime;
344      }
345    
346    
347    
348      /**
349       * Retrieves the time that the Directory Server was started.
350       *
351       * @return  The time that the Directory Server was started, or {@code null} if
352       *          it was not included in the monitor entry.
353       */
354      public Date getStartTime()
355      {
356        return startTime;
357      }
358    
359    
360    
361      /**
362       * Retrieves the name assigned to the Directory Server instance.
363       *
364       * @return  The name assigned to the Directory Server instance, or
365       *          {@code null} if it was not included in the monitor entry.
366       */
367      public String getInstanceName()
368      {
369        return instanceName;
370      }
371    
372    
373    
374      /**
375       * Retrieves a relatively compact identifier generated at the time the
376       * Directory Server was started.
377       *
378       * @return  A relatively compact identifier generated at the time the
379       *          Directory Server was started, or {@code null} if it was not
380       *          included in the monitor entry.
381       */
382      public String getStartupID()
383      {
384        return startupID;
385      }
386    
387    
388    
389      /**
390       * Retrieves the UUID that was generated when the Directory Server was
391       * started.
392       *
393       * @return  The UUID that was generated when the Directory Server was started,
394       *          or {@code null} if it was not included in the monitor entry.
395       */
396      public String getStartupUUID()
397      {
398        return startupUUID;
399      }
400    
401    
402    
403      /**
404       * Retrieves the Directory Server uptime in milliseconds.
405       *
406       * @return  The Directory Server uptime in milliseconds, or {@code null} if
407       *          either the current time or the start time was not available.
408       */
409      public Long getUptimeMillis()
410      {
411        if ((currentTime == null) || (startTime == null))
412        {
413          return null;
414        }
415    
416        return currentTime.getTime() - startTime.getTime();
417      }
418    
419    
420    
421      /**
422       * Retrieves the human-readable string representation of the Directory Server
423       * uptime.
424       *
425       * @return  The human-readable string representation of the Directory Server
426       *          uptime, or {@code null} if it was not included in the monitor
427       *          entry.
428       */
429      public String getUptimeString()
430      {
431        return uptime;
432      }
433    
434    
435    
436      /**
437       * Retrieves the Directory Server product name.
438       *
439       * @return  The Directory Serve product name, or {@code null} if it was not
440       *          included in the monitor entry.
441       */
442      public String getProductName()
443      {
444        return productName;
445      }
446    
447    
448    
449      /**
450       * Retrieves the Directory Server vendor name string.
451       *
452       * @return  The Directory Server vendor name string, or {@code null} if it
453       *          was not included in the monitor entry.
454       */
455      public String getVendorName()
456      {
457        return vendorName;
458      }
459    
460    
461    
462      /**
463       * Retrieves the Directory Server version string.
464       *
465       * @return  The Directory Server version string, or {@code null} if it was not
466       *          included in the monitor entry.
467       */
468      public String getVersionString()
469      {
470        return versionString;
471      }
472    
473    
474    
475      /**
476       * Retrieves the names of any alert types which may have caused the server to
477       * be currently classified as "degraded".
478       *
479       * @return  The names of any alert types which may have caused the server to
480       *          be currently classified as "degraded", or an empty list if it was
481       *          not included in the monitor entry (which likely indicates that the
482       *          server is not classified as "degraded").
483       */
484      public List<String> getDegradedAlertTypes()
485      {
486        return degradedAlertTypes;
487      }
488    
489    
490    
491      /**
492       * Retrieves the names of any alert types which may have caused the server to
493       * be currently classified as "unavailable".
494       *
495       * @return  The names of any alert types which may have caused the server to
496       *          be currently classified as "unavailable", or an empty list if it
497       *          was not included in the monitor entry (which likely indicates that
498       *          the server is not classified as "unavailable").
499       */
500      public List<String> getUnavailableAlertTypes()
501      {
502        return unavailableAlertTypes;
503      }
504    
505    
506    
507      /**
508       * Retrieves the DNs of the configuration entries for any third-party
509       * extensions currently loaded in the server.
510       *
511       * @return  The DNs of the configuration entries for any third-party
512       *          extensions currently loaded in the server, or an empty list if it
513       *          was not included in the monitor entry.
514       */
515      public List<String> getThirdPartyExtensionDNs()
516      {
517        return thirdPartyExtensionDNs;
518      }
519    
520    
521    
522      /**
523       * {@inheritDoc}
524       */
525      @Override()
526      public String getMonitorDisplayName()
527      {
528        return INFO_GENERAL_MONITOR_DISPNAME.get();
529      }
530    
531    
532    
533      /**
534       * {@inheritDoc}
535       */
536      @Override()
537      public String getMonitorDescription()
538      {
539        return INFO_GENERAL_MONITOR_DESC.get();
540      }
541    
542    
543    
544      /**
545       * {@inheritDoc}
546       */
547      @Override()
548      public Map<String,MonitorAttribute> getMonitorAttributes()
549      {
550        final LinkedHashMap<String,MonitorAttribute> attrs =
551             new LinkedHashMap<String,MonitorAttribute>();
552    
553        if (productName != null)
554        {
555          addMonitorAttribute(attrs,
556               ATTR_PRODUCT_NAME,
557               INFO_GENERAL_DISPNAME_PRODUCT_NAME.get(),
558               INFO_GENERAL_DESC_PRODUCT_NAME.get(),
559               productName);
560        }
561    
562        if (vendorName != null)
563        {
564          addMonitorAttribute(attrs,
565               ATTR_VENDOR_NAME,
566               INFO_GENERAL_DISPNAME_VENDOR_NAME.get(),
567               INFO_GENERAL_DESC_VENDOR_NAME.get(),
568               vendorName);
569        }
570    
571        if (versionString != null)
572        {
573          addMonitorAttribute(attrs,
574               ATTR_VERSION,
575               INFO_GENERAL_DISPNAME_VERSION.get(),
576               INFO_GENERAL_DESC_VERSION.get(),
577               versionString);
578        }
579    
580        if (instanceName != null)
581        {
582          addMonitorAttribute(attrs,
583               ATTR_INSTANCE_NAME,
584               INFO_GENERAL_DISPNAME_INSTANCE_NAME.get(),
585               INFO_GENERAL_DESC_INSTANCE_NAME.get(),
586               instanceName);
587        }
588    
589        if (startTime != null)
590        {
591          addMonitorAttribute(attrs,
592               ATTR_START_TIME,
593               INFO_GENERAL_DISPNAME_START_TIME.get(),
594               INFO_GENERAL_DESC_START_TIME.get(),
595               startTime);
596        }
597    
598        if (startupID != null)
599        {
600          addMonitorAttribute(attrs,
601               ATTR_STARTUP_ID,
602               INFO_GENERAL_DISPNAME_STARTUP_ID.get(),
603               INFO_GENERAL_DESC_STARTUP_ID.get(),
604               startupID);
605        }
606    
607        if (startupUUID != null)
608        {
609          addMonitorAttribute(attrs,
610               ATTR_STARTUP_UUID,
611               INFO_GENERAL_DISPNAME_STARTUP_UUID.get(),
612               INFO_GENERAL_DESC_STARTUP_UUID.get(),
613               startupUUID);
614        }
615    
616        if (currentTime != null)
617        {
618          addMonitorAttribute(attrs,
619               ATTR_CURRENT_TIME,
620               INFO_GENERAL_DISPNAME_CURRENT_TIME.get(),
621               INFO_GENERAL_DESC_CURRENT_TIME.get(),
622               currentTime);
623        }
624    
625        if (uptime != null)
626        {
627          addMonitorAttribute(attrs,
628               ATTR_UP_TIME,
629               INFO_GENERAL_DISPNAME_UPTIME.get(),
630               INFO_GENERAL_DESC_UPTIME.get(),
631               uptime);
632        }
633    
634        if ((startTime != null) && (currentTime != null))
635        {
636          addMonitorAttribute(attrs,
637               "upTimeMillis",
638               INFO_GENERAL_DISPNAME_UPTIME_MILLIS.get(),
639               INFO_GENERAL_DESC_UPTIME_MILLIS.get(),
640               Long.valueOf(currentTime.getTime() - startTime.getTime()));
641        }
642    
643        if (currentConnections != null)
644        {
645          addMonitorAttribute(attrs,
646               ATTR_CURRENT_CONNECTIONS,
647               INFO_GENERAL_DISPNAME_CURRENT_CONNECTIONS.get(),
648               INFO_GENERAL_DESC_CURRENT_CONNECTIONS.get(),
649               currentConnections);
650        }
651    
652        if (maxConnections != null)
653        {
654          addMonitorAttribute(attrs,
655               ATTR_MAX_CONNECTIONS,
656               INFO_GENERAL_DISPNAME_MAX_CONNECTIONS.get(),
657               INFO_GENERAL_DESC_MAX_CONNECTIONS.get(),
658               maxConnections);
659        }
660    
661        if (totalConnections != null)
662        {
663          addMonitorAttribute(attrs,
664               ATTR_TOTAL_CONNECTIONS,
665               INFO_GENERAL_DISPNAME_TOTAL_CONNECTIONS.get(),
666               INFO_GENERAL_DESC_TOTAL_CONNECTIONS.get(),
667               totalConnections);
668        }
669    
670        if (! degradedAlertTypes.isEmpty())
671        {
672          addMonitorAttribute(attrs,
673               ATTR_DEGRADED_ALERT_TYPE,
674               INFO_GENERAL_DISPNAME_DEGRADED_ALERT_TYPE.get(),
675               INFO_GENERAL_DESC_DEGRADED_ALERT_TYPE.get(),
676               degradedAlertTypes);
677        }
678    
679        if (! unavailableAlertTypes.isEmpty())
680        {
681          addMonitorAttribute(attrs,
682               ATTR_UNAVAILABLE_ALERT_TYPE,
683               INFO_GENERAL_DISPNAME_UNAVAILABLE_ALERT_TYPE.get(),
684               INFO_GENERAL_DESC_UNAVAILABLE_ALERT_TYPE.get(),
685               unavailableAlertTypes);
686        }
687    
688        if (! thirdPartyExtensionDNs.isEmpty())
689        {
690          addMonitorAttribute(attrs,
691               ATTR_THIRD_PARTY_EXTENSION_DN,
692               INFO_GENERAL_DISPNAME_THIRD_PARTY_EXTENSION_DN.get(),
693               INFO_GENERAL_DESC_THIRD_PARTY_EXTENSION_DN.get(),
694               thirdPartyExtensionDNs);
695        }
696    
697        return Collections.unmodifiableMap(attrs);
698      }
699    }