Search in sources :

Example 1 with JmxDatacollectionConfig

use of org.opennms.xmlns.xsd.config.jmx_datacollection.JmxDatacollectionConfig in project opennms by OpenNMS.

the class JmxDatacollectionConfiggenerator method generateJmxConfigModel.

public JmxDatacollectionConfig generateJmxConfigModel(List<String> ids, MBeanServerConnection mBeanServerConnection, String serviceName, Boolean runStandardVmBeans, Boolean skipNonNumber, Map<String, String> dictionary) throws MBeanServerQueryException, IOException, JMException {
    logger.debug("Startup values: \n serviceName: " + serviceName + "\n runStandardVmBeans: " + runStandardVmBeans + "\n dictionary" + dictionary);
    aliasList.clear();
    aliasMap.clear();
    nameCutter.setDictionary(dictionary);
    final QueryResult queryResult = queryMbeanServer(ids, mBeanServerConnection, runStandardVmBeans);
    final JmxDatacollectionConfig xmlJmxDatacollectionConfig = createJmxDataCollectionConfig(serviceName, rrd);
    final JmxCollection xmlJmxCollection = xmlJmxDatacollectionConfig.getJmxCollection().get(0);
    for (QueryResult.MBeanResult eachMBeanResult : queryResult.getMBeanResults()) {
        final ObjectName objectName = eachMBeanResult.objectName;
        final Mbean xmlMbean = createMbean(objectName);
        final QueryResult.AttributeResult attributeResult = eachMBeanResult.attributeResult;
        for (MBeanAttributeInfo jmxBeanAttributeInfo : attributeResult.attributes) {
            // check for CompositeData
            if ("javax.management.openmbean.CompositeData".equals(jmxBeanAttributeInfo.getType())) {
                CompAttrib compAttrib = createCompAttrib(mBeanServerConnection, objectName, jmxBeanAttributeInfo, skipNonNumber);
                if (compAttrib != null) {
                    xmlMbean.getCompAttrib().add(compAttrib);
                }
            }
            if (skipNonNumber && !numbers.contains(jmxBeanAttributeInfo.getType())) {
                logger.warn("The type of attribute '{}' is '{}' and '--skipNonNumber' is set. Ignoring.", jmxBeanAttributeInfo.getName(), jmxBeanAttributeInfo.getType());
            } else {
                Attrib xmlJmxAttribute = createAttr(jmxBeanAttributeInfo);
                xmlMbean.getAttrib().add(xmlJmxAttribute);
            }
        }
        if (!xmlMbean.getAttrib().isEmpty() || !xmlMbean.getCompAttrib().isEmpty()) {
            xmlJmxCollection.getMbeans().getMbean().add(xmlMbean);
        }
    }
    if (xmlJmxCollection.getMbeans().getMbean().size() != queryResult.getMBeanResults().size()) {
        logger.warn("Queried {} MBeans, but only got {} in the result set.", queryResult.getMBeanResults().size(), xmlJmxCollection.getMbeans().getMbean().size());
    }
    return xmlJmxDatacollectionConfig;
}
Also used : QueryResult(org.opennms.features.jmxconfiggenerator.jmxconfig.query.QueryResult) Mbean(org.opennms.xmlns.xsd.config.jmx_datacollection.Mbean) JmxDatacollectionConfig(org.opennms.xmlns.xsd.config.jmx_datacollection.JmxDatacollectionConfig) CompAttrib(org.opennms.xmlns.xsd.config.jmx_datacollection.CompAttrib) JmxCollection(org.opennms.xmlns.xsd.config.jmx_datacollection.JmxCollection) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) CompAttrib(org.opennms.xmlns.xsd.config.jmx_datacollection.CompAttrib) Attrib(org.opennms.xmlns.xsd.config.jmx_datacollection.Attrib) ObjectName(javax.management.ObjectName)

Example 2 with JmxDatacollectionConfig

