use of org.opennms.netmgt.measurements.api.FetchResults in project opennms by OpenNMS.
the class FetchResultsTest method canConvertTableToAndFromFetchResults.
@Test
public void canConvertTableToAndFromFetchResults() {
final double delta = 0.0000001;
// Simple table with 3 columns and 3 rows
RowSortedTable<Long, String, Double> table = TreeBasedTable.create();
table.put(0L, Filter.TIMESTAMP_COLUMN_NAME, 0d);
table.put(0L, "x", 1d);
table.put(1L, Filter.TIMESTAMP_COLUMN_NAME, 100d);
table.put(1L, "x", 1d);
// Don't add values for x, but add a value for y in the last row
table.put(2L, Filter.TIMESTAMP_COLUMN_NAME, 200d);
table.put(2L, "y", 99d);
// Create the fetch results using the table
Map<String, Object> constants = Maps.newHashMap();
FetchResults results = new FetchResults(table, 300, constants);
// Verify
Map<String, double[]> columns = results.getColumns();
assertArrayEquals(columns.get("x"), new double[] { 1d, 1d, Double.NaN }, delta);
assertArrayEquals(columns.get("y"), new double[] { Double.NaN, Double.NaN, 99d }, delta);
// Convert back to a table
table = results.asRowSortedTable();
// Verify
assertEquals(3, table.columnKeySet().size());
assertTrue(table.columnKeySet().containsAll(Lists.newArrayList(Filter.TIMESTAMP_COLUMN_NAME, "x", "y")));
assertEquals(0d, table.get(0L, Filter.TIMESTAMP_COLUMN_NAME), delta);
assertEquals(100d, table.get(1L, Filter.TIMESTAMP_COLUMN_NAME), delta);
assertEquals(200d, table.get(2L, Filter.TIMESTAMP_COLUMN_NAME), delta);
assertEquals(1d, table.get(0L, "x"), delta);
assertEquals(1d, table.get(1L, "x"), delta);
assertEquals(Double.NaN, table.get(2L, "x"), delta);
assertEquals(Double.NaN, table.get(0L, "y"), delta);
assertEquals(Double.NaN, table.get(1L, "y"), delta);
assertEquals(99d, table.get(2L, "y"), delta);
}
use of org.opennms.netmgt.measurements.api.FetchResults in project opennms by OpenNMS.
the class JRrd2FetchStrategy method fetchMeasurements.
@Override
protected FetchResults fetchMeasurements(long start, long end, long step, int maxrows, Map<Source, String> rrdsBySource, Map<String, Object> constants) throws RrdException {
final long startInSeconds = (long) Math.floor(start / 1000d);
final long endInSeconds = (long) Math.floor(end / 1000d);
long stepInSeconds = (long) Math.floor(step / 1000d);
// The step must be strictly positive
if (stepInSeconds <= 0) {
stepInSeconds = 1;
}
// Use labels without spaces when executing the xport command
// These are mapped back to the requested labels in the response
final Map<String, String> labelMap = Maps.newHashMap();
int k = 0;
List<String> argv = Lists.newLinkedList();
for (final Map.Entry<Source, String> entry : rrdsBySource.entrySet()) {
final Source source = entry.getKey();
final String rrdFile = entry.getValue();
final String tempLabel = Integer.toString(++k);
labelMap.put(tempLabel, source.getLabel());
argv.add(String.format("DEF:%s=%s:%s:%s", tempLabel, Utils.escapeColons(rrdFile), Utils.escapeColons(source.getEffectiveDataSource()), source.getAggregation()));
argv.add(String.format("XPORT:%s:%s", tempLabel, tempLabel));
}
org.opennms.netmgt.rrd.jrrd2.api.FetchResults xportResults;
try {
xportResults = jrrd2.xport(startInSeconds, endInSeconds, stepInSeconds, maxrows, argv.toArray(new String[argv.size()]));
} catch (JRrd2Exception e) {
throw new RrdException("Xport failed.", e);
}
// Convert to ms
final long[] timestamps = xportResults.getTimestamps();
for (int i = 0; i < timestamps.length; i++) {
timestamps[i] *= 1000;
}
// Map the column labels from their temporary values to their requested values
Map<String, double[]> valuesByTempLabel = xportResults.getColumnsWithValues();
Map<String, double[]> valuesByLabel = Maps.newLinkedHashMap();
for (Entry<String, double[]> entry : valuesByTempLabel.entrySet()) {
valuesByLabel.put(labelMap.get(entry.getKey()), entry.getValue());
}
return new FetchResults(timestamps, valuesByLabel, xportResults.getStep() * 1000, constants);
}
use of org.opennms.netmgt.measurements.api.FetchResults in project opennms by OpenNMS.
the class RrdtoolXportFetchStrategy method fetchMeasurements.
/**
* {@inheritDoc}
*/
@Override
protected FetchResults fetchMeasurements(long start, long end, long step, int maxrows, Map<Source, String> rrdsBySource, Map<String, Object> constants) throws RrdException {
String rrdBinary = System.getProperty("rrd.binary");
if (rrdBinary == null) {
throw new RrdException("No RRD binary is set.");
}
final long startInSeconds = (long) Math.floor(start / 1000d);
final long endInSeconds = (long) Math.floor(end / 1000d);
long stepInSeconds = (long) Math.floor(step / 1000d);
// The step must be strictly positive
if (stepInSeconds <= 0) {
stepInSeconds = 1;
}
final CommandLine cmdLine = new CommandLine(rrdBinary);
cmdLine.addArgument("xport");
cmdLine.addArgument("--step");
cmdLine.addArgument("" + stepInSeconds);
cmdLine.addArgument("--start");
cmdLine.addArgument("" + startInSeconds);
cmdLine.addArgument("--end");
cmdLine.addArgument("" + endInSeconds);
if (maxrows > 0) {
cmdLine.addArgument("--maxrows");
cmdLine.addArgument("" + maxrows);
}
// Use labels without spaces when executing the xport command
// These are mapped back to the requested labels in the response
final Map<String, String> labelMap = Maps.newHashMap();
int k = 0;
for (final Map.Entry<Source, String> entry : rrdsBySource.entrySet()) {
final Source source = entry.getKey();
final String rrdFile = entry.getValue();
final String tempLabel = Integer.toString(++k);
labelMap.put(tempLabel, source.getLabel());
cmdLine.addArgument(String.format("DEF:%s=%s:%s:%s", tempLabel, Utils.escapeColons(rrdFile), Utils.escapeColons(source.getEffectiveDataSource()), source.getAggregation()));
cmdLine.addArgument(String.format("XPORT:%s:%s", tempLabel, tempLabel));
}
// Use commons-exec to execute rrdtool
final DefaultExecutor executor = new DefaultExecutor();
// Capture stdout/stderr
final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
executor.setStreamHandler(new PumpStreamHandler(stdout, stderr, null));
// Fail if we get a non-zero exit code
executor.setExitValue(0);
// Fail if the process takes too long
final ExecuteWatchdog watchdog = new ExecuteWatchdog(XPORT_TIMEOUT_MS);
executor.setWatchdog(watchdog);
// Export
RrdXport rrdXport;
try {
LOG.debug("Executing: {}", cmdLine);
executor.execute(cmdLine);
final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
final SAXSource source = new SAXSource(xmlReader, new InputSource(new StringReader(stdout.toString())));
final JAXBContext jc = JAXBContext.newInstance(RrdXport.class);
final Unmarshaller u = jc.createUnmarshaller();
rrdXport = (RrdXport) u.unmarshal(source);
} catch (IOException e) {
throw new RrdException("An error occured while executing '" + StringUtils.join(cmdLine.toStrings(), " ") + "' with stderr: " + stderr.toString(), e);
} catch (SAXException | JAXBException e) {
throw new RrdException("The output generated by 'rrdtool xport' could not be parsed.", e);
}
final int numRows = rrdXport.getRows().size();
final int numColumns = rrdXport.getMeta().getLegends().size();
final long xportStartInMs = rrdXport.getMeta().getStart() * 1000;
final long xportStepInMs = rrdXport.getMeta().getStep() * 1000;
final long[] timestamps = new long[numRows];
final double[][] values = new double[numColumns][numRows];
// Convert rows to columns
int i = 0;
for (final XRow row : rrdXport.getRows()) {
// Derive the timestamp from the start and step since newer versions
// of rrdtool no longer include it as part of the rows
timestamps[i] = xportStartInMs + xportStepInMs * i;
for (int j = 0; j < numColumns; j++) {
if (row.getValues() == null) {
// NMS-7710: Avoid NPEs, in certain cases the list of values may be null
throw new RrdException("The output generated by 'rrdtool xport' was not recognized. Try upgrading your rrdtool binaries.");
}
values[j][i] = row.getValues().get(j);
}
i++;
}
// Map the columns by label
// The legend entries are in the same order as the column values
final Map<String, double[]> columns = Maps.newHashMapWithExpectedSize(numColumns);
i = 0;
for (String label : rrdXport.getMeta().getLegends()) {
columns.put(labelMap.get(label), values[i++]);
}
return new FetchResults(timestamps, columns, xportStepInMs, constants);
}
use of org.opennms.netmgt.measurements.api.FetchResults in project opennms by OpenNMS.
the class JEXLExpressionEngineEnhancedTest method performExpression.
private double[] performExpression(String expression, Map<String, Object> constants) throws ExpressionException {
// Build a simple request with the given expression
QueryRequest request = new QueryRequest();
Source constant = new Source();
constant.setLabel("x");
request.setSources(Lists.newArrayList(constant));
Expression exp = new Expression();
exp.setLabel("y");
exp.setExpression(expression);
request.setExpressions(Lists.newArrayList(exp));
// Build the fetch results with known values
final int N = 100;
long[] timestamps = new long[N];
double[] xValues = new double[N];
for (int i = 0; i < N; i++) {
timestamps[i] = i * 1000;
// note for tests values start from 5
xValues[i] = Double.valueOf(i + 5);
}
Map<String, double[]> values = Maps.newHashMap();
values.put("x", xValues);
FetchResults results = new FetchResults(timestamps, values, 1, constants);
// Use the engine to evaluate the expression
jexlExpressionEngine.applyExpressions(request, results);
// Retrieve the results
return results.getColumns().get("y");
}
use of org.opennms.netmgt.measurements.api.FetchResults in project opennms by OpenNMS.
the class ReportDefinitionTest method testFilteredResourceAttributeFilteringWithMatch.
public void testFilteredResourceAttributeFilteringWithMatch() throws Exception {
OnmsAttribute rrdAttribute = new RrdGraphAttribute("IfInOctets", "something", "something else");
ExternalValueAttribute externalValueAttribute = new ExternalValueAttribute("ifSpeed", "100000000");
Set<OnmsAttribute> attributes = new HashSet<>();
attributes.add(rrdAttribute);
attributes.add(externalValueAttribute);
final OnmsNode node = new OnmsNode();
node.setId(1);
node.setLabel("Node One");
EasyMock.expect(m_nodeDao.load(1)).andReturn(node);
MockResourceType resourceType = new MockResourceType();
resourceType.setName("interfaceSnmp");
OnmsResource resource = new OnmsResource(node.getId().toString(), node.getLabel(), resourceType, attributes, ResourcePath.get("foo"));
ReportDefinition def = createReportDefinition();
def.getReport().getPackage().setFilter("");
def.setResourceAttributeKey(externalValueAttribute.getName());
def.setResourceAttributeValueMatch(externalValueAttribute.getValue());
ReportInstance report = def.createReport(m_nodeDao, m_resourceDao, m_fetchStrategy, m_filterDao);
SortedMap<Integer, String> sortedNodeMap = new TreeMap<Integer, String>();
sortedNodeMap.put(node.getId(), node.getLabel());
EasyMock.expect(m_filterDao.getNodeMap("")).andReturn(sortedNodeMap);
EasyMock.expect(m_resourceDao.getResourceForNode(node)).andReturn(resource);
Source source = new Source();
source.setLabel("result");
source.setResourceId(resource.getId().toString());
source.setAttribute(rrdAttribute.getName());
source.setAggregation("AVERAGE");
FetchResults results = new FetchResults(new long[] { report.getStartTime() }, Collections.singletonMap("result", new double[] { 100.0 }), report.getEndTime() - report.getStartTime(), Collections.emptyMap());
EasyMock.expect(m_fetchStrategy.fetch(report.getStartTime(), report.getEndTime(), 1, 0, null, null, Collections.singletonList(source), false)).andReturn(results);
m_mocks.replayAll();
report.walk();
assertEquals("results size", 1, report.getResults().size());
}
Aggregations