Search in sources :

Example 1 with FetchData

use of org.jrobin.core.FetchData in project opennms by OpenNMS.

the class JRobinRrdStrategy method fetchLastValue.

/** {@inheritDoc} */
@Override
public Double fetchLastValue(final String fileName, final String ds, final String consolidationFunction, final int interval) throws org.opennms.netmgt.rrd.RrdException {
    RrdDb rrd = null;
    try {
        long now = System.currentTimeMillis();
        long collectTime = (now - (now % interval)) / 1000L;
        rrd = new RrdDb(fileName, true);
        FetchData data = rrd.createFetchRequest(consolidationFunction, collectTime, collectTime).fetchData();
        LOG.debug(data.toString());
        double[] vals = data.getValues(ds);
        if (vals.length > 0) {
            return new Double(vals[vals.length - 1]);
        }
        return null;
    } catch (IOException e) {
        throw new org.opennms.netmgt.rrd.RrdException("Exception occurred fetching data from " + fileName, e);
    } catch (RrdException e) {
        throw new org.opennms.netmgt.rrd.RrdException("Exception occurred fetching data from " + fileName, e);
    } finally {
        if (rrd != null) {
            try {
                rrd.close();
            } catch (IOException e) {
                LOG.error("Failed to close rrd file: {}", fileName, e);
            }
        }
    }
}
Also used : RrdDb(org.jrobin.core.RrdDb) IOException(java.io.IOException) RrdException(org.jrobin.core.RrdException) FetchData(org.jrobin.core.FetchData)

Example 2 with FetchData

use of org.jrobin.core.FetchData in project opennms by OpenNMS.

the class JRobinRrdStrategy method fetchLastValueInRange.

/** {@inheritDoc} */
@Override
public Double fetchLastValueInRange(final String fileName, final String ds, final int interval, final int range) throws NumberFormatException, org.opennms.netmgt.rrd.RrdException {
    RrdDb rrd = null;
    try {
        rrd = new RrdDb(fileName, true);
        long now = System.currentTimeMillis();
        long latestUpdateTime = (now - (now % interval)) / 1000L;
        long earliestUpdateTime = ((now - (now % interval)) - range) / 1000L;
        LOG.debug("fetchInRange: fetching data from {} to {}", earliestUpdateTime, latestUpdateTime);
        FetchData data = rrd.createFetchRequest("AVERAGE", earliestUpdateTime, latestUpdateTime).fetchData();
        double[] vals = data.getValues(ds);
        long[] times = data.getTimestamps();
        for (int i = vals.length - 1; i >= 0; i--) {
            if (Double.isNaN(vals[i])) {
                LOG.debug("fetchInRange: Got a NaN value at interval: {} continuing back in time", times[i]);
            } else {
                LOG.debug("Got a non NaN value at interval: {} : {}", times[i], vals[i]);
                return new Double(vals[i]);
            }
        }
        return null;
    } catch (IOException e) {
        throw new org.opennms.netmgt.rrd.RrdException("Exception occurred fetching data from " + fileName, e);
    } catch (RrdException e) {
        throw new org.opennms.netmgt.rrd.RrdException("Exception occurred fetching data from " + fileName, e);
    } finally {
        if (rrd != null) {
            try {
                rrd.close();
            } catch (IOException e) {
                LOG.error("Failed to close rrd file: {}", fileName, e);
            }
        }
    }
}
Also used : RrdDb(org.jrobin.core.RrdDb) IOException(java.io.IOException) RrdException(org.jrobin.core.RrdException) FetchData(org.jrobin.core.FetchData)

Example 3 with FetchData

use of org.jrobin.core.FetchData in project opennms by OpenNMS.

the class JRobinConverterTest method checkArchive.

