Search in sources :

Example 1 with JRrd2Exception

use of org.opennms.netmgt.rrd.jrrd2.api.JRrd2Exception in project opennms by OpenNMS.

the class MultithreadedJniRrdStrategy method fetchLastValueInRange.

/** {@inheritDoc} */
@Override
public Double fetchLastValueInRange(String rrdFile, String ds, int interval, int range) throws NumberFormatException, RrdException {
    try {
        final long now = System.currentTimeMillis();
        final long latestUpdateTime = (now - (now % interval)) / 1000L;
        final long earliestUpdateTime = ((now - (now % interval)) - range) / 1000L;
        FetchResults results = jrrd2.fetch(rrdFile, "AVERAGE", earliestUpdateTime, latestUpdateTime, 1);
        // Determine the index of the requested data-source
        final String[] dsNames = results.getColumns();
        int dsIndex = 0;
        for (int i = 0; i < dsNames.length; i++) {
            if (dsNames[i].equals(ds)) {
                dsIndex = i;
            }
        }
        final double[][] dsValues = results.getValues();
        final int numRows = dsValues[dsIndex].length;
        for (int i = numRows - 1; i >= 0; i--) {
            final double value = dsValues[dsIndex][i];
            if (!Double.isNaN(value)) {
                LOG.debug("fetchInRange: fetch successful: {}= {}", ds, value);
                return value;
            } else {
                LOG.debug("fetchInRange: Got a NaN value - continuing back in time");
            }
        }
        return dsValues[dsIndex][numRows - 1];
    } catch (JRrd2Exception e) {
        LOG.error("Fetch failed", e);
    }
    return null;
}
Also used : FetchResults(org.opennms.netmgt.rrd.jrrd2.api.FetchResults) JRrd2Exception(org.opennms.netmgt.rrd.jrrd2.api.JRrd2Exception)

Example 2 with JRrd2Exception

use of org.opennms.netmgt.rrd.jrrd2.api.JRrd2Exception 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 / 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;
    }
    // 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);
}
Also used : JRrd2Exception(org.opennms.netmgt.rrd.jrrd2.api.JRrd2Exception) Source(org.opennms.netmgt.measurements.model.Source) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) RrdException(org.jrobin.core.RrdException) Map(java.util.Map)

Example 3 with JRrd2Exception

use of org.opennms.netmgt.rrd.jrrd2.api.JRrd2Exception in project opennms by OpenNMS.

the class MultithreadedJniRrdStrategy method fetchLastValue.

/** {@inheritDoc} */
@Override
public Double fetchLastValue(String rrdFile, String ds, String consolidationFunction, int interval) {
    try {
        final long now = new Date().getTime();
        final long start = now - interval / 1000;
        final long end = now + interval / 1000;
        FetchResults results = jrrd2.fetch(rrdFile, consolidationFunction, start, end, 1);
        // Determine the index of the requested data-source
        final String[] dsNames = results.getColumns();
        int dsIndex = 0;
        for (int i = 0; i < dsNames.length; i++) {
            if (dsNames[i].equals(ds)) {
                dsIndex = i;
            }
        }
        // Grab the last value from that column
        final double[][] dsValues = results.getValues();
        final int numRows = dsValues[dsIndex].length;
        return dsValues[dsIndex][numRows - 1];
    } catch (JRrd2Exception e) {
        LOG.error("Fetch failed", e);
        return null;
    }
}
Also used : FetchResults(org.opennms.netmgt.rrd.jrrd2.api.FetchResults) JRrd2Exception(org.opennms.netmgt.rrd.jrrd2.api.JRrd2Exception) Date(java.util.Date)

Aggregations

JRrd2Exception (org.opennms.netmgt.rrd.jrrd2.api.JRrd2Exception)3 FetchResults (org.opennms.netmgt.rrd.jrrd2.api.FetchResults)2 Date (java.util.Date)1 Map (java.util.Map)1 RrdException (org.jrobin.core.RrdException)1 FetchResults (org.opennms.netmgt.measurements.api.FetchResults)1 Source (org.opennms.netmgt.measurements.model.Source)1