rrd4j的使用詳解2--從rrd文件中讀取數據

從rrd文件中讀取數據例子:


import static org.rrd4j.ConsolFun.AVERAGE;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import org.rrd4j.core.FetchData;
import org.rrd4j.core.FetchRequest;
import org.rrd4j.core.RrdDb;
import org.rrd4j.core.Util;

/**
 * Simple demo just to check that everything is OK with this library. Creates two files in your
 * $HOME/rrd4j-demo directory: demo.rrd and demo.png.
 */
public class DemoRead {
    static final long SEED = 1909752002L;
    static final Random RANDOM = new Random(SEED);
    static final String FILE = "demo";

    /*static final long START = Util.getTimestamp(2013, 10, 19);
    static final long END = Util.getTimestamp(2013, 10, 21);*/
   
    static final int MAX_STEP = 300;

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

    /**
     * <p>To start the demo, use the following command:</p>
     * <pre>
     * java -cp rrd4j-{version}.jar org.rrd4j.demo.Demo
     * </pre>
     *
     * @param args the name of the backend factory to use (optional)
     * @throws IOException Thrown
     */
    public static void main(String[] args) throws IOException {
    	
    	 //獲取昨天當前時間
        Calendar cal = Calendar.getInstance();
        long END = Util.getTimestamp(cal);
        cal.add(Calendar.DATE, -1);
        long START = Util.getTimestamp(cal) + 3*60;
        System.out.println("start=" + START);
        System.out.println("end=" + END);
        
    	System.out.println("date=" + new Date(1396422600L * 1000));
        
        long start = START ;
        long end = END;
        
        String rrdPath = Util.getRrd4jDemoPath(FILE + ".rrd");
        String logPath = Util.getRrd4jDemoPath(FILE + ".log");
        PrintWriter log = new PrintWriter(new BufferedOutputStream(new FileOutputStream(logPath, false)));
        // creation
        
        // test read-only access!
        //rrdPath = "d:/rrd/test.rrd";
        rrdPath = Util.getUserHomeDirectory() + "\\rrd\\NetworkDevice\\switch-B_slot-1_switch-ether_port-1.rrd";
        RrdDb rrdDb = new RrdDb(rrdPath, true);
        println("File reopen in read-only mode");
        println("== Last update time was: " + rrdDb.getLastUpdateTime());
        println("== Last info was: " + rrdDb.getInfo());

        // fetch data
        println("== Fetching data for the whole month");
        FetchRequest request = rrdDb.createFetchRequest(AVERAGE, start, end);
        println(request.dump());
        log.println(request.dump());
        FetchData fetchData = request.fetchData();
        //double[] value = fetchData.getValues("cpu_usagemhz");
        println("== Data fetched. " + fetchData.getRowCount() + " points obtained");
        println(fetchData.toString());
        log.close();
    }

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

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

    static class GaugeSource {
        private double value;
        private double step;

        GaugeSource(double value, double step) {
            this.value = value;
            this.step = step;
        }

        long getValue() {
            double oldValue = value;
            double increment = RANDOM.nextDouble() * step;
            if (RANDOM.nextDouble() > 0.5) {
                increment *= -1;
            }
            value += increment;
            if (value <= 0) {
                value = 0;
            }
            return Math.round(oldValue);
        }
    }
}


結果:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章