001    /* ============================================================
002     * JRobin : Pure java implementation of RRDTool's functionality
003     * ============================================================
004     *
005     * Project Info:  http://www.jrobin.org
006     * Project Lead:  Sasa Markovic (saxon@jrobin.org);
007     *
008     * (C) Copyright 2003, by Sasa Markovic.
009     *
010     * Developers:    Sasa Markovic (saxon@jrobin.org)
011     *
012     *
013     * This library is free software; you can redistribute it and/or modify it under the terms
014     * of the GNU Lesser General Public License as published by the Free Software Foundation;
015     * either version 2.1 of the License, or (at your option) any later version.
016     *
017     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
018     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
019     * See the GNU Lesser General Public License for more details.
020     *
021     * You should have received a copy of the GNU Lesser General Public License along with this
022     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
023     * Boston, MA 02111-1307, USA.
024     */
025    
026    package org.jrobin.convertor;
027    
028    import org.jrobin.core.RrdDb;
029    import org.jrobin.core.RrdException;
030    
031    import java.io.*;
032    import java.text.DecimalFormat;
033    import java.util.Date;
034    
035    /**
036     * Simple utility class to convert RRD files created with RRDTool 1.0.x to
037     * JRobin's native RRD format. Conversion process is quite fast.
038     */
039    public class Convertor {
040            private static final String FACTORY_NAME = "FILE";
041            private static final String SUFFIX = ".jrb";
042            private static final DecimalFormat secondsFormatter = new DecimalFormat("##0.000");
043            private static final DecimalFormat countFormatter = new DecimalFormat("0000");
044    
045            private String[] files;
046            private int totalCount, badCount, goodCount;
047    
048            private Convertor(String[] files) {
049                    try {
050                            RrdDb.setDefaultFactory(FACTORY_NAME);
051                    }
052                    catch (RrdException e) {
053                            e.printStackTrace();
054                            System.exit(-1);
055                    }
056                    this.files = files;
057            }
058    
059            private void convertAll() {
060                    Date t1 = new Date();
061                    final String ruler = "=======================================================================";
062                    println(ruler);
063                    println("Converting RRDTool files to JRobin native format.");
064                    println("Original RRDTool files will not be modified in any way");
065                    println("JRobin files created during the process will have a " + SUFFIX + " suffix");
066                    println(ruler);
067                    for (String file : files) {
068                            convertFile(file);
069                    }
070                    println(ruler);
071                    println("Finished: " + totalCount + " total, " +
072                                    goodCount + " OK, " + badCount + " failed");
073                    Date t2 = new Date();
074                    double secs = (t2.getTime() - t1.getTime()) / 1000.0;
075                    println("Conversion took " + secondsFormatter.format(secs) + " sec");
076                    if (totalCount > 0) {
077                            double avgSec = secs / totalCount;
078                            println("Average per-file conversion time: " + secondsFormatter.format(avgSec) + " sec");
079                    }
080            }
081    
082            private void convertFile(String path) {
083                    long start = System.currentTimeMillis();
084                    totalCount++;
085                    try {
086                            File rrdFile = new File(path);
087                            print(countFormatter.format(totalCount) + "/" + countFormatter.format(files.length) +
088                                            " " + rrdFile.getName() + " ");
089                            String sourcePath = rrdFile.getCanonicalPath();
090                            String destPath = sourcePath + SUFFIX;
091                            RrdDb rrd = new RrdDb(destPath, RrdDb.PREFIX_RRDTool + sourcePath);
092                            rrd.close();
093                            goodCount++;
094                            double seconds = (System.currentTimeMillis() - start) / 1000.0;
095                            println("[OK, " + secondsFormatter.format(seconds) + " sec]");
096                    }
097                    catch (Exception e) {
098                            badCount++;
099                            println("[" + e + "]");
100                    }
101            }
102    
103            private static void println(String msg) {
104                    System.out.println(msg);
105            }
106    
107            private static void print(String msg) {
108                    System.out.print(msg);
109            }
110    
111            /**
112             * <p>To convert RRD files created with RRDTool use the following syntax:</p>
113             * <pre>
114             * java -cp jrobin-{version} org.jrobin.convertor.Convert [path to RRD file(s)]
115             * <pre>
116             * <p>For example:</p>
117             * <pre>
118             * java -cp jrobin-{version} org.jrobin.convertor.Convert rrdtool/files/*.rrd
119             * </pre>
120             * <p>...and enjoy the show.</p>
121             *
122             * @param args
123             */
124            public static void main(String[] args) {
125                    if (args.length == 0) {
126                            println("Usage  : java -jar convertor.jar <RRD file pattern> ...");
127                            println("Example: java -jar convertor.jar files/*.rrd");
128                            System.exit(1);
129                    }
130                    Convertor c = new Convertor(args);
131                    c.convertAll();
132            }
133    }