📄 jrobinlogger.java
字号:
def.addArchive("MAX", 0.5, 1, 5760); def.addArchive("AVERAGE", 0.5, 5, 13824); def.addArchive("MIN", 0.5, 5, 13824); def.addArchive("MAX", 0.5, 5, 13824); def.addArchive("AVERAGE", 0.5, 60, 16704); def.addArchive("MIN", 0.5, 60, 16704); def.addArchive("MAX", 0.5, 60, 16704); def.addArchive("AVERAGE", 0.5, 1440, 50000); def.addArchive("MIN", 0.5, 1440, 50000); def.addArchive("MAX", 0.5, 1440, 50000); rrd = new RrdDb(def); complain(LOG_INFO, CH_LOGGER, "New RRD created in " + (System.currentTimeMillis() - start) + "ms"); } private void refillRRD() throws IOException, RrdException { // - Open the trace file // - Get its length // - As we read each line, update the estimated time // - If the estimatd time has changed significantly, log new estimate File traceFile = new File(getTraceLocation()); if ( !traceFile.exists() ) { complain(LOG_WARNING, CH_LOGGER, "refill: no trace file at " + getTraceLocation() + "???"); return; } long total_size = traceFile.length(); long current_size = 0; long start_time = System.currentTimeMillis(); long end_time = start_time/1000; long line_count = 0; BufferedReader br = new BufferedReader(new FileReader(getTraceLocation())); while ( true ) { String line = br.readLine(); if ( line == null ) { break; } line_count++; rrdupdate(line); current_size += line.length(); // What percentage we have already completed? double share = (double)total_size/(double)current_size; // How long did it take us? long elapsed_time = System.currentTimeMillis() - start_time; // How long we think it will take us? long ETAmillis = start_time + (long)(elapsed_time * share); // See if this time is much different from what we thought it // would be long ETA = ETAmillis / 1000; if ( (ETA / 10) != (end_time / 10) ) { // If it is more than 10 seconds off, let's tell them complain(LOG_INFO, CH_LOGGER, "refill: " + Round.round1((1/share) * 100) + "%, ETA: " + new Date(ETAmillis)); end_time = ETA; } } end_time = System.currentTimeMillis(); // How long did it really take? long elapsed_time = (end_time - start_time) / 1000; // How many lines per second? double lps = (double)line_count / (double) elapsed_time; complain(LOG_NOTICE, CH_LOGGER, "refill: " + Round.round3(lps) + " records per second"); } private synchronized void generateIntervalDefinitions() { intervalDefs.clear(); IntervalDefinition id = null; // Remember that interval is in seconds, not milliseconds id = new IntervalDefinition("Temperature, last hour", 60 * 60, "temp-1h", "LAST"); intervalDefs.add(id); id = new IntervalDefinition("Temperature, last 3 hours", 60 * 60 * 3, "temp-3h", "LAST"); intervalDefs.add(id); id = new IntervalDefinition("Temperature, last 6 hours", 60 * 60 * 6, "temp-6h", "LAST"); intervalDefs.add(id); id = new IntervalDefinition("Temperature, last 32 hours", 60 * 60 * 32, "temp-32h", "LAST"); intervalDefs.add(id); id = new IntervalDefinition("Temperature, last 8 days", 60 * 60 * 24 * 8, "temp-8d", "AVERAGE"); intervalDefs.add(id); id = new IntervalDefinition("Temperature, last 5 weeks", 60 * 60 * 24 * 7 * 5, "temp-5w", "AVERAGE"); intervalDefs.add(id); id = new IntervalDefinition("Temperature, last 13 months", 60 * 60 * 24 * 30 * 13, "temp-13m", "AVERAGE"); intervalDefs.add(id); id = new IntervalDefinition("Temperature, last 3 years", 60 * 60 * 24 * 365 * 3, "temp-3y", "AVERAGE"); intervalDefs.add(id); } private synchronized void resetIntervalDefinitions() { for ( Iterator i = intervalDefs.iterator(); i.hasNext(); ) { IntervalDefinition id = (IntervalDefinition)i.next(); id.reset(); } } protected synchronized void graph() throws Throwable { File rrdFile = new File(getRrdLocation()); if ( !rrdFile.exists() ) { complain(LOG_WARNING, CH_LOGGER, "RRD doesn't (yet?) exist: " + getRrdLocation()); return; } // VT: FIXME: It was simpler to create arbitrary graphs from command // line... Maybe it'll make sense to just create a wrapper for // JRobin grapher and run it from the cron job, indeed? Let's see // how JDK 1.5 performs. // VT: This will be done even if there's no RRD database to speak // of. Even though the graphs will be empty with no data in them, // they will be produced. complain(LOG_INFO, CH_LOGGER, "Creating the graphs..."); long start = System.currentTimeMillis(); for ( Iterator i = intervalDefs.iterator(); i.hasNext(); ) { long local_start = System.currentTimeMillis(); IntervalDefinition id = (IntervalDefinition)i.next(); RrdGraph graph = new RrdGraph(id.getGraphDef()); graph.saveAsPNG(new File(imageDir, id.getFileName() + ".png").toString(), 800, 400); complain(LOG_DEBUG, CH_LOGGER, "Done creating " + id.getFileName() + " in " + (System.currentTimeMillis() - local_start) + "ms"); } complain(LOG_DEBUG, CH_LOGGER, "Done creating graphs in " + (System.currentTimeMillis() - start) + "ms"); } /** * Defines graph features that are not easily formalized in Java. * */ protected class IntervalDefinition { /** * Graph title. */ private String title; /** * Interval to collect the data for, in seconds. */ private long interval; /** * Image file base name (without the path part). * * Extension is <strong>not</strong> allowed, so if you specify a * name "graph.png", the resulting file name will be * "graph.png.png". Dots, on the other hand, are allowed. */ private String fileName; /** * Archive. * * Possible values are "LAST", "AVERAGE", "MIN", "MAX". */ private String archive; /** * Generated <code>RrdGraphDef</code> object. */ private RrdGraphDef graphDef; public IntervalDefinition(String title, long interval, String fileName, String archive) { this.title = title; this.interval = interval; this.fileName = fileName; this.archive = archive; } public String getFileName() { return fileName; } public RrdGraphDef getGraphDef() throws RrdException { if ( graphDef == null ) { graphDef = new RrdGraphDef(); graphDef.setVerticalLabel("Temperature, C\u00B0"); graphDef.setTitle(title); // VT: FIXME: Make this configurable graphDef.setBackColor(new Color(0x2c, 0x24, 0x50)); graphDef.setCanvasColor(new Color(0x00, 0x33, 0x00)); graphDef.setImageBorder(new Color(0x66, 0x66, 0x99), 2); graphDef.setTitleFontColor(new Color(0xdd, 0xb1, 0x04)); graphDef.setDefaultFontColor(new Color(0xdd, 0xb1, 0x04)); graphDef.setMajorGridColor(new Color(0x77, 0x77, 0x77)); graphDef.setMinorGridColor(new Color(0x2c, 0x24, 0x50)); graphDef.setFrameColor(new Color(0x2c, 0x24, 0x50)); graphDef.setArrowColor(new Color(0xff, 0x00, 0x00)); for ( Iterator i = iterator(); i.hasNext(); ) { String device = i.next().toString(); graphDef.datasource("def_" + device, getRrdLocation(), device, archive); } // VT: FIXME: // - Allow custom draw order // - Allow for configurable colors // - Allow for custom descriptions int colorIndex = 0; for ( Iterator i = iterator(); i.hasNext(); ) { String device = i.next().toString(); graphDef.line("def_" + device, graphColors[colorIndex++ % graphColors.length], device + "\n"); } } long now = System.currentTimeMillis() / 1000; graphDef.setTimePeriod((now - interval), now); return graphDef; } public void reset() { graphDef = null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -