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