Use rrd4j and collectD to generate images

Source: Internet
Author: User
Tags rrd rrdtool

The RRD file generated by collectD cannot be parsed directly by rrd4j. At this time, rrdtool is used to convert the RRD file into an XML file, and rrd4j converts the XML file into an RRD file that rrd4j can recognize. The figure is shown as follows.

① Rrdtool converts RRD files to XML files
/Usr/bin/rrdtool dump/var/lib/collectD/RRD/localhost/load. RRD> load. xml

② Rrd4j converts an XML file to an RRD file that can be recognized by rrd4j, and finally draws a picture. The Code is as follows:
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 = 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. setdefafactory Factory (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 (" ");
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 );
}

}

In this way, the image load.png is generated in the specified path c: \ Users \ Wujing \ rrd4j-demo.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.