Search in sources :

Example 1 with JdbcQuery

use of org.opennms.netmgt.config.jdbc.JdbcQuery in project opennms by OpenNMS.

the class JdbcCollectorTest method canCollectNodeLevelResource.

@Test
public void canCollectNodeLevelResource() throws Exception {
    // Build the query
    JdbcQuery query = new JdbcQuery();
    query.setIfType("ignore");
    // This is the group name
    query.setQueryName("someJdbcQuery");
    JdbcColumn column = new JdbcColumn();
    column.setColumnName("someColumnName");
    column.setAlias("someAlias");
    column.setDataType(AttributeType.GAUGE);
    query.addJdbcColumn(column);
    JdbcDataCollection collection = new JdbcDataCollection();
    collection.addQuery(query);
    // Mock the result set
    ResultSet resultSet = mock(ResultSet.class);
    when(resultSet.getRow()).thenReturn(1);
    when(resultSet.getString("someColumnName")).thenReturn("99");
    when(resultSet.next()).thenReturn(true).thenReturn(false);
    // Collect and verify
    CollectionSet collectionSet = collect(collection, resultSet);
    assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    List<String> collectionSetKeys = CollectionSetUtils.flatten(collectionSet);
    assertEquals(Arrays.asList("snmp/1/someJdbcQuery/someAlias[null,99.0]"), collectionSetKeys);
}
Also used : JdbcQuery(org.opennms.netmgt.config.jdbc.JdbcQuery) JdbcColumn(org.opennms.netmgt.config.jdbc.JdbcColumn) JdbcDataCollection(org.opennms.netmgt.config.jdbc.JdbcDataCollection) ResultSet(java.sql.ResultSet) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Test(org.junit.Test)

Example 2 with JdbcQuery

use of org.opennms.netmgt.config.jdbc.JdbcQuery in project opennms by OpenNMS.

the class JdbcCollectorTest method canCollectGenericResource.

@Test
public void canCollectGenericResource() throws Exception {
    // Build the query
    JdbcQuery query = new JdbcQuery();
    query.setQueryName("pg_tablespace_size");
    query.setIfType("all");
    query.setResourceType("pgTableSpace");
    query.setInstanceColumn("spcname");
    JdbcColumn spcnameColumn = new JdbcColumn();
    spcnameColumn.setColumnName("spcname");
    spcnameColumn.setAlias("spcname");
    spcnameColumn.setDataType(AttributeType.STRING);
    query.addJdbcColumn(spcnameColumn);
    JdbcColumn tssizeColumn = new JdbcColumn();
    tssizeColumn.setColumnName("ts_size");
    tssizeColumn.setAlias("ts_size");
    tssizeColumn.setDataType(AttributeType.GAUGE);
    query.addJdbcColumn(tssizeColumn);
    JdbcDataCollection collection = new JdbcDataCollection();
    collection.addQuery(query);
    // Mock the result set
    ResultSet resultSet = mock(ResultSet.class);
    when(resultSet.getRow()).thenReturn(2);
    when(resultSet.getString("spcname")).thenReturn("some: name");
    when(resultSet.getString("ts_size")).thenReturn("41").thenReturn("52");
    when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false);
    // Define the resource type
    ResourceType resourceType = new ResourceType();
    resourceType.setName("pgTableSpace");
    resourceType.setLabel("PostgreSQL Tablespace");
    resourceType.setResourceLabel("${spcname}");
    StorageStrategy storageStrategy = new StorageStrategy();
    storageStrategy.setClazz(IndexStorageStrategy.class.getCanonicalName());
    resourceType.setStorageStrategy(storageStrategy);
    PersistenceSelectorStrategy persistenceSelectorStrategy = new PersistenceSelectorStrategy();
    persistenceSelectorStrategy.setClazz(PersistAllSelectorStrategy.class.getCanonicalName());
    resourceType.setPersistenceSelectorStrategy(persistenceSelectorStrategy);
    // Collect and verify
    CollectionSet collectionSet = collect(collection, resultSet, resourceType);
    assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    List<String> collectionSetKeys = CollectionSetUtils.flatten(collectionSet);
    assertEquals(Arrays.asList("snmp/1/pgTableSpace/some__name/pg_tablespace_size/spcname[some: name,null]", "snmp/1/pgTableSpace/some__name/pg_tablespace_size/ts_size[null,41.0]", "snmp/1/pgTableSpace/some__name/pg_tablespace_size/spcname[some: name,null]", "snmp/1/pgTableSpace/some__name/pg_tablespace_size/ts_size[null,52.0]"), collectionSetKeys);
}
Also used : JdbcQuery(org.opennms.netmgt.config.jdbc.JdbcQuery) StorageStrategy(org.opennms.netmgt.config.datacollection.StorageStrategy) IndexStorageStrategy(org.opennms.netmgt.collection.support.IndexStorageStrategy) PersistAllSelectorStrategy(org.opennms.netmgt.collection.support.PersistAllSelectorStrategy) JdbcColumn(org.opennms.netmgt.config.jdbc.JdbcColumn) IndexStorageStrategy(org.opennms.netmgt.collection.support.IndexStorageStrategy) JdbcDataCollection(org.opennms.netmgt.config.jdbc.JdbcDataCollection) ResultSet(java.sql.ResultSet) ResourceType(org.opennms.netmgt.config.datacollection.ResourceType) PersistenceSelectorStrategy(org.opennms.netmgt.config.datacollection.PersistenceSelectorStrategy) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Test(org.junit.Test)

Example 3 with JdbcQuery

use of org.opennms.netmgt.config.jdbc.JdbcQuery 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)

Aggregations

ResultSet (java.sql.ResultSet)3 JdbcColumn (org.opennms.netmgt.config.jdbc.JdbcColumn)3 JdbcDataCollection (org.opennms.netmgt.config.jdbc.JdbcDataCollection)3 JdbcQuery (org.opennms.netmgt.config.jdbc.JdbcQuery)3 Test (org.junit.Test)2 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 JdbcAgentState (org.opennms.netmgt.collectd.jdbc.JdbcAgentState)1 AttributeType (org.opennms.netmgt.collection.api.AttributeType)1 IndexStorageStrategy (org.opennms.netmgt.collection.support.IndexStorageStrategy)1 PersistAllSelectorStrategy (org.opennms.netmgt.collection.support.PersistAllSelectorStrategy)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 PersistenceSelectorStrategy (org.opennms.netmgt.config.datacollection.PersistenceSelectorStrategy)1 ResourceType (org.opennms.netmgt.config.datacollection.ResourceType)1 StorageStrategy (org.opennms.netmgt.config.datacollection.StorageStrategy)1