Search in sources :

Example 16 with Resource

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);
}
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 17 with Resource

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;
        }
    };
}
Also used : AggregationFunction(org.opennms.newts.api.query.AggregationFunction) Measurement(org.opennms.newts.api.Measurement) OnmsResource(org.opennms.netmgt.model.OnmsResource) Resource(org.opennms.newts.api.Resource) ResultDescriptor(org.opennms.newts.api.query.ResultDescriptor) Row(org.opennms.newts.api.Results.Row) Callable(java.util.concurrent.Callable) Source(org.opennms.netmgt.measurements.model.Source)

Example 18 with Resource

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);
}
Also used : IAnswer(org.easymock.IAnswer) BooleanQuery(org.opennms.newts.api.search.BooleanQuery) TermQuery(org.opennms.newts.api.search.TermQuery) ResourcePath(org.opennms.netmgt.model.ResourcePath) Set(java.util.Set) TermQuery(org.opennms.newts.api.search.TermQuery) BooleanQuery(org.opennms.newts.api.search.BooleanQuery) Query(org.opennms.newts.api.search.Query) Resource(org.opennms.newts.api.Resource) SearchResults(org.opennms.newts.api.search.SearchResults)

Example 19 with Resource

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());
}
Also used : Context(org.opennms.newts.api.Context) EscapableResourceIdSplitter(org.opennms.newts.cassandra.search.EscapableResourceIdSplitter) Resource(org.opennms.newts.api.Resource) ResourceMetadata(org.opennms.newts.cassandra.search.ResourceMetadata) Test(org.junit.Test)

Example 20 with Resource

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)));
}
Also used : Results(org.opennms.newts.api.Results) SearchResults(org.opennms.newts.api.search.SearchResults) Sample(org.opennms.newts.api.Sample) Resource(org.opennms.newts.api.Resource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

Resource (org.opennms.newts.api.Resource)65 Test (org.junit.Test)48 Row (org.opennms.newts.api.Results.Row)33 ResultDescriptor (org.opennms.newts.api.query.ResultDescriptor)31 MeasurementRowsBuilder (org.opennms.newts.aggregate.Utils.MeasurementRowsBuilder)20 Sample (org.opennms.newts.api.Sample)19 SampleRowsBuilder (org.opennms.newts.aggregate.Utils.SampleRowsBuilder)18 Measurement (org.opennms.newts.api.Measurement)14 Context (org.opennms.newts.api.Context)12 MetricRegistry (com.codahale.metrics.MetricRegistry)9 SearchResults (org.opennms.newts.api.search.SearchResults)8 Timestamp (org.opennms.newts.api.Timestamp)7 ResourcePath (org.opennms.netmgt.model.ResourcePath)6 ContextConfigurations (org.opennms.newts.cassandra.ContextConfigurations)6 Map (java.util.Map)5 Results (org.opennms.newts.api.Results)5 CassandraSession (org.opennms.newts.cassandra.CassandraSession)5 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)4 List (java.util.List)4 Result (org.opennms.newts.api.search.SearchResults.Result)4