Search in sources :

Example 1 with JdbcAgentState

use of org.opennms.netmgt.collectd.jdbc.JdbcAgentState in project opennms by OpenNMS.

the class JdbcCollector method collect.

@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> parameters) throws CollectionException {
    final JdbcDataCollection collection = (JdbcDataCollection) parameters.get(JDBC_COLLECTION_KEY);
    JdbcAgentState agentState = null;
    Connection con = null;
    ResultSet results = null;
    Statement stmt = null;
    try {
        agentState = createAgentState(agent.getAddress(), parameters);
        agentState.setupDatabaseConnections(parameters);
        // Create a new collection set.
        CollectionSetBuilder builder = new CollectionSetBuilder(agent);
        // Creating a single resource object, because all node-level metric must belong to the exact same resource.
        final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
        // Cycle through all of the queries for this collection
        for (JdbcQuery query : collection.getQueries()) {
            // Verify if we should check for availability of a query.
            if (agentState.shouldCheckAvailability(query.getQueryName(), query.getRecheckInterval())) {
                // Check to see if the query is available.
                if (!isGroupAvailable(agentState, query)) {
                    LOG.warn("Group is not available.");
                    continue;
                }
            }
            try {
                // If the query is available, lets collect it.
                if (agentState.groupIsAvailable(query.getQueryName())) {
                    if (agentState.getUseDataSourceName()) {
                        initDatabaseConnectionFactory(agentState.getDataSourceName());
                        con = DataSourceFactory.getInstance(agentState.getDataSourceName()).getConnection();
                    } else {
                        con = agentState.getJdbcConnection();
                    }
                    stmt = agentState.createStatement(con);
                    results = agentState.executeJdbcQuery(stmt, query);
                    // Determine if there were any results for this query to                    
                    if (results.isBeforeFirst() && results.isAfterLast()) {
                        LOG.warn("Query '{}' returned no results.", query.getQueryName());
                        // Close the statement, but retain the connection.
                        agentState.closeResultSet(results);
                        agentState.closeStmt(stmt);
                        continue;
                    }
                    // Determine if there are results and how many.
                    results.last();
                    boolean singleInstance = (results.getRow() == 1) ? true : false;
                    results.beforeFirst();
                    // Iterate through each row.
                    while (results.next()) {
                        Resource resource = null;
                        // Create the appropriate resource container.
                        if (singleInstance) {
                            resource = nodeResource;
                        } else {
                            // Retrieve the name of the column to use as the instance key for multi-row queries.
                            String instance = results.getString(query.getInstanceColumn());
                            resource = new DeferredGenericTypeResource(nodeResource, query.getResourceType(), instance);
                        }
                        for (JdbcColumn curColumn : query.getJdbcColumns()) {
                            final AttributeType type = curColumn.getDataType();
                            String columnName = null;
                            if (curColumn.getDataSourceName() != null && curColumn.getDataSourceName().length() != 0) {
                                columnName = curColumn.getDataSourceName();
                            } else {
                                columnName = curColumn.getColumnName();
                            }
                            String columnValue = results.getString(columnName);
                            if (columnValue == null) {
                                LOG.debug("Skipping column named '{}' with null value.", curColumn.getColumnName());
                                continue;
                            }
                            if (type.isNumeric()) {
                                Double numericValue = Double.NaN;
                                try {
                                    numericValue = Double.parseDouble(columnValue);
                                } catch (NumberFormatException e) {
                                    LOG.warn("Value '{}' for column named '{}' cannot be converted to a number. Skipping.", columnValue, curColumn.getColumnName());
                                    continue;
                                }
                                builder.withNumericAttribute(resource, query.getQueryName(), curColumn.getAlias(), numericValue, type);
                            } else {
                                builder.withStringAttribute(resource, query.getQueryName(), curColumn.getAlias(), columnValue);
                            }
                        }
                    }
                }
            } catch (SQLException e) {
                // Close the statement but retain the connection, log the exception and continue to the next query.
                LOG.warn("There was a problem executing query '{}' Please review the query or configuration. Reason: {}", query.getQueryName(), e.getMessage());
                continue;
            } finally {
                agentState.closeResultSet(results);
                agentState.closeStmt(stmt);
                agentState.closeConnection(con);
            }
        }
        builder.withStatus(CollectionStatus.SUCCEEDED);
        return builder.build();
    } finally {
        if (agentState != null) {
            // Make sure that when we're done we close all results, statements and connections.
            agentState.closeResultSet(results);
            agentState.closeStmt(stmt);
            agentState.closeConnection(con);
        //agentState.closeAgentConnection();
        }
    }
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) JdbcColumn(org.opennms.netmgt.config.jdbc.JdbcColumn) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) Resource(org.opennms.netmgt.collection.support.builder.Resource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) JdbcAgentState(org.opennms.netmgt.collectd.jdbc.JdbcAgentState) JdbcQuery(org.opennms.netmgt.config.jdbc.JdbcQuery) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) AttributeType(org.opennms.netmgt.collection.api.AttributeType) JdbcDataCollection(org.opennms.netmgt.config.jdbc.JdbcDataCollection) ResultSet(java.sql.ResultSet)

