From: Sasa M. <sa...@us...> - 2004-08-18 12:48:52
|
Update of /cvsroot/jrobin/src/org/jrobin/convertor In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31899/org/jrobin/convertor Modified Files: Convertor.java Log Message: Greatly improved importer utility. Now it reads RRDTool binary files directly. Index: Convertor.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/convertor/Convertor.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Convertor.java 19 Feb 2004 10:46:08 -0000 1.2 --- Convertor.java 18 Aug 2004 12:48:39 -0000 1.3 *************** *** 1,129 **** package org.jrobin.convertor; import org.jrobin.core.RrdDb; - import org.jrobin.core.RrdException; - import java.io.*; class Convertor { ! static final String SUFFIX = ".jrb"; ! static final String SEPARATOR = System.getProperty("file.separator"); ! static final Runtime RUNTIME = Runtime.getRuntime(); ! ! private String rrdtoolBinary; ! private String workingDirectory; ! private String suffix; ! private int okCount, badCount; ! private Convertor(String rrdtoolBinary, String workingDirectory, String suffix) { ! this.rrdtoolBinary = rrdtoolBinary; ! this.workingDirectory = workingDirectory; ! this.suffix = suffix; } ! private void convert() { ! println("Converting RRDTool files to JRobin native format"); ! println("Converted files will be placed in the same directory, with " + ! suffix + " suffix appended"); ! println("=========================================="); ! long start = System.currentTimeMillis(); ! if(!workingDirectory.endsWith(SEPARATOR)) { ! workingDirectory += SEPARATOR; ! } ! File parent = new File(workingDirectory); ! if(parent.isDirectory() && parent.exists()) { ! // directory ! FileFilter filter = new FileFilter() { ! public boolean accept(File f) { ! try { ! return !f.isDirectory() && f.getCanonicalPath().endsWith(".rrd"); ! } catch (IOException e) { ! return false; ! } ! } ! }; ! File[] files = parent.listFiles(filter); ! for(int i = 0; i < files.length; i++) { ! print("[" + i + "/" + files.length + "] "); ! convertFile(files[i]); ! } ! } ! else if(!parent.isDirectory() && parent.exists()) { ! // single file ! convertFile(parent); ! } ! else { ! println("Nothing to do"); } ! println("Conversion finished, " + okCount + " files ok, " + badCount + " files bad"); ! long secs = (System.currentTimeMillis() - start + 500L) / 1000L; ! long mins = secs / 60; ! secs %= 60; ! println("Time elapsed: " + mins + ":" + ((secs < 10)? "0": "") + secs); } ! private long convertFile(File rrdFile) { long start = System.currentTimeMillis(); ! String xmlPath = null, destPath = null; try { String sourcePath = rrdFile.getCanonicalPath(); ! xmlPath = sourcePath + ".xml"; ! destPath = sourcePath + suffix; ! print(rrdFile.getName() + " "); ! xmlDump(sourcePath, xmlPath); ! RrdDb rrd = new RrdDb(destPath, xmlPath); rrd.close(); ! rrd = null; ! System.gc(); ! okCount++; ! long elapsed = System.currentTimeMillis() - start; ! println("[OK, " + (elapsed / 1000.0) + "]"); ! return elapsed; ! } catch (IOException e) { ! removeFile(destPath); ! badCount++; ! println("[IO ERROR]"); ! return -1; ! } catch (RrdException e) { ! removeFile(destPath); badCount++; ! println("[RRD ERROR]"); ! return -2; ! } ! finally { ! removeFile(xmlPath); ! } ! } ! ! private static boolean removeFile(String filePath) { ! if(filePath != null) { ! return new File(filePath).delete(); ! } ! return true; ! } ! ! private void xmlDump(String sourcePath, String xmlPath) throws IOException { ! String[] cmd = new String[] { rrdtoolBinary, "dump", sourcePath }; ! Process p = RUNTIME.exec(cmd); ! OutputStream outStream = new BufferedOutputStream(new FileOutputStream(xmlPath, false)); ! transportStream(p.getInputStream(), outStream); ! transportStream(p.getErrorStream(), null); ! try { ! p.waitFor(); ! } ! catch(InterruptedException ie) { ! // NOP ! } ! } ! ! public static void main(String[] args) { ! if(args.length < 2 || args.length > 3) { ! println("Usage: java -jar convertor.jar " + ! "<path to RRDTool binary> <RRD directory/file path> [converted file suffix]"); ! } ! else { ! Convertor c = new Convertor(args[0], args[1], args.length == 3? args[2]: SUFFIX); ! c.convert(); } } --- 1,76 ---- + /* ============================================================ + * JRobin : Pure java implementation of RRDTool's functionality + * ============================================================ + * + * Project Info: http://www.jrobin.org + * Project Lead: Sasa Markovic (sa...@jr...); + * + * (C) Copyright 2003, by Sasa Markovic. + * + * Developers: Sasa Markovic (sa...@jr...) + * Arne Vandamme (cob...@jr...) + * + * 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 org.jrobin.convertor; import org.jrobin.core.RrdDb; import java.io.*; + import java.text.DecimalFormat; class Convertor { ! private static final String SUFFIX = ".jrobin"; ! private static final DecimalFormat secondsFormatter = new DecimalFormat("##0.000"); ! private static final DecimalFormat countFormatter = new DecimalFormat("00000"); ! private String[] files; ! private int totalCount, badCount, goodCount; ! private Convertor(String[] files) { ! this.files = files; } ! private void convertAll() { ! final String ruler = "======================================================================="; ! println(ruler); ! println("Converting RRDTool files to JRobin native format."); ! println("Original RRDTool files will not be modified in any way"); ! println("JRobin files created during the process will have a " + SUFFIX + " suffix"); ! println(ruler); ! for(int i = 0; i < files.length; i++) { ! convertFile(files[i]); } ! println(ruler); ! println("Finished: " + totalCount + " total, " + ! goodCount + " OK, " + badCount + " failed."); } ! private void convertFile(String path) { long start = System.currentTimeMillis(); ! totalCount++; try { + File rrdFile = new File(path); + print(countFormatter.format(totalCount) + " " + rrdFile.getName() + " "); String sourcePath = rrdFile.getCanonicalPath(); ! String destPath = sourcePath + SUFFIX; ! RrdDb rrd = new RrdDb(destPath, RrdDb.PREFIX_RRDTool + sourcePath); rrd.close(); ! goodCount++; ! double seconds = (System.currentTimeMillis() - start) / 1000.0; ! println("[OK, " + secondsFormatter.format(seconds) + " sec]"); ! } catch (Exception e) { badCount++; ! println("[" + e + "]"); } } *************** *** 137,156 **** } ! private static void transportStream(InputStream in, OutputStream out) throws IOException { ! try { ! int b; ! while((b = in.read()) != -1) { ! if(out != null) { ! out.write(b); ! } ! } ! } ! finally { ! in.close(); ! if(out != null) { ! out.flush(); ! out.close(); ! } } } } --- 84,95 ---- } ! public static void main(String[] args) { ! if(args.length == 0) { ! println("Usage : java -jar convertor.jar <RRD file pattern> ..."); ! println("Example: java -jar convertor.jar files/*.rrd"); ! System.exit(1); } + Convertor c = new Convertor(args); + c.convertAll(); } } |