Search in sources :

Example 16 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 17 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 18 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 19 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)

Example 20 with RrdException

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

the class RrdConvertUtils method restoreRrd.

/**
 * Restores a RRD.
 *
 * @param rrd the RRD object
 * @param targetFile the target file
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws RrdException the RRD exception
 */
public static void restoreRrd(RRDv3 rrd, File targetFile) throws IOException, RrdException {
    String rrdBinary = System.getProperty("rrd.binary");
    if (rrdBinary == null) {
        throw new IllegalArgumentException("rrd.binary property must be set");
    }
    try {
        File xmlDest = new File(targetFile + ".xml");
        JaxbUtils.marshal(rrd, new FileWriter(xmlDest));
        Process process = Runtime.getRuntime().exec(new String[] { rrdBinary, "restore", xmlDest.getAbsolutePath(), targetFile.getAbsolutePath() });
        process.waitFor();
        if (!xmlDest.delete()) {
            LOG.warn("Could not delete file: {}", xmlDest.getPath());
        }
    } catch (Exception e) {
        throw new RrdException("Can't restore RRD", e);
    }
}
Also used : FileWriter(java.io.FileWriter) RrdException(org.jrobin.core.RrdException) File(java.io.File) IOException(java.io.IOException) RrdException(org.jrobin.core.RrdException)

Aggregations

RrdException (org.jrobin.core.RrdException)37 Node (org.w3c.dom.Node)13 IOException (java.io.IOException)10 Map (java.util.Map)4 Date (java.util.Date)3 FetchData (org.jrobin.core.FetchData)3 RrdDb (org.jrobin.core.RrdDb)3 FetchResults (org.opennms.netmgt.measurements.api.FetchResults)3 Source (org.opennms.netmgt.measurements.model.Source)3 File (java.io.File)2 Calendar (java.util.Calendar)2 JAXBContext (javax.xml.bind.JAXBContext)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 SAXSource (javax.xml.transform.sax.SAXSource)2 TimeParser (org.jrobin.core.timespec.TimeParser)2 TimeSpec (org.jrobin.core.timespec.TimeSpec)2 Color (java.awt.Color)1 Font (java.awt.Font)1 Graphics (java.awt.Graphics)1 BufferedImage (java.awt.image.BufferedImage)1