%--
Document : jmx_connection_test
Created on : May 1, 2014, 3:09:18 PM
Author : nalvarez
Notes:
Add the following line to ${OPENNMS_HOME}/jetty-webapps/opennms/WEB-INF/applicationContext-spring-security.xml:
Add the following lines to ${OPENNMS_HOME}/jetty-webapps/opennms/WEB-INF/jsp/support/index.jsp
just before the last closing tag:
Support Tools
--%>
<%@ page language="java" contentType="text/html" session="true" %>
<%@ page import="java.io.IOException,
java.io.PrintWriter,
java.io.StringWriter,
java.lang.management.RuntimeMXBean,
java.math.BigDecimal,
java.nio.channels.UnresolvedAddressException,
java.rmi.ConnectException,
java.util.Date,
java.util.HashMap,
java.util.Map,
javax.management.MBeanServerConnection,
javax.management.ObjectInstance,
javax.management.ObjectName,
javax.management.openmbean.CompositeData,
javax.management.openmbean.TabularDataSupport,
javax.management.remote.JMXConnector,
javax.management.remote.JMXConnectorFactory,
javax.management.remote.JMXServiceURL,
javax.naming.ServiceUnavailableException" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
String jmxUrl = request.getParameter("jmxUrl");
String jmxUserId = "";
String jmxPassword = "";
String vmName = null;
String vmVendor = null;
String javaVersion = null;
String osName = null;
String osVersion = null;
String osArch = null;
Number availableProcessors = null;
Number physicalMemory = null;
Number uptime = null;
Throwable jmxException = null;
String errorHelp = "";
if (jmxUrl == null) {
jmxUrl = "";
}
pageContext.setAttribute("jmxUrl", jmxUrl == null ? "" : jmxUrl);
if (!jmxUrl.equals("")) {
try {
final JMXServiceURL url = new JMXServiceURL(jmxUrl);
final String protocol = url.getProtocol();
jmxUserId = "opennms";
if (protocol.equals("remoting-jmx")) {
jmxPassword = "0peNm$1!";
} else if (protocol.equals("rmi")) {
jmxPassword = "OpenNMS";
}
final Map env = new HashMap();
env.put(JMXConnector.CREDENTIALS, new String[]{jmxUserId, jmxPassword});
final JMXConnector connector = JMXConnectorFactory.connect(url, env);
final MBeanServerConnection connection = connector.getMBeanServerConnection();
final ObjectName runtimeObjectName = new ObjectName("java.lang:type=Runtime");
final TabularDataSupport systemProperties = (TabularDataSupport) connection.getAttribute(runtimeObjectName, "SystemProperties");
final CompositeData data = systemProperties.get(new Object[]{"java.runtime.version"});
javaVersion = (String) data.get("value");
vmName = (String) connection.getAttribute(runtimeObjectName, "VmName");
vmVendor = (String) connection.getAttribute(runtimeObjectName, "VmVendor");
final ObjectName osObjectName = new ObjectName("java.lang:type=OperatingSystem");
osName = (String) connection.getAttribute(osObjectName, "Name");
osVersion = (String) connection.getAttribute(osObjectName, "Version");
osArch = (String) connection.getAttribute(osObjectName, "Arch");
availableProcessors = (Number) connection.getAttribute(osObjectName, "AvailableProcessors");
physicalMemory = (Number) connection.getAttribute(osObjectName, "TotalPhysicalMemorySize");
uptime = (Number) connection.getAttribute(runtimeObjectName, "Uptime");
} catch (IOException e) {
jmxException = e;
Throwable ce = e;
if (e instanceof IOException
&& e.getCause() instanceof ServiceUnavailableException
&& e.getCause().getCause() instanceof java.rmi.ConnectException) {
ce = e.getCause().getCause();
} else if (e instanceof IOException
&& e.getCause() instanceof javax.naming.CommunicationException
&& e.getCause().getCause() instanceof java.rmi.ConnectIOException
&& e.getCause().getCause().getCause() instanceof java.net.NoRouteToHostException) {
ce = e.getCause().getCause().getCause();
} else if (e instanceof IOException
&& e.getCause() instanceof javax.naming.ConfigurationException
&& e.getCause().getCause() instanceof java.rmi.UnknownHostException) {
ce = e.getCause().getCause();
}
if (ce instanceof java.net.ConnectException
|| ce instanceof java.rmi.ConnectException
|| ce instanceof java.net.NoRouteToHostException) {
final String msg = ce.getMessage();
if (msg.startsWith("Connection refused")) {
errorHelp = "Connection refused: Check if the JMX server is listening on the specified port.";
} else if (msg.startsWith("Connection timed out")) {
errorHelp = "Connection timed out: Check iptables/firewall rules";
} else if (msg.startsWith("No route to host")) {
errorHelp = "No route to host: Check iptables/firewall rules";
}
} else if (ce instanceof java.rmi.UnknownHostException) {
errorHelp = "Unknown host: Check if the host name is correct";
}
} catch (SecurityException e) {
jmxException = e;
if (e.getMessage().startsWith("Authentication")) {
errorHelp = "Authentication failed: Check if the correct username and password are configured on the JMX server";
}
} catch (UnresolvedAddressException e) {
jmxException = e;
errorHelp = "Unknown host: Check if the host name is correct";
} catch (Exception e) {
jmxException = e;
}
}
%>
JMX Connection Test Page
<% if (javaVersion != null) { %>
Test succeeded
Java VM Uptime:
<%
final long uptimeDays = uptime.longValue() / (24 * 60 * 60 * 1000);
final long uptimeHours = (uptime.longValue() / (60 * 60 * 1000)) % 24;
final long uptimeMinutes = (uptime.longValue() / (60 * 1000)) % 60;
final long uptimeSeconds = (uptime.longValue() / 1000) % 60;
%>
<%= uptimeDays %> days <%= uptimeHours %> hours <%= uptimeMinutes %> minutes <%= uptimeSeconds %> seconds
Java VM:
<%= vmName %> (<%= vmVendor %>)
Java Version:
<%= javaVersion %>
Operating System:
<%= osName %> <%= osVersion %> (<%= osArch %>)
Available Processors:
<%= availableProcessors %>
Physical Memory:
<%= BigDecimal.valueOf(physicalMemory.longValue()).divide(BigDecimal.valueOf(1024L * 1024L * 1024L), 2, BigDecimal.ROUND_HALF_EVEN) %> GB
<% } %>
<% if (jmxException != null) { %>
<%
final StringWriter exceptionWriter = new StringWriter();
jmxException.printStackTrace(new PrintWriter(exceptionWriter));
%>
Test failed
<%= errorHelp %>
<<%= new Date() %>>
<%= exceptionWriter %>
<% } %>