KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > demo > ComplexDemo


1 package org.jrobin.demo;
2
3 /* ============================================================
4  * JRobin : Pure java implementation of RRDTool's functionality
5  * ============================================================
6  *
7  * Project Info: http://www.jrobin.org
8  * Project Lead: Sasa Markovic (saxon@jrobin.org);
9  *
10  * (C) Copyright 2003, by Sasa Markovic.
11  *
12  * This library is free software; you can redistribute it and/or modify it under the terms
13  * of the GNU Lesser General Public License as published by the Free Software Foundation;
14  * either version 2.1 of the License, or (at your option) any later version.
15  *
16  * Developers: Sasa Markovic (saxon@jrobin.org)
17  * Arne Vandamme (cobralord@jrobin.org)
18  *
19  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
20  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21  * See the GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License along with this
24  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
25  * Boston, MA 02111-1307, USA.
26  */

27
28 import java.awt.Color JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.GregorianCalendar JavaDoc;
31 import java.text.SimpleDateFormat JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 import org.jrobin.core.*;
35 import org.jrobin.graph.*;
36
37 /**
38  * <p>Extended graphing demo for JRobin.</p>
39  *
40  * @author Arne Vandamme (cobralord@jrobin.org)
41  */

42 class ComplexDemo {
43     private static final String JavaDoc filename = "complexdemo";
44
45     private static String JavaDoc getPath(String JavaDoc ext) {
46         return Util.getJRobinDemoPath(filename + "." + ext);
47     }
48
49     private static String JavaDoc getPath(int version, String JavaDoc ext) {
50         return Util.getJRobinDemoPath(filename + version + "." + ext);
51     }
52
53     private static void println(String JavaDoc msg) {
54         System.out.println(msg);
55     }
56
57     private static void createDatabase(String JavaDoc xmlPath) throws IOException JavaDoc, RrdException {
58         // Import database from XML
59
String JavaDoc rrdPath = getPath("rrd");
60         println("-- Importing XML file: " + xmlPath);
61         println("-- to RRD file: " + rrdPath);
62         RrdDbPool pool = RrdDbPool.getInstance();
63         RrdDb rrd = pool.requestRrdDb(rrdPath, xmlPath);
64         println("-- RRD file created");
65         pool.release(rrd);
66     }
67
68     private static void createGraphs() throws RrdException, IOException JavaDoc {
69         GregorianCalendar JavaDoc start, stop;
70         RrdGraph graph = new RrdGraph(true);
71         String JavaDoc rrdPath = getPath("rrd");
72
73         // Create traffic overview of a week
74
println("-- Creating graph 1");
75         start = new GregorianCalendar JavaDoc(2003, 7, 20);
76         stop = new GregorianCalendar JavaDoc(2003, 7, 27);
77
78         RrdGraphDef def = new RrdGraphDef(start, stop);
79         def.setImageBorder(Color.GRAY, 1);
80         def.setTitle("JRobinComplexDemo@Ldemo graph 1@r\nNetwork traffic overview");
81         def.setVerticalLabel("bits per second");
82         def.datasource("ifInOctets", rrdPath, "ifInOctets", "AVERAGE");
83         def.datasource("ifOutOctets", rrdPath, "ifOutOctets", "AVERAGE");
84         def.datasource("bitIn", "ifInOctets,8,*");
85         def.datasource("bitOut", "ifOutOctets,8,*");
86         def.comment(" ");
87         def.area("bitIn", new Color JavaDoc(0x00, 0xFF, 0x00), "Incoming traffic ");
88         def.line("bitOut", new Color JavaDoc(0x00, 0x00, 0x33), "Outgoing traffic\n\n");
89         def.gprint("bitIn", "MAX", "Max: @6.1 @sbit/s");
90         def.gprint("bitOut", "MAX", " @6.1 @sbit/s\n");
91         def.gprint("bitIn", "MIN", "Min: @6.1 @sbit/s");
92         def.gprint("bitOut", "MIN", " @6.1 @sbit/s");
93         def.comment(" Connection: 100 Mbit/s\n");
94         def.gprint("bitIn", "AVG", "Avg: @6.1 @sbit/s");
95         def.gprint("bitOut", "AVG", " @6.1 @sbit/s");
96         def.comment(" Duplex mode: FD - fixed\n\n");
97         def.gprint("bitIn", "LAST", "Cur: @6.1 @sbit/s");
98         def.gprint("bitOut", "LAST", " @6.1 @sbit/s\n\n");
99         def.comment("[ courtesy of www.cherrymon.org ]@L");
100         def.comment("Generated: " + timestamp() + " @r");
101
102         graph.setGraphDef(def);
103         String JavaDoc pngFile = getPath(1, "png");
104         graph.saveAsPNG(pngFile);
105         String JavaDoc gifFile = getPath(1, "gif");
106         graph.saveAsGIF(gifFile);
107         String JavaDoc jpgFile = getPath(1, "jpg");
108         graph.saveAsJPEG(jpgFile, 0.6F);
109
110         // Create server load and cpu usage of a day
111
println("-- Creating graph 2");
112         start = new GregorianCalendar JavaDoc(2003, 7, 19);
113         stop = new GregorianCalendar JavaDoc(2003, 7, 20);
114
115         def = new RrdGraphDef(start, stop);
116         def.setImageBorder(Color.GRAY, 1);
117         def.setTitle("JRobinComplexDemo@Ldemo graph 2@r\nServer load and CPU utilization");
118         def.datasource("load", rrdPath, "serverLoad", "AVERAGE");
119         def.datasource("user", rrdPath, "serverCPUUser", "AVERAGE");
120         def.datasource("nice", rrdPath, "serverCPUNice", "AVERAGE");
121         def.datasource("system", rrdPath, "serverCPUSystem", "AVERAGE");
122         def.datasource("idle", rrdPath, "serverCPUIdle", "AVERAGE");
123         def.datasource("total", "user,nice,+,system,+,idle,+");
124         def.datasource("busy", "user,nice,+,system,+,total,/,100,*");
125         def.datasource("p25t50", "busy,25,GT,busy,50,LE,load,0,IF,0,IF");
126         def.datasource("p50t75", "busy,50,GT,busy,75,LE,load,0,IF,0,IF");
127         def.datasource("p75t90", "busy,75,GT,busy,90,LE,load,0,IF,0,IF");
128         def.datasource("p90t100", "busy,90,GT,load,0,IF");
129         def.comment("CPU utilization (%)\n ");
130         def.area("load", new Color JavaDoc(0x66, 0x99, 0xcc), " 0 - 25%");
131         def.area("p25t50", new Color JavaDoc(0x00, 0x66, 0x99), "25 - 50%@L");
132         def.gprint("busy", "MIN", "Minimum:@5.1@s%");
133         def.gprint("busy", "MAX", "Maximum:@5.1@s% @r ");
134         def.area("p50t75", new Color JavaDoc(0x66, 0x66, 0x00), "50 - 75%");
135         def.area("p75t90", new Color JavaDoc(0xff, 0x66, 0x00), "75 - 90%");
136         def.area("p90t100", new Color JavaDoc(0xcc, 0x33, 0x00), "90 - 100%@L");
137         def.gprint("busy", "AVG", " Average:@5.1@s%");
138         def.gprint("busy", "LAST", "Current:@5.1@s% @r ");
139         def.comment("\nServer load\n ");
140         def.line("load", new Color JavaDoc(0x00, 0x00, 0x00), "Load average (5 min)@L");
141         def.gprint("load", "MIN", "Minimum:@5.2@s%");
142         def.gprint("load", "MAX", "Maximum:@5.2@s% @r ");
143         def.gprint("load", "AVG", "Average:@5.2@s%");
144         def.gprint("load", "LAST", "Current:@5.2@s% @r");
145         def.comment("\n\n[ courtesy of www.cherrymon.org ]@L");
146         def.comment("Generated: " + timestamp() + " @r");
147
148         graph.setGraphDef(def);
149         pngFile = getPath(2, "png");
150         graph.saveAsPNG(pngFile);
151         gifFile = getPath(2, "gif");
152         graph.saveAsGIF(gifFile);
153         jpgFile = getPath(2, "jpg");
154         graph.saveAsJPEG(jpgFile, 0.6F);
155
156         // Create ftp graph for a month
157
println("-- Creating graph 3");
158         start = new GregorianCalendar JavaDoc(2003, 7, 19, 12, 00);
159         stop = new GregorianCalendar JavaDoc(2003, 7, 20, 12, 00);
160
161         def = new RrdGraphDef(start, stop);
162         def.setImageBorder(Color.GRAY, 1);
163         def.setFrontGrid(false);
164         def.setTitle("JRobinComplexDemo@Ldemo graph 3@r\nFTP Usage");
165         def.datasource("ftp", rrdPath, "ftpUsers", "AVERAGE");
166         def.line("ftp", new Color JavaDoc(0x00, 0x00, 0x33), "FTP connections");
167         def.gprint("ftp", "AVG", "( average: @0,");
168         def.gprint("ftp", "MIN", "never below: @0 )\n\n");
169         def.comment(" Usage spread:");
170         def.area(new GregorianCalendar JavaDoc(2003, 7, 19, 17, 00), Double.MIN_VALUE,
171             new GregorianCalendar JavaDoc(2003, 7, 19, 23, 00), Double.MAX_VALUE,
172             Color.RED, "peak period");
173         def.area(new GregorianCalendar JavaDoc(2003, 7, 20, 5, 00), Double.MIN_VALUE,
174             new GregorianCalendar JavaDoc(2003, 7, 20, 8, 30), Double.MAX_VALUE,
175             Color.LIGHT_GRAY, "quiet period\n");
176         def.comment(" Rise/descend:");
177         def.area("ftp", new Color JavaDoc(0x00, 0x00, 0x33), null);
178         def.line(new GregorianCalendar JavaDoc(2003, 7, 19, 12, 00), 110,
179             new GregorianCalendar JavaDoc(2003, 7, 19, 20, 30), 160,
180             Color.PINK, "climb slope", 2);
181         def.line(new GregorianCalendar JavaDoc(2003, 7, 19, 20, 30), 160,
182             new GregorianCalendar JavaDoc(2003, 7, 20, 8, 00), 45,
183             Color.CYAN, "fall-back slope\n", 2);
184         def.vrule(new GregorianCalendar JavaDoc(2003, 7, 20), Color.YELLOW, null);
185         def.comment("\n\n[ courtesy of www.cherrymon.org ]@L");
186         def.comment("Generated: " + timestamp() + " @r");
187
188         graph.setGraphDef(def);
189         pngFile = getPath(3, "png");
190         graph.saveAsPNG(pngFile, 500, 300);
191         gifFile = getPath(3, "gif");
192         graph.saveAsGIF(gifFile, 500, 300);
193         jpgFile = getPath(3, "jpg");
194         graph.saveAsJPEG(jpgFile, 500, 300, 0.6F);
195         println("-- Finished");
196         println("**************************************");
197         println("Check your " + Util.getJRobinDemoDirectory() + " directory.");
198         println("You should see nine nice looking graphs starting with [" + filename + "],");
199         println("three different graphs, each in three different image formats");
200         println("**************************************");
201     }
202
203     private static String JavaDoc timestamp() {
204         SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc("dd/MM/yyyy HH:mm");
205         return df.format(new Date JavaDoc());
206     }
207
208     public static void main(String JavaDoc[] args) throws IOException JavaDoc, RrdException {
209         if(args.length == 0) {
210             println("Usage: ComplexDemo [path to rrdtool_dump.xml file]");
211             println("You can download separate rrdtool_dump.xml file from:");
212             println("http://www.sourceforge.net/projects/jrobin");
213             System.exit(-1);
214         }
215         long start = System.currentTimeMillis();
216
217         println("********************************************************************");
218         println("* JRobinComplexDemo *");
219         println("* *");
220         println("* This demo creates 3 separate graphs and stores them under *");
221         println("* several formats in 9 files. Values are selected from a large *");
222         println("* RRD file that will be created by importing an XML dump *");
223         println("* of approx. 7 MB. *");
224         println("* *");
225         println("* Graphs are created using real-life values, original RRD file *");
226         println("* provided by www.cherrymon.org. See the ComplexDemo *");
227         println("* sourcecode on how to create the graphs generated by this demo. *");
228         println("********************************************************************");
229
230         createDatabase(args[0]);
231         createGraphs();
232
233         long stop = System.currentTimeMillis();
234         println("-- Demo finished in " + ((stop - start) / 1000.0) + " seconds.");
235     }
236 }
237
Popular Tags