protected void checkArchive(final RrdDb rrd, final Integer nanSample, final Integer numberSample, final Double numberValue, final Integer archiveIndex, final Integer numDses, final Integer dsIndex, String dsName) throws RrdException, IOException {
    LogUtils.debugf(this, "checking archive %s for consistency", rrd);
    final Archive archive = rrd.getArchive(archiveIndex);
    final Map<String, Integer> indexes = m_converter.getDsIndexes(rrd);
    assertEquals(numDses, Integer.valueOf(indexes.size()));
    final Robin robin = archive.getRobin(dsIndex);
    if (nanSample == null) {
        for (final double value : robin.getValues()) {
            assertTrue(!Double.isNaN(value));
        }
    } else {
        assertTrue(Double.isNaN(robin.getValue(nanSample)));
    }
    assertEquals(numberValue, Double.valueOf(robin.getValue(numberSample)));
    // Make sure FetchData matches
    final FetchData data = rrd.createFetchRequest("AVERAGE", archive.getStartTime(), archive.getEndTime()).fetchData();
    final double[] values = data.getValues(dsName);
    if (nanSample == null) {
        for (final double value : values) {
            assertTrue(!Double.isNaN(value));
        }
    } else {
        assertTrue(Double.isNaN(values[nanSample]));
    }
    assertEquals(numberValue, Double.valueOf(values[numberSample]));
}
Also used : Archive(org.jrobin.core.Archive) Robin(org.jrobin.core.Robin) FetchData(org.jrobin.core.FetchData)

Example 4 with FetchData

use of org.jrobin.core.FetchData in project opennms by OpenNMS.

the class JRobinConverterTest method testFetch.

@Test
public void testFetch() throws Exception {
    createMockSineRrds(null);
    RrdDb rrd = new RrdDb(m_sineFull);
    final long endTime = rrd.getLastArchiveUpdateTime();
    final long startTime = endTime - (60L * 60L * 24L * 365L);
    final FetchData fd = rrd.createFetchRequest("AVERAGE", startTime, endTime, 300).fetchData();
    double[] values = fd.getValues("a");
    assertEquals(367, values.length);
}
Also used : RrdDb(org.jrobin.core.RrdDb) FetchData(org.jrobin.core.FetchData) Test(org.junit.Test)

Example 5 with FetchData

use of org.jrobin.core.FetchData in project opennms by OpenNMS.

the class SpikeHunter method replaceInArchive.

private static void replaceInArchive(org.jrobin.core.Archive arc) {
    String consolFun = "";
    int arcSteps = 0;
    long startTime = 0;
    long endTime = 0;
    FetchData data = null;
    Robin robin = null;
    try {
        consolFun = arc.getConsolFun();
        arcSteps = arc.getSteps();
        startTime = arc.getStartTime();
        endTime = arc.getEndTime();
    } catch (IOException e) {
        System.out.println("IO Exception trying to get archive information from RRD file: " + e.getMessage());
        System.exit(-1);
    }
    printToUser("Operating on archive with CF " + consolFun + ", " + arcSteps + " steps");
    try {
        data = m_rrdFile.createFetchRequest(consolFun, startTime, endTime).fetchData();
    } catch (RrdException rrde) {
        System.out.println("RRD Exception trying to create fetch request: " + rrde.getMessage());
        System.exit(-1);
    } catch (IOException ioe) {
        System.out.println("IO Exception trying to create fetch request: " + ioe.getMessage());
        System.exit(-1);
    }
    String[] dsNames;
    if (m_dsNames == null) {
        dsNames = data.getDsNames();
    } else {
        dsNames = m_dsNames.split(",");
    }
    for (String dsName : dsNames) {
        replaceInDs(arc, data, dsName);
    }
}
Also used : IOException(java.io.IOException) RrdException(org.jrobin.core.RrdException) FetchData(org.jrobin.core.FetchData) Robin(org.jrobin.core.Robin)

Aggregations

FetchData (org.jrobin.core.FetchData)5 IOException (java.io.IOException)3 RrdDb (org.jrobin.core.RrdDb)3 RrdException (org.jrobin.core.RrdException)3 Robin (org.jrobin.core.Robin)2 Archive (org.jrobin.core.Archive)1 Test (org.junit.Test)1