使用rrd4j绘制图形的问题

时间:2012-11-16 11:29:07

标签: java rrdtool rrd

我正在使用rrd4j以循环方式存储会话计数(一小时循环),查看xml似乎数据已成功更新,但是当我调用RrdGraph时,进程被触发。 我的Init,修改和创建代码如下

我将nodeCount设置为2并使用随机函数填充数据。

    import static org.rrd4j.ConsolFun.AVERAGE;
import static org.rrd4j.ConsolFun.MAX;

import java.awt.Color;
import java.io.IOException;
import java.util.Date;

import org.rrd4j.DsType;
import org.rrd4j.core.RrdDb;
import org.rrd4j.core.RrdDef;
import org.rrd4j.core.Sample;
import org.rrd4j.core.Util;
import org.rrd4j.graph.RrdGraph;
import org.rrd4j.graph.RrdGraphDef;

public class Add2RRD {

    static final String FILE = "sessionCount";

    static final Date currentDate = new Date();
    static final long SECINDAY = 24 * 3600;
    static final long MAX_STEP = 3600L;// one hour
    static final int IMG_WIDTH = 700;
    static final int IMG_HEIGHT = 500;
    private String rrdPath = null;
    private String xmlPath = null;
    private String imgPath = null;
    private String rrdRestoredPath = null;
    private long debugTimestamp = 0L;
    private int nodeCount;
    private int rows;


    private static Add2RRD add2rrd = null;


    private Add2RRD() {
        debugTimestamp = Util.getTimestamp();
        rrdPath = Util.getRrd4jDemoPath(FILE + ".rrd");
        xmlPath = Util.getRrd4jDemoPath(FILE + ".xml");
        imgPath = Util.getRrd4jDemoPath(FILE + ".png");
        rrdRestoredPath = Util.getRrd4jDemoPath(FILE + "_restored.rrd");

        rows = (int) ((JMXProbe.reportRange * 24 * 60 * 60) / MAX_STEP);
        // rows = 24;
    }

    public static Add2RRD getInstance() {
        if (null == add2rrd) {
            add2rrd = new Add2RRD();
        }
        return add2rrd;
    }

    public void initRRD() throws IOException {
        long start = Util.getTimestamp();

        RrdDef rrdDef = new RrdDef(rrdPath, start, MAX_STEP);

        for (int i = 1; i < nodeCount + 1; i++) {
            rrdDef.addDatasource("Node" + i, DsType.GAUGE, MAX_STEP * 2, 0,
                    Double.NaN);
        }

        rrdDef.addArchive(MAX, 0.5, 1, rows);

        println(rrdDef.dump());
        println("Estimated file size: " + rrdDef.getEstimatedSize());

        RrdDb rrdDb = new RrdDb(rrdDef);

        println("RRD file created.");

        rrdDb.close();
        println("RRD file closed.");
    }

    public void updateRRD(long timeStamp, Long[] values) throws IOException {

        RrdDb rrdDb = new RrdDb(rrdPath);
        Sample sample = rrdDb.createSample();
        debugTimestamp += MAX_STEP;
        // System.out.println(debugTimestamp);
        sample.setTime(debugTimestamp);
        for (int i = 0; i < values.length; i++) {
            sample.setValue("Node" + (i + 1), Math.round(values[i]));
        }
        println("Update dump" + sample.dump());
        sample.update();
        rrdDb.close();
        println("RRD file updated " + debugTimestamp);

    }

    public RrdDb createRRDRestorePoint() throws IOException {
        RrdDb rrdDb = new RrdDb(rrdPath);
        rrdDb.exportXml(xmlPath);
        println("Creating RRD file " + rrdRestoredPath + " from XML file "
                + xmlPath);
        RrdDb rrdRestoredDb = new RrdDb(rrdRestoredPath, xmlPath);
        println("== Closing both RRD files");
        rrdDb.close();
        return rrdRestoredDb;
    }

