1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
37
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
113
114
115
116
117
118
119
120
121
122
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 }