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);
}
}
}
}
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);
}
}
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);
}
}
}
}
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);
}
}
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);
}
}
Aggregations