use of org.opennms.xmlns.xsd.config.jmx_datacollection.JmxDatacollectionConfig 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);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) Slf4jLogAdapter(org.opennms.features.jmxconfiggenerator.log.Slf4jLogAdapter) MBeanServerQueryException(org.opennms.features.jmxconfiggenerator.jmxconfig.query.MBeanServerQueryException) JmxDatacollectionConfig(org.opennms.xmlns.xsd.config.jmx_datacollection.JmxDatacollectionConfig) IOException(java.io.IOException) JmxConnectionConfig(org.opennms.netmgt.jmx.connection.JmxConnectionConfig) JmxServerConnectionException(org.opennms.netmgt.jmx.connection.JmxServerConnectionException) JmxDatacollectionConfiggenerator(org.opennms.features.jmxconfiggenerator.jmxconfig.JmxDatacollectionConfiggenerator) DefaultJmxConnector(org.opennms.netmgt.jmx.impl.connection.connectors.DefaultJmxConnector) JMException(javax.management.JMException) JmxServerConnectionWrapper(org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper) JmxConnectionConfigBuilder(org.opennms.netmgt.jmx.connection.JmxConnectionConfigBuilder)

Example 3 with JmxDatacollectionConfig

use of org.opennms.xmlns.xsd.config.jmx_datacollection.JmxDatacollectionConfig in project opennms by OpenNMS.

the class MBeansView method createJmxDataCollectionAccordingToSelection.

/**
	 * The whole point was to select/deselect
	 * Mbeans/Attribs/CompMembers/CompAttribs. In this method we simply create a
	 * JmxDatacollectionConfig considering the choices we made in the gui. To do
	 * this, we clone the original <code>JmxDatacollectionConfig</code>
	 * loaded at the beginning. After that we remove all
	 * MBeans/Attribs/CompMembers/CompAttribs and add all selected
	 * MBeans/Attribs/CompMembers/CompAttribs afterwards.
	 *
	 * @return
	 */
private static JmxDatacollectionConfig createJmxDataCollectionAccordingToSelection(final UiModel uiModel, final SelectionManager selectionManager) {
    /*
		 * At First we clone the original collection. This is done, because if
		 * we make any modifications (e.g. deleting not selected elements) the
		 * data isn't available in the GUI, too. To avoid reloading the data
		 * from server, we just clone it.
		 */
    JmxDatacollectionConfig clone = JmxCollectionCloner.clone(uiModel.getRawModel());
    /*
		 * At second we remove all MBeans from original data and get only
		 * selected once.
		 */
    List<Mbean> exportBeans = clone.getJmxCollection().get(0).getMbeans().getMbean();
    exportBeans.clear();
    Iterable<Mbean> selectedMbeans = selectionManager.getSelectedMbeans();
    for (Mbean mbean : selectedMbeans) {
        /*
			 * We remove all Attributes from Mbean, because we only want
			 * selected ones.
			 */
        Mbean exportBean = JmxCollectionCloner.clone(mbean);
        // we only want selected ones :)
        exportBean.getAttrib().clear();
        for (Attrib att : selectionManager.getSelectedAttributes(mbean)) {
            exportBean.getAttrib().add(JmxCollectionCloner.clone(att));
        }
        // we do not add the parent
        if (!exportBean.getAttrib().isEmpty()) {
            exportBeans.add(exportBean);
        }
        /*
			 * We remove all CompAttribs and CompMembers from MBean,
			 * because we only want selected ones.
			 */
        exportBean.getCompAttrib().clear();
        for (CompAttrib compAtt : selectionManager.getSelectedCompositeAttributes(mbean)) {
            CompAttrib cloneCompAtt = JmxCollectionCloner.clone(compAtt);
            cloneCompAtt.getCompMember().clear();
            for (CompMember compMember : selectionManager.getSelectedCompositeMembers(compAtt)) {
                cloneCompAtt.getCompMember().add(JmxCollectionCloner.clone(compMember));
            }
            // we do not add the child
            if (!cloneCompAtt.getCompMember().isEmpty()) {
                exportBean.getCompAttrib().add(cloneCompAtt);
            }
        }
    }
    // we sort the order, so the result is deterministic
    sort(exportBeans);
    // Last but not least, we need to update the service name
    clone.getJmxCollection().get(0).setName(uiModel.getServiceName());
    return clone;
}
Also used : Mbean(org.opennms.xmlns.xsd.config.jmx_datacollection.Mbean) JmxDatacollectionConfig(org.opennms.xmlns.xsd.config.jmx_datacollection.JmxDatacollectionConfig) CompAttrib(org.opennms.xmlns.xsd.config.jmx_datacollection.CompAttrib) CompAttrib(org.opennms.xmlns.xsd.config.jmx_datacollection.CompAttrib) Attrib(org.opennms.xmlns.xsd.config.jmx_datacollection.Attrib) CompMember(org.opennms.xmlns.xsd.config.jmx_datacollection.CompMember)

