1 package org.jrobin.demo; 2 3 27 28 import java.awt.Color ; 29 import java.util.Date ; 30 import java.util.GregorianCalendar ; 31 import java.text.SimpleDateFormat ; 32 import java.io.IOException ; 33 34 import org.jrobin.core.*; 35 import org.jrobin.graph.*; 36 37 42 class ComplexDemo { 43 private static final String filename = "complexdemo"; 44 45 private static String getPath(String ext) { 46 return Util.getJRobinDemoPath(filename + "." + ext); 47 } 48 49 private static String getPath(int version, String ext) { 50 return Util.getJRobinDemoPath(filename + version + "." + ext); 51 } 52 53 private static void println(String msg) { 54 System.out.println(msg); 55 } 56 57 private static void createDatabase(String xmlPath) throws IOException , RrdException { 58 String 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 { 69 GregorianCalendar start, stop; 70 RrdGraph graph = new RrdGraph(true); 71 String rrdPath = getPath("rrd"); 72 73 println("-- Creating graph 1"); 75 start = new GregorianCalendar (2003, 7, 20); 76 stop = new GregorianCalendar (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 (0x00, 0xFF, 0x00), "Incoming traffic "); 88 def.line("bitOut", new Color (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 pngFile = getPath(1, "png"); 104 graph.saveAsPNG(pngFile); 105 String gifFile = getPath(1, "gif"); 106 graph.saveAsGIF(gifFile); 107 String jpgFile = getPath(1, "jpg"); 108 graph.saveAsJPEG(jpgFile, 0.6F); 109 110 println("-- Creating graph 2"); 112 start = new GregorianCalendar (2003, 7, 19); 113 stop = new GregorianCalendar (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 (0x66, 0x99, 0xcc), " 0 - 25%"); 131 def.area("p25t50", new Color (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 (0x66, 0x66, 0x00), "50 - 75%"); 135 def.area("p75t90", new Color (0xff, 0x66, 0x00), "75 - 90%"); 136 def.area("p90t100", new Color (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 (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 println("-- Creating graph 3"); 158 start = new GregorianCalendar (2003, 7, 19, 12, 00); 159 stop = new GregorianCalendar (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 (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 (2003, 7, 19, 17, 00), Double.MIN_VALUE, 171 new GregorianCalendar (2003, 7, 19, 23, 00), Double.MAX_VALUE, 172 Color.RED, "peak period"); 173 def.area(new GregorianCalendar (2003, 7, 20, 5, 00), Double.MIN_VALUE, 174 new GregorianCalendar (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 (0x00, 0x00, 0x33), null); 178 def.line(new GregorianCalendar (2003, 7, 19, 12, 00), 110, 179 new GregorianCalendar (2003, 7, 19, 20, 30), 160, 180 Color.PINK, "climb slope", 2); 181 def.line(new GregorianCalendar (2003, 7, 19, 20, 30), 160, 182 new GregorianCalendar (2003, 7, 20, 8, 00), 45, 183 Color.CYAN, "fall-back slope\n", 2); 184 def.vrule(new GregorianCalendar (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 timestamp() { 204 SimpleDateFormat df = new SimpleDateFormat ("dd/MM/yyyy HH:mm"); 205 return df.format(new Date ()); 206 } 207 208 public static void main(String [] args) throws IOException , 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 |