/** * Splits single RRD file with several datasources into a number of smaller RRD files * with a single datasource in it. All archived values are preserved. If * you have a RRD file named 'traffic.rrd' with two datasources, 'in' and 'out', this * method will create two files (with a single datasource, in the same directory) * named 'in-traffic.rrd' and 'out-traffic.rrd'. * * @param sourcePath Path to a RRD file with multiple datasources defined * @throws IOException Thrown in case of I/O error */ public static void split(String sourcePath) throws IOException { RrdDb rrdSource = new RrdDb(sourcePath); try { String[] dsNames = rrdSource.getDsNames(); for (String dsName : dsNames) { RrdDef rrdDef = rrdSource.getRrdDef(); rrdDef.setPath(createSplitPath(dsName, sourcePath)); rrdDef.saveSingleDatasource(dsName); RrdDb rrdDest = new RrdDb(rrdDef); try { rrdSource.copyStateTo(rrdDest); } finally { rrdDest.close(); } } } finally { rrdSource.close(); } }
/** * Performs update of the rrd with the current value of the gauge * @throws IOException */ public void update() throws IOException { logger.debug("RRDRequestExecutionTimeGauge.update() rrdFileName is {}", rrdFileName); RrdDb rrdDb = new RrdDb(rrdFileName); try { Sample sample = rrdDb.createSample(); long currentTimeSecs = Util.getTime(); String update = Long.toString(currentTimeSecs) +':'+ (long) gauge.resetAndGetAverageExecutionTime()+':'; sample.setAndUpdate(update); logger.debug("RRDRequestExecutionTimeGauge.update() updated with : {}", update); } finally { rrdDb.close(); logger.debug("RRDRequestExecutionTimeGauge.update() succeeded"); } }
/** * <p>Prepares fetch request to be executed on this RRD. Use returned * <code>FetchRequest</code> object and its {@link FetchRequest#fetchData() fetchData()} * method to actually fetch data from this RRD. Data will be fetched with the smallest * possible resolution (see RRDTool's * <a href="../../../../man/rrdfetch.html" target="man">rrdfetch man page</a> * for the explanation of the resolution parameter).</p> * * @param consolFun Consolidation function to be used in fetch request. Allowed values are * AVERAGE, MIN, MAX and LAST (see {@link ConsolFun} enum). * @param fetchStart Starting timestamp for fetch request. * @param fetchEnd Ending timestamp for fetch request. * @return Request object that should be used to actually fetch data from RRD. */ public FetchRequest createFetchRequest(ConsolFun consolFun, long fetchStart, long fetchEnd) { return createFetchRequest(consolFun, fetchStart, fetchEnd, 1); }
/** * Returns Datasource object corresponding to the given datasource name. * * @param dsName Datasource name * @return Datasource object corresponding to the give datasource name or null if not found. * @throws java.io.IOException Thrown in case of I/O error */ public Datasource getDatasource(String dsName) throws IOException { try { return getDatasource(getDsIndex(dsName)); } catch (IllegalArgumentException e) { return null; } }
/** * Sets datasource heartbeat to a new value. * * @param sourcePath Path to exisiting RRD file (will be updated) * @param datasourceName Name of the datasource in the specified RRD file * @param newHeartbeat New datasource heartbeat * @throws IOException Thrown in case of I/O error */ public static void setDsHeartbeat(String sourcePath, String datasourceName, long newHeartbeat) throws IOException { RrdDb rrd = new RrdDb(sourcePath); try { Datasource ds = rrd.getDatasource(datasourceName); ds.setHeartbeat(newHeartbeat); } finally { rrd.close(); } }
boolean setFile(File newFile) { try { RrdDb rrd = new RrdDb(newFile.getAbsolutePath(), true); try { DefaultMutableTreeNode root = new DefaultMutableTreeNode(new RrdNode(rrd)); int dsCount = rrd.getRrdDef().getDsCount(); int arcCount = rrd.getRrdDef().getArcCount(); for (int dsIndex = 0; dsIndex < dsCount; dsIndex++) { DefaultMutableTreeNode dsNode = rrd.close();
if (args.length > 0) { println("Setting default backend factory to " + args[0]); RrdDb.setDefaultFactory(args[0]); log.println(rrdDef.dump()); println("Estimated file size: " + rrdDef.getEstimatedSize()); RrdDb rrdDb = new RrdDb(rrdDef); println("== RRD file created."); if (rrdDb.getRrdDef().equals(rrdDef)) { println("Checking RRD file structure... OK"); } else { rrdDb.close(); println("== RRD file closed."); long t = start; int n = 0; rrdDb = new RrdDb(rrdPath); Sample sample = rrdDb.createSample(); rrdDb.close(); rrdDb = new RrdDb(rrdPath, true); println("File reopen in read-only mode"); println("== Last update time was: " + rrdDb.getLastUpdateTime()); println("== Last info was: " + rrdDb.getInfo()); FetchRequest request = rrdDb.createFetchRequest(AVERAGE, start, end); println(request.dump());
/** * Renames single datasource in the given RRD file. * * @param sourcePath Path to a RRD file * @param oldDsName Old datasource name * @param newDsName New datasource name * @throws IOException Thrown in case of I/O error */ public static void renameDatasource(String sourcePath, String oldDsName, String newDsName) throws IOException { RrdDb rrd = new RrdDb(sourcePath); try { if (rrd.containsDs(oldDsName)) { Datasource datasource = rrd.getDatasource(oldDsName); datasource.setDsName(newDsName); } else { throw new IllegalArgumentException("Could not find datasource [" + oldDsName + "] in file " + sourcePath); } } finally { rrd.close(); } }
/** * * @throws IOException */ public void update() throws IOException { logger.debug("RRDRequestCounter.update() rrdFileName is {}", rrdFileName); RrdDb rrdDb = new RrdDb(rrdFileName); Sample sample = rrdDb.createSample(); long currentTimeSecs = Util.getTime(); StringBuilder sb = new StringBuilder(); sb.append(currentTimeSecs).append(':'); sb.append(counter.getTotalRequests()).append(':'); sb.append(counter.getFailed()); sample.setAndUpdate(sb.toString()); logger.debug("RRDRequestCounter.update() updated with : {}", sb); rrdDb.close(); logger.debug("RRDRequestCounter.update() succeeded"); logger.debug("RRDRequestCounter.update() let us try to fetch data"); rrdDb = new RrdDb(rrdFileName); if(dumpstart == null) { dumpstart = currentTimeSecs - 100; } FetchRequest fetchRequest = rrdDb.createFetchRequest(ConsolFun.AVERAGE, dumpstart, currentTimeSecs+1); FetchData fetchData = fetchRequest.fetchData(); logger.debug("RRDRequestCounter.update() dump is: {}", fetchData.dump()); rrdDb.close(); }
System.out.print("fixed"); RrdDb rrd = new RrdDb(path); if (rrd.getRrdDef().getEstimatedSize() == files[i].length() && rrd.getDatasource(0).getHeartbeat() == heartbeat && rrd.getDatasource(1).getHeartbeat() == heartbeat) { System.out.println(", verified"); System.out.println(", ********** ERROR **********"); rrd.close();
/** * <p>Opens an existing RRD with read/write access. * The path will be parsed as an URI and checked against the active factories. * If it's a relative URI (no scheme given, or just a plain path), the default factory will be used.</p> * * @param path Path to existing RRD. * @throws java.io.IOException Thrown in case of I/O error. */ public static RrdDb of(String path) throws IOException { return new RrdDb(path, null, false, null, null); }
stCfg.addArchive(ConsolFun.AVERAGE, 0.5, 60, 1440); // every five minutes for five days statDB = new RrdDb(stCfg); statSample = statDB.createSample(); statLastDump = System.currentTimeMillis();
/** * Sets single archive's X-files factor to a new value. * * @param sourcePath Path to existing RRD file (will be updated) * @param consolFun Consolidation function of the target archive * @param steps Number of sptes of the target archive * @param newXff New X-files factor for the target archive * @throws IOException Thrown in case of I/O error */ public static void setArcXff(String sourcePath, ConsolFun consolFun, int steps, double newXff) throws IOException { RrdDb rrd = new RrdDb(sourcePath); try { Archive arc = rrd.getArchive(consolFun, steps); arc.setXff(newXff); } finally { rrd.close(); } }
protected void finalize() throws Throwable { try { close(); } finally { super.finalize(); } }
void setFile(File newFile) { try { values = null; String path = newFile.getAbsolutePath(); RrdDb rrd = new RrdDb(path, true); try { Header header = rrd.getHeader(); String signature = header.getSignature(); String step = "" + header.getStep(); String lastTimestamp = header.getLastUpdateTime() + " [" + new Date(header.getLastUpdateTime() * 1000L) + "]"; String datasources = "" + header.getDsCount(); String archives = "" + header.getArcCount(); String size = rrd.getRrdBackend().getLength() + " bytes"; values = new Object[]{ path, signature, step, lastTimestamp, datasources, archives, size }; } finally { rrd.close(); } fireTableDataChanged(); } catch (Exception e) { Util.error(null, e); } } }
public void testCreateDb() throws Exception { URL resource = Thread.currentThread().getContextClassLoader().getResource("etm/contrib/rrd/rrd4j/resources/basic_db_template.xml"); File path = File.createTempFile("test", ".rrd"); try { path.delete(); Rrd4jUtil rrd4jUtil = new Rrd4jUtil(); rrd4jUtil.createRrdDb(resource, path, null); assertTrue(path.exists()); RrdDb db = new RrdDb(path.getAbsolutePath(), true); assertEquals(4, db.getDsCount()); db.close(); } finally { if (path.exists()) { path.delete(); } } }
@JsonIgnore public Sample createSample(RrdDb rrdDb, Long time) throws IOException { final Sample sample; if (time != null) sample = rrdDb.createSample(time); else sample = rrdDb.createSample(); return sample; }
public synchronized void release(RrdDb rrdDb) throws IOException { // null pointer should not kill the thread, just ignore it if (rrdDb == null) { return; } String canonicalPath = Util.getCanonicalPath(rrdDb.getPath()); if (!rrdMap.containsKey(canonicalPath)) { throw new IllegalStateException("Could not release [" + canonicalPath + "], the file was never requested"); } RrdEntry entry = rrdMap.get(canonicalPath); if (--entry.count <= 0) { // no longer used rrdMap.remove(canonicalPath); notifyAll(); entry.rrdDb.close(); } }