    public void createRRDGraph() throws IOException {
        RrdDb rrdRestoredDb = createRRDRestorePoint();
        RrdDef rrdDef = rrdRestoredDb.getRrdDef();
        int dsCount = rrdDef.getDsCount();

        System.out.println(" dsCount " + dsCount);
        RrdGraphDef gDef = new RrdGraphDef();
        gDef.setWidth(IMG_WIDTH);
        gDef.setHeight(IMG_HEIGHT);
        gDef.setFilename(imgPath);


        long start = Util.getTimestamp() + + (2 * 60 * 60), end = start + (18 * 60 * 60);

/*      FetchRequest request = rrdRestoredDb.createFetchRequest(AVERAGE, start, end);
        println(request.dump());
        FetchData fetchData = request.fetchData();
        println("Data fetched. " + fetchData.getRowCount()
                + " points obtained");

        println(fetchData.toString());*/

        rrdRestoredDb.close();
        //gDef.datasource("nodeName", fetchData);
        gDef.setStartTime(start);
        gDef.setEndTime(end);

        gDef.setTitle(" Session Count : " );//+ new Date().getMonth());
        gDef.setVerticalLabel("Session Count");

        for (int i = 0; i < dsCount; i++) {
            String nodeName = "Node" + (i + 1);
            System.out.println(" nodeName " + nodeName);
            gDef.datasource(nodeName, rrdPath, nodeName, AVERAGE);
            gDef.line(nodeName, Color.GREEN, nodeName + " count");
            gDef.gprint(nodeName, MAX, nodeName + " max = %.3f%s");
            gDef.gprint(nodeName, AVERAGE, nodeName + " avg = %.3f%S\\r");
            gDef.print(nodeName, MAX, nodeName +" max = %.3f%s");
            gDef.print(nodeName, AVERAGE, nodeName +" avg = %.3f%S\\r");
        }
        gDef.setImageInfo("");
        gDef.setPoolUsed(false);
        gDef.setImageFormat("png");
        println("Rendering graph " + Util.getLapTime());
        RrdGraph graph = new RrdGraph(gDef);
        println(graph.getRrdGraphInfo().dump());
        println("Graph created " + Util.getLapTime());
    }

    public void setNodeCount(int nodeCount) {
        this.nodeCount = nodeCount;
    }

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

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

我生成的xml是:

    <rrd>
   <!-- RRD4J, version 0.1 -->
   <version>0003</version>
   <!-- Seconds -->
   <step>3600</step>
   <!-- Sat Nov 17 15:32:33 GMT+05:30 2012 -->
   <lastupdate>1353146553</lastupdate>
   <ds>
      <name>Node1</name>
      <type>GAUGE</type>
      <minimal_heartbeat>7200</minimal_heartbeat>
      <min>+0.0000000000E00</min>
      <max>NaN</max>
      <!-- PDP Status -->
      <last_ds>+9.4000000000E01</last_ds>
      <value>+1.4382000000E04</value>
      <unknown_sec>0</unknown_sec>
   </ds>
   <ds>
      <name>Node2</name>
      <type>GAUGE</type>
      <minimal_heartbeat>7200</minimal_heartbeat>
      <min>+0.0000000000E00</min>
      <max>NaN</max>
      <!-- PDP Status -->
      <last_ds>+2.9000000000E01</last_ds>
      <value>+4.4370000000E03</value>
      <unknown_sec>0</unknown_sec>
   </ds>
   <rra>
      <cf>MAX</cf>
      <!-- 3600 seconds -->
      <pdp_per_row>1</pdp_per_row>
      <xff>+5.0000000000E-01</xff>
      <cdp_prep>
         <ds>
            <value>NaN</value>
            <unknown_datapoints>0</unknown_datapoints>
         </ds>
         <ds>
            <value>NaN</value>
            <unknown_datapoints>0</unknown_datapoints>
         </ds>
      </cdp_prep>
            <database>
         <!-- Thu Nov 15 16:30:00 GMT+05:30 2012 / 1352977200 -->
         <row>
            <v>NaN</v>
            <v>NaN</v>
         </row>
         <!-- Thu Nov 15 17:30:00 GMT+05:30 2012 / 1352980800 -->
         <row>
            <v>NaN</v>
            <v>NaN</v>
         </row>
        .
        .
        .
        <!-- Sat Nov 17 14:30:00 GMT+05:30 2012 / 1353142800 -->
         <row>
            <v>+1.3317000000E02</v>
            <v>+5.5107500000E01</v>
         </row>
         <!-- Sat Nov 17 15:30:00 GMT+05:30 2012 / 1353146400 -->
         <row>
            <v>+9.5657500000E01</v>
            <v>+3.0147500000E01</v>
         </row>
      </database>
   </rra>
</rrd>

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

最后我弄清楚了我的错误 在createGraph方法中,我使用AVERAGE添加数据,但在创建时,它是由MAX类型定义的。我只是在createGraph中将datasourse改为MAX类型并且它可以工作