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.RrdException;
30  import org.jrobin.core.Sample;
31  import org.jrobin.core.Util;
32  
33  import java.io.IOException;
34  
35  class RrdUpdateCmd extends RrdToolCmd {
36  	String getCmdType() {
37  		return "update";
38  	}
39  
40  	Object execute() throws RrdException, IOException {
41  		String template = getOptionValue("t", "template");
42  		String[] dsNames = (template != null) ? new ColonSplitter(template).split() : null;
43  		String[] words = getRemainingWords();
44  		if (words.length < 3) {
45  			throw new RrdException("Insufficent number of parameters for rrdupdate command");
46  		}
47  		String path = words[1];
48  		RrdDb rrdDb = getRrdDbReference(path);
49  		try {
50  			if (dsNames != null) {
51  				// template specified, check datasource names
52  				for (String dsName : dsNames) {
53  					if (!rrdDb.containsDs(dsName)) {
54  						throw new RrdException("Invalid datasource name: " + dsName);
55  					}
56  				}
57  			}
58  			// parse update strings
59  			long timestamp = -1;
60  			for (int i = 2; i < words.length; i++) {
61  				String[] tokens = new ColonSplitter(words[i]).split();
62  				if (dsNames != null && dsNames.length + 1 != tokens.length) {
63  					throw new RrdException("Template requires " + dsNames.length + " values, " +
64  							(tokens.length - 1) + " value(s) found in: " + words[i]);
65  				}
66  				int dsCount = rrdDb.getHeader().getDsCount();
67  				if (dsNames == null && dsCount + 1 != tokens.length) {
68  					throw new RrdException("Expected " + dsCount + " values, " +
69  							(tokens.length - 1) + " value(s) found in: " + words[i]);
70  				}
71  				timestamp = Util.getTimestamp(tokens[0]);
72  				Sample sample = rrdDb.createSample(timestamp);
73  				for (int j = 1; j < tokens.length; j++) {
74  					if (dsNames == null) {
75  						sample.setValue(j - 1, parseDouble(tokens[j]));
76  					}
77  					else {
78  						sample.setValue(dsNames[j - 1], parseDouble(tokens[j]));
79  					}
80  				}
81  				sample.update();
82  			}
83  			return timestamp;
84  		}
85  		finally {
86  			releaseRrdDbReference(rrdDb);
87  		}
88  	}
89  }
90