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-2005, 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.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  		// DEF:name:type:heratbeat:min:max
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  		// RRA:cfun:xff:steps:rows
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 }