Example 4 with JmxDatacollectionConfig

use of org.opennms.xmlns.xsd.config.jmx_datacollection.JmxDatacollectionConfig in project opennms by OpenNMS.

the class JmxConfigCreateCommand method execute.

@Override
protected void execute(MBeanServerConnection mbeanServerConnection) throws IOException, MBeanServerQueryException, JMException {
    JmxDatacollectionConfiggenerator jmxConfigGenerator = new JmxDatacollectionConfiggenerator(LOG);
    Map<String, String> dictionary = loadDictionary();
    JmxDatacollectionConfig generateJmxConfigModel = jmxConfigGenerator.generateJmxConfigModel(ids, mbeanServerConnection, serviceName, !skipDefaultVM, skipNonNumber, dictionary);
    jmxConfigGenerator.writeJmxConfigFile(generateJmxConfigModel, outFile);
}
Also used : JmxDatacollectionConfiggenerator(org.opennms.features.jmxconfiggenerator.jmxconfig.JmxDatacollectionConfiggenerator) JmxDatacollectionConfig(org.opennms.xmlns.xsd.config.jmx_datacollection.JmxDatacollectionConfig)

Example 5 with JmxDatacollectionConfig

use of org.opennms.xmlns.xsd.config.jmx_datacollection.JmxDatacollectionConfig in project opennms by OpenNMS.

the class JmxDatacollectionConfiggeneratorTest method testGenerateJmxConfigModelSkipJvmMBeans.

@Test
public void testGenerateJmxConfigModelSkipJvmMBeans() throws MBeanServerQueryException, IOException, JMException {
    JmxDatacollectionConfig jmxConfigModel = jmxConfiggenerator.generateJmxConfigModel(platformMBeanServer, "testService", false, true, dictionary);
    Assert.assertEquals(1, jmxConfigModel.getJmxCollection().size());
    Assert.assertEquals(1, jmxConfigModel.getJmxCollection().get(0).getMbeans().getMbean().size());
    Assert.assertEquals("org.opennms.tools.jmxconfiggenerator.jmxconfig.JmxTest", jmxConfigModel.getJmxCollection().get(0).getMbeans().getMbean().get(0).getName());
    Assert.assertEquals(4, jmxConfigModel.getJmxCollection().get(0).getMbeans().getMbean().get(0).getAttrib().size());
    LOG.info(prettyPrint(jmxConfigModel));
}
Also used : JmxDatacollectionConfig(org.opennms.xmlns.xsd.config.jmx_datacollection.JmxDatacollectionConfig) Test(org.junit.Test)

Aggregations

JmxDatacollectionConfig (org.opennms.xmlns.xsd.config.jmx_datacollection.JmxDatacollectionConfig)11 Mbean (org.opennms.xmlns.xsd.config.jmx_datacollection.Mbean)6 Test (org.junit.Test)5 JmxCollection (org.opennms.xmlns.xsd.config.jmx_datacollection.JmxCollection)3 ArrayList (java.util.ArrayList)2 JmxDatacollectionConfiggenerator (org.opennms.features.jmxconfiggenerator.jmxconfig.JmxDatacollectionConfiggenerator)2 Attrib (org.opennms.xmlns.xsd.config.jmx_datacollection.Attrib)2 CompAttrib (org.opennms.xmlns.xsd.config.jmx_datacollection.CompAttrib)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 UnknownHostException (java.net.UnknownHostException)1 JMException (javax.management.JMException)1 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)1 ObjectName (javax.management.ObjectName)1 MBeanServerQueryException (org.opennms.features.jmxconfiggenerator.jmxconfig.query.MBeanServerQueryException)1 QueryResult (org.opennms.features.jmxconfiggenerator.jmxconfig.query.QueryResult)1 Slf4jLogAdapter (org.opennms.features.jmxconfiggenerator.log.Slf4jLogAdapter)1 JmxConnectionConfig (org.opennms.netmgt.jmx.connection.JmxConnectionConfig)1 JmxConnectionConfigBuilder (org.opennms.netmgt.jmx.connection.JmxConnectionConfigBuilder)1 JmxServerConnectionException (org.opennms.netmgt.jmx.connection.JmxServerConnectionException)1