/
RRDToolStore.java
150 lines (125 loc) · 3.87 KB
/
RRDToolStore.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package jrds.store;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import jrds.JrdsSample;
import jrds.Probe;
import jrds.factories.ProbeBean;
import org.apache.log4j.Level;
import org.rrd4j.core.jrrd.ConsolidationFunctionType;
import org.rrd4j.core.jrrd.DataChunk;
import org.rrd4j.core.jrrd.RRDatabase;
import org.rrd4j.data.DataProcessor;
import org.rrd4j.graph.RrdGraphDef;
@ProbeBean({"rrdfile"})
public class RRDToolStore extends AbstractStore<RRDatabase, DataChunk> {
public RRDToolStore(Probe<?, ?> p) {
super(p);
}
private File rrdpath;
public Boolean configure(File rrdpath) {
this.rrdpath = rrdpath;
try {
log(Level.TRACE, "rrd is %s", rrdpath.getCanonicalPath());
} catch (IOException e) {
}
return rrdpath.canRead();
}
public void setRrdfile(String rrdpath) {
this.rrdpath = new File(rrdpath);
}
public String getRrdfile() {
return getPath();
}
public String getPath() {
return rrdpath.getPath();
}
/* (non-Javadoc)
* @see jrds.Probe#checkStoreFile()
*/
@Override
public boolean checkStoreFile() {
return rrdpath.canRead();
}
/* (non-Javadoc)
* @see jrds.Probe#getLastUpdate()
*/
@Override
public Date getLastUpdate() {
try {
RRDatabase db = new RRDatabase(rrdpath);
return db.getLastUpdate();
} catch (IOException e) {
}
return new Date(0);
}
@Override
public void commit(JrdsSample sample) {
}
@Override
public Map<String, Number> getLastValues() {
return Collections.emptyMap();
}
@Override
public AbstractExtractor<DataChunk> getExtractor() {
final RRDatabase db;
try {
db = new RRDatabase(rrdpath);
} catch (IOException e1) {
return null;
}
return new AbstractExtractor<DataChunk>() {
@Override
public int getColumnCount() {
return db.getDataSourcesName().size();
}
@Override
protected int getSignature(ExtractInfo ei) {
return 0;
}
@Override
public void fill(RrdGraphDef gd, ExtractInfo ei) {
try {
DataChunk dc = db.getData(ConsolidationFunctionType.AVERAGE, ei.start, ei.end, ei.step);
for(Map.Entry<String, String> e: sources.entrySet()) {
gd.datasource(e.getKey(), dc.toPlottable(e.getValue()));
}
} catch (IOException e) {
throw new RuntimeException("Failed to access rrd file " + db.toString(), e);
}
}
@Override
public void fill(DataProcessor dp, ExtractInfo ei) {
try {
DataChunk dc = db.getData(ConsolidationFunctionType.AVERAGE, ei.start, ei.end, ei.step);
for(Map.Entry<String, String> e: sources.entrySet()) {
dp.addDatasource(e.getKey(), dc.toPlottable(e.getValue()));
}
} catch (IOException e) {
throw new RuntimeException("Failed to access rrd file " + RRDToolStore.this.getPath(), e);
}
}
@Override
public String getPath() {
return RRDToolStore.this.getPath();
}
};
}
@Override
public RRDatabase getStoreObject() {
try {
return new RRDatabase(rrdpath);
} catch (IOException e) {
return null;
}
}
@Override
public void closeStoreObject(Object object) {
try {
((RRDatabase) object).close();
} catch (IOException e) {
}
}
}