use of org.opennms.newts.api.Resource 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.newts.api.Resource in project opennms by OpenNMS.
the class NewtsFetchStrategy method getMeasurementsForResourceCallable.
private Callable<Collection<Row<Measurement>>> getMeasurementsForResourceCallable(final String newtsResourceId, final List<Source> listOfSources, final Optional<Timestamp> start, final Optional<Timestamp> end, final LateAggregationParams lag) {
return new Callable<Collection<Row<Measurement>>>() {
@Override
public Collection<Row<Measurement>> call() throws Exception {
ResultDescriptor resultDescriptor = new ResultDescriptor(lag.getInterval());
for (Source source : listOfSources) {
final String metricName = source.getAttribute();
final String name = source.getLabel();
final AggregationFunction fn = toAggregationFunction(source.getAggregation());
resultDescriptor.datasource(name, metricName, lag.getHeartbeat(), fn);
resultDescriptor.export(name);
}
LOG.debug("Querying Newts for resource id {} with result descriptor: {}", newtsResourceId, resultDescriptor);
Results<Measurement> results = m_sampleRepository.select(m_context, new Resource(newtsResourceId), start, end, resultDescriptor, Optional.of(Duration.millis(lag.getStep())), limitConcurrentAggregationsCallback);
Collection<Row<Measurement>> rows = results.getRows();
LOG.debug("Found {} rows.", rows.size());
return rows;
}
};
}
use of org.opennms.newts.api.Resource in project opennms by OpenNMS.
the class NewtsResourceStorageDaoTest method replay.
private void replay() {
EasyMock.expect(m_searcher.search(EasyMock.eq(m_context), EasyMock.anyObject(), EasyMock.anyBoolean())).andAnswer(new IAnswer<SearchResults>() {
public SearchResults answer() throws Throwable {
// Assume there is a single term query
Query q = (Query) EasyMock.getCurrentArguments()[1];
BooleanQuery bq = (BooleanQuery) q;
TermQuery tq = (TermQuery) bq.getClauses().get(0).getQuery();
String field = tq.getTerm().getField("");
String value = tq.getTerm().getValue();
SearchResults searchResults = new SearchResults();
for (Entry<ResourcePath, Set<String>> entry : m_indexedPaths.entrySet()) {
Map<String, String> attributes = Maps.newHashMap();
// Build the indexed attributes and attempt to match them against the given query
NewtsUtils.addIndicesToAttributes(entry.getKey(), attributes);
if (value.equals(attributes.get(field))) {
searchResults.addResult(new Resource(NewtsUtils.toResourceId(entry.getKey())), entry.getValue());
}
}
return searchResults;
}
}).atLeastOnce();
EasyMock.expect(m_searcher.getResourceAttributes(EasyMock.eq(m_context), EasyMock.anyObject())).andReturn(Maps.newHashMap()).anyTimes();
EasyMock.replay(m_searcher);
}
use of org.opennms.newts.api.Resource in project opennms by OpenNMS.
the class RedisResourceMetadataCacheIT method canGetEntriesWithPrefix.
@Test
public void canGetEntriesWithPrefix() {
Context ctx = Context.DEFAULT_CONTEXT;
RedisResourceMetadataCache cache = new RedisResourceMetadataCache(REDIS_HOSTNAME, REDIS_PORT, 8, m_registry, new EscapableResourceIdSplitter());
assertTrue(cache.getResourceIdsWithPrefix(ctx, "a").isEmpty());
Resource resource = new Resource("a:b:c");
ResourceMetadata resourceMetadata = new ResourceMetadata();
cache.merge(ctx, resource, resourceMetadata);
assertTrue(cache.getResourceIdsWithPrefix(ctx, "a").contains("a:b:c"));
assertTrue(cache.getResourceIdsWithPrefix(ctx, "a:b").contains("a:b:c"));
assertTrue(cache.getResourceIdsWithPrefix(ctx, "a:b:c").contains("a:b:c"));
assertTrue(cache.getResourceIdsWithPrefix(ctx, "a:b:c:d").isEmpty());
}
use of org.opennms.newts.api.Resource in project newts by OpenNMS.
the class ResultSerializationTest method testSamples.
@Test
public void testSamples() throws JsonProcessingException {
// Use the optional attributes map at least once.
Map<String, String> attributes = Maps.newHashMap();
attributes.put("units", "bytes");
Results<Sample> data = new Results<>();
data.addElement(new Sample(Timestamp.fromEpochSeconds(900000000), new Resource("localhost"), "ifInOctets", COUNTER, ValueType.compose(5000, COUNTER)));
data.addElement(new Sample(Timestamp.fromEpochSeconds(900000000), new Resource("localhost"), "ifOutOctets", COUNTER, ValueType.compose(6000, COUNTER), attributes));
data.addElement(new Sample(Timestamp.fromEpochSeconds(900000300), new Resource("localhost"), "ifInOctets", COUNTER, ValueType.compose(6000, COUNTER)));
data.addElement(new Sample(Timestamp.fromEpochSeconds(900000300), new Resource("localhost"), "ifOutOctets", COUNTER, ValueType.compose(7000, COUNTER)));
String json = "[" + " [" + " {" + " \"name\": \"ifOutOctets\"," + " \"timestamp\":900000000000," + " \"type\":\"COUNTER\"," + " \"value\":6000," + " \"attributes\":{\"units\":\"bytes\"}" + " }," + " {" + " \"name\": \"ifInOctets\"," + " \"timestamp\":900000000000," + " \"type\":\"COUNTER\"," + " \"value\":5000" + " }" + " ]," + " [" + " {" + " \"name\": \"ifOutOctets\"," + " \"timestamp\":900000300000," + " \"type\":\"COUNTER\"," + " \"value\":7000" + " }," + " {" + " \"name\": \"ifInOctets\"," + " \"timestamp\":900000300000," + " \"type\":\"COUNTER\"," + " \"value\":6000" + " }" + " ]" + "]";
assertThat(new ObjectMapper().writeValueAsString(Transform.sampleDTOs(data)), is(normalize(json)));
}
Aggregations