Search in sources :

Example 16 with Sample

use of org.opennms.newts.api.Sample in project opennms by OpenNMS.

the class NewtsConverter method injectSamplesToNewts.

private void injectSamplesToNewts(final ResourcePath resourcePath, final String group, final List<? extends AbstractDS> dataSources, final SortedMap<Long, List<Double>> samples) {
    final ResourcePath groupPath = ResourcePath.get(resourcePath, group);
    // Create a resource ID from the resource path
    final String groupId = NewtsUtils.toResourceId(groupPath);
    // Build indexing attributes
    final Map<String, String> attributes = Maps.newHashMap();
    NewtsUtils.addIndicesToAttributes(groupPath, attributes);
    // Create the NewTS resource to insert
    final Resource resource = new Resource(groupId, Optional.of(attributes));
    // Transform the RRD samples into NewTS samples
    List<Sample> batch = new ArrayList<>(this.batchSize);
    for (final Map.Entry<Long, List<Double>> s : samples.entrySet()) {
        for (int i = 0; i < dataSources.size(); i++) {
            final double value = s.getValue().get(i);
            if (Double.isNaN(value)) {
                continue;
            }
            final AbstractDS ds = dataSources.get(i);
            final Timestamp timestamp = Timestamp.fromEpochSeconds(s.getKey());
            try {
                batch.add(toSample(ds, resource, timestamp, value));
            } catch (IllegalArgumentException e) {
                // type i.e. negative for a counter, so we silently skip these
                continue;
            }
            if (batch.size() >= this.batchSize) {
                this.repository.insert(batch, true);
                this.processedSamples.getAndAdd(batch.size());
                batch = new ArrayList<>(this.batchSize);
            }
        }
    }
    if (!batch.isEmpty()) {
        this.repository.insert(batch, true);
        this.processedSamples.getAndAdd(batch.size());
    }
    this.processedMetrics.getAndAdd(dataSources.size());
    LOG.trace("Stats: {} / {}", this.processedMetrics, this.processedSamples);
}
Also used : Sample(org.opennms.newts.api.Sample) Resource(org.opennms.newts.api.Resource) ArrayList(java.util.ArrayList) Timestamp(org.opennms.newts.api.Timestamp) AbstractDS(org.opennms.netmgt.rrd.model.AbstractDS) ResourcePath(org.opennms.netmgt.model.ResourcePath) UnsignedLong(com.google.common.primitives.UnsignedLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 17 with Sample

use of org.opennms.newts.api.Sample in project opennms by OpenNMS.

the class NewtsPersistOperationBuilder method getSamplesToInsert.

public List<Sample> getSamplesToInsert() {
    final List<Sample> samples = Lists.newLinkedList();
    ResourcePath path = ResourceTypeUtils.getResourcePathWithRepository(m_repository, ResourcePath.get(m_resource.getPath(), m_name));
    // Add extra attributes that can be used to walk the resource tree.
    NewtsUtils.addIndicesToAttributes(path, m_metaData);
    Resource resource = new Resource(NewtsUtils.toResourceId(path), Optional.of(m_metaData));
    // Convert numeric attributes to samples
    Timestamp timestamp = Timestamp.fromEpochMillis(m_timeKeeper.getCurrentTime());
    for (Entry<CollectionAttributeType, Number> entry : m_declarations.entrySet()) {
        CollectionAttributeType attrType = entry.getKey();
        MetricType type = mapType(attrType.getType());
        if (type == null) {
            // Skip attributes with no type
            continue;
        }
        Number value = entry.getValue();
        if (value == null) {
            // Skip attributes with no value (see NMS-8103)
            continue;
        }
        samples.add(new Sample(timestamp, m_context, resource, attrType.getName(), type, ValueType.compose(entry.getValue(), type)));
    }
    return samples;
}
Also used : ResourcePath(org.opennms.netmgt.model.ResourcePath) Sample(org.opennms.newts.api.Sample) MetricType(org.opennms.newts.api.MetricType) Resource(org.opennms.newts.api.Resource) CollectionAttributeType(org.opennms.netmgt.collection.api.CollectionAttributeType) Timestamp(org.opennms.newts.api.Timestamp)

Example 18 with Sample

use of org.opennms.newts.api.Sample 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 19 with Sample

use of org.opennms.newts.api.Sample 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)

Example 20 with Sample

use of org.opennms.newts.api.Sample in project newts by OpenNMS.

the class ImportRunner method toJSON.

public static Func1<List<Sample>, String> toJSON() {
    return new Func1<List<Sample>, String>() {

        @Override
        public String call(List<Sample> samples) {
            JSONBuilder bldr = new JSONBuilder();
            for (Sample sample : samples) {
                if (isNaN(sample))
                    continue;
                //System.err.println("Importing: " + sample);
                bldr.newObject();
                bldr.attr("timestamp", sample.getTimestamp().asMillis());
                bldr.attr("resource", sample.getResource().getId());
                bldr.attr("name", sample.getName());
                bldr.attr("type", sample.getType().name());
                if (sample.getType() == MetricType.GAUGE) {
                    bldr.attr("value", sample.getValue().doubleValue());
                } else {
                    bldr.attr("value", sample.getValue().longValue());
                }
            }
            return bldr.toString();
        }
    };
}
Also used : Sample(org.opennms.newts.api.Sample) List(java.util.List) Func1(rx.functions.Func1)

Aggregations

Sample (org.opennms.newts.api.Sample)37 Resource (org.opennms.newts.api.Resource)18 Test (org.junit.Test)14 Timestamp (org.opennms.newts.api.Timestamp)12 MetricRegistry (com.codahale.metrics.MetricRegistry)9 Gauge (org.opennms.newts.api.Gauge)6 ContextConfigurations (org.opennms.newts.cassandra.ContextConfigurations)6 Timer (com.codahale.metrics.Timer)5 List (java.util.List)5 Counter (org.opennms.newts.api.Counter)5 Row (org.opennms.newts.api.Results.Row)5 CassandraSession (org.opennms.newts.cassandra.CassandraSession)5 Map (java.util.Map)4 Context (org.opennms.newts.api.Context)4 Results (org.opennms.newts.api.Results)4 Meter (com.codahale.metrics.Meter)3 BoundStatement (com.datastax.driver.core.BoundStatement)3 PreparedStatement (com.datastax.driver.core.PreparedStatement)3 RegularStatement (com.datastax.driver.core.RegularStatement)3 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)3