📄 rrddb.java
字号:
*
* @param destination Output stream to receive XML data
* @throws IOException Thrown in case of I/O related error
*/
public synchronized void dumpXml(OutputStream destination) throws IOException {
XmlWriter writer = new XmlWriter(destination);
writer.startTag("rrd");
// dump header
header.appendXml(writer);
// dump datasources
for (Datasource datasource : datasources) {
datasource.appendXml(writer);
}
// dump archives
for (Archive archive : archives) {
archive.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/>
* <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>
* <p/>
* <code>RrdDb rrd = new RrdDb("original.rrd");
* rrd.dumpXml("original.xml");</code>
* <p/>
* <p>Use <code>original.xml</code> file to create the corresponding RRDTool file
* (from your command line):
* <p/>
* <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/>
* <p>Example:</p>
* <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 (Datasource datasource : datasources) {
DsDef dsDef = new DsDef(datasource.getDsName(),
datasource.getDsType(), datasource.getHeartbeat(),
datasource.getMinValue(), datasource.getMaxValue());
rrdDef.addDatasource(dsDef);
}
// add archives
for (Archive archive : archives) {
ArcDef arcDef = new ArcDef(archive.getConsolFun(),
archive.getXff(), archive.getSteps(), archive.getRows());
rrdDef.addArchive(arcDef);
}
return rrdDef;
}
protected void finalize() throws Throwable {
super.finalize();
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();
}
/**
* 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);
}
/**
* Returns an array of last datasource values. The first value in the array corresponds
* to the first datasource defined in the RrdDb and so on.
*
* @return Array of last datasource values
* @throws IOException Thrown in case of I/O error
*/
public synchronized double[] getLastDatasourceValues() throws IOException {
double[] values = new double[datasources.length];
for (int i = 0; i < values.length; i++) {
values[i] = datasources[i].getLastValue();
}
return values;
}
/**
* Returns the last stored value for the given datasource.
*
* @param dsName Datasource name
* @return Last stored value for the given datasource
* @throws IOException Thrown in case of I/O error
* @throws RrdException Thrown if no datasource in this RrdDb matches the given datasource name
*/
public synchronized double getLastDatasourceValue(String dsName) throws IOException, RrdException {
int dsIndex = getDsIndex(dsName);
return datasources[dsIndex].getLastValue();
}
/**
* Returns the number of datasources defined in the file
*
* @return The number of datasources defined in the file
*/
public int getDsCount() {
return datasources.length;
}
/**
* Returns the number of RRA arcihves defined in the file
*
* @return The number of RRA arcihves defined in the file
*/
public int getArcCount() {
return archives.length;
}
/**
* Returns the last time when some of the archives in this RRD was updated. This time is not the
* same as the {@link #getLastUpdateTime()} since RRD file can be updated without updating any of
* the archives.
*
* @return last time when some of the archives in this RRD was updated
* @throws IOException Thrown in case of I/O error
*/
public long getLastArchiveUpdateTime() throws IOException {
long last = 0;
for (Archive archive : archives) {
last = Math.max(last, archive.getEndTime());
}
return last;
}
public synchronized String getInfo() throws IOException {
return header.getInfo();
}
public synchronized void setInfo(String info) throws IOException {
header.setInfo(info);
}
public static void main(String[] args) {
System.out.println("JRobin Java Library :: RRDTool choice for the Java world");
System.out.println("==================================================================");
System.out.println("JRobin base directory: " + Util.getJRobinHomeDirectory());
long time = Util.getTime();
System.out.println("Current timestamp: " + time + ": " + new Date(time * 1000L));
System.out.println("------------------------------------------------------------------");
System.out.println("For the latest information visit: http://www.jrobin.org");
System.out.println("(C) 2003-2005 Sasa Markovic. All rights reserved.");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -