001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.monitors;
028
029import static org.opends.server.util.ServerConstants.*;
030
031import java.util.LinkedList;
032import java.util.List;
033
034import org.opends.server.admin.std.server.ConnectionHandlerCfg;
035import org.opends.server.admin.std.server.MonitorProviderCfg;
036import org.opends.server.api.ClientConnection;
037import org.opends.server.api.ConnectionHandler;
038import org.opends.server.api.MonitorProvider;
039import org.opends.server.core.DirectoryServer;
040import org.opends.server.types.*;
041
042/**
043 * This class implements a monitor provider that will report generic information
044 * for an enabled Directory Server connection handler, including its protocol,
045 * listeners, and established connections.
046 */
047public class ConnectionHandlerMonitor
048       extends MonitorProvider<MonitorProviderCfg>
049{
050  /** The attribute type that will be used to report the established connections. */
051  private AttributeType connectionsType;
052
053  /** The attribute type that will be used to report the listeners. */
054  private AttributeType listenerType;
055
056  /**
057   * The attribute type that will be used to report the number of established
058   * client connections.
059   */
060  private AttributeType numConnectionsType;
061
062  /** The attribute type that will be used to report the protocol. */
063  private AttributeType protocolType;
064
065  /** The attribute type that will be used to report the config dn . */
066  private AttributeType configDnType;
067
068  /** The connection handler with which this monitor is associated. */
069  private ConnectionHandler<?> connectionHandler;
070
071  /** The name for this monitor. */
072  private String monitorName;
073
074
075
076  /**
077   * Creates a new instance of this connection handler monitor provider that
078   * will work with the provided connection handler.  Most of the initialization
079   * should be handled in the {@code initializeMonitorProvider} method.
080   *
081   * @param  connectionHandler  The connection handler with which this monitor
082   *                            is associated.
083   */
084  public ConnectionHandlerMonitor(
085       ConnectionHandler<? extends ConnectionHandlerCfg> connectionHandler)
086  {
087    this.connectionHandler = connectionHandler;
088  }
089
090
091
092  /** {@inheritDoc} */
093  @Override
094  public void initializeMonitorProvider(MonitorProviderCfg configuration)
095  {
096    monitorName = connectionHandler.getConnectionHandlerName();
097
098    connectionsType = DirectoryServer.getAttributeTypeOrDefault(ATTR_MONITOR_CONNHANDLER_CONNECTION);
099    listenerType = DirectoryServer.getAttributeTypeOrDefault(ATTR_MONITOR_CONNHANDLER_LISTENER);
100    numConnectionsType = DirectoryServer.getAttributeTypeOrDefault(ATTR_MONITOR_CONNHANDLER_NUMCONNECTIONS);
101    protocolType = DirectoryServer.getAttributeTypeOrDefault(ATTR_MONITOR_CONNHANDLER_PROTOCOL);
102    configDnType = DirectoryServer.getAttributeTypeOrDefault(ATTR_MONITOR_CONFIG_DN);
103  }
104
105
106
107  /** {@inheritDoc} */
108  @Override
109  public String getMonitorInstanceName()
110  {
111    return monitorName;
112  }
113
114
115
116  /**
117   * Retrieves the objectclass that should be included in the monitor entry
118   * created from this monitor provider.
119   *
120   * @return  The objectclass that should be included in the monitor entry
121   *          created from this monitor provider.
122   */
123  @Override
124  public ObjectClass getMonitorObjectClass()
125  {
126    return DirectoryConfig.getObjectClass(OC_MONITOR_CONNHANDLER, true);
127  }
128
129
130
131  /** {@inheritDoc} */
132  @Override
133  public List<Attribute> getMonitorData()
134  {
135    LinkedList<Attribute> attrs = new LinkedList<>();
136
137    // Configuration DN
138    attrs.add(Attributes.create(configDnType, connectionHandler.getComponentEntryDN().toString()));
139
140    int numConnections = 0;
141    LinkedList<ClientConnection> conns = new LinkedList<>(connectionHandler.getClientConnections());
142    LinkedList<HostPort> listeners = new LinkedList<>(connectionHandler.getListeners());
143
144    attrs.add(Attributes.create(protocolType, connectionHandler.getProtocol()));
145
146    if (!listeners.isEmpty())
147    {
148      AttributeBuilder builder = new AttributeBuilder(listenerType);
149      builder.addAllStrings(listeners);
150      attrs.add(builder.toAttribute());
151    }
152
153    if (!conns.isEmpty())
154    {
155      AttributeBuilder builder = new AttributeBuilder(connectionsType);
156      for (ClientConnection c : conns)
157      {
158        numConnections++;
159        builder.add(c.getMonitorSummary());
160      }
161      attrs.add(builder.toAttribute());
162    }
163
164    attrs.add(Attributes.create(numConnectionsType, String
165        .valueOf(numConnections)));
166
167    return attrs;
168  }
169}