当前位置: 首页 > 工具软件 > rrd4j > 使用案例 >

rrd4j与collectd结合生成图片

禹昊穹
2023-12-01

collectd生成rrd文件,rrd4j无法直接解析。这时候先用rrdtool把rrd文件转换为xml文件,再由rrd4j将xml文件转换为rrd4j可以识别的rrd文件,最后出图,过程如下。

①rrdtool把rrd文件转换为xml文件
/usr/bin/rrdtool dump /var/lib/collectd/rrd/localhost/load/load.rrd>load.xml

②由rrd4j将xml文件转换为rrd4j可以识别的rrd文件,最后出图,代码如下:
import org.rrd4j.core.*;
import org.rrd4j.graph.RrdGraph;
import org.rrd4j.graph.RrdGraphDef;
import java.awt.Color;
import java.io.IOException;
import java.util.Random;
import static org.rrd4j.ConsolFun.*;


public class Graph {
static final long SEED = 1909752002L;
static final Random RANDOM = new Random(SEED);
static final String FILE = "load";


static final long START = Util.getTimestamp(2012, 9, 1);
static final long END = Util.getTimestamp(2012, 12, 1);
static final int MAX_STEP = 300;


static final int IMG_WIDTH = 500;
static final int IMG_HEIGHT = 300;


public static void main(String[] args) {
println("== Starting");
long startMillis = System.currentTimeMillis();
if (args.length > 0) {
println("Setting default backend factory to " + args[0]);
RrdDb.setDefaultFactory(args[0]);
}
long start = START;
long end = END;

String xmlPath = Util.getRrd4jDemoPath(FILE + ".xml");
String rrdRestoredPath = Util.getRrd4jDemoPath(FILE + "_restored.rrd");
String imgPath = Util.getRrd4jDemoPath(FILE + ".png");


println("== Creating RRD file " + rrdRestoredPath + " from XML file "
+ xmlPath);
RrdDb rrdRestoredDb;
try {
rrdRestoredDb = new RrdDb(rrdRestoredPath, xmlPath);
// create graph
println("Creating graph " + Util.getLapTime());
println("== Creating graph from the second file");
RrdGraphDef gDef = new RrdGraphDef();
gDef.setWidth(IMG_WIDTH);
gDef.setHeight(IMG_HEIGHT);
gDef.setFilename(imgPath);
gDef.setStartTime(start);
gDef.setEndTime(end);
gDef.setTitle(FILE);
gDef.setVerticalLabel(FILE);
String[] dsNames = null;
Color[] colors = new Color[] { Color.GREEN, Color.BLUE,
Color.MAGENTA, Color.YELLOW, Color.RED, Color.CYAN,
Color.ORANGE, Color.PINK, Color.BLACK };
dsNames = rrdRestoredDb.getDsNames();
for (int i = 0; i < dsNames.length; i++) {
String dsName = dsNames[i];
String legend = dsName;
System.out.println("dsName:" + legend);
gDef.datasource(legend, rrdRestoredPath, legend, AVERAGE);
gDef.line(legend, colors[i], legend);
}
gDef.setImageInfo("<img src='%s' width='%d' height = '%d'>");
gDef.setPoolUsed(false);
gDef.setImageFormat("png");
// close files
rrdRestoredDb.close();
println("== Second file closed");
println("Rendering graph " + Util.getLapTime());
// create graph finally
RrdGraph graph = new RrdGraph(gDef);
println(graph.getRrdGraphInfo().dump());
println("== Graph created " + Util.getLapTime());
// locks info
println("== Locks info ==");
println(RrdSafeFileBackend.getLockInfo());
// demo ends
println("== Demo completed in "
+ ((System.currentTimeMillis() - startMillis) / 1000.0)
+ " sec");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


static void println(String msg) {
// System.out.println(msg + " " + Util.getLapTime());
System.out.println(msg);
}


static void print(String msg) {
System.out.print(msg);
}


}

这样,最后在指定路径C:\Users\wujing\rrd4j-demo生成图片load.png.


 类似资料: