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.cmd;
27
28 import org.jrobin.core.RrdDb;
29 import org.jrobin.core.RrdDef;
30 import org.jrobin.core.RrdException;
31 import org.jrobin.core.Util;
32
33 import java.io.IOException;
34
35 class RrdCreateCmd extends RrdToolCmd {
36 static final String DEFAULT_START = "now-10s";
37 static final String DEFAULT_STEP = "300";
38
39 private RrdDef rrdDef;
40
41 String getCmdType() {
42 return "create";
43 }
44
45 Object execute() throws RrdException, IOException {
46 String startStr = getOptionValue("b", "start", DEFAULT_START);
47 long start = Util.getTimestamp(startStr);
48 String stepStr = getOptionValue("s", "step", DEFAULT_STEP);
49 long step = parseLong(stepStr);
50 String[] words = getRemainingWords();
51 if (words.length < 2) {
52 throw new RrdException("RRD file path not specified");
53 }
54 String path = words[1];
55 rrdDef = new RrdDef(path, start, step);
56 for (int i = 2; i < words.length; i++) {
57 if (words[i].startsWith("DS:")) {
58 parseDef(words[i]);
59 }
60 else if (words[i].startsWith("RRA:")) {
61 parseRra(words[i]);
62 }
63 else {
64 throw new RrdException("Invalid rrdcreate syntax: " + words[i]);
65 }
66 }
67 return createRrdDb();
68 }
69
70 private void parseDef(String word) throws RrdException {
71
72 String[] tokens = new ColonSplitter(word).split();
73 if (tokens.length < 6) {
74 throw new RrdException("Invalid DS definition: " + word);
75 }
76 String dsName = tokens[1];
77 String dsType = tokens[2];
78 long heartbeat = parseLong(tokens[3]);
79 double min = parseDouble(tokens[4]);
80 double max = parseDouble(tokens[5]);
81 rrdDef.addDatasource(dsName, dsType, heartbeat, min, max);
82 }
83
84 private void parseRra(String word) throws RrdException {
85
86 String[] tokens = new ColonSplitter(word).split();
87 if (tokens.length < 5) {
88 throw new RrdException("Invalid RRA definition: " + word);
89 }
90 String cf = tokens[1];
91 double xff = parseDouble(tokens[2]);
92 int steps = parseInt(tokens[3]);
93 int rows = parseInt(tokens[4]);
94 rrdDef.addArchive(cf, xff, steps, rows);
95 }
96
97 private String createRrdDb() throws IOException, RrdException {
98 RrdDb rrdDb = getRrdDbReference(rrdDef);
99 releaseRrdDbReference(rrdDb);
100 return rrdDef.getPath();
101 }
102 }