📄 rrddb.java
字号:
header.appendXml(writer);
// dump datasources
for(int i = 0; i < datasources.length; i++) {
datasources[i].appendXml(writer);
}
// dump archives
for(int i = 0; i < archives.length; i++) {
archives[i].appendXml(writer);
}
writer.closeTag();
writer.flush();
}
/**
* This method is just an alias for {@link #dumpXml(OutputStream) dumpXml} method.
* @throws IOException Thrown in case of I/O related error
*/
public synchronized void exportXml(OutputStream destination) throws IOException {
dumpXml(destination);
}
/**
* <p>Returns string representing internal RRD state in XML format. This format
* is fully compatible with RRDTool's XML dump format and can be used for conversion
* purposes or debugging.</p>
* @return Internal RRD state in XML format.
* @throws IOException Thrown in case of I/O related error
* @throws RrdException Thrown in case of JRobin specific error
*/
public synchronized String getXml() throws IOException, RrdException {
ByteArrayOutputStream destination = new ByteArrayOutputStream(XML_INITIAL_BUFFER_CAPACITY);
dumpXml(destination);
return destination.toString();
}
/**
* This method is just an alias for {@link #getXml() getXml} method.
* @return Internal RRD state in XML format.
* @throws IOException Thrown in case of I/O related error
* @throws RrdException Thrown in case of JRobin specific error
*/
public synchronized String exportXml() throws IOException, RrdException {
return getXml();
}
/**
* <p>Dumps internal RRD state to XML file.
* Use this XML file to convert your JRobin RRD to RRDTool format.</p>
*
* <p>Suppose that you have a JRobin RRD file <code>original.rrd</code> and you want
* to convert it to RRDTool format. First, execute the following java code:</p>
*
* <code>RrdDb rrd = new RrdDb("original.rrd");
* rrd.dumpXml("original.xml");</code>
*
* <p>Use <code>original.xml</code> file to create the corresponding RRDTool file
* (from your command line):
*
* <code>rrdtool restore copy.rrd original.xml</code>
*
* @param filename Path to XML file which will be created.
* @throws IOException Thrown in case of I/O related error.
* @throws RrdException Thrown in case of JRobin related error.
*/
public synchronized void dumpXml(String filename) throws IOException, RrdException {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(filename, false);
dumpXml(outputStream);
}
finally {
if(outputStream != null) {
outputStream.close();
}
}
}
/**
* This method is just an alias for {@link #dumpXml(String) dumpXml(String)} method.
* @throws IOException Thrown in case of I/O related error
* @throws RrdException Thrown in case of JRobin specific error
*/
public synchronized void exportXml(String filename) throws IOException, RrdException {
dumpXml(filename);
}
/**
* Returns time of last update operation as timestamp (in seconds).
* @return Last update time (in seconds).
*/
public synchronized long getLastUpdateTime() throws IOException {
return header.getLastUpdateTime();
}
/**
* <p>Returns RRD definition object which can be used to create new RRD
* with the same creation parameters but with no data in it.</p>
*
* <p>Example:</p>
*
* <pre>
* RrdDb rrd1 = new RrdDb("original.rrd");
* RrdDef def = rrd1.getRrdDef();
* // fix path
* def.setPath("empty_copy.rrd");
* // create new RRD file
* RrdDb rrd2 = new RrdDb(def);
* </pre>
* @return RRD definition.
* @throws RrdException Thrown in case of JRobin specific error.
*/
public synchronized RrdDef getRrdDef() throws RrdException, IOException {
// set header
long startTime = header.getLastUpdateTime();
long step = header.getStep();
String path = backend.getPath();
RrdDef rrdDef = new RrdDef(path, startTime, step);
// add datasources
for(int i = 0; i < datasources.length; i++) {
DsDef dsDef = new DsDef(datasources[i].getDsName(),
datasources[i].getDsType(), datasources[i].getHeartbeat(),
datasources[i].getMinValue(), datasources[i].getMaxValue());
rrdDef.addDatasource(dsDef);
}
// add archives
for(int i = 0; i < archives.length; i++) {
ArcDef arcDef = new ArcDef(archives[i].getConsolFun(),
archives[i].getXff(), archives[i].getSteps(), archives[i].getRows());
rrdDef.addArchive(arcDef);
}
return rrdDef;
}
/**
* <p>Returns current lock mode. This function can return one of the following values:
* <ul>
* <li><code>NO_LOCKS</code>: RRD files are not locked (default). Simultaneous access
* to the same RRD file is allowed. This locking mode provides fastest read/write
* (fetch/update) operations, but could lead to inconsisten RRD data.</li>
* <li><code>WAIT_IF_LOCKED</code>: RRD files are locked exclusively.
* Simultaneous access to the same underlying RRD file is not allowed.
* If a <code>RrdDb</code> object tries to access already locked RRD file,
* it will wait until the lock is released. The lock is released when
* the {@link #close() close()} method is called.</li>
* <li><code>EXCEPTION_IF_LOCKED</code>: RRD files are locked exclusively.
* Simultaneous access to the same underlying RRD file is not allowed.
* If a <code>RrdDb</code> object tries to access already locked RRD file,
* an exception is thrown.</li></p>
* </ul>
* <p>Note that <code>WAIT_IF_LOCKED</code> and <code>EXCEPTION_IF_LOCKED</code>
* modes guarantee data consistency but affect the speed of read/write operations.
* Call {@link #close() close()} as soon as possible in your code to avoid long wait states
* (or locking exceptions). </p>
* @return The current locking behaviour.
*/
public static int getLockMode() {
return RrdDb.lockMode;
}
/**
* Sets the current locking mode. See {@link #getLockMode() getLockMode()} for more
* information.
* @param lockMode Lock mode. Valid values are <code>NO_LOCKS</code>,
* <code>WAIT_IF_LOCKED</code> and <code>EXCEPTION_IF_LOCKED</code>.
*/
public static void setLockMode(int lockMode) {
RrdDb.lockMode = lockMode;
}
protected void finalize() throws Throwable {
close();
}
/**
* Copies object's internal state to another RrdDb object.
* @param other New RrdDb object to copy state to
* @throws IOException Thrown in case of I/O error
* @throws RrdException Thrown if supplied argument is not a compatible RrdDb object
*/
public synchronized void copyStateTo(RrdUpdater other) throws IOException, RrdException {
if(!(other instanceof RrdDb)) {
throw new RrdException(
"Cannot copy RrdDb object to " + other.getClass().getName());
}
RrdDb otherRrd = (RrdDb) other;
header.copyStateTo(otherRrd.header);
for(int i = 0; i < datasources.length; i++) {
int j = Util.getMatchingDatasourceIndex(this, i, otherRrd);
if(j >= 0) {
datasources[i].copyStateTo(otherRrd.datasources[j]);
}
}
for(int i = 0; i < archives.length; i++) {
int j = Util.getMatchingArchiveIndex(this, i, otherRrd);
if(j >= 0) {
archives[i].copyStateTo(otherRrd.archives[j]);
}
}
}
/**
* 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 IOException Thrown in case of I/O error
*/
public Datasource getDatasource(String dsName) throws IOException {
try {
return getDatasource(getDsIndex(dsName));
}
catch (RrdException e) {
return null;
}
}
/**
* Returns index of Archive object with the given consolidation function and the number
* of steps. Exception is thrown if such archive could not be found.
* @param consolFun Consolidation function
* @param steps Number of archive steps
* @return Requested Archive object
* @throws IOException Thrown in case of I/O error
* @throws RrdException Thrown if no such archive could be found
*/
public int getArcIndex(String consolFun, int steps) throws RrdException, IOException {
for(int i = 0; i < archives.length; i++) {
if(archives[i].getConsolFun().equals(consolFun) &&
archives[i].getSteps() == steps) {
return i;
}
}
throw new RrdException("Could not find archive " + consolFun + "/" + steps);
}
/**
* Returns Archive object with the given consolidation function and the number
* of steps.
* @param consolFun Consolidation function
* @param steps Number of archive steps
* @return Requested Archive object or null if no such archive could be found
* @throws IOException Thrown in case of I/O error
*/
public Archive getArchive(String consolFun, int steps) throws IOException {
try {
return getArchive(getArcIndex(consolFun, steps));
}
catch (RrdException e) {
return null;
}
}
/**
* Returns canonical path to the underlying RRD file. Note that this method makes sense just for
* ordinary RRD files created on the disk - an exception will be thrown for RRD objects created in
* memory or with custom backends.
* @return Canonical path to RRD file;
* @throws IOException Thrown in case of I/O error or if the underlying backend is
* not derived from RrdFileBackend.
*/
public String getCanonicalPath() throws IOException {
if(backend instanceof RrdFileBackend) {
return ((RrdFileBackend) backend).getCanonicalPath();
}
else {
throw new IOException("The underlying backend has no canonical path");
}
}
/**
* Returns path to this RRD.
* @return Path to this RRD.
*/
public String getPath() {
return backend.getPath();
}
/**
* Returns backend object for this RRD which performs actual I/O operations.
* @return RRD backend for this RRD.
*/
public RrdBackend getRrdBackend() {
return backend;
}
/**
* Required to implement RrdUpdater interface. You should never call this method directly.
* @return Allocator object
*/
public RrdAllocator getRrdAllocator() {
return allocator;
}
/**
* Returns an array of bytes representing the whole RRD.
* @return All RRD bytes
* @throws IOException Thrown in case of I/O related error.
*/
public synchronized byte[] getBytes() throws IOException {
return backend.readAll();
}
/**
* This method forces all RRD data cached in memory but not yet stored in the persistant
* storage, to be stored in it. This method should be used only if you RrdDb object uses
* RrdBackend which implements any kind of data caching (like {@link RrdNioBackend}). This method
* need not be called before the {@link #close()} method call since the close() method always
* synchronizes all data in memory with the data in the persisant storage.<p>
*
* When this method returns it is guaranteed that RRD data in the persistent storage is
* synchronized with the RrdDb data in memory.<p>
*
* @throws IOException Thrown in case of I/O error
*/
public synchronized void sync() throws IOException {
backend.sync();
}
/**
* Sets default backend factory to be used. This method is just an alias for
* {@link RrdBackendFactory#setDefaultFactory(java.lang.String)}.<p>
* @param factoryName Name of the backend factory to be set as default.
* @throws RrdException Thrown if invalid factory name is supplied, or not called
* before the first backend object (before the first RrdDb object) is created.
*/
public static void setDefaultFactory(String factoryName) throws RrdException {
RrdBackendFactory.setDefaultFactory(factoryName);
}
public static void main(String[] args) {
System.out.println("JRobin base directory: " + Util.getJRobinHomeDirectory());
System.out.println("JRobin Java Library :: RRDTool choice for the Java world");
System.out.println("http://www.jrobin.org");
System.out.println("(C) 2004 Sasa Markovic & Arne Vandamme");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -