View Javadoc

1   /* ============================================================
2    * JRobin : Pure java implementation of RRDTool's functionality
3    * ============================================================
4    *
5    * Project Info:  http://www.jrobin.org
6    * Project Lead:  Sasa Markovic (saxon@jrobin.org);
7    *
8    * (C) Copyright 2003, by Sasa Markovic.
9    *
10   * Developers:    Sasa Markovic (saxon@jrobin.org)
11   *
12   *
13   * This library is free software; you can redistribute it and/or modify it under the terms
14   * of the GNU Lesser General Public License as published by the Free Software Foundation;
15   * either version 2.1 of the License, or (at your option) any later version.
16   *
17   * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18   * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19   * See the GNU Lesser General Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser General Public License along with this
22   * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23   * Boston, MA 02111-1307, USA.
24   */
25  
26  package org.jrobin.convertor;
27  
28  import org.jrobin.core.RrdDb;
29  import org.jrobin.core.RrdException;
30  
31  import java.io.*;
32  import java.text.DecimalFormat;
33  import java.util.Date;
34  
35  /**
36   * Simple utility class to convert RRD files created with RRDTool 1.0.x to
37   * JRobin's native RRD format. Conversion process is quite fast.
38   */
39  public class Convertor {
40  	private static final String FACTORY_NAME = "FILE";
41  	private static final String SUFFIX = ".jrb";
42  	private static final DecimalFormat secondsFormatter = new DecimalFormat("##0.000");
43  	private static final DecimalFormat countFormatter = new DecimalFormat("0000");
44  
45  	private String[] files;
46  	private int totalCount, badCount, goodCount;
47  
48  	private Convertor(String[] files) {
49  		try {
50  			RrdDb.setDefaultFactory(FACTORY_NAME);
51  		}
52  		catch (RrdException e) {
53  			e.printStackTrace();
54  			System.exit(-1);
55  		}
56  		this.files = files;
57  	}
58  
59  	private void convertAll() {
60  		Date t1 = new Date();
61  		final String ruler = "=======================================================================";
62  		println(ruler);
63  		println("Converting RRDTool files to JRobin native format.");
64  		println("Original RRDTool files will not be modified in any way");
65  		println("JRobin files created during the process will have a " + SUFFIX + " suffix");
66  		println(ruler);
67  		for (String file : files) {
68  			convertFile(file);
69  		}
70  		println(ruler);
71  		println("Finished: " + totalCount + " total, " +
72  				goodCount + " OK, " + badCount + " failed");
73  		Date t2 = new Date();
74  		double secs = (t2.getTime() - t1.getTime()) / 1000.0;
75  		println("Conversion took " + secondsFormatter.format(secs) + " sec");
76  		if (totalCount > 0) {
77  			double avgSec = secs / totalCount;
78  			println("Average per-file conversion time: " + secondsFormatter.format(avgSec) + " sec");
79  		}
80  	}
81  
82  	private void convertFile(String path) {
83  		long start = System.currentTimeMillis();
84  		totalCount++;
85  		try {
86  			File rrdFile = new File(path);
87  			print(countFormatter.format(totalCount) + "/" + countFormatter.format(files.length) +
88  					" " + rrdFile.getName() + " ");
89  			String sourcePath = rrdFile.getCanonicalPath();
90  			String destPath = sourcePath + SUFFIX;
91  			RrdDb rrd = new RrdDb(destPath, RrdDb.PREFIX_RRDTool + sourcePath);
92  			rrd.close();
93  			goodCount++;
94  			double seconds = (System.currentTimeMillis() - start) / 1000.0;
95  			println("[OK, " + secondsFormatter.format(seconds) + " sec]");
96  		}
97  		catch (Exception e) {
98  			badCount++;
99  			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 }