Search in sources :

Example 6 with JmxServerConnectionWrapper

use of org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper in project opennms by OpenNMS.

the class DefaultConnectionManager method connect.

@Override
public JmxServerConnectionWrapper connect(JmxConnectors connectorName, InetAddress ipAddress, Map<String, String> properties, RetryCallback retryCallback) throws JmxServerConnectionException {
    // if null, use dummy implementation
    if (retryCallback == null) {
        retryCallback = NULL_CALLBACK;
    }
    JmxServerConnectionException lastException = null;
    final JmxServerConnector connector = getConnector(connectorName);
    for (int i = 0; i < retries; i++) {
        LOG.debug("{}/{}: Try connecting to {}", (i + 1), retries, ipAddress);
        retryCallback.onRetry();
        try {
            JmxServerConnectionWrapper connectionWrapper = connector.createConnection(ipAddress, properties);
            if (connectionWrapper == null) {
                throw new JmxServerConnectionException("Received null connection");
            }
            return connectionWrapper;
        } catch (JmxServerConnectionException ex) {
            LOG.debug("Connection could not be established", ex);
            lastException = ex;
        }
    }
    if (lastException != null) {
        throw lastException;
    }
    throw new JmxServerConnectionException("Connection could not be established. Reason: No retries left.");
}
Also used : JmxServerConnectionException(org.opennms.netmgt.jmx.connection.JmxServerConnectionException) JmxServerConnectionWrapper(org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper) JmxServerConnector(org.opennms.netmgt.jmx.connection.JmxServerConnector)

Example 7 with JmxServerConnectionWrapper

use of org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper in project opennms by OpenNMS.

the class DefaultJmxConnector method createConnection.

