📄 rrddef.java
字号:
* @param consolFun Consolidation function. Valid values are "AVERAGE",
* "MIN", "MAX" and "LAST" (these constants are conveniently defined in the
* {@link ConsolFuns} class)
* @param xff X-files factor. Valid values are between 0 and 1.
* @param steps Number of archive steps
* @param rows Number of archive rows
* @throws RrdException Thrown if archive with the same consolidation function
* and the same number of steps is already added.
*/
public void addArchive(String consolFun, double xff, int steps, int rows)
throws RrdException {
addArchive(new ArcDef(consolFun, xff, steps, rows));
}
/**
* Adds single archive to RRD definition from a RRDTool-like
* archive definition string. The string must have five elements separated with colons
* (:) in the following order:<p>
* <pre>
* RRA:consolidationFunction:XFilesFactor:steps:rows
* </pre>
* For example:</p>
* <pre>
* RRA:AVERAGE:0.5:10:1000
* </pre>
* For more information on archive definition parameters see <code>rrdcreate</code>
* man page.<p>
*
* @param rrdToolArcDef Archive definition string with the syntax borrowed from RRDTool.
* @throws RrdException Thrown if invalid string is supplied.
*/
public void addArchive(String rrdToolArcDef) throws RrdException {
RrdException rrdException = new RrdException(
"Wrong rrdtool-like archive definition: " + rrdToolArcDef);
StringTokenizer tokenizer = new StringTokenizer(rrdToolArcDef, ":");
if (tokenizer.countTokens() != 5) {
throw rrdException;
}
String[] tokens = new String[5];
for (int curTok = 0; tokenizer.hasMoreTokens(); curTok++) {
tokens[curTok] = tokenizer.nextToken();
}
if (!tokens[0].equalsIgnoreCase("RRA")) {
throw rrdException;
}
String consolFun = tokens[1];
double xff;
try {
xff = Double.parseDouble(tokens[2]);
}
catch (NumberFormatException nfe) {
throw rrdException;
}
int steps;
try {
steps = Integer.parseInt(tokens[3]);
}
catch (NumberFormatException nfe) {
throw rrdException;
}
int rows;
try {
rows = Integer.parseInt(tokens[4]);
}
catch (NumberFormatException nfe) {
throw rrdException;
}
addArchive(new ArcDef(consolFun, xff, steps, rows));
}
void validate() throws RrdException {
if (dsDefs.size() == 0) {
throw new RrdException("No RRD datasource specified. At least one is needed.");
}
if (arcDefs.size() == 0) {
throw new RrdException("No RRD archive specified. At least one is needed.");
}
}
/**
* Returns all data source definition objects specified so far.
*
* @return Array of data source definition objects
*/
public DsDef[] getDsDefs() {
return dsDefs.toArray(new DsDef[0]);
}
/**
* Returns all archive definition objects specified so far.
*
* @return Array of archive definition objects.
*/
public ArcDef[] getArcDefs() {
return arcDefs.toArray(new ArcDef[0]);
}
/**
* Returns number of defined datasources.
*
* @return Number of defined datasources.
*/
public int getDsCount() {
return dsDefs.size();
}
/**
* Returns number of defined archives.
*
* @return Number of defined archives.
*/
public int getArcCount() {
return arcDefs.size();
}
/**
* Returns string that represents all specified RRD creation parameters. Returned string
* has the syntax of RRDTool's <code>create</code> command.
*
* @return Dumped content of <code>RrdDb</code> object.
*/
public String dump() {
StringBuffer buffer = new StringBuffer("create \"");
buffer.append(path).append("\"");
buffer.append(" --start ").append(getStartTime());
buffer.append(" --step ").append(getStep()).append(" ");
for (DsDef dsDef : dsDefs) {
buffer.append(dsDef.dump()).append(" ");
}
for (ArcDef arcDef : arcDefs) {
buffer.append(arcDef.dump()).append(" ");
}
return buffer.toString().trim();
}
String getRrdToolCommand() {
return dump();
}
void removeDatasource(String dsName) throws RrdException {
for (int i = 0; i < dsDefs.size(); i++) {
DsDef dsDef = dsDefs.get(i);
if (dsDef.getDsName().equals(dsName)) {
dsDefs.remove(i);
return;
}
}
throw new RrdException("Could not find datasource named '" + dsName + "'");
}
void saveSingleDatasource(String dsName) {
Iterator<DsDef> it = dsDefs.iterator();
while (it.hasNext()) {
DsDef dsDef = it.next();
if (!dsDef.getDsName().equals(dsName)) {
it.remove();
}
}
}
void removeArchive(String consolFun, int steps) throws RrdException {
ArcDef arcDef = findArchive(consolFun, steps);
if (!arcDefs.remove(arcDef)) {
throw new RrdException("Could not remove archive " + consolFun + "/" + steps);
}
}
ArcDef findArchive(String consolFun, int steps) throws RrdException {
for (ArcDef arcDef : arcDefs) {
if (arcDef.getConsolFun().equals(consolFun) && arcDef.getSteps() == steps) {
return arcDef;
}
}
throw new RrdException("Could not find archive " + consolFun + "/" + steps);
}
/**
* Exports RrdDef object to output stream in XML format. Generated XML code can be parsed
* with {@link RrdDefTemplate} class.
*
* @param out Output stream
*/
public void exportXmlTemplate(OutputStream out) {
XmlWriter xml = new XmlWriter(out);
xml.startTag("rrd_def");
xml.writeTag("path", getPath());
xml.writeTag("step", getStep());
xml.writeTag("start", getStartTime());
// datasources
DsDef[] dsDefs = getDsDefs();
for (DsDef dsDef : dsDefs) {
xml.startTag("datasource");
xml.writeTag("name", dsDef.getDsName());
xml.writeTag("type", dsDef.getDsType());
xml.writeTag("heartbeat", dsDef.getHeartbeat());
xml.writeTag("min", dsDef.getMinValue(), "U");
xml.writeTag("max", dsDef.getMaxValue(), "U");
xml.closeTag(); // datasource
}
ArcDef[] arcDefs = getArcDefs();
for (ArcDef arcDef : arcDefs) {
xml.startTag("archive");
xml.writeTag("cf", arcDef.getConsolFun());
xml.writeTag("xff", arcDef.getXff());
xml.writeTag("steps", arcDef.getSteps());
xml.writeTag("rows", arcDef.getRows());
xml.closeTag(); // archive
}
xml.closeTag(); // rrd_def
xml.flush();
}
/**
* Exports RrdDef object to string in XML format. Generated XML string can be parsed
* with {@link RrdDefTemplate} class.
*
* @return XML formatted string representing this RrdDef object
*/
public String exportXmlTemplate() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
exportXmlTemplate(out);
return out.toString();
}
/**
* Exports RrdDef object to a file in XML format. Generated XML code can be parsed
* with {@link RrdDefTemplate} class.
*
* @param filePath Path to the file
*/
public void exportXmlTemplate(String filePath) throws IOException {
FileOutputStream out = new FileOutputStream(filePath, false);
exportXmlTemplate(out);
out.close();
}
/**
* Returns the number of storage bytes required to create RRD from this
* RrdDef object.
*
* @return Estimated byte count of the underlying RRD storage.
*/
public long getEstimatedSize() {
int dsCount = dsDefs.size();
int arcCount = arcDefs.size();
int rowsCount = 0;
for (ArcDef arcDef : arcDefs) {
rowsCount += arcDef.getRows();
}
return calculateSize(dsCount, arcCount, rowsCount);
}
static long calculateSize(int dsCount, int arcCount, int rowsCount) {
// return 64L + 128L * dsCount + 56L * arcCount +
// 20L * dsCount * arcCount + 8L * dsCount * rowsCount;
return (24L + 48L * dsCount + 16L * arcCount +
20L * dsCount * arcCount + 8L * dsCount * rowsCount) +
(1L + 2L * dsCount + arcCount) * 2L * RrdPrimitive.STRING_LENGTH;
}
/**
* Compares the current RrdDef with another. RrdDefs are considered equal if:<p>
* <ul>
* <li>RRD steps match
* <li>all datasources have exactly the same definition in both RrdDef objects (datasource names,
* types, heartbeat, min and max values must match)
* <li>all archives have exactly the same definition in both RrdDef objects (archive consolidation
* functions, X-file factors, step and row counts must match)
* </ul>
*
* @param obj The second RrdDef object
* @return true if RrdDefs match exactly, false otherwise
*/
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof RrdDef)) {
return false;
}
RrdDef rrdDef2 = (RrdDef) obj;
// check primary RRD step
if (step != rrdDef2.step) {
return false;
}
// check datasources
DsDef[] dsDefs = getDsDefs(), dsDefs2 = rrdDef2.getDsDefs();
if (dsDefs.length != dsDefs2.length) {
return false;
}
for (DsDef dsDef : dsDefs) {
boolean matched = false;
for (DsDef dsDef2 : dsDefs2) {
if (dsDef.exactlyEqual(dsDef2)) {
matched = true;
break;
}
}
// this datasource could not be matched
if (!matched) {
return false;
}
}
// check archives
ArcDef[] arcDefs = getArcDefs(), arcDefs2 = rrdDef2.getArcDefs();
if (arcDefs.length != arcDefs2.length) {
return false;
}
for (ArcDef arcDef : arcDefs) {
boolean matched = false;
for (ArcDef arcDef2 : arcDefs2) {
if (arcDef.exactlyEqual(arcDef2)) {
matched = true;
break;
}
}
// this archive could not be matched
if (!matched) {
return false;
}
}
// everything matches
return true;
}
/**
* Removes all datasource definitions.
*/
public void removeDatasources() {
dsDefs.clear();
}
/**
* Removes all RRA archive definitions.
*/
public void removeArchives() {
arcDefs.clear();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -