Search in sources :

Example 6 with RrdException

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

the class JrobinFetchStrategy 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 {
    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 DataProcessor dproc = new DataProcessor(startInSeconds, endInSeconds);
    if (maxrows > 0) {
        dproc.setPixelCount(maxrows);
    }
    dproc.setFetchRequestResolution(stepInSeconds);
    for (final Map.Entry<Source, String> entry : rrdsBySource.entrySet()) {
        final Source source = entry.getKey();
        final String rrdFile = entry.getValue();
        dproc.addDatasource(source.getLabel(), rrdFile, source.getEffectiveDataSource(), source.getAggregation());
    }
    try {
        dproc.processData();
    } catch (IOException e) {
        throw new RrdException("JRB processing failed.", e);
    }
    final long[] timestamps = dproc.getTimestamps();
    // Convert the timestamps to milliseconds
    for (int i = 0; i < timestamps.length; i++) {
        timestamps[i] *= 1000;
    }
    final Map<String, double[]> columns = Maps.newHashMapWithExpectedSize(rrdsBySource.keySet().size());
    for (Source source : rrdsBySource.keySet()) {
        columns.put(source.getLabel(), dproc.getValues(source.getLabel()));
    }
    return new FetchResults(timestamps, columns, dproc.getStep() * 1000, constants);
}
Also used : FetchResults(org.opennms.netmgt.measurements.api.FetchResults) DataProcessor(org.jrobin.data.DataProcessor) IOException(java.io.IOException) RrdException(org.jrobin.core.RrdException) Map(java.util.Map) Source(org.opennms.netmgt.measurements.model.Source)

Example 7 with RrdException

use of org.jrobin.core.RrdException 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 8 with RrdException

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

the class JRobinRrdStrategy method parseGraphColor.

/**
     * @param colorArg Should have the form COLORTAG#RRGGBB[AA]
     * @see http://www.jrobin.org/support/man/rrdgraph.html
     */
private void parseGraphColor(final RrdGraphDef graphDef, final String colorArg) throws IllegalArgumentException {
    // Parse for format COLORTAG#RRGGBB[AA]
    String[] colorArgParts = tokenize(colorArg, "#", false);
    if (colorArgParts.length != 2) {
        throw new IllegalArgumentException("--color must be followed by value with format COLORTAG#RRGGBB[AA]");
    }
    String colorTag = colorArgParts[0].toUpperCase();
    String colorHex = colorArgParts[1].toUpperCase();
    // validate hex color input is actually an RGB hex color value
    if (colorHex.length() != 6 && colorHex.length() != 8) {
        throw new IllegalArgumentException("--color must be followed by value with format COLORTAG#RRGGBB[AA]");
    }
    // this might throw NumberFormatException, but whoever wrote
    // createGraph didn't seem to care, so I guess I don't care either.
    // It'll get wrapped in an RrdException anyway.
    Color color = getColor(colorHex);
    // These are the documented RRD color tags
    try {
        if (colorTag.equals("BACK")) {
            graphDef.setColor("BACK", color);
        } else if (colorTag.equals("CANVAS")) {
            graphDef.setColor("CANVAS", color);
        } else if (colorTag.equals("SHADEA")) {
            graphDef.setColor("SHADEA", color);
        } else if (colorTag.equals("SHADEB")) {
            graphDef.setColor("SHADEB", color);
        } else if (colorTag.equals("GRID")) {
            graphDef.setColor("GRID", color);
        } else if (colorTag.equals("MGRID")) {
            graphDef.setColor("MGRID", color);
        } else if (colorTag.equals("FONT")) {
            graphDef.setColor("FONT", color);
        } else if (colorTag.equals("FRAME")) {
            graphDef.setColor("FRAME", color);
        } else if (colorTag.equals("ARROW")) {
            graphDef.setColor("ARROW", color);
        } else {
            throw new org.jrobin.core.RrdException("Unknown color tag " + colorTag);
        }
    } catch (Throwable e) {
        LOG.error("JRobin: exception occurred creating graph", e);
    }
}
Also used : Color(java.awt.Color) RrdException(org.jrobin.core.RrdException)

Example 9 with RrdException

use of org.jrobin.core.RrdException 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 10 with RrdException

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

the class RrdConvertUtils method dumpRrd.

/**
     * Dumps a RRD.
     *
     * @param sourceFile the source file
     * @return the RRD Object
     * @throws IOException Signals that an I/O exception has occurred.
     * @throws RrdException the RRD exception
     */
public static RRDv3 dumpRrd(File sourceFile) throws IOException, RrdException {
    String rrdBinary = System.getProperty("rrd.binary");
    if (rrdBinary == null) {
        throw new IllegalArgumentException("rrd.binary property must be set");
    }
    try {
        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        Process process = Runtime.getRuntime().exec(new String[] { rrdBinary, "dump", sourceFile.getAbsolutePath() });
        SAXSource source = new SAXSource(xmlReader, new InputSource(new InputStreamReader(process.getInputStream())));
        JAXBContext jc = JAXBContext.newInstance(RRDv3.class);
        Unmarshaller u = jc.createUnmarshaller();
        return (RRDv3) u.unmarshal(source);
    } catch (Exception e) {
        throw new RrdException("Can't parse RRD Dump", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) InputStreamReader(java.io.InputStreamReader) RRDv3(org.opennms.netmgt.rrd.model.v3.RRDv3) JAXBContext(javax.xml.bind.JAXBContext) RrdException(org.jrobin.core.RrdException) Unmarshaller(javax.xml.bind.Unmarshaller) XMLReader(org.xml.sax.XMLReader) IOException(java.io.IOException) RrdException(org.jrobin.core.RrdException)

Aggregations

RrdException (org.jrobin.core.RrdException)13 IOException (java.io.IOException)8 Map (java.util.Map)3 FetchData (org.jrobin.core.FetchData)3 FetchResults (org.opennms.netmgt.measurements.api.FetchResults)3 Source (org.opennms.netmgt.measurements.model.Source)3 JAXBContext (javax.xml.bind.JAXBContext)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 SAXSource (javax.xml.transform.sax.SAXSource)2 RrdDb (org.jrobin.core.RrdDb)2 TimeParser (org.jrobin.core.timespec.TimeParser)2 TimeSpec (org.jrobin.core.timespec.TimeSpec)2 InputSource (org.xml.sax.InputSource)2 XMLReader (org.xml.sax.XMLReader)2 Color (java.awt.Color)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1