Example 2 with JdbcAgentState

use of org.opennms.netmgt.collectd.jdbc.JdbcAgentState in project opennms by OpenNMS.

the class JdbcCollectorTest method collect.

public CollectionSet collect(JdbcDataCollection collection, ResultSet resultSet, ResourceType... resourceTypes) throws Exception {
    final int nodeId = 1;
    JdbcDataCollectionConfig config = new JdbcDataCollectionConfig();
    config.setRrdRepository(tempFolder.getRoot().getAbsolutePath());
    JdbcDataCollectionConfigDao jdbcCollectionDao = mock(JdbcDataCollectionConfigDao.class);
    when(jdbcCollectionDao.getConfig()).thenReturn(config);
    when(jdbcCollectionDao.getDataCollectionByName(null)).thenReturn(collection);
    ResourceTypeMapper.getInstance().setResourceTypeMapper((name) -> {
        for (ResourceType resourceType : resourceTypes) {
            if (resourceType.getName().equals(name)) {
                return resourceType;
            }
        }
        return null;
    });
    MyJdbcCollector jdbcCollector = new MyJdbcCollector();
    jdbcCollector.setJdbcCollectionDao(jdbcCollectionDao);
    jdbcCollector.initialize();
    CollectionAgent agent = mock(CollectionAgent.class);
    when(agent.getNodeId()).thenReturn(nodeId);
    when(agent.getAddress()).thenReturn(InetAddressUtils.ONE_TWENTY_SEVEN);
    when(agent.getStorageResourcePath()).thenReturn(ResourcePath.get("snmp", Integer.toString(nodeId)));
    JdbcAgentState jdbcAgentState = mock(JdbcAgentState.class);
    when(jdbcAgentState.groupIsAvailable(any(String.class))).thenReturn(true);
    when(jdbcAgentState.executeJdbcQuery(anyObject(), anyObject())).thenReturn(resultSet);
    jdbcCollector.setJdbcAgentState(jdbcAgentState);
    Map<String, Object> params = new HashMap<>();
    params.putAll(jdbcCollector.getRuntimeAttributes(agent, params));
    CollectionSet collectionSet = jdbcCollector.collect(agent, params);
    return collectionSet;
}
Also used : JdbcDataCollectionConfigDao(org.opennms.netmgt.dao.JdbcDataCollectionConfigDao) HashMap(java.util.HashMap) ResourceType(org.opennms.netmgt.config.datacollection.ResourceType) Matchers.anyObject(org.mockito.Matchers.anyObject) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) JdbcDataCollectionConfig(org.opennms.netmgt.config.jdbc.JdbcDataCollectionConfig) JdbcAgentState(org.opennms.netmgt.collectd.jdbc.JdbcAgentState) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet)

Aggregations

JdbcAgentState (org.opennms.netmgt.collectd.jdbc.JdbcAgentState)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 HashMap (java.util.HashMap)1 Matchers.anyObject (org.mockito.Matchers.anyObject)1 AttributeType (org.opennms.netmgt.collection.api.AttributeType)1 CollectionAgent (org.opennms.netmgt.collection.api.CollectionAgent)1 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)1 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)1 DeferredGenericTypeResource (org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource)1 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)1 Resource (org.opennms.netmgt.collection.support.builder.Resource)1 ResourceType (org.opennms.netmgt.config.datacollection.ResourceType)1 JdbcColumn (org.opennms.netmgt.config.jdbc.JdbcColumn)1 JdbcDataCollection (org.opennms.netmgt.config.jdbc.JdbcDataCollection)1 JdbcDataCollectionConfig (org.opennms.netmgt.config.jdbc.JdbcDataCollectionConfig)1 JdbcQuery (org.opennms.netmgt.config.jdbc.JdbcQuery)1 JdbcDataCollectionConfigDao (org.opennms.netmgt.dao.JdbcDataCollectionConfigDao)1