|
From: <sa...@us...> - 2004-03-08 13:38:59
|
Update of /cvsroot/jrobin/src/org/jrobin/mrtg/server In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10511/org/jrobin/mrtg/server Modified Files: Config.java Plotter.java RrdWriter.java Server.java Log Message: XML templates improved Index: Config.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/mrtg/server/Config.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Config.java 10 Nov 2003 08:52:29 -0000 1.2 --- Config.java 8 Mar 2004 13:14:39 -0000 1.3 *************** *** 25,33 **** package org.jrobin.mrtg.server; import java.io.File; ! class Config { private static final String DELIM = System.getProperty("file.separator"); - private static final String HOME_DIR = System.getProperty("user.home") + DELIM + "mrtg" + DELIM; --- 25,35 ---- package org.jrobin.mrtg.server; + import org.jrobin.mrtg.MrtgConstants; + import java.io.File; ! class Config implements MrtgConstants { ! // various paths private static final String DELIM = System.getProperty("file.separator"); private static final String HOME_DIR = System.getProperty("user.home") + DELIM + "mrtg" + DELIM; *************** *** 35,38 **** --- 37,42 ---- private static final String RRD_DIR = HOME_DIR + "rrd" + DELIM; private static final String HARDWARE_FILE = CONF_DIR + "mrtg.dat"; + private static final String RRD_DEF_TEMPLATE_FILE = CONF_DIR + "rrd_template.xml"; + private static final String RRD_GRAPH_DEF_TEMPLATE_FILE = CONF_DIR + "graph_template.xml"; static { *************** *** 57,59 **** --- 61,71 ---- return HARDWARE_FILE; } + + public static String getRrdTemplateFile() { + return RRD_DEF_TEMPLATE_FILE; + } + + public static String getGraphTemplateFile() { + return RRD_GRAPH_DEF_TEMPLATE_FILE; + } } Index: Plotter.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/mrtg/server/Plotter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Plotter.java 9 Dec 2003 12:22:04 -0000 1.1 --- Plotter.java 8 Mar 2004 13:14:39 -0000 1.2 *************** *** 27,36 **** import org.jrobin.core.RrdException; import org.jrobin.graph.RrdGraph; import org.jrobin.graph.RrdGraphDef; import org.jrobin.mrtg.MrtgConstants; import org.jrobin.mrtg.MrtgException; - import java.awt.*; import java.io.IOException; import java.util.Date; --- 27,37 ---- import org.jrobin.core.RrdException; import org.jrobin.graph.RrdGraph; + import org.jrobin.graph.RrdGraphDefTemplate; import org.jrobin.graph.RrdGraphDef; import org.jrobin.mrtg.MrtgConstants; import org.jrobin.mrtg.MrtgException; import java.io.IOException; + import java.io.File; import java.util.Date; *************** *** 38,43 **** --- 39,59 ---- private String ifDescr, host, alias; + private static RrdGraphDefTemplate rrdGraphDefTemplate = null; + + static { + try { + rrdGraphDefTemplate = + new RrdGraphDefTemplate(new File(Config.getGraphTemplateFile())); + } catch (IOException e) { + e.printStackTrace(); + } catch (RrdException e) { + e.printStackTrace(); + } + } Plotter(String host, String ifDescr) throws MrtgException { + if(rrdGraphDefTemplate == null) { + throw new MrtgException("Could not load graph XML template"); + } this.host = host; this.ifDescr = ifDescr; *************** *** 57,90 **** } ! RrdGraph getRrdGraph(long start, long stop) throws MrtgException { ! String filename = RrdWriter.getRrdFilename(host, ifDescr); ! RrdGraph graph = new RrdGraph(true); ! RrdGraphDef graphDef = new RrdGraphDef(); ! try { ! graphDef.setImageBorder(Color.WHITE, 0); // Don't show border ! graphDef.setTimePeriod(start, stop); ! graphDef.setTitle(ifDescr + "@" + host); ! graphDef.setVerticalLabel("transfer speed [bits/sec]"); ! graphDef.datasource("in", filename, "in", "AVERAGE"); ! graphDef.datasource("out", filename, "out", "AVERAGE"); ! graphDef.datasource("in8", "in,8,*"); ! graphDef.datasource("out8", "out,8,*"); ! graphDef.area("out8", Color.GREEN, "output traffic\n"); ! graphDef.line("in8", Color.BLUE, "input traffic"); ! graphDef.comment("\n"); ! graphDef.gprint("in8", "AVERAGE", "Average input: @7.2 @sbits/s"); ! graphDef.gprint("in8", "MAX", "Maximum input: @7.2 @Sbits/s\n"); ! graphDef.gprint("out8", "AVERAGE", "Average output:@7.2 @sbits/s"); ! graphDef.gprint("out8", "MAX", "Maximum output:@7.2 @Sbits/s\n"); ! graphDef.comment("\n"); ! graphDef.comment("Description on device: " + alias); ! graphDef.comment("\n"); ! graphDef.comment("Graph from " + new Date(start * 1000L)); ! graphDef.comment("to " + new Date(stop * 1000L + 1)); ! graph.setGraphDef(graphDef); ! return graph; ! } catch (RrdException e) { ! throw new MrtgException(e); } } } --- 73,97 ---- } ! RrdGraph getRrdGraph(long start, long end) throws MrtgException { ! RrdGraphDef rrdGraphDef; ! // only one template parsed, many threads plotting ! synchronized(rrdGraphDefTemplate) { ! rrdGraphDefTemplate.setVariable("start", start); ! rrdGraphDefTemplate.setVariable("end", end); ! rrdGraphDefTemplate.setVariable("interface", ifDescr); ! rrdGraphDefTemplate.setVariable("host", host); ! rrdGraphDefTemplate.setVariable("rrd", RrdWriter.getRrdFilename(host, ifDescr)); ! rrdGraphDefTemplate.setVariable("alias", alias); ! rrdGraphDefTemplate.setVariable("date_start", new Date(start * 1000L).toString()); ! rrdGraphDefTemplate.setVariable("date_end", new Date(end * 1000L).toString()); ! try { ! rrdGraphDef = rrdGraphDefTemplate.getRrdGraphDef(); ! } catch (RrdException e) { ! throw new MrtgException(e); ! } } + RrdGraph graph = new RrdGraph(true); // use pool + graph.setGraphDef(rrdGraphDef); + return graph; } } Index: RrdWriter.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/mrtg/server/RrdWriter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RrdWriter.java 9 Dec 2003 12:22:04 -0000 1.1 --- RrdWriter.java 8 Mar 2004 13:14:39 -0000 1.2 *************** *** 27,30 **** --- 27,32 ---- import org.jrobin.core.*; import org.jrobin.mrtg.Debug; + import org.jrobin.mrtg.MrtgConstants; + import org.jrobin.mrtg.MrtgException; import java.io.File; *************** *** 34,38 **** import java.util.List; ! class RrdWriter extends Thread { private int sampleCount, badSavesCount, goodSavesCount; private List queue = Collections.synchronizedList(new LinkedList()); --- 36,41 ---- import java.util.List; ! class RrdWriter extends Thread implements MrtgConstants { ! private RrdDefTemplate rrdDefTemplate; private int sampleCount, badSavesCount, goodSavesCount; private List queue = Collections.synchronizedList(new LinkedList()); *************** *** 42,46 **** private volatile boolean active = true; ! RrdWriter() { start(); } --- 45,57 ---- private volatile boolean active = true; ! RrdWriter() throws MrtgException { ! // get definition from template ! try { ! rrdDefTemplate = new RrdDefTemplate(new File(Config.getRrdTemplateFile())); ! } catch (IOException e) { ! throw new MrtgException(e); ! } catch (RrdException e) { ! throw new MrtgException(e); ! } start(); } *************** *** 112,121 **** } ! void store(RawSample sample) { queue.add(sample); sampleCount++; ! synchronized(this) { ! notify(); ! } } --- 123,130 ---- } ! synchronized void store(RawSample sample) { queue.add(sample); sampleCount++; ! notify(); } *************** *** 128,149 **** else { // create RRD file first ! final RrdDef rrdDef = new RrdDef(rrdFile); ! rrdDef.setStep(300); ! rrdDef.setStartTime(rawSample.getTimestamp() - 10); ! rrdDef.addDatasource("in", "COUNTER", 600, Double.NaN, Double.NaN); ! rrdDef.addDatasource("out", "COUNTER", 600, Double.NaN, Double.NaN); ! rrdDef.addArchive("AVERAGE", 0.5, 1, 600); ! rrdDef.addArchive("AVERAGE", 0.5, 6, 700); ! rrdDef.addArchive("AVERAGE", 0.5, 24, 775); ! rrdDef.addArchive("AVERAGE", 0.5, 288, 797); ! rrdDef.addArchive("MAX", 0.5, 1, 600); ! rrdDef.addArchive("MAX", 0.5, 6, 700); ! rrdDef.addArchive("MAX", 0.5, 24, 775); ! rrdDef.addArchive("MAX", 0.5, 288, 797); ! rrdDef.addArchive("MIN", 0.5, 1, 600); ! rrdDef.addArchive("MIN", 0.5, 6, 700); ! rrdDef.addArchive("MIN", 0.5, 24, 775); ! rrdDef.addArchive("MIN", 0.5, 288, 797); ! Debug.print("Created: " + rrdFile); return pool.requestRrdDb(rrdDef); } --- 137,143 ---- else { // create RRD file first ! rrdDefTemplate.setVariable("path", rrdFile); ! RrdDef rrdDef = rrdDefTemplate.getRrdDef(); ! Debug.print("Creating: " + rrdFile); return pool.requestRrdDb(rrdDef); } Index: Server.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/mrtg/server/Server.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** Server.java 2 Mar 2004 09:47:49 -0000 1.7 --- Server.java 8 Mar 2004 13:14:39 -0000 1.8 *************** *** 26,31 **** import org.jrobin.core.RrdDb; - import org.jrobin.core.RrdDbPool; import org.jrobin.mrtg.MrtgException; import org.w3c.dom.Document; import org.w3c.dom.Element; --- 26,31 ---- import org.jrobin.core.RrdDb; import org.jrobin.mrtg.MrtgException; + import org.jrobin.mrtg.MrtgConstants; import org.w3c.dom.Document; import org.w3c.dom.Element; *************** *** 42,50 **** import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import java.util.Hashtable; import java.util.Vector; ! public class Server { private static Server instance; --- 42,51 ---- import java.io.FileOutputStream; import java.io.IOException; + import java.io.FileWriter; import java.util.Date; import java.util.Hashtable; import java.util.Vector; ! public class Server implements MrtgConstants { private static Server instance; *************** *** 74,77 **** --- 75,87 ---- throw new MrtgException("Cannot start Server, already started"); } + // create template files + try { + createXmlTemplateIfNecessary(Config.getRrdTemplateFile(), RRD_TEMPLATE_STR); + createXmlTemplateIfNecessary(Config.getGraphTemplateFile(), GRAPH_TEMPLATE_STR); + } + catch(IOException ioe) { + throw new MrtgException(ioe); + } + // load configuration String hwFile = Config.getHardwareFile(); if(new File(hwFile).exists()) { *************** *** 89,92 **** --- 99,113 ---- } + private void createXmlTemplateIfNecessary(String filePath, String fileContent) + throws IOException { + File file = new File(filePath); + if(!file.exists()) { + FileWriter writer = new FileWriter(filePath, false); + writer.write(fileContent); + writer.flush(); + writer.close(); + } + } + public synchronized void stop() throws MrtgException { if(!active) { |