|
From: <sa...@us...> - 2003-10-11 20:34:48
|
Update of /cvsroot/jrobin/src/jrobin/demo In directory sc8-pr-cvs1:/tmp/cvs-serv2803 Modified Files: JRobinComplexGraph.java JRobinDemo.java Log Message: Index: JRobinComplexGraph.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/demo/JRobinComplexGraph.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** JRobinComplexGraph.java 3 Oct 2003 10:09:52 -0000 1.4 --- JRobinComplexGraph.java 11 Oct 2003 20:34:39 -0000 1.5 *************** *** 1,167 **** ! /* ============================================================ ! * JRobin : Pure java implementation of RRDTool's functionality ! * ============================================================ ! * ! * Project Info: http://www.sourceforge.net/projects/jrobin ! * Project Lead: Sasa Markovic (sa...@eu...); ! * ! * (C) Copyright 2003, by Sasa Markovic. ! * ! * This library is free software; you can redistribute it and/or modify it under the terms ! * of the GNU Lesser General Public License as published by the Free Software Foundation; ! * either version 2.1 of the License, or (at your option) any later version. ! * ! * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; ! * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ! * See the GNU Lesser General Public License for more details. ! * ! * You should have received a copy of the GNU Lesser General Public License along with this ! * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, ! * Boston, MA 02111-1307, USA. ! */ ! /* ! * Created on 26-aug-2003 ! * ! * To change the template for this generated file go to ! * Window - Preferences - Java - Code Generation - Code and Comments ! */ ! package jrobin.demo; ! ! import java.util.*; ! import java.awt.*; ! ! import jrobin.graph.*; ! ! /** ! * @author cbld ! * ! * To change the template for this generated type comment go to ! * Window - Preferences - Java - Code Generation - Code and Comments ! */ ! public class JRobinComplexGraph { ! ! public static void main(String[] args) ! { ! GregorianCalendar start = new GregorianCalendar(2003, 7, 23, 0, 0); ! GregorianCalendar end = new GregorianCalendar(2003, 7, 24, 0, 0); ! ! RrdGraphDef gDef = new RrdGraphDef(); ! ! try ! { ! gDef.setTimePeriod(start, end); ! gDef.setTitle("Server load baseline projection"); ! gDef.datasource("load", "c:/test.rrd", "serverLoad", "AVERAGE"); ! gDef.datasource("user", "c:/test.rrd", "serverCPUUser", "AVERAGE"); ! gDef.datasource("nice", "c:/test.rrd", "serverCPUNice", "AVERAGE"); ! gDef.datasource("system", "c:/test.rrd", "serverCPUSystem", "AVERAGE"); ! gDef.datasource("idle", "c:/test.rrd", "serverCPUIdle", "AVERAGE"); ! gDef.datasource("total", "user,nice,+,system,+,idle,+"); ! gDef.datasource("busy", "user,nice,+,system,+,total,/,100,*"); ! gDef.datasource("p25t50", "busy,25,GT,busy,50,LE,load,0,IF,0,IF"); ! gDef.datasource("p50t75", "busy,50,GT,busy,75,LE,load,0,IF,0,IF"); ! gDef.datasource("p75t90", "busy,75,GT,busy,90,LE,load,0,IF,0,IF"); ! gDef.datasource("p90t100", "busy,90,GT,load,0,IF"); ! gDef.comment("CPU utilization (%)\n"); ! gDef.comment(" "); ! gDef.area("load", new Color(0x66,0x99,0xcc), " 0 - 25%"); ! gDef.area("p25t50", new Color(0x00,0x66,0x99), "25 - 50%"); ! gDef.comment(" "); ! gDef.gprint("busy", "MIN", "Minimum:@5.1@s%"); ! gDef.gprint("busy", "MAX", "Maximum: @5.1@s%"); ! gDef.comment("\n"); ! gDef.comment(" "); ! gDef.area("p50t75", new Color(0x66,0x66,0x00), "50 - 75%"); ! gDef.area("p75t90", new Color(0xff,0x66,0x00), "75 - 90%"); ! gDef.area("p90t100", new Color(0xcc,0x33,0x00), "90 - 100%"); ! //gDef.rule(10.0, Color.ORANGE, null); ! gDef.gprint("busy", "AVERAGE", " Average:@5.1@s%"); ! gDef.gprint("busy", "LAST", "Current: @5.1@s%"); ! gDef.comment("\n"); ! gDef.comment("\n"); ! gDef.comment("Server load\n"); ! gDef.comment(" "); ! gDef.line("load", new Color(0x00,0x00,0x00), "Load average (5 min)" ); ! //gDef.area("load", Color.RED, " hmm \n"); ! //gDef.stack("p75t90", Color.GREEN, " hmm \n"); ! gDef.comment(" "); ! gDef.gprint("load", "MIN", " Minimum: @5.2@s"); ! gDef.gprint("load", "MAX", "Maximum: @6.2@s"); ! gDef.comment("\n"); ! gDef.comment(" "); ! gDef.comment(" "); ! gDef.gprint("load", "AVERAGE", "Average: @5.2@s"); ! gDef.gprint("load", "LAST", "Current: @6.2@s"); ! gDef.comment("\n"); ! gDef.comment("\n"); ! gDef.comment("-------------------------------------------------------------------------------@c"); ! gDef.comment("Generated: " + new Date() + "@r"); ! gDef.setBackColor( Color.DARK_GRAY ); ! gDef.setCanvasColor( Color.LIGHT_GRAY ); ! gDef.setValueAxisLabel("server load"); ! gDef.setFontColor( Color.WHITE ); ! //gDef.setGridX( false ); ! //gDef.setGridY( false ); ! gDef.setImageBorder( Color.BLACK, 1 ); ! gDef.setFrontGrid(false); ! gDef.setShowLegend(true); ! gDef.setMajorGridColor(Color.BLACK); ! gDef.setMinorGridColor( new Color( 130, 30, 30) ); ! gDef.setFrameColor( Color.YELLOW ); ! gDef.setAxisColor( Color.RED ); ! gDef.setArrowColor( Color.GREEN ); ! gDef.setChartLeftPadding( 40 ); ! // Create actual graph ! RrdGraph graph = new RrdGraph(gDef); ! graph.saveAsPNG("/zzzzzz.png", 0, 0); ! ! ! // -- New graph ! RrdGraphDef gd = new RrdGraphDef(); ! //gd.setBackColor( Color.WHITE ); ! gd.setTimePeriod( start, end ); ! gd.datasource("in2", "c:/test.rrd", "ifInOctets", "AVERAGE"); ! gd.datasource("out2", "c:/test.rrd", "ifOutOctets", "AVERAGE"); ! gd.datasource("in", "in2,8,*"); ! gd.datasource("out", "out2,8,*"); ! gd.area("in", Color.GREEN, null); ! gd.line("out", Color.BLUE, null); ! gd.setRigidGrid(true); ! RrdGraph graph2 = new RrdGraph(gd); ! graph2.saveAsPNG("/traff.png", 0, 0); ! ! ////////////////////////////// ! gd = new RrdGraphDef(); ! //gd.setBackColor( Color.WHITE ); ! gd.setTimePeriod( start, end ); ! gd.datasource("in2", "c:/test.rrd", "ifInUcastPkts", "AVERAGE"); ! gd.datasource("out2", "c:/test.rrd", "ifOutUcastPkts", "AVERAGE"); ! gd.datasource("in", "in2,8,*"); ! gd.datasource("out", "out2,8,*"); ! gd.area("in", Color.GREEN, null); ! gd.line("out", Color.BLUE, null); ! ! graph2 = new RrdGraph(gd); ! graph2.saveAsPNG("/pkts.png", 0, 0); ! ! gd = new RrdGraphDef(); ! gd.setBackColor( Color.WHITE ); ! gd.setTimePeriod( start, end ); ! gd.datasource("ftp", "c:/test.rrd", "ftpUsers", "AVERAGE"); ! gd.area("ftp", Color.BLUE, null); ! ! graph2 = new RrdGraph(gd); ! graph2.saveAsPNG("/ftp.png", 0, 0); ! ! ! //graph.saveAsPNG("c:/demo.png", 495, 200); ! } ! catch (Exception e) { ! System.err.println( e.getMessage() ); ! System.exit(1); ! } ! ! System.out.println("Complex demo graph created."); ! } ! } ! --- 1,184 ---- ! /* ============================================================ ! * JRobin : Pure java implementation of RRDTool's functionality ! * ============================================================ ! * ! * Project Info: http://www.sourceforge.net/projects/jrobin ! * Project Lead: Sasa Markovic (sa...@eu...); ! * ! * (C) Copyright 2003, by Sasa Markovic. ! * ! * This library is free software; you can redistribute it and/or modify it under the terms ! * of the GNU Lesser General Public License as published by the Free Software Foundation; ! * either version 2.1 of the License, or (at your option) any later version. ! * ! * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; ! * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ! * See the GNU Lesser General Public License for more details. ! * ! * You should have received a copy of the GNU Lesser General Public License along with this ! * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, ! * Boston, MA 02111-1307, USA. ! */ ! /* ! * Created on 26-aug-2003 ! * ! * To change the template for this generated file go to ! * Window - Preferences - Java - Code Generation - Code and Comments ! */ ! package jrobin.demo; ! ! import java.util.*; ! import java.awt.*; ! import javax.swing.*; ! ! import jrobin.graph.*; ! ! /** ! * @author cbld ! * ! * To change the template for this generated type comment go to ! * Window - Preferences - Java - Code Generation - Code and Comments ! */ ! public class JRobinComplexGraph { ! ! public static void main(String[] args) ! { ! GregorianCalendar start = new GregorianCalendar(2003, 7, 23, 0, 0); ! GregorianCalendar end = new GregorianCalendar(2003, 7, 25, 0, 0); ! ! RrdGraphDef gDef = new RrdGraphDef(); ! ! try ! { ! gDef.setTimePeriod(start, end); ! gDef.setTitle("Server load baseline projection"); ! //gDef.setOverlay("/pkts.png"); ! gDef.datasource("load", "c:/test.rrd", "serverLoad", "AVERAGE"); ! gDef.datasource("user", "c:/test.rrd", "serverCPUUser", "AVERAGE"); ! gDef.datasource("nice", "c:/test.rrd", "serverCPUNice", "AVERAGE"); ! gDef.datasource("system", "c:/test.rrd", "serverCPUSystem", "AVERAGE"); ! gDef.datasource("idle", "c:/test.rrd", "serverCPUIdle", "AVERAGE"); ! gDef.datasource("total", "user,nice,+,system,+,idle,+"); ! gDef.datasource("busy", "user,nice,+,system,+,total,/,100,*"); ! gDef.datasource("p25t50", "busy,25,GT,busy,50,LE,load,0,IF,0,IF"); ! gDef.datasource("p50t75", "busy,50,GT,busy,75,LE,load,0,IF,0,IF"); ! gDef.datasource("p75t90", "busy,75,GT,busy,90,LE,load,0,IF,0,IF"); ! gDef.datasource("p90t100", "busy,90,GT,load,0,IF"); ! gDef.comment("CPU utilization (%)\n"); ! gDef.comment(" "); ! gDef.area("load", new Color(0x66,0x99,0xcc), " 0 - 25%"); ! gDef.area("p25t50", new Color(0x00,0x66,0x99), "25 - 50%"); ! gDef.comment(" "); ! gDef.gprint("busy", "MIN", "Minimum:@5.1@s%"); ! gDef.gprint("busy", "MAX", "Maximum: @5.1@s%"); ! gDef.comment("\n"); ! gDef.comment(" "); ! gDef.area("p50t75", new Color(0x66,0x66,0x00), "50 - 75%"); ! gDef.area("p75t90", new Color(0xff,0x66,0x00), "75 - 90%"); ! gDef.area("p90t100", new Color(0xcc,0x33,0x00), "90 - 100%"); ! //gDef.rule(10.0, Color.ORANGE, null); ! gDef.gprint("busy", "AVERAGE", " Average:@5.1@s%"); ! gDef.gprint("busy", "LAST", "Current: @5.1@s%"); ! gDef.comment("\n"); ! gDef.comment("\n"); ! gDef.comment("Server load\n"); ! gDef.comment(" "); ! gDef.line("load", new Color(0x00,0x00,0x00), "Load average (5 min)" ); ! //gDef.area("load", Color.RED, " hmm \n"); ! //gDef.stack("p75t90", Color.GREEN, " hmm \n"); ! gDef.comment(" "); ! gDef.gprint("load", "MIN", " Minimum: @5.2@s"); ! gDef.gprint("load", "MAX", "Maximum: @6.2@s"); ! gDef.comment("\n"); ! gDef.comment(" "); ! gDef.comment(" "); ! gDef.gprint("load", "AVERAGE", "Average: @5.2@s"); ! gDef.gprint("load", "LAST", "Current: @6.2@s"); ! gDef.comment("\n"); ! gDef.comment("\n"); ! gDef.comment("-------------------------------------------------------------------------------@c"); ! gDef.comment("Generated: " + new Date() + "@r"); ! gDef.setBackColor( Color.DARK_GRAY ); ! gDef.setCanvasColor( Color.LIGHT_GRAY ); ! gDef.setValueAxisLabel("server load"); ! gDef.setFontColor( Color.WHITE ); ! //gDef.setGridX( false ); ! //gDef.setGridY( false ); ! gDef.setImageBorder( Color.BLACK, 1 ); ! //gDef.setFrontGrid(false); ! gDef.setShowLegend(true); ! gDef.setMajorGridColor(Color.YELLOW); ! gDef.setMinorGridColor( new Color( 130, 30, 30) ); ! gDef.setFrameColor( Color.BLACK ); ! gDef.setAxisColor( Color.RED ); ! gDef.setArrowColor( Color.GREEN ); ! gDef.setChartLeftPadding( 40 ); ! gDef.setAntiAliasing(true); ! gDef.setTimeAxis( TimeAxisUnit.HOUR, 6, TimeAxisUnit.DAY, 1, "EEEEE dd MMM", true ); ! gDef.setValueAxis( 2.5, 5 ); ! // Create actual graph ! RrdGraph graph = new RrdGraph(gDef); ! graph.saveAsPNG("/zzzzzz.png", 0, 0); ! //graph.saveAsJPEG("/zzzzzz.jpg", 0, 0, 1f); ! ! // -- New graph ! RrdGraphDef gd = new RrdGraphDef(); ! //gd.setBackColor( Color.WHITE ); ! gd.setTimePeriod( start, end ); ! //gd.setBackground("/ftp.png"); ! gd.datasource("in2", "c:/test.rrd", "ifInOctets", "AVERAGE"); ! gd.datasource("out2", "c:/test.rrd", "ifOutOctets", "AVERAGE"); ! gd.datasource("in", "in2,8,*"); ! gd.datasource("out", "out2,8,*"); ! gd.area("in", Color.GREEN, null); ! gd.line("out", Color.BLUE, null); ! gd.setRigidGrid(true); ! RrdGraph graph2 = new RrdGraph(gd); ! graph2.saveAsPNG("/traff.png", 0, 0); ! ! ////////////////////////////// ! gd = new RrdGraphDef(); ! gd.setBackColor( Color.WHITE ); ! gd.setTimePeriod( start, end ); ! gd.datasource("in2", "c:/test.rrd", "ifInUcastPkts", "AVERAGE"); ! gd.datasource("out2", "c:/test.rrd", "ifOutUcastPkts", "AVERAGE"); ! gd.datasource("in", "in2,8,*"); ! gd.datasource("out", "out2,8,*"); ! gd.area("in", Color.GREEN, null); ! gd.line("out", Color.BLUE, null); ! //gd.setUnitsExponent(6); ! graph2 = new RrdGraph(gd); ! graph2.saveAsPNG("/pkts.png", 0, 0); ! ! gd = new RrdGraphDef(); ! gd.setBackColor( Color.WHITE ); ! gd.setTimePeriod( start, end ); ! gd.datasource("ftp", "c:/test.rrd", "ftpUsers", "AVERAGE"); ! gd.area("ftp", Color.BLUE, null); ! ! graph2 = new RrdGraph(gd); ! graph2.saveAsPNG("/ftp.png", 0, 0); ! ! /* ! try { ! JFrame frame = new JFrame("Simple chartpanel test"); ! ! frame.getContentPane().add(graph2.getChartPanel()); ! frame.pack(); ! frame.setVisible(true); ! } catch (Exception e) { ! e.printStackTrace(); ! } ! */ ! ! //graph.saveAsPNG("c:/demo.png", 495, 200); ! } ! catch (Exception e) { ! System.err.println( e.getMessage() ); ! System.exit(1); ! } ! ! System.out.println("Complex demo graph created."); ! } ! } ! Index: JRobinDemo.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/demo/JRobinDemo.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** JRobinDemo.java 22 Sep 2003 17:15:17 -0000 1.2 --- JRobinDemo.java 11 Oct 2003 20:34:39 -0000 1.3 *************** *** 1,240 **** ! /* ============================================================ ! * JRobin : Pure java implementation of RRDTool's functionality ! * ============================================================ ! * ! * Project Info: http://www.sourceforge.net/projects/jrobin ! * Project Lead: Sasa Markovic (sa...@eu...); ! * ! * (C) Copyright 2003, by Sasa Markovic. ! * ! * This library is free software; you can redistribute it and/or modify it under the terms ! * of the GNU Lesser General Public License as published by the Free Software Foundation; ! * either version 2.1 of the License, or (at your option) any later version. ! * ! * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; ! * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ! * See the GNU Lesser General Public License for more details. ! * ! * You should have received a copy of the GNU Lesser General Public License along with this ! * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, ! * Boston, MA 02111-1307, USA. ! */ ! ! package jrobin.demo; ! ! import jrobin.core.*; ! import jrobin.graph.RrdGraph; ! import jrobin.graph.RrdGraphDef; ! ! import java.awt.*; ! import java.io.BufferedOutputStream; ! import java.io.FileOutputStream; ! import java.io.IOException; ! import java.io.PrintWriter; ! import java.util.GregorianCalendar; ! ! /** ! * <p>Class used to demonstrate almost all of JRobin capabilities. To run this ! * demonstration execute the following command:</p> ! * ! * <pre> ! * java -jar JRobin-{version}.jar ! * </pre> ! * ! * <p>The jar-file can be found in the <code>libs</code> directory of this distribution. ! * <b>On Linux platforms, graphs cannot be generated if your computer has no X-windows ! * server up and running</b> (you'll end up with a nasty looking exception).</p> ! * ! * @author <a href="mailto:sa...@eu...">Sasa Markovic</a> ! */ ! public class JRobinDemo { ! static final String HOME = System.getProperty("user.home"); ! static final String SEPARATOR = System.getProperty("file.separator"); ! static final String FILE = "demo"; ! static final GregorianCalendar START = new GregorianCalendar(2003, 4, 1); ! static final GregorianCalendar END = new GregorianCalendar(2003, 5, 1); ! static final int MAX_STEP = 240; ! ! /** ! * <p>Runs the demonstration. ! * Demonstration consists of the following steps: (all files will be ! * created in your HOME directory):</p> ! * ! * <ul> ! * <li>Sample RRD database 'demo.rrd' is created (uses two GAUGE data sources). ! * <li>RRD file is updated more than 20.000 times, simulating one month of updates with ! * time steps of max. 4 minutes. ! * <li>Last update time is printed. ! * <li>Sample fetch request is executed, fetching data from RRD file for the whole month. ! * <li>Fetched data is printed on the screen. ! * <li>RRD file is dumped to file 'demo.xml' (XML format). ! * <li>New RRD file 'demo_restored.rrd' is created from XML file. ! * <li>RRD graph for the whole month is created in memory. ! * <li>Graph is saved in PNG and JPEG format (files 'demo.png' and 'demo.jpeg'). ! * <li>All files are closed. ! * <li>Log file 'demo.log' is created. Log file consist of RRDTool commands - execute this file ! * as Linux shell script to compare RRDTool's and Jrobin's fetch results (should be exactly the ! * same). ! * </ul> ! * ! * <p>If everything goes well, no exceptions are thrown and your graphs should look like:</p> ! * <p align="center"><img src="../../../images/demo.png" border="1"></p> ! * <p>Red and blue lines are simple (DEF) graph sources. Green line (median value) ! * represents complex (CDEF) graph source which is defined by a RPN expression. Filled ! * area (orange color) represents the difference between data source values. Sine line is here ! * just to represent JRobin's advanced graphing capabilities.</p> ! * ! * @param args Empty. This demo does not accept command line parameters. ! * @throws RrdException Thrown in case of JRobin specific error. ! * @throws IOException Thrown in case of I/O related error. ! */ ! public static void main(String[] args) throws RrdException, IOException { ! // setup ! println("==Starting demo"); ! RrdDb.setLockMode(RrdDb.WAIT_IF_LOCKED); ! ! long startMillis = System.currentTimeMillis(); ! long start = START.getTime().getTime() / 1000L; ! long end = END.getTime().getTime() / 1000L; ! String rrdFile = getFullPath(FILE + ".rrd"); ! String xmlFile = getFullPath(FILE + ".xml"); ! String rrdFile2 = getFullPath(FILE + "_restored.rrd"); ! //String pngFile = getFullPath(FILE + ".png"); ! String pngFile = "/zzzzzz.png"; ! String jpegFile = getFullPath(FILE + ".jpeg"); ! String logFile = getFullPath(FILE + ".log"); ! PrintWriter pw = new PrintWriter( ! new BufferedOutputStream( ! new FileOutputStream(logFile, false)) ! ); ! ! // creation ! println("==Creating RRD file " + rrdFile); ! RrdDef rrdDef = new RrdDef(rrdFile, start - 1, 300); ! rrdDef.addDatasource("sun", "GAUGE", 600, 0, Double.NaN); ! rrdDef.addDatasource("shade", "GAUGE", 600, 0, Double.NaN); ! rrdDef.addArchive("AVERAGE", 0.5, 1, 600); ! rrdDef.addArchive("AVERAGE", 0.5, 6, 700); ! rrdDef.addArchive("AVERAGE", 0.5, 24, 797); ! rrdDef.addArchive("AVERAGE", 0.5, 288, 775); ! rrdDef.addArchive("MAX", 0.5, 1, 600); ! rrdDef.addArchive("MAX", 0.5, 6, 700); ! rrdDef.addArchive("MAX", 0.5, 24, 797); ! rrdDef.addArchive("MAX", 0.5, 288, 775); ! println(rrdDef.dump()); ! pw.println(rrdDef.dump()); ! RrdDb rrdDb = new RrdDb(rrdDef); ! println("==RRD file created."); ! ! // update database ! GaugeSource sunSource = new GaugeSource(1200, 20); ! GaugeSource shadeSource = new GaugeSource(300, 10); ! println("==Simulating one month of RRD file updates with step not larger than " + ! MAX_STEP + " seconds (* denotes 1000 updates)"); ! long t = start; int n = 0; ! while(t <= end) { ! Sample sample = rrdDb.createSample(t); ! sample.setValue("sun", sunSource.getValue()); ! sample.setValue("shade", shadeSource.getValue()); ! pw.println(sample.dump()); ! sample.update(); ! t += Math.random() * MAX_STEP + 1; ! if(((++n) % 1000) == 0) { ! System.out.print("*"); ! }; ! } ! System.out.println(""); ! println("==Finished. RRD file updated " + n + " times"); ! println("==Last update time was: " + rrdDb.getLastUpdateTime()); ! ! // fetch data ! println("==Fetching data for the whole month"); ! FetchRequest request = rrdDb.createFetchRequest("AVERAGE", start, end); ! println(request.dump()); ! pw.println(request.dump()); ! FetchPoint[] points = request.fetch(); ! println("==Data fetched. " + points.length + " points obtained"); ! for(int i = 0; i < points.length; i++) { ! println(points[i].dump()); ! } ! println("==Fetch completed"); ! println("==Dumping RRD file to XML file " + xmlFile + " (can be restored with RRDTool)"); ! //rrdDb.dumpXml(xmlFile); ! println("==Creating RRD file " + rrdFile2 + " from " + xmlFile); ! //RrdDb rrdDb2 = new RrdDb(rrdFile2, xmlFile); ! // close files ! println("==Closing both RRD files"); ! rrdDb.close(); ! //rrdDb2.close(); ! ! // creating graph ! println("==Creating graph from the second file"); ! RrdGraphDef gDef = new RrdGraphDef(); ! gDef.setTimePeriod(start, end); ! gDef.setTimeAxisLabel("day in month"); ! gDef.setTitle("Temperatures in May 2003"); ! gDef.setValueAxisLabel("temperature"); ! gDef.datasource("sun", rrdFile, "sun", "AVERAGE"); ! gDef.datasource("shade", rrdFile, "shade", "AVERAGE"); ! gDef.datasource("median", "sun,shade,+,2,/"); ! gDef.datasource("diff", "sun,shade,-,ABS,-1,*"); ! gDef.datasource("sine", "TIME," + start + ",-," + (end - start) + ! ",/,2,PI,*,*,SIN,1000,*"); ! gDef.line("sun", Color.RED, "sun temp"); ! gDef.line("shade", Color.BLUE, "shade temp"); ! gDef.line("median", Color.GREEN, "median value"); ! gDef.area("diff", Color.ORANGE, "difference"); ! gDef.line("sine", Color.CYAN, "sine function demo"); ! gDef.gprint("sun", "MAX", "\nmaxSun = @3@s"); ! gDef.gprint("sun", "AVERAGE", "avgSun = @3@S@r"); ! gDef.gprint("shade", "MAX", "maxShade = @3@s"); ! gDef.gprint("shade", "AVERAGE", "avgShade = @3@S@r"); ! RrdGraph graph = new RrdGraph(gDef); ! println("==Graph created"); ! println("==Saving graph as PNG file " + pngFile); ! graph.saveAsPNG(pngFile, 600, 400); ! println("==Saving graph as JPEG file " + jpegFile); ! graph.saveAsJPEG(jpegFile, 600, 400, 0.8F); ! ! // demo ends ! pw.close(); ! println("Demo completed in " + ! ((System.currentTimeMillis() - startMillis) / 1000.0) + ! " sec"); ! } ! ! static void println(String msg) { ! System.out.println(msg); ! } ! ! static String getFullPath(String path) { ! return HOME + SEPARATOR + path; ! } ! } ! ! class GaugeSource { ! private double value; ! private double step; ! ! GaugeSource(double value, double step) { ! this.value = value; ! this.step = step; ! } ! ! double getValue() { ! double oldValue = value; ! double increment = Math.random() * step; ! if(Math.random() > 0.5) { ! increment *= -1; ! } ! value += increment; ! if(value <= 0) { ! value = 0; ! } ! return oldValue; ! } ! ! ! } ! ! --- 1,240 ---- ! /* ============================================================ ! * JRobin : Pure java implementation of RRDTool's functionality ! * ============================================================ ! * ! * Project Info: http://www.sourceforge.net/projects/jrobin ! * Project Lead: Sasa Markovic (sa...@eu...); ! * ! * (C) Copyright 2003, by Sasa Markovic. ! * ! * This library is free software; you can redistribute it and/or modify it under the terms ! * of the GNU Lesser General Public License as published by the Free Software Foundation; ! * either version 2.1 of the License, or (at your option) any later version. ! * ! * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; ! * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ! * See the GNU Lesser General Public License for more details. ! * ! * You should have received a copy of the GNU Lesser General Public License along with this ! * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, ! * Boston, MA 02111-1307, USA. ! */ ! ! package jrobin.demo; ! ! import jrobin.core.*; ! import jrobin.graph.RrdGraph; ! import jrobin.graph.RrdGraphDef; ! ! import java.awt.*; ! import java.io.BufferedOutputStream; ! import java.io.FileOutputStream; ! import java.io.IOException; ! import java.io.PrintWriter; ! import java.util.GregorianCalendar; ! ! /** ! * <p>Class used to demonstrate almost all of JRobin capabilities. To run this ! * demonstration execute the following command:</p> ! * ! * <pre> ! * java -jar JRobin-{version}.jar ! * </pre> ! * ! * <p>The jar-file can be found in the <code>libs</code> directory of this distribution. ! * <b>On Linux platforms, graphs cannot be generated if your computer has no X-windows ! * server up and running</b> (you'll end up with a nasty looking exception).</p> ! * ! * @author <a href="mailto:sa...@eu...">Sasa Markovic</a> ! */ ! public class JRobinDemo { ! static final String HOME = System.getProperty("user.home"); ! static final String SEPARATOR = System.getProperty("file.separator"); ! static final String FILE = "demo"; ! static final GregorianCalendar START = new GregorianCalendar(2003, 4, 1); ! static final GregorianCalendar END = new GregorianCalendar(2003, 5, 1); ! static final int MAX_STEP = 240; ! ! /** ! * <p>Runs the demonstration. ! * Demonstration consists of the following steps: (all files will be ! * created in your HOME directory):</p> ! * ! * <ul> ! * <li>Sample RRD database 'demo.rrd' is created (uses two GAUGE data sources). ! * <li>RRD file is updated more than 20.000 times, simulating one month of updates with ! * time steps of max. 4 minutes. ! * <li>Last update time is printed. ! * <li>Sample fetch request is executed, fetching data from RRD file for the whole month. ! * <li>Fetched data is printed on the screen. ! * <li>RRD file is dumped to file 'demo.xml' (XML format). ! * <li>New RRD file 'demo_restored.rrd' is created from XML file. ! * <li>RRD graph for the whole month is created in memory. ! * <li>Graph is saved in PNG and JPEG format (files 'demo.png' and 'demo.jpeg'). ! * <li>All files are closed. ! * <li>Log file 'demo.log' is created. Log file consist of RRDTool commands - execute this file ! * as Linux shell script to compare RRDTool's and Jrobin's fetch results (should be exactly the ! * same). ! * </ul> ! * ! * <p>If everything goes well, no exceptions are thrown and your graphs should look like:</p> ! * <p align="center"><img src="../../../images/demo.png" border="1"></p> ! * <p>Red and blue lines are simple (DEF) graph sources. Green line (median value) ! * represents complex (CDEF) graph source which is defined by a RPN expression. Filled ! * area (orange color) represents the difference between data source values. Sine line is here ! * just to represent JRobin's advanced graphing capabilities.</p> ! * ! * @param args Empty. This demo does not accept command line parameters. ! * @throws RrdException Thrown in case of JRobin specific error. ! * @throws IOException Thrown in case of I/O related error. ! */ ! public static void main(String[] args) throws RrdException, IOException { ! // setup ! println("==Starting demo"); ! RrdDb.setLockMode(RrdDb.WAIT_IF_LOCKED); ! ! long startMillis = System.currentTimeMillis(); ! long start = START.getTime().getTime() / 1000L; ! long end = END.getTime().getTime() / 1000L; ! String rrdFile = getFullPath(FILE + ".rrd"); ! String xmlFile = getFullPath(FILE + ".xml"); ! String rrdFile2 = getFullPath(FILE + "_restored.rrd"); ! //String pngFile = getFullPath(FILE + ".png"); ! String pngFile = "/zzzzzz.png"; ! String jpegFile = getFullPath(FILE + ".jpeg"); ! String logFile = getFullPath(FILE + ".log"); ! PrintWriter pw = new PrintWriter( ! new BufferedOutputStream( ! new FileOutputStream(logFile, false)) ! ); ! ! // creation ! println("==Creating RRD file " + rrdFile); ! RrdDef rrdDef = new RrdDef(rrdFile, start - 1, 300); ! rrdDef.addDatasource("sun", "GAUGE", 600, 0, Double.NaN); ! rrdDef.addDatasource("shade", "GAUGE", 600, 0, Double.NaN); ! rrdDef.addArchive("AVERAGE", 0.5, 1, 600); ! rrdDef.addArchive("AVERAGE", 0.5, 6, 700); ! rrdDef.addArchive("AVERAGE", 0.5, 24, 797); ! rrdDef.addArchive("AVERAGE", 0.5, 288, 775); ! rrdDef.addArchive("MAX", 0.5, 1, 600); ! rrdDef.addArchive("MAX", 0.5, 6, 700); ! rrdDef.addArchive("MAX", 0.5, 24, 797); ! rrdDef.addArchive("MAX", 0.5, 288, 775); ! println(rrdDef.dump()); ! pw.println(rrdDef.dump()); ! RrdDb rrdDb = new RrdDb(rrdDef); ! println("==RRD file created."); ! ! // update database ! GaugeSource sunSource = new GaugeSource(1200, 20); ! GaugeSource shadeSource = new GaugeSource(300, 10); ! println("==Simulating one month of RRD file updates with step not larger than " + ! MAX_STEP + " seconds (* denotes 1000 updates)"); ! long t = start; int n = 0; ! while(t <= end) { ! Sample sample = rrdDb.createSample(t); ! sample.setValue("sun", sunSource.getValue()); ! sample.setValue("shade", shadeSource.getValue()); ! pw.println(sample.dump()); ! sample.update(); ! t += Math.random() * MAX_STEP + 1; ! if(((++n) % 1000) == 0) { ! System.out.print("*"); ! }; ! } ! System.out.println(""); ! println("==Finished. RRD file updated " + n + " times"); ! println("==Last update time was: " + rrdDb.getLastUpdateTime()); ! ! // fetch data ! println("==Fetching data for the whole month"); ! FetchRequest request = rrdDb.createFetchRequest("AVERAGE", start, end); ! println(request.dump()); ! pw.println(request.dump()); ! FetchPoint[] points = request.fetch(); ! println("==Data fetched. " + points.length + " points obtained"); ! for(int i = 0; i < points.length; i++) { ! println(points[i].dump()); ! } ! println("==Fetch completed"); ! println("==Dumping RRD file to XML file " + xmlFile + " (can be restored with RRDTool)"); ! //rrdDb.dumpXml(xmlFile); ! println("==Creating RRD file " + rrdFile2 + " from " + xmlFile); ! //RrdDb rrdDb2 = new RrdDb(rrdFile2, xmlFile); ! // close files ! println("==Closing both RRD files"); ! rrdDb.close(); ! //rrdDb2.close(); ! ! // creating graph ! println("==Creating graph from the second file"); ! RrdGraphDef gDef = new RrdGraphDef(); ! gDef.setTimePeriod(start, end); ! gDef.setTimeAxisLabel("day in month"); ! gDef.setTitle("Temperatures in May 2003"); ! gDef.setValueAxisLabel("temperature"); ! gDef.datasource("sun", rrdFile, "sun", "AVERAGE"); ! gDef.datasource("shade", rrdFile, "shade", "AVERAGE"); ! gDef.datasource("median", "sun,shade,+,2,/"); ! gDef.datasource("diff", "sun,shade,-,ABS,-1,*"); ! gDef.datasource("sine", "TIME," + start + ",-," + (end - start) + ! ",/,2,PI,*,*,SIN,1000,*"); ! gDef.line("sun", Color.RED, "sun temp"); ! gDef.line("shade", Color.BLUE, "shade temp"); ! gDef.line("median", Color.GREEN, "median value"); ! gDef.area("diff", Color.ORANGE, "difference"); ! gDef.line("sine", Color.CYAN, "sine function demo"); ! gDef.gprint("sun", "MAX", "\nmaxSun = @3@s"); ! gDef.gprint("sun", "AVERAGE", "avgSun = @3@S@r"); ! gDef.gprint("shade", "MAX", "maxShade = @3@s"); ! gDef.gprint("shade", "AVERAGE", "avgShade = @3@S@r"); ! RrdGraph graph = new RrdGraph(gDef); ! println("==Graph created"); ! println("==Saving graph as PNG file " + pngFile); ! graph.saveAsPNG(pngFile, 600, 400); ! println("==Saving graph as JPEG file " + jpegFile); ! graph.saveAsJPEG(jpegFile, 600, 400, 0.8F); ! ! // demo ends ! pw.close(); ! println("Demo completed in " + ! ((System.currentTimeMillis() - startMillis) / 1000.0) + ! " sec"); ! } ! ! static void println(String msg) { ! System.out.println(msg); ! } ! ! static String getFullPath(String path) { ! return HOME + SEPARATOR + path; ! } ! } ! ! class GaugeSource { ! private double value; ! private double step; ! ! GaugeSource(double value, double step) { ! this.value = value; ! this.step = step; ! } ! ! double getValue() { ! double oldValue = value; ! double increment = Math.random() * step; ! if(Math.random() > 0.5) { ! increment *= -1; ! } ! value += increment; ! if(value <= 0) { ! value = 0; ! } ! return oldValue; ! } ! ! ! } ! ! |