use of org.opennms.newts.api.Resource in project newts by OpenNMS.
the class LineParser method parseLine.
List<Sample> parseLine(String line) throws ParseException {
LOG.trace("Parsing {}", line);
List<Sample> samples = Lists.newArrayList();
Resource station = new Resource(stringAt(line, 0));
String wban = stringAt(line, 7);
String dateYMD = stringAt(line, 14);
Date date = getDateFormat().parse(dateYMD);
Timestamp ts = new Timestamp(date.getTime(), TimeUnit.MILLISECONDS);
double meanTemp = doubleAt(line, 24);
samples.add(new Sample(ts, station, "meanTemperature", GAUGE, LineParser.valueFor(meanTemp, 9999.9)));
double dewpoint = doubleAt(line, 35);
samples.add(new Sample(ts, station, "dewPoint", GAUGE, LineParser.valueFor(dewpoint, 9999.9)));
double slp = doubleAt(line, 46);
Gauge seaLevelPressure = LineParser.valueFor(slp, 9999.9);
samples.add(new Sample(ts, station, "seaLevelPressure", GAUGE, seaLevelPressure));
double stp = doubleAt(line, 57);
Gauge stationPressure = LineParser.valueFor(stp, 9999.9);
samples.add(new Sample(ts, station, "stationPressure", GAUGE, stationPressure));
double vis = doubleAt(line, 68);
Gauge visibility = LineParser.valueFor(vis, 999.9);
samples.add(new Sample(ts, station, "visibility", GAUGE, visibility));
double speed = doubleAt(line, 78);
Gauge meanWindSpeed = new Gauge(speed);
samples.add(new Sample(ts, station, "meanWindSpeed", GAUGE, meanWindSpeed));
double maxSpeed = doubleAt(line, 88);
Gauge maxWindSpeed = LineParser.valueFor(maxSpeed, 999.9);
samples.add(new Sample(ts, station, "maxWindSpeed", GAUGE, maxWindSpeed));
double maxGust = doubleAt(line, 95);
Gauge maxWindGust = LineParser.valueFor(maxGust, 999.9);
samples.add(new Sample(ts, station, "maxWindGust", GAUGE, maxWindGust));
double maxTemp = doubleAt(line, 102);
Gauge maxTemperature = LineParser.valueFor(maxTemp, 9999.9);
samples.add(new Sample(ts, station, "maxTemperature", GAUGE, maxTemperature));
double minTemp = doubleAt(line, 110);
Gauge minTemperature = LineParser.valueFor(minTemp, 9999.9);
samples.add(new Sample(ts, station, "minTemperature", GAUGE, minTemperature));
LOG.trace("Station number {}, WBAN {}, date {}, Max Temp {}...", station, wban, dateYMD, maxTemperature);
return samples;
}
use of org.opennms.newts.api.Resource in project newts by OpenNMS.
the class InsertSelectMeasurementsITCase method testWithCounter.
@Test
public void testWithCounter() {
Iterator<Row<Sample>> testSamples = new SampleRowsBuilder(new Resource("localhost"), MetricType.COUNTER).row(900000000).element("mCounter", // Thu Jul 9 11:00:00 CDT 1998
0).row(900000300).element("mCounter", 300).row(900000600).element("mCounter", 600).row(900000900).element("mCounter", 900).row(900001200).element("mCounter", 1200).row(900001500).element("mCounter", 1500).row(900001800).element("mCounter", 1800, mapFor("a", "1")).row(900002100).element("mCounter", 2100).row(900002400).element("mCounter", 2400, mapFor("b", "2")).row(900002700).element("mCounter", 2700).row(900003000).element("mCounter", 3000).row(900003300).element("mCounter", 3300).row(900003600).element("mCounter", 3600).row(900003900).element("mCounter", 3900).row(900004200).element("mCounter", 4200).row(900004500).element("mCounter", 4500).row(900004800).element("mCounter", 4800).row(900005100).element("mCounter", 5100, mapFor("c", "3")).row(900005400).element("mCounter", 5400).row(900005700).element("mCounter", 5700, mapFor("d", "4")).row(900006000).element("mCounter", 6000).row(900006300).element("mCounter", 6300).row(900006600).element("mCounter", 6600).row(900006900).element("mCounter", 6900).row(900007200).element("mCounter", // Thu Jul 9 13:00:00 CDT 1998
7200).build();
ResultDescriptor rDescriptor = new ResultDescriptor(Duration.seconds(300)).datasource("mCounter-avg", "mCounter", Duration.seconds(600), AVERAGE).export("mCounter-avg");
Iterator<Row<Measurement>> expected = new MeasurementRowsBuilder(new Resource("localhost")).row(900003600).element("mCounter-avg", 1).row(900007200).element("mCounter-avg", 1).build();
writeSamples(testSamples);
Results<Measurement> results = getRepository().select(Context.DEFAULT_CONTEXT, new Resource("localhost"), Optional.of(Timestamp.fromEpochSeconds(900003600)), Optional.of(Timestamp.fromEpochSeconds(900007200)), rDescriptor, Optional.of(Duration.minutes(60)));
// Validate results
assertRowsEqual(expected, results.iterator());
// Validate merged attributes
Iterator<Row<Measurement>> rows = results.iterator();
assertAttributes(rows.next().getElement("mCounter-avg"), mapFor("a", "1", "b", "2"));
assertAttributes(rows.next().getElement("mCounter-avg"), mapFor("c", "3", "d", "4"));
}
use of org.opennms.newts.api.Resource in project newts by OpenNMS.
the class InsertSelectSamplesITCase method test.
@Test
public void test() {
List<Sample> samples = Lists.newArrayList();
int rows = 10, cols = 3;
Resource resource = new Resource("r");
for (int i = 1; i <= rows; i++) {
Timestamp ts = Timestamp.fromEpochMillis(i * 1000);
for (int j = 1; j <= cols; j++) {
samples.add(new Sample(ts, resource, "m" + j, GAUGE, new Gauge((i + 1) * j)));
}
}
// Override the shard period to ensure we test query concurrency
m_contextConfigurations.addContextConfig(Context.DEFAULT_CONTEXT, Duration.seconds(1), ConsistencyLevel.ALL, ConsistencyLevel.ALL);
getRepository().insert(samples);
Timestamp start = Timestamp.fromEpochMillis(0), end = Timestamp.fromEpochMillis(rows * 1000);
Iterator<Row<Sample>> results = getRepository().select(Context.DEFAULT_CONTEXT, resource, Optional.of(start), Optional.of(end)).iterator();
for (int i = 1; i <= rows; i++) {
assertTrue("Insufficient number of results", results.hasNext());
Timestamp timestamp = Timestamp.fromEpochMillis(i * 1000);
Row<Sample> row = results.next();
assertEquals("Unexpected timestamp for row " + i, timestamp, row.getTimestamp());
assertEquals("Unexpected resource name", resource, row.getResource());
for (int j = 1; j <= cols; j++) {
assertNotNull("Missing sample: m" + j, row.getElement("m" + j));
Sample sample = row.getElement("m" + j);
assertEquals("Unexpected timestamp for metric m" + j, timestamp, sample.getTimestamp());
assertEquals("Unexpected resource name", resource, sample.getResource());
assertEquals("Unexpected metric name", "m" + j, sample.getName());
assertEquals("Unexpected metric type", GAUGE, sample.getType());
assertEquals((double) ((i + 1) * j), sample.getValue().doubleValue(), 0.0d);
}
}
}
use of org.opennms.newts.api.Resource in project newts by OpenNMS.
the class ResultSerializationTest method testMeasurements.
@Test
public void testMeasurements() throws JsonProcessingException {
Results<Measurement> data = new Results<>();
data.addElement(new Measurement(Timestamp.fromEpochSeconds(900000000), new Resource("localhost"), "ifInOctets", 5000));
data.addElement(new Measurement(Timestamp.fromEpochSeconds(900000000), new Resource("localhost"), "ifOutOctets", 6000));
data.addElement(new Measurement(Timestamp.fromEpochSeconds(900000300), new Resource("localhost"), "ifInOctets", 6000));
data.addElement(new Measurement(Timestamp.fromEpochSeconds(900000300), new Resource("localhost"), "ifOutOctets", 7000));
String json = "[" + " [" + " {" + " \"name\": \"ifOutOctets\"," + " \"timestamp\":900000000000," + " \"value\":6000.0" + " }," + " {" + " \"name\": \"ifInOctets\"," + " \"timestamp\":900000000000," + " \"value\":5000.0" + " }" + " ]," + " [" + " {" + " \"name\": \"ifOutOctets\"," + " \"timestamp\":900000300000," + " \"value\":7000.0" + " }," + " {" + " \"name\": \"ifInOctets\"," + " \"timestamp\":900000300000," + " \"value\":6000.0" + " }" + " ]" + "]";
assertThat(new ObjectMapper().writeValueAsString(Transform.measurementDTOs(data)), is(normalize(json)));
}
use of org.opennms.newts.api.Resource in project opennms by OpenNMS.
the class NewtsConverterIT method execute.
private void execute(final boolean storeByGroup, final boolean storeByForeignSource, final boolean useRrdTool) throws Exception {
final Path data = OPENNMS_HOME.resolve("share").resolve(useRrdTool ? "rrd" : "jrb").resolve(storeByGroup ? "sbg" : "sbm").resolve(storeByForeignSource ? "fs" : "id");
assertTrue(Files.isDirectory(data));
exit.expectSystemExitWithStatus(0);
exit.checkAssertionAfterwards(() -> {
assertThat(resourceStorageDao.exists(RESOURCE_PATH_SNMP, 0), is(true));
assertThat(resourceStorageDao.getAttributes(RESOURCE_PATH_SNMP), hasItems(allOf(hasProperty("name", is("ifInOctets"))), allOf(hasProperty("name", is("ifSpeed")), hasProperty("value", is("1000")))));
assertThat(resourceStorageDao.exists(RESOURCE_PATH_RESPONSE, 0), is(true));
assertThat(resourceStorageDao.getAttributes(RESOURCE_PATH_RESPONSE), hasItems(allOf(hasProperty("name", is("icmp")))));
final Results<Measurement> result = repository.select(Context.DEFAULT_CONTEXT, new Resource(NewtsUtils.toResourceId(ResourcePath.get(RESOURCE_PATH_SNMP, "mib2-interfaces"))), Optional.of(Timestamp.fromEpochSeconds(1414504800)), Optional.of(Timestamp.fromEpochSeconds(1417047045)), new ResultDescriptor(Duration.seconds(7200)).datasource("ifInOctets", StandardAggregationFunctions.AVERAGE).export("ifInOctets"), Optional.of(Duration.seconds(7200)));
assertThat(result.getRows().size(), is(EXPECTED_DATA.length));
int i = 0;
for (Results.Row<Measurement> r : result) {
final double deltaAbs = r.getElement("ifInOctets").getValue().doubleValue() - EXPECTED_DATA[i].value;
final double deltaRel = deltaAbs / EXPECTED_DATA[i].value * 100.0;
/* Use the following for debugging of non-matching entries...
System.out.println(String.format(
"%4d: %-24s %11.2f %11.2f %11.2f (%6.2f) %36s|%-36s %f%%",
i,
r.getElement("ifInOctets").getTimestamp().asDate().toString(),
r.getElement("ifInOctets").getValue().doubleValue(),
EXPECTED_DATA[i].value,
deltaAbs,
deltaRel,
deltaAbs < -1.0 ? Strings.repeat("▒", (int) Math.abs(Math.log10(-deltaAbs) * 10.0)) : "",
deltaAbs > 1.0 ? Strings. repeat("▒", (int) Math.abs(Math.log10(deltaAbs) * 10.0)) : "",
Math.abs(deltaAbs / EXPECTED_DATA[i].value * 100.0)));
*/
assertThat(r.getTimestamp().asSeconds(), is(EXPECTED_DATA[i].timestamp));
if (i != 270) {
// We got some errors on the RRA boundaries - ignore them
assertThat(r.getElement("ifInOctets").getValue().doubleValue(), is(anyOf(equalTo(EXPECTED_DATA[i].value), // Allow a relative error of 0.3%
closeTo(EXPECTED_DATA[i].value, EXPECTED_DATA[i].value * 0.003))));
}
i++;
}
});
NewtsConverter.main("-o", OPENNMS_HOME.toString(), "-r", data.toString(), "-t", Boolean.toString(useRrdTool), "-T", RRD_BINARY.toAbsolutePath().toString(), "-s", Boolean.toString(storeByGroup));
}
Aggregations