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.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
52 for (String dsName : dsNames) {
53 if (!rrdDb.containsDs(dsName)) {
54 throw new RrdException("Invalid datasource name: " + dsName);
55 }
56 }
57 }
58
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