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);
}
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);
}
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;
}
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);
}
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);
}
}
Aggregations