Search in sources :

Example 1 with Source

use of org.opennms.netmgt.measurements.model.Source in project opennms by OpenNMS.

the class MeasurementsRestServiceWithRrdIT method canRetrieveMeasurementsFromRrd.

@Test
public void canRetrieveMeasurementsFromRrd() throws Exception {
    QueryRequest request = new QueryRequest();
    request.setStart(1414602000000L);
    request.setEnd(1417046400000L);
    request.setStep(1000L);
    request.setMaxRows(700);
    // Average
    Source ifInOctetsAvg = new Source();
    ifInOctetsAvg.setResourceId("node[1].interfaceSnmp[eth0-04013f75f101]");
    ifInOctetsAvg.setAttribute("ifInOctets");
    ifInOctetsAvg.setAggregation("AVERAGE");
    ifInOctetsAvg.setLabel("ifInOctetsAvg");
    // Min
    Source ifInOctetsMin = new Source();
    ifInOctetsMin.setResourceId("node[1].interfaceSnmp[eth0-04013f75f101]");
    ifInOctetsMin.setAttribute("ifInOctets");
    ifInOctetsMin.setAggregation("MIN");
    ifInOctetsMin.setLabel("ifInOctetsMin");
    // Max
    Source ifInOctetsMax = new Source();
    ifInOctetsMax.setResourceId("node[1].interfaceSnmp[eth0-04013f75f101]");
    ifInOctetsMax.setAttribute("ifInOctets");
    ifInOctetsMax.setAggregation("MAX");
    ifInOctetsMax.setLabel("ifInOctetsMax");
    request.setSources(Lists.newArrayList(ifInOctetsAvg, ifInOctetsMin, ifInOctetsMax));
    // Perform the query
    QueryResponse response = m_svc.query(request);
    // Validate the results
    long[] timestamps = response.getTimestamps();
    final Map<String, double[]> columns = response.columnsWithLabels();
    assertEquals(3600000L, response.getStep());
    assertEquals(680, timestamps.length);
    // Verify the values at an arbitrary index
    final int idx = 8;
    assertEquals(1414630800000L, timestamps[idx]);
    assertEquals(270.66140826873385, columns.get("ifInOctetsAvg")[idx], 0.0001);
    assertEquals(259.54086378737543, columns.get("ifInOctetsMin")[idx], 0.0001);
    assertEquals(67872.22455490529, columns.get("ifInOctetsMax")[idx], 0.0001);
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) Source(org.opennms.netmgt.measurements.model.Source) Test(org.junit.Test)

Example 2 with Source

use of org.opennms.netmgt.measurements.model.Source in project opennms by OpenNMS.

the class MeasurementsRestServiceWithJrbIT method canApplyFilters.

@Test
public void canApplyFilters() {
    QueryRequest request = new QueryRequest();
    request.setStart(1414602000000L);
    request.setEnd(1418046400000L);
    request.setStep(1000L);
    request.setMaxRows(700);
    Source ifInOctets = new Source();
    ifInOctets.setResourceId("node[1].interfaceSnmp[eth0-04013f75f101]");
    ifInOctets.setAttribute("ifInOctets");
    ifInOctets.setAggregation("MAX");
    ifInOctets.setLabel("ifInOctets");
    request.setSources(Lists.newArrayList(ifInOctets));
    // Apply a chomp filter - cutting some of the first row off, and the trailing NaNs
    FilterDef chompFilter = new FilterDef("Chomp", "cutoffDate", "1414630000.0", "stripNaNs", "true");
    request.setFilters(Lists.newArrayList(chompFilter));
    LOG.debug(JaxbUtils.marshal(request));
    QueryResponse response = m_svc.query(request);
    // Verify the values for the first and last rows
    final Map<String, double[]> columns = response.columnsWithLabels();
    double[] ifInOctetsColumn = columns.get("ifInOctets");
    assertEquals(67872.22455490529, ifInOctetsColumn[0], 0.0001);
    assertEquals(1649961.9593111263, ifInOctetsColumn[ifInOctetsColumn.length - 1], 0.0001);
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) FilterDef(org.opennms.netmgt.measurements.model.FilterDef) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) Source(org.opennms.netmgt.measurements.model.Source) Test(org.junit.Test)

Example 3 with Source

use of org.opennms.netmgt.measurements.model.Source in project opennms by OpenNMS.

the class MeasurementsRestServiceWithJrbIT method canRetrieveMeasurementsFromJrb.