public JmxServerConnectionWrapper createConnection(JmxConnectionConfig config) throws JmxServerConnectionException {
    try {
        // If we're trying to create a connection to a localhost address...
        if (config.isLocalConnection()) {
            // this JVM's MBeanServer directly.
            return new PlatformMBeanServerConnector().createConnection();
        }
        // Create URL
        final String urlString = config.getUrl();
        final JMXServiceURL url = new JMXServiceURL(urlString);
        LOG.debug("JMX: {} - {}", config.getFactory(), url);
        // Apply password strategy
        final Map<String, Object> env = new HashMap<>();
        config.getPasswordStategy().apply(env, config);
        // Create connection and connect
        final JMXConnector connector = JMXConnectorFactory.newJMXConnector(url, env);
        try {
            connector.connect(env);
        } catch (SecurityException x) {
            throw new JmxServerConnectionException("Security exception: bad credentials", x);
        }
        // Wrap Connection
        MBeanServerConnection connection = connector.getMBeanServerConnection();
        JmxServerConnectionWrapper connectionWrapper = new DefaultConnectionWrapper(connector, connection);
        return connectionWrapper;
    } catch (MalformedURLException e) {
        throw new JmxServerConnectionException(e);
    } catch (IOException e) {
        throw new JmxServerConnectionException(e);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) IOException(java.io.IOException) JmxServerConnectionException(org.opennms.netmgt.jmx.connection.JmxServerConnectionException) JMXConnector(javax.management.remote.JMXConnector) JmxServerConnectionWrapper(org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 8 with JmxServerConnectionWrapper

use of org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper in project opennms by OpenNMS.

the class JMXDetector method isServiceDetected.

public final boolean isServiceDetected(final InetAddress address, Map<String, String> runtimeAttributes) {
    final String ipAddr = InetAddressUtils.str(address);
    final int port = getPort();
    final int retries = getRetries();
    final int timeout = getTimeout();
    LOG.info("isServiceDetected: {}: Checking address: {} for capability on port {}", getServiceName(), ipAddr, port);
    for (int attempts = 0; attempts < retries; attempts++) {
        try (final JmxServerConnectionWrapper client = this.connect(address, port, timeout, runtimeAttributes)) {
            LOG.info("isServiceDetected: {}: Attempting to connect to address: {}, port: {}, attempt: #{}", getServiceName(), ipAddr, port, attempts);
            if (client.getMBeanServerConnection().getMBeanCount() <= 0) {
                return false;
            }
            if (m_object != null) {
                client.getMBeanServerConnection().getObjectInstance(new ObjectName(m_object));
            }
            return true;
        } catch (ConnectException e) {
            // Connection refused!! Continue to retry.
            LOG.info("isServiceDetected: {}: Unable to connect to address: {} port {}, attempt #{}", getServiceName(), ipAddr, port, attempts, e);
        } catch (NoRouteToHostException e) {
            // No Route to host!!!
            LOG.info("isServiceDetected: {}: No route to address {} was available", getServiceName(), ipAddr, e);
        } catch (final PortUnreachableException e) {
            // Port unreachable
            LOG.info("isServiceDetected: {}: Port unreachable while connecting to address {} port {} within timeout: {} attempt: {}", getServiceName(), ipAddr, port, timeout, attempts, e);
        } catch (InterruptedIOException e) {
            // Expected exception
            LOG.info("isServiceDetected: {}: Did not connect to address {} port {} within timeout: {} attempt: {}", getServiceName(), ipAddr, port, timeout, attempts, e);
        } catch (MalformedObjectNameException e) {
            LOG.info("isServiceDetected: {}: Object instance {} is not valid on address {} port {} within timeout: {} attempt: {}", getServiceName(), m_object, ipAddr, port, timeout, attempts, e);
        } catch (InstanceNotFoundException e) {
            LOG.info("isServiceDetected: {}: Object instance {} does not exists on address {} port {} within timeout: {} attempt: {}", getServiceName(), m_object, ipAddr, port, timeout, attempts, e);
        } catch (IOException e) {
            // NMS-8096: Because the JMX connections wrap lower-level exceptions in an IOException,
            // we need to unwrap the exceptions to provide INFO log messages about failures
            boolean loggedIt = false;
            // Unwrap exception
            Throwable cause = e.getCause();
            while (cause != null && loggedIt == false) {
                if (cause instanceof ConnectException) {
                    // Connection refused!! Continue to retry.
                    LOG.info("isServiceDetected: {}: Unable to connect to address: {} port {}, attempt #{}", getServiceName(), ipAddr, port, attempts, e);
                    loggedIt = true;
                } else if (cause instanceof NoRouteToHostException) {
                    // No Route to host!!!
                    LOG.info("isServiceDetected: {}: No route to address {} was available", getServiceName(), ipAddr, e);
                    loggedIt = true;
                } else if (cause instanceof PortUnreachableException) {
                    // Port unreachable
                    LOG.info("isServiceDetected: {}: Port unreachable while connecting to address {} port {} within timeout: {} attempt: {}", getServiceName(), ipAddr, port, timeout, attempts, e);
                    loggedIt = true;
                } else if (cause instanceof InterruptedIOException) {
                    // Expected exception
                    LOG.info("isServiceDetected: {}: Did not connect to address {} port {} within timeout: {} attempt: {}", getServiceName(), ipAddr, port, timeout, attempts, e);
                    loggedIt = true;
                } else if (cause instanceof NameNotFoundException) {
                    LOG.info("isServiceDetected: {}: Name {} not found on address {} port {} within timeout: {} attempt: {}", getServiceName(), m_object, ipAddr, port, timeout, attempts, e);
                    loggedIt = true;
                } else if (cause instanceof MalformedObjectNameException) {
                    LOG.info("isServiceDetected: {}: Object instance {} is not valid on address {} port {} within timeout: {} attempt: {}", getServiceName(), m_object, ipAddr, port, timeout, attempts, e);
                    loggedIt = true;
                } else if (cause instanceof InstanceNotFoundException) {
                    LOG.info("isServiceDetected: {}: Object instance {} does not exists on address {} port {} within timeout: {} attempt: {}", getServiceName(), m_object, ipAddr, port, timeout, attempts, e);
                    loggedIt = true;
                }
                cause = cause.getCause();
            }
            if (!loggedIt) {
                // If none of the causes are an expected type, log an error
                LOG.error("isServiceDetected: {}: An unexpected I/O exception occured contacting address {} port {}", getServiceName(), ipAddr, port, e);
            }
        } catch (Throwable t) {
            LOG.error("isServiceDetected: {}: Unexpected error trying to detect {} on address {} port {}", getServiceName(), getServiceName(), ipAddr, port, t);
        }
    }
    return false;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) PortUnreachableException(java.net.PortUnreachableException) MalformedObjectNameException(javax.management.MalformedObjectNameException) NameNotFoundException(javax.naming.NameNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) NoRouteToHostException(java.net.NoRouteToHostException) ObjectName(javax.management.ObjectName) JmxServerConnectionWrapper(org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper) ConnectException(java.net.ConnectException)

Aggregations

JmxServerConnectionWrapper (org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper)8 JmxServerConnectionException (org.opennms.netmgt.jmx.connection.JmxServerConnectionException)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 ConnectException (java.net.ConnectException)2 MalformedURLException (java.net.MalformedURLException)2 MBeanServerConnection (javax.management.MBeanServerConnection)2 JmxConnectionManager (org.opennms.netmgt.jmx.connection.JmxConnectionManager)2 DefaultConnectionManager (org.opennms.netmgt.jmx.impl.connection.connectors.DefaultConnectionManager)2 InterruptedIOException (java.io.InterruptedIOException)1 InetAddress (java.net.InetAddress)1 NoRouteToHostException (java.net.NoRouteToHostException)1 PortUnreachableException (java.net.PortUnreachableException)1 UnknownHostException (java.net.UnknownHostException)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 JMException (javax.management.JMException)1