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 2012-2015 ForgeRock AS.
026 */
027package org.opends.server.monitors;
028
029import static org.opends.server.util.ServerConstants.*;
030
031import java.lang.management.ManagementFactory;
032import java.lang.management.RuntimeMXBean;
033import java.net.InetAddress;
034import java.util.ArrayList;
035import java.util.Arrays;
036import java.util.Collection;
037import java.util.Collections;
038import java.util.List;
039
040import javax.net.ssl.SSLContext;
041import javax.net.ssl.SSLParameters;
042
043import org.forgerock.i18n.slf4j.LocalizedLogger;
044import org.forgerock.opendj.config.server.ConfigException;
045import org.opends.server.admin.std.server.SystemInfoMonitorProviderCfg;
046import org.opends.server.api.MonitorProvider;
047import org.opends.server.core.DirectoryServer;
048import org.opends.server.types.Attribute;
049import org.opends.server.types.AttributeBuilder;
050import org.opends.server.types.Attributes;
051import org.opends.server.types.InitializationException;
052
053/**
054 * This class defines a Directory Server monitor provider that can be used to
055 * collect information about the system and the JVM on which the Directory
056 * Server is running.
057 */
058public class SystemInfoMonitorProvider
059       extends MonitorProvider<SystemInfoMonitorProviderCfg>
060{
061  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
062
063  @Override
064  public void initializeMonitorProvider(
065                   SystemInfoMonitorProviderCfg configuration)
066         throws ConfigException, InitializationException
067  {
068    // No initialization is required.
069  }
070
071  @Override
072  public String getMonitorInstanceName()
073  {
074    return "System Information";
075  }
076
077  @Override
078  public List<Attribute> getMonitorData()
079  {
080    ArrayList<Attribute> attrs = new ArrayList<>(13);
081
082    attrs.add(createAttribute("javaVersion", System.getProperty("java.version")));
083    attrs.add(createAttribute("javaVendor", System.getProperty("java.vendor")));
084    attrs.add(createAttribute("jvmVersion", System.getProperty("java.vm.version")));
085    attrs.add(createAttribute("jvmVendor", System.getProperty("java.vm.vendor")));
086    attrs.add(createAttribute("javaHome", System.getProperty("java.home")));
087    attrs.add(createAttribute("classPath", System.getProperty("java.class.path")));
088    attrs.add(createAttribute("workingDirectory", System.getProperty("user.dir")));
089
090    String osInfo = System.getProperty("os.name") + " " +
091                    System.getProperty("os.version") + " " +
092                    System.getProperty("os.arch");
093    attrs.add(createAttribute("operatingSystem", osInfo));
094    String sunOsArchDataModel = System.getProperty("sun.arch.data.model");
095    if (sunOsArchDataModel != null)
096    {
097      String jvmArch = sunOsArchDataModel;
098      if (! sunOsArchDataModel.toLowerCase().equals("unknown"))
099      {
100        jvmArch += "-bit";
101      }
102      attrs.add(createAttribute("jvmArchitecture", jvmArch));
103    }
104    else
105    {
106      attrs.add(createAttribute("jvmArchitecture","unknown"));
107    }
108
109    try
110    {
111      attrs.add(createAttribute("systemName",
112                     InetAddress.getLocalHost().getCanonicalHostName()));
113    }
114    catch (Exception e)
115    {
116      logger.traceException(e);
117    }
118
119
120    Runtime runtime = Runtime.getRuntime();
121    attrs.add(createAttribute("availableCPUs", runtime.availableProcessors()));
122    attrs.add(createAttribute("maxMemory", runtime.maxMemory()));
123    attrs.add(createAttribute("usedMemory", runtime.totalMemory()));
124    attrs.add(createAttribute("freeUsedMemory", runtime.freeMemory()));
125    String installPath = DirectoryServer.getServerRoot();
126    if (installPath != null)
127    {
128      attrs.add(createAttribute("installPath", installPath));
129    }
130    String instancePath = DirectoryServer.getInstanceRoot();
131    if (instancePath != null)
132    {
133      attrs.add(createAttribute("instancePath", instancePath));
134    }
135
136    // Get the JVM input arguments.
137    RuntimeMXBean rtBean = ManagementFactory.getRuntimeMXBean();
138    List<String> jvmArguments = rtBean.getInputArguments();
139    if (jvmArguments != null && ! jvmArguments.isEmpty())
140    {
141      StringBuilder argList = new StringBuilder();
142      for (String jvmArg : jvmArguments)
143      {
144        if (argList.length() > 0)
145        {
146          argList.append(" ");
147        }
148
149        argList.append("\"");
150        argList.append(jvmArg);
151        argList.append("\"");
152      }
153
154      attrs.add(createAttribute("jvmArguments", argList.toString()));
155    }
156
157    // Get the list of supported SSL protocols and ciphers.
158    Collection<String> supportedTlsProtocols;
159    Collection<String> supportedTlsCiphers;
160    try
161    {
162      final SSLContext context = SSLContext.getDefault();
163      final SSLParameters parameters = context.getSupportedSSLParameters();
164      supportedTlsProtocols = Arrays.asList(parameters.getProtocols());
165      supportedTlsCiphers = Arrays.asList(parameters.getCipherSuites());
166    }
167    catch (Exception e)
168    {
169      // A default SSL context should always be available.
170      supportedTlsProtocols = Collections.emptyList();
171      supportedTlsCiphers = Collections.emptyList();
172    }
173
174    addAttribute(attrs, ATTR_SUPPORTED_TLS_PROTOCOLS, supportedTlsProtocols);
175    addAttribute(attrs, ATTR_SUPPORTED_TLS_CIPHERS, supportedTlsCiphers);
176
177    return attrs;
178  }
179
180  private void addAttribute(ArrayList<Attribute> attrs, String attrName, Collection<String> values)
181  {
182    AttributeBuilder builder = new AttributeBuilder(attrName);
183    builder.addAllStrings(values);
184    attrs.add(builder.toAttribute());
185  }
186
187  private Attribute createAttribute(String name, Object value)
188  {
189    return Attributes.create(name, String.valueOf(value));
190  }
191}
192