|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.jrobin.core.RrdDb
public class RrdDb
Main class used to create and manipulate round robin databases (RRDs). Use this class to perform
update and fetch operations on exisiting RRDs, to create new RRD from
the definition (object of class RrdDef
) or
from XML file (dumped content of RRDTool's or JRobin's RRD file).
Each RRD is backed with some kind of storage. For example, RRDTool supports only one kind of storage (disk file). On the contrary, JRobin gives you freedom to use other storage (backend) types even to create your own backend types for some special purposes. JRobin by default stores RRD data in files (as RRDTool), but you might choose to store RRD data in memory (this is supported in JRobin), to use java.nio.* instead of java.io.* package for file manipulation (also supported) or to store whole RRDs in the SQL database (you'll have to extend some classes to do this).
Note that JRobin uses binary format different from RRDTool's format. You cannot use this class to manipulate RRD files created with RRDTool. However, if you perform the same sequence of create, update and fetch operations, you will get exactly the same results from JRobin and RRDTool.
You will not be able to use JRobin API if you are not familiar with basic RRDTool concepts. Good place to start is the official RRD tutorial and relevant RRDTool man pages: rrdcreate, rrdupdate, rrdfetch and rrdgraph. For RRDTool's advanced graphing capabilities (RPN extensions), also supported in JRobin, there is an excellent CDEF tutorial.
RrdBackend
,
RrdBackendFactory
Field Summary | |
---|---|
static int |
EXCEPTION_IF_LOCKED
See getLockMode() for explanation |
static int |
NO_LOCKS
See getLockMode() for explanation |
static int |
WAIT_IF_LOCKED
See getLockMode() for explanation |
Constructor Summary | |
---|---|
RrdDb(RrdDef rrdDef)
Constructor used to create new RRD object from the definition. |
|
RrdDb(RrdDef rrdDef,
RrdBackendFactory factory)
Constructor used to create new RRD object from the definition object but with a storage (backend) different from default. |
|
RrdDb(java.lang.String path)
Constructor used to open already existing RRD in R/W mode, with a default storage (backend) type (file on the disk). |
|
RrdDb(java.lang.String path,
boolean readOnly)
Constructor used to open already existing RRD. |
|
RrdDb(java.lang.String path,
boolean readOnly,
RrdBackendFactory factory)
Constructor used to open already existing RRD backed with a storage (backend) different from default. |
|
RrdDb(java.lang.String path,
RrdBackendFactory factory)
Constructor used to open already existing RRD in R/W mode with a storage (backend) type different from default. |
|
RrdDb(java.lang.String rrdPath,
java.lang.String xmlPath)
Constructor used to create new RRD from XML dump. |
|
RrdDb(java.lang.String rrdPath,
java.lang.String xmlPath,
RrdBackendFactory factory)
Constructor used to create new RRD from XML dump but with a storage (backend) type different from default. |
Method Summary | |
---|---|
void |
close()
Closes RRD. |
void |
copyStateTo(org.jrobin.core.RrdUpdater other)
Copies object's internal state to another RrdDb object. |
FetchRequest |
createFetchRequest(java.lang.String consolFun,
long fetchStart,
long fetchEnd)
Prepares fetch request to be executed on this RRD. |
FetchRequest |
createFetchRequest(java.lang.String consolFun,
long fetchStart,
long fetchEnd,
long resolution)
Prepares fetch request to be executed on this RRD. |
Sample |
createSample()
Creates new sample with the current timestamp and all data source values set to 'unknown'. |
Sample |
createSample(long time)
Creates new sample with the given timestamp and all datasource values set to 'unknown'. |
java.lang.String |
dump()
Returns string representing complete internal RRD state. |
void |
dumpXml(java.io.OutputStream destination)
Writes the RRD content to OutputStream using XML format. |
void |
dumpXml(java.lang.String filename)
Dumps internal RRD state to XML file. |
java.lang.String |
exportXml()
This method is just an alias for getXml method. |
void |
exportXml(java.io.OutputStream destination)
This method is just an alias for dumpXml method. |
void |
exportXml(java.lang.String filename)
This method is just an alias for dumpXml(String) method. |
protected void |
finalize()
|
Archive |
findMatchingArchive(FetchRequest request)
|
Archive |
findStartMatchArchive(java.lang.String consolFun,
long startTime,
long resolution)
Finds the archive that best matches to the start time (time period being start-time until now) and requested resolution. |
Archive |
getArchive(int arcIndex)
Returns Archive object for the given archive index. |
Archive |
getArchive(java.lang.String consolFun,
int steps)
Returns Archive object with the given consolidation function and the number of steps. |
int |
getArcIndex(java.lang.String consolFun,
int steps)
Returns index of Archive object with the given consolidation function and the number of steps. |
byte[] |
getBytes()
Returns an array of bytes representing the whole RRD. |
java.lang.String |
getCanonicalPath()
Returns canonical path to the underlying RRD file. |
Datasource |
getDatasource(int dsIndex)
Returns Datasource object for the given datasource index. |
Datasource |
getDatasource(java.lang.String dsName)
Returns Datasource object corresponding to the given datasource name. |
int |
getDsIndex(java.lang.String dsName)
Returns internal index number for the given datasource name. |
java.lang.String[] |
getDsNames()
Returns an array of datasource names defined in RRD. |
Header |
getHeader()
Returns RRD header. |
long |
getLastUpdateTime()
Returns time of last update operation as timestamp (in seconds). |
static int |
getLockMode()
Returns current lock mode. |
java.lang.String |
getPath()
Returns path to this RRD. |
org.jrobin.core.RrdAllocator |
getRrdAllocator()
Required to implement RrdUpdater interface. |
RrdBackend |
getRrdBackend()
Returns backend object for this RRD which performs actual I/O operations. |
RrdDef |
getRrdDef()
Returns RRD definition object which can be used to create new RRD with the same creation parameters but with no data in it. |
java.lang.String |
getXml()
Returns string representing internal RRD state in XML format. |
boolean |
isClosed()
Returns true if the RRD is closed. |
static void |
main(java.lang.String[] args)
|
static void |
setDefaultFactory(java.lang.String factoryName)
Sets default backend factory to be used. |
static void |
setLockMode(int lockMode)
Sets the current locking mode. |
void |
sync()
This method forces all RRD data cached in memory but not yet stored in the persistant storage, to be stored in it. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int NO_LOCKS
getLockMode()
for explanation
public static final int WAIT_IF_LOCKED
getLockMode()
for explanation
public static final int EXCEPTION_IF_LOCKED
getLockMode()
for explanation
Constructor Detail |
---|
public RrdDb(RrdDef rrdDef) throws RrdException, java.io.IOException
Constructor used to create new RRD object from the definition. This RRD object will be backed
with a storage (backend) of the default type. Initially, storage type defaults to "NIO"
(RRD bytes will be put in a file on the disk). Default storage type can be changed with a static
RrdBackendFactory.setDefaultFactory(String)
method call.
New RRD file structure is specified with an object of class
RrdDef
. The underlying RRD storage is created as soon
as the constructor returns.
Typical scenario:
// create new RRD definition RrdDef def = new RrdDef("test.rrd", 300); def.addDatasource("input", "COUNTER", 600, 0, Double.NaN); def.addDatasource("output", "COUNTER", 600, 0, Double.NaN); def.addArchive("AVERAGE", 0.5, 1, 600); def.addArchive("AVERAGE", 0.5, 6, 700); def.addArchive("AVERAGE", 0.5, 24, 797); def.addArchive("AVERAGE", 0.5, 288, 775); def.addArchive("MAX", 0.5, 1, 600); def.addArchive("MAX", 0.5, 6, 700); def.addArchive("MAX", 0.5, 24, 797); def.addArchive("MAX", 0.5, 288, 775); // RRD definition is now completed, create the database! RrdDb rrd = new RrdDb(def); // new RRD file has been created on your disk
rrdDef
- Object describing the structure of the new RRD file.
java.io.IOException
- Thrown in case of I/O error.
RrdException
- Thrown if invalid RrdDef object is supplied.public RrdDb(RrdDef rrdDef, RrdBackendFactory factory) throws RrdException, java.io.IOException
Constructor used to create new RRD object from the definition object but with a storage (backend) different from default.
JRobin uses factories to create RRD backend objecs. There are three different backend factories supplied with JRobin, and each factory has its unique name:
For example, to create RRD in memory, use the following code
RrdBackendFactory factory = RrdBackendFactory.getFactory("MEMORY"); RrdDb rrdDb = new RrdDb(rrdDef, factory); rrdDb.close();
New RRD file structure is specified with an object of class
RrdDef
. The underlying RRD storage is created as soon
as the constructor returns.
rrdDef
- RRD definition objectfactory
- The factory which will be used to create storage for this RRD
RrdException
- Thrown if invalid factory or definition is supplied
java.io.IOException
- Thrown in case of I/O errorRrdBackendFactory
public RrdDb(java.lang.String path, boolean readOnly) throws java.io.IOException, RrdException
Constructor used to open already existing RRD. This RRD object will be backed with a storage (backend) of the default type (file on the disk). Constructor obtains read or read/write access to this RRD.
path
- Path to existing RRD.readOnly
- Should be set to false
if you want to update
the underlying RRD. If you want just to fetch data from the RRD file
(read-only access), specify true
. If you try to update RRD file
open in read-only mode (readOnly
set to true
),
IOException
will be thrown.
java.io.IOException
- Thrown in case of I/O error.
RrdException
- Thrown in case of JRobin specific error.public RrdDb(java.lang.String path, boolean readOnly, RrdBackendFactory factory) throws java.io.IOException, RrdException
Constructor used to open already existing RRD backed with a storage (backend) different from default. Constructor obtains read or read/write access to this RRD.
path
- Path to existing RRD.readOnly
- Should be set to false
if you want to update
the underlying RRD. If you want just to fetch data from the RRD file
(read-only access), specify true
. If you try to update RRD file
open in read-only mode (readOnly
set to true
),
IOException
will be thrown.factory
- Backend factory which will be used for this RRD.
java.io.IOException
- Thrown in case of I/O error.
RrdException
- Thrown in case of JRobin specific error.RrdBackendFactory
public RrdDb(java.lang.String path) throws java.io.IOException, RrdException
Constructor used to open already existing RRD in R/W mode, with a default storage (backend) type (file on the disk).
path
- Path to existing RRD.
java.io.IOException
- Thrown in case of I/O error.
RrdException
- Thrown in case of JRobin specific error.public RrdDb(java.lang.String path, RrdBackendFactory factory) throws java.io.IOException, RrdException
Constructor used to open already existing RRD in R/W mode with a storage (backend) type different from default.
path
- Path to existing RRD.factory
- Backend factory used to create this RRD.
java.io.IOException
- Thrown in case of I/O error.
RrdException
- Thrown in case of JRobin specific error.RrdBackendFactory
public RrdDb(java.lang.String rrdPath, java.lang.String xmlPath) throws java.io.IOException, RrdException
Constructor used to create new RRD from XML dump. Newly created RRD will be backed with a default storage (backend) type (file on the disk). JRobin and RRDTool use the same format for XML dump and this constructor should be used to (re)create JRobin RRD from XML. In other words, it is possible to convert RRDTool RRD files to JRobin RRD files: first, dump the content of RRDTool RRD file (use command line):
rrdtool dump original.rrd > original.xml
Than, use file original.xml
to create JRobin RRD file
copy.rrd
:
RrdDb rrd = new RrdDb("copy.rrd", "original.xml");
See documentation for dumpXml()
method
how to convert JRobin files to RRDTool format.
rrdPath
- Path to RRD file which will be createdxmlPath
- Path to file containing XML dump of RRDTool's or JRobin's RRD file
java.io.IOException
- Thrown in case of I/O error
RrdException
- Thrown in case of JRobin specific errorpublic RrdDb(java.lang.String rrdPath, java.lang.String xmlPath, RrdBackendFactory factory) throws java.io.IOException, RrdException
Constructor used to create new RRD from XML dump but with a storage (backend) type different from default.
rrdPath
- Path to RRD which will be createdxmlPath
- Path to file containing XML dump of RRDTool's or JRobin's RRD filefactory
- Backend factory which will be used to create storage (backend) for this RRD.
java.io.IOException
- Thrown in case of I/O error
RrdException
- Thrown in case of JRobin specific errorRrdBackendFactory
Method Detail |
---|
public void close() throws java.io.IOException
java.io.IOException
- Thrown in case of I/O related error.public boolean isClosed()
public Header getHeader()
public Datasource getDatasource(int dsIndex)
dsIndex
- Datasource index (zero based)
public Archive getArchive(int arcIndex)
arcIndex
- Archive index (zero based)
public java.lang.String[] getDsNames() throws java.io.IOException
Returns an array of datasource names defined in RRD.
java.io.IOException
- Thrown in case of I/O error.public Sample createSample(long time) throws java.io.IOException
Creates new sample with the given timestamp and all datasource values set to
'unknown'. Use returned Sample
object to specify
datasource values for the given timestamp. See documentation for
Sample
for an explanation how to do this.
Once populated with data source values, call Sample's
update()
method to actually
store sample in the RRD associated with it.
time
- Sample timestamp rounded to the nearest second (without milliseconds).
java.io.IOException
- Thrown in case of I/O error.public Sample createSample() throws java.io.IOException
Creates new sample with the current timestamp and all data source values set to
'unknown'. Use returned Sample
object to specify
datasource values for the current timestamp. See documentation for
Sample
for an explanation how to do this.
Once populated with data source values, call Sample's
update()
method to actually
store sample in the RRD associated with it.
java.io.IOException
- Thrown in case of I/O error.public FetchRequest createFetchRequest(java.lang.String consolFun, long fetchStart, long fetchEnd, long resolution) throws RrdException
Prepares fetch request to be executed on this RRD. Use returned
FetchRequest
object and its fetch()
method to actually fetch data from the RRD file.
consolFun
- Consolidation function to be used in fetch request. Allowed values are
"AVERAGE", "MIN", "MAX" and "LAST".fetchStart
- Starting timestamp for fetch request.fetchEnd
- Ending timestamp for fetch request.resolution
- Fetch resolution (see RRDTool's
rrdfetch man page for an
explanation of this parameter.
RrdException
- In case of JRobin related error (invalid consolidation function or
invalid time span).public FetchRequest createFetchRequest(java.lang.String consolFun, long fetchStart, long fetchEnd) throws RrdException
Prepares fetch request to be executed on this RRD. Use returned
FetchRequest
object and its fetch()
method to actually fetch data from this RRD. Data will be fetched with the smallest
possible resolution (see RRDTool's
rrdfetch man page
for the explanation of the resolution parameter).
consolFun
- Consolidation function to be used in fetch request. Allowed values are
"AVERAGE", "MIN", "MAX" and "LAST".fetchStart
- Starting timestamp for fetch request.fetchEnd
- Ending timestamp for fetch request.
RrdException
- In case of JRobin related error (invalid consolidation function or
invalid time span).public Archive findMatchingArchive(FetchRequest request) throws RrdException, java.io.IOException
RrdException
java.io.IOException
public Archive findStartMatchArchive(java.lang.String consolFun, long startTime, long resolution) throws java.io.IOException
consolFun
- Consolidation function of the datasource.startTime
- Start time of the time period in seconds.resolution
- Requested fetch resolution.
java.io.IOException
- Thrown in case of I/O related error.public java.lang.String dump() throws java.io.IOException
Returns string representing complete internal RRD state. The returned
string can be printed to stdout
and/or used for debugging purposes.
java.io.IOException
- Thrown in case of I/O related error.public int getDsIndex(java.lang.String dsName) throws RrdException, java.io.IOException
Returns internal index number for the given datasource name. This index is heavily used by jrobin.graph package and has no value outside of it.
dsName
- Data source name.
RrdException
- Thrown in case of JRobin related error (invalid data source name,
for example)
java.io.IOException
- Thrown in case of I/O error.public void dumpXml(java.io.OutputStream destination) throws java.io.IOException
Writes the RRD content to OutputStream using XML format. This format is fully compatible with RRDTool's XML dump format and can be used for conversion purposes or debugging.
destination
- Output stream to receive XML data
java.io.IOException
- Thrown in case of I/O related errorpublic void exportXml(java.io.OutputStream destination) throws java.io.IOException
dumpXml
method.
java.io.IOException
- Thrown in case of I/O related errorpublic java.lang.String getXml() throws java.io.IOException, RrdException
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.
java.io.IOException
- Thrown in case of I/O related error
RrdException
- Thrown in case of JRobin specific errorpublic java.lang.String exportXml() throws java.io.IOException, RrdException
getXml
method.
java.io.IOException
- Thrown in case of I/O related error
RrdException
- Thrown in case of JRobin specific errorpublic void dumpXml(java.lang.String filename) throws java.io.IOException, RrdException
Dumps internal RRD state to XML file. Use this XML file to convert your JRobin RRD to RRDTool format.
Suppose that you have a JRobin RRD file original.rrd
and you want
to convert it to RRDTool format. First, execute the following java code:
RrdDb rrd = new RrdDb("original.rrd");
rrd.dumpXml("original.xml");
Use original.xml
file to create the corresponding RRDTool file
(from your command line):
rrdtool restore copy.rrd original.xml
filename
- Path to XML file which will be created.
java.io.IOException
- Thrown in case of I/O related error.
RrdException
- Thrown in case of JRobin related error.public void exportXml(java.lang.String filename) throws java.io.IOException, RrdException
dumpXml(String)
method.
java.io.IOException
- Thrown in case of I/O related error
RrdException
- Thrown in case of JRobin specific errorpublic long getLastUpdateTime() throws java.io.IOException
java.io.IOException
public RrdDef getRrdDef() throws RrdException, java.io.IOException
Returns RRD definition object which can be used to create new RRD with the same creation parameters but with no data in it.
Example:
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);
RrdException
- Thrown in case of JRobin specific error.
java.io.IOException
public static int getLockMode()
Returns current lock mode. This function can return one of the following values:
NO_LOCKS
: 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.WAIT_IF_LOCKED
: RRD files are locked exclusively.
Simultaneous access to the same underlying RRD file is not allowed.
If a RrdDb
object tries to access already locked RRD file,
it will wait until the lock is released. The lock is released when
the close()
method is called.EXCEPTION_IF_LOCKED
: RRD files are locked exclusively.
Simultaneous access to the same underlying RRD file is not allowed.
If a RrdDb
object tries to access already locked RRD file,
an exception is thrown.Note that WAIT_IF_LOCKED
and EXCEPTION_IF_LOCKED
modes guarantee data consistency but affect the speed of read/write operations.
Call close()
as soon as possible in your code to avoid long wait states
(or locking exceptions).
public static void setLockMode(int lockMode)
getLockMode()
for more
information.
lockMode
- Lock mode. Valid values are NO_LOCKS
,
WAIT_IF_LOCKED
and EXCEPTION_IF_LOCKED
.protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
public void copyStateTo(org.jrobin.core.RrdUpdater other) throws java.io.IOException, RrdException
other
- New RrdDb object to copy state to
java.io.IOException
- Thrown in case of I/O error
RrdException
- Thrown if supplied argument is not a compatible RrdDb objectpublic Datasource getDatasource(java.lang.String dsName) throws java.io.IOException
dsName
- Datasource name
java.io.IOException
- Thrown in case of I/O errorpublic int getArcIndex(java.lang.String consolFun, int steps) throws RrdException, java.io.IOException
consolFun
- Consolidation functionsteps
- Number of archive steps
java.io.IOException
- Thrown in case of I/O error
RrdException
- Thrown if no such archive could be foundpublic Archive getArchive(java.lang.String consolFun, int steps) throws java.io.IOException
consolFun
- Consolidation functionsteps
- Number of archive steps
java.io.IOException
- Thrown in case of I/O errorpublic java.lang.String getCanonicalPath() throws java.io.IOException
java.io.IOException
- Thrown in case of I/O error or if the underlying backend is
not derived from RrdFileBackend.public java.lang.String getPath()
public RrdBackend getRrdBackend()
public org.jrobin.core.RrdAllocator getRrdAllocator()
public byte[] getBytes() throws java.io.IOException
java.io.IOException
- Thrown in case of I/O related error.public void sync() throws java.io.IOException
RrdNioBackend
). This method
need not be called before the close()
method call since the close() method always
synchronizes all data in memory with the data in the persisant storage.When this method returns it is guaranteed that RRD data in the persistent storage is synchronized with the RrdDb data in memory.
java.io.IOException
- Thrown in case of I/O errorpublic static void setDefaultFactory(java.lang.String factoryName) throws RrdException
RrdBackendFactory.setDefaultFactory(java.lang.String)
.
factoryName
- Name of the backend factory to be set as default.
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 main(java.lang.String[] args)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |