Search in sources :

Example 1 with CollectionSet

use of org.opennms.netmgt.collection.api.CollectionSet 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 CollectionSet

use of org.opennms.netmgt.collection.api.CollectionSet 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 CollectionSet

use of org.opennms.netmgt.collection.api.CollectionSet 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)

Example 4 with CollectionSet

use of org.opennms.netmgt.collection.api.CollectionSet in project opennms by OpenNMS.

the class NewtsPersisterIT method canPersist.

@Test
public void canPersist() throws InterruptedException {
    ServiceParameters params = new ServiceParameters(Collections.emptyMap());
    RrdRepository repo = new RrdRepository();
    // Only the last element of the path matters here
    repo.setRrdBaseDir(Paths.get("a", "path", "that", "ends", "with", "snmp").toFile());
    Persister persister = m_persisterFactory.createPersister(params, repo);
    int nodeId = 1;
    CollectionAgent agent = mock(CollectionAgent.class);
    when(agent.getStorageResourcePath()).thenReturn(ResourcePath.get(Integer.toString(nodeId)));
    NodeLevelResource nodeLevelResource = new NodeLevelResource(nodeId);
    // Build a collection set with a single sample
    Timestamp now = Timestamp.now();
    CollectionSet collectionSet = new CollectionSetBuilder(agent).withNumericAttribute(nodeLevelResource, "metrics", "metric", 900, AttributeType.GAUGE).withTimestamp(now.asDate()).build();
    // Persist
    collectionSet.visit(persister);
    // Wait for the sample(s) to be flushed
    Thread.sleep(5 * 1000);
    // Fetch the (persisted) sample
    Resource resource = new Resource("snmp:1:metrics");
    Timestamp end = Timestamp.now();
    Results<Sample> samples = m_sampleRepository.select(Context.DEFAULT_CONTEXT, resource, Optional.of(now), Optional.of(end));
    assertEquals(1, samples.getRows().size());
    Row<Sample> row = samples.getRows().iterator().next();
    assertEquals(900, row.getElement("metric").getValue().doubleValue(), 0.00001);
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) Sample(org.opennms.newts.api.Sample) Resource(org.opennms.newts.api.Resource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) Timestamp(org.opennms.newts.api.Timestamp) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) Persister(org.opennms.netmgt.collection.api.Persister) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) Test(org.junit.Test)

Example 5 with CollectionSet

use of org.opennms.netmgt.collection.api.CollectionSet in project opennms by OpenNMS.

the class CollectorTestUtils method collectNTimes.

public static void collectNTimes(RrdStrategy<?, ?> rrdStrategy, ResourceStorageDao resourceStorageDao, CollectionSpecification spec, CollectionAgent agent, int numUpdates) throws InterruptedException, CollectionException {
    for (int i = 0; i < numUpdates; i++) {
        // now do the actual collection
        CollectionSet collectionSet = spec.collect(agent);
        assertEquals("collection status", CollectionStatus.SUCCEEDED, collectionSet.getStatus());
        persistCollectionSet(rrdStrategy, resourceStorageDao, spec, collectionSet);
        System.err.println("COLLECTION " + i + " FINISHED");
        //need a one second time elapse to update the RRD
        Thread.sleep(1010);
    }
}
Also used : CollectionSet(org.opennms.netmgt.collection.api.CollectionSet)

Aggregations

CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)52 Test (org.junit.Test)30 HashMap (java.util.HashMap)17 CollectionAgent (org.opennms.netmgt.collection.api.CollectionAgent)17 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)11 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)11 CollectionSetVisitor (org.opennms.netmgt.collection.api.CollectionSetVisitor)10 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)10 File (java.io.File)9 RrdRepository (org.opennms.netmgt.rrd.RrdRepository)8 RrdDb (org.jrobin.core.RrdDb)7 JUnitCollector (org.opennms.core.collection.test.JUnitCollector)7 OnmsNode (org.opennms.netmgt.model.OnmsNode)7 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)6 JUnitSnmpAgent (org.opennms.core.test.snmp.annotations.JUnitSnmpAgent)6 Map (java.util.Map)5 Datasource (org.jrobin.core.Datasource)5 Date (java.util.Date)4 GenericTypeResource (org.opennms.netmgt.collection.support.builder.GenericTypeResource)4 ResourcePath (org.opennms.netmgt.model.ResourcePath)4