use of org.opennms.netmgt.jmx.connection.JmxServerConnectionException in project opennms by OpenNMS.
the class DetectMBeansJob method execute.
@Override
public JmxDatacollectionConfig execute() throws JobManager.TaskRunException {
final JmxConnectionConfig connectionConfig = new JmxConnectionConfigBuilder().withUrl(config.getConnection()).withUsername(config.getUser()).withPassword(config.getPassword()).build();
try (JmxServerConnectionWrapper connector = new DefaultJmxConnector().createConnection(connectionConfig)) {
final JmxDatacollectionConfiggenerator jmxConfigGenerator = new JmxDatacollectionConfiggenerator(new Slf4jLogAdapter(JmxDatacollectionConfiggenerator.class));
final JmxDatacollectionConfig generatedJmxConfigModel = jmxConfigGenerator.generateJmxConfigModel(connector.getMBeanServerConnection(), "anyservice", !config.isSkipDefaultVM(), config.isSkipNonNumber(), JmxHelper.loadInternalDictionary());
applyFilters(generatedJmxConfigModel);
return generatedJmxConfigModel;
} catch (IOException | MBeanServerQueryException | JMException | JmxServerConnectionException e) {
if (e instanceof UnknownHostException || e.getCause() instanceof UnknownHostException) {
throw new JobManager.TaskRunException(String.format("Unknown host: %s", config.getConnection()), e);
}
if (e instanceof MalformedURLException || e.getCause() instanceof MalformedURLException) {
throw new JobManager.TaskRunException(String.format("Cannot create valid JMX Connection URL. Connection: '%s'", config.getConnection()), e);
}
throw new JobManager.TaskRunException("Error while retrieving MBeans from server.", e);
}
}
use of org.opennms.netmgt.jmx.connection.JmxServerConnectionException in project opennms by OpenNMS.
the class JMXMonitor method poll.
/**
* {@inheritDoc}
*/
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> map) {
final InetAddress ipv4Addr = svc.getAddress();
PollStatus serviceStatus = PollStatus.unavailable();
try {
final Timer timer = new Timer();
final JmxConnectionManager connectionManager = new DefaultConnectionManager(ParameterMap.getKeyedInteger(map, "retry", 3));
final JmxConnectionManager.RetryCallback retryCallback = new JmxConnectionManager.RetryCallback() {
@Override
public void onRetry() {
timer.reset();
}
};
try (JmxServerConnectionWrapper connection = connectionManager.connect(getConnectionName(), ipv4Addr, JmxUtils.convertToStringMap(map), retryCallback)) {
// Start with simple communication
connection.getMBeanServerConnection().getMBeanCount();
// Take time just here to get not influenced by test execution
// time
final long nanoResponseTime = System.nanoTime() - timer.getStartTime();
// Find all variable definitions
final Map<String, Object> variables = Maps.newHashMap();
for (final String key : map.keySet()) {
// Skip fast if it does not start with the prefix
if (!key.startsWith(PARAM_BEAN_PREFIX)) {
continue;
}
// Get the variable name
final String variable = key.substring(PARAM_BEAN_PREFIX.length());
// Get the variable definition
final String definition = ParameterMap.getKeyedString(map, key, null);
// Store wrapper for variable definition
variables.put(variable, ObjectNameWrapper.create(connection.getMBeanServerConnection(), definition));
}
// Find all test definitions
final Map<String, Expression> tests = Maps.newHashMap();
for (final String key : map.keySet()) {
// Skip fast if it does not start with the prefix
if (!key.startsWith(PARAM_TEST_PREFIX)) {
continue;
}
// Get the test name
final String variable = key.substring(PARAM_TEST_PREFIX.length());
// Get the test definition
final String definition = ParameterMap.getKeyedString(map, key, null);
// Build the expression from the definition
final Expression expression = JEXL_ENGINE.createExpression(definition);
// Store expressions
tests.put(variable, expression);
}
// Also handle a single test
if (map.containsKey(PARAM_TEST)) {
// Get the test definition
final String definition = ParameterMap.getKeyedString(map, PARAM_TEST, null);
// Build the expression from the definition
final Expression expression = JEXL_ENGINE.createExpression(definition);
// Store expressions
tests.put(null, expression);
}
// Build the context for all tests
final JexlContext context = new ReadonlyContext(new MapContext(variables));
serviceStatus = PollStatus.up(nanoResponseTime / 1000000.0);
// Execute all tests
for (final Map.Entry<String, Expression> e : tests.entrySet()) {
if (!(boolean) e.getValue().evaluate(context)) {
serviceStatus = PollStatus.down("Test failed: " + e.getKey());
break;
}
}
} catch (JmxServerConnectionException mbse) {
// Number of retries exceeded
String reason = "IOException while polling address: " + ipv4Addr;
LOG.debug(reason);
serviceStatus = PollStatus.unavailable(reason);
}
} catch (Throwable e) {
String reason = "Monitor - failed! " + InetAddressUtils.str(ipv4Addr);
LOG.debug(reason);
serviceStatus = PollStatus.unavailable(reason);
}
return serviceStatus;
}
use of org.opennms.netmgt.jmx.connection.JmxServerConnectionException 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.");
}
use of org.opennms.netmgt.jmx.connection.JmxServerConnectionException 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);
}
}
Aggregations