@Test
public void canRetrieveMeasurementsFromJrb() {
    QueryRequest request = new QueryRequest();
    request.setStart(1414602000000L);
    request.setEnd(1417046400000L);
    request.setStep(1000L);
    request.setMaxRows(700);
    // Average
    Source ifInOctetsAvg = new Source();
    ifInOctetsAvg.setResourceId("node[1].interfaceSnmp[eth0-04013f75f101]");
    ifInOctetsAvg.setAttribute("ifInOctets");
    ifInOctetsAvg.setAggregation("AVERAGE");
    ifInOctetsAvg.setLabel("ifInOctetsAvg");
    // Min
    Source ifInOctetsMin = new Source();
    ifInOctetsMin.setResourceId("node[1].interfaceSnmp[eth0-04013f75f101]");
    ifInOctetsMin.setAttribute("ifInOctets");
    ifInOctetsMin.setAggregation("MIN");
    ifInOctetsMin.setLabel("ifInOctetsMin");
    // Max
    Source ifInOctetsMax = new Source();
    ifInOctetsMax.setResourceId("node[1].interfaceSnmp[eth0-04013f75f101]");
    ifInOctetsMax.setAttribute("ifInOctets");
    ifInOctetsMax.setAggregation("MAX");
    ifInOctetsMax.setLabel("ifInOctetsMax");
    request.setSources(Lists.newArrayList(ifInOctetsAvg, ifInOctetsMin, ifInOctetsMax));
    // Perform the query
    QueryResponse response = m_svc.query(request);
    // Validate the results
    long[] timestamps = response.getTimestamps();
    final Map<String, double[]> columns = response.columnsWithLabels();
    assertEquals(3600000L, response.getStep());
    assertEquals(680, timestamps.length);
    // Verify the values at an arbitrary index
    final int idx = 8;
    assertEquals(1414630800000L, timestamps[idx]);
    assertEquals(270.66140826873385, columns.get("ifInOctetsAvg")[idx], 0.0001);
    assertEquals(259.54086378737543, columns.get("ifInOctetsMin")[idx], 0.0001);
    assertEquals(67872.22455490529, columns.get("ifInOctetsMax")[idx], 0.0001);
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) Source(org.opennms.netmgt.measurements.model.Source) Test(org.junit.Test)

Example 4 with Source

use of org.opennms.netmgt.measurements.model.Source 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 / 1000);
    final long endInSeconds = (long) Math.floor(end / 1000);
    long stepInSeconds = (long) Math.floor(step / 1000);
    // 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);
}
Also used : InputSource(org.xml.sax.InputSource) JAXBContext(javax.xml.bind.JAXBContext) XRow(org.opennms.netmgt.rrd.model.XRow) RrdXport(org.opennms.netmgt.rrd.model.RrdXport) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Source(org.opennms.netmgt.measurements.model.Source) SAXException(org.xml.sax.SAXException) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) StringReader(java.io.StringReader) RrdException(org.jrobin.core.RrdException) Unmarshaller(javax.xml.bind.Unmarshaller) XMLReader(org.xml.sax.XMLReader) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) JAXBException(javax.xml.bind.JAXBException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CommandLine(org.apache.commons.exec.CommandLine) SAXSource(javax.xml.transform.sax.SAXSource) Map(java.util.Map)

Example 5 with Source

use of org.opennms.netmgt.measurements.model.Source in project opennms by OpenNMS.

the class MeasurementsRestServiceITCase method cannotRetrieveUnknownAttributeAndUnknownFallbackAttribute.

@Test(expected = javax.ws.rs.WebApplicationException.class)
public void cannotRetrieveUnknownAttributeAndUnknownFallbackAttribute() {
    QueryRequest request = new QueryRequest();
    request.setStart(1414602000000L);
    request.setEnd(1417046400000L);
    request.setStep(1000L);
    request.setMaxRows(700);
    // Average
    Source ifInOctetsAvg = new Source();
    ifInOctetsAvg.setResourceId("node[1].interfaceSnmp[eth0-04013f75f101]");
    ifInOctetsAvg.setAttribute("willNotBeFound");
    ifInOctetsAvg.setFallbackAttribute("willNotBeFoundToo");
    ifInOctetsAvg.setAggregation("AVERAGE");
    ifInOctetsAvg.setLabel("ifInOctetsAvg");
    request.setSources(Lists.newArrayList(ifInOctetsAvg));
    // Perform the query - this must fail
    m_svc.query(request);
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) Source(org.opennms.netmgt.measurements.model.Source) Test(org.junit.Test)

Aggregations

Source (org.opennms.netmgt.measurements.model.Source)35 FetchResults (org.opennms.netmgt.measurements.api.FetchResults)18 Test (org.junit.Test)13 QueryRequest (org.opennms.netmgt.measurements.model.QueryRequest)13 OnmsResource (org.opennms.netmgt.model.OnmsResource)9 RrdGraphAttribute (org.opennms.netmgt.model.RrdGraphAttribute)9 QueryResponse (org.opennms.netmgt.measurements.model.QueryResponse)7 OnmsAttribute (org.opennms.netmgt.model.OnmsAttribute)6 Map (java.util.Map)5 MockResourceType (org.opennms.netmgt.mock.MockResourceType)4 RrdException (org.jrobin.core.RrdException)3 Expression (org.opennms.netmgt.measurements.model.Expression)3 ResourceId (org.opennms.netmgt.model.ResourceId)3 Measurement (org.opennms.newts.api.Measurement)3 Resource (org.opennms.newts.api.Resource)3 Row (org.opennms.newts.api.Results.Row)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Callable (java.util.concurrent.Callable)2