Committed by
Gerrit Code Review
[ONOS-4176] Implement influxdb retriever for querying metrics
Change-Id: Ia1f3fc4fb3c76fafd003320940b8fe16b039ddae
Showing
6 changed files
with
726 additions
and
0 deletions
... | @@ -21,5 +21,10 @@ | ... | @@ -21,5 +21,10 @@ |
21 | <bundle>mvn:${project.groupId}/onos-app-influxdb/${project.version}</bundle> | 21 | <bundle>mvn:${project.groupId}/onos-app-influxdb/${project.version}</bundle> |
22 | <bundle>wrap:mvn:com.izettle/metrics-influxdb/1.1.1$Bundle-SymbolicName=metrics-influxdb&Bundle-Version=1.1.1</bundle> | 22 | <bundle>wrap:mvn:com.izettle/metrics-influxdb/1.1.1$Bundle-SymbolicName=metrics-influxdb&Bundle-Version=1.1.1</bundle> |
23 | <bundle>mvn:commons-codec/commons-codec/1.10</bundle> | 23 | <bundle>mvn:commons-codec/commons-codec/1.10</bundle> |
24 | + <bundle>wrap:mvn:org.influxdb/influxdb-java/2.1$Bundle-SymbolicName=influxdb-java&Bundle-Version=2.1</bundle> | ||
25 | + <bundle>wrap:mvn:com.squareup.retrofit/retrofit/1.9.0$Bundle-SymbolicName=retrofit&Bundle-Version=1.9.0</bundle> | ||
26 | + <bundle>wrap:mvn:com.squareup.okhttp/okhttp/2.4.0$Bundle-SymbolicName=okhttp&Bundle-Version=2.4.0</bundle> | ||
27 | + <bundle>wrap:mvn:com.squareup.okio/okio/1.4.0$Bundle-SymbolicName=okio&Bundle-Version=1.4.0</bundle> | ||
28 | + <bundle>mvn:com.google.code.gson/gson/2.3.1</bundle> | ||
24 | </feature> | 29 | </feature> |
25 | </features> | 30 | </features> | ... | ... |
... | @@ -71,6 +71,11 @@ | ... | @@ -71,6 +71,11 @@ |
71 | <version>1.1.1</version> | 71 | <version>1.1.1</version> |
72 | </dependency> | 72 | </dependency> |
73 | <dependency> | 73 | <dependency> |
74 | + <groupId>org.influxdb</groupId> | ||
75 | + <artifactId>influxdb-java</artifactId> | ||
76 | + <version>2.1</version> | ||
77 | + </dependency> | ||
78 | + <dependency> | ||
74 | <groupId>org.onosproject</groupId> | 79 | <groupId>org.onosproject</groupId> |
75 | <artifactId>onos-api</artifactId> | 80 | <artifactId>onos-api</artifactId> |
76 | <version>${project.version}</version> | 81 | <version>${project.version}</version> | ... | ... |
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.influxdbmetrics; | ||
17 | + | ||
18 | +import com.google.common.collect.BiMap; | ||
19 | +import com.google.common.collect.EnumHashBiMap; | ||
20 | +import com.google.common.collect.Lists; | ||
21 | +import com.google.common.collect.Maps; | ||
22 | +import com.google.common.collect.Sets; | ||
23 | +import org.apache.commons.lang.StringUtils; | ||
24 | +import org.apache.felix.scr.annotations.Activate; | ||
25 | +import org.apache.felix.scr.annotations.Component; | ||
26 | +import org.apache.felix.scr.annotations.Deactivate; | ||
27 | +import org.apache.felix.scr.annotations.Modified; | ||
28 | +import org.apache.felix.scr.annotations.Property; | ||
29 | +import org.apache.felix.scr.annotations.Reference; | ||
30 | +import org.apache.felix.scr.annotations.ReferenceCardinality; | ||
31 | +import org.apache.felix.scr.annotations.Service; | ||
32 | +import org.influxdb.InfluxDB; | ||
33 | +import org.influxdb.InfluxDBFactory; | ||
34 | +import org.influxdb.dto.Query; | ||
35 | +import org.influxdb.dto.QueryResult; | ||
36 | +import org.onlab.util.Tools; | ||
37 | +import org.onosproject.cfg.ComponentConfigService; | ||
38 | +import org.onosproject.cluster.NodeId; | ||
39 | +import org.onosproject.core.CoreService; | ||
40 | +import org.osgi.service.component.ComponentContext; | ||
41 | +import org.slf4j.Logger; | ||
42 | + | ||
43 | +import java.util.Dictionary; | ||
44 | +import java.util.List; | ||
45 | +import java.util.Map; | ||
46 | +import java.util.Set; | ||
47 | +import java.util.concurrent.TimeUnit; | ||
48 | + | ||
49 | +import static org.slf4j.LoggerFactory.getLogger; | ||
50 | + | ||
51 | +/** | ||
52 | + * A Metric retriever implementation for querying metrics from influxDB server. | ||
53 | + */ | ||
54 | +@Component(immediate = true) | ||
55 | +@Service | ||
56 | +public class DefaultInfluxDbMetricsRetriever implements InfluxDbMetricsRetriever { | ||
57 | + | ||
58 | + private final Logger log = getLogger(getClass()); | ||
59 | + | ||
60 | + private static final String DEFAULT_PROTOCOL = "http"; | ||
61 | + private static final String DEFAULT_ADDRESS = "localhost"; | ||
62 | + private static final int DEFAULT_PORT = 8086; | ||
63 | + private static final String DEFAULT_DATABASE = "onos"; | ||
64 | + private static final String DEFAULT_USERNAME = "onos"; | ||
65 | + private static final String DEFAULT_PASSWORD = "onos.password"; | ||
66 | + private static final String DEFAULT_POLICY = "default"; | ||
67 | + private static final String COLON_SEPARATOR = ":"; | ||
68 | + private static final String SLASH_SEPARATOR = "//"; | ||
69 | + private static final String BRACKET_START = "["; | ||
70 | + private static final String BRACKET_END = "]"; | ||
71 | + private static final String METRIC_DELIMITER = "."; | ||
72 | + private static final int NUB_OF_DELIMITER = 3; | ||
73 | + | ||
74 | + private static final BiMap<TimeUnit, String> TIME_UNIT_MAP = | ||
75 | + EnumHashBiMap.create(TimeUnit.class); | ||
76 | + | ||
77 | + static { | ||
78 | + // key is TimeUnit enumeration type | ||
79 | + // value is influx database time unit keyword | ||
80 | + TIME_UNIT_MAP.put(TimeUnit.DAYS, "d"); | ||
81 | + TIME_UNIT_MAP.put(TimeUnit.HOURS, "h"); | ||
82 | + TIME_UNIT_MAP.put(TimeUnit.MINUTES, "m"); | ||
83 | + TIME_UNIT_MAP.put(TimeUnit.SECONDS, "s"); | ||
84 | + } | ||
85 | + | ||
86 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
87 | + protected CoreService coreService; | ||
88 | + | ||
89 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
90 | + protected ComponentConfigService cfgService; | ||
91 | + | ||
92 | + @Property(name = "address", value = DEFAULT_ADDRESS, | ||
93 | + label = "IP address of influxDB server; " + | ||
94 | + "default is localhost") | ||
95 | + protected String address = DEFAULT_ADDRESS; | ||
96 | + | ||
97 | + @Property(name = "port", intValue = DEFAULT_PORT, | ||
98 | + label = "Port number of influxDB server; " + | ||
99 | + "default is 8086") | ||
100 | + protected int port = DEFAULT_PORT; | ||
101 | + | ||
102 | + @Property(name = "database", value = DEFAULT_DATABASE, | ||
103 | + label = "Database name of influxDB server; " + | ||
104 | + "default is onos") | ||
105 | + protected String database = DEFAULT_DATABASE; | ||
106 | + | ||
107 | + @Property(name = "username", value = DEFAULT_USERNAME, | ||
108 | + label = "Username of influxDB server; default is onos") | ||
109 | + protected String username = DEFAULT_USERNAME; | ||
110 | + | ||
111 | + @Property(name = "password", value = DEFAULT_PASSWORD, | ||
112 | + label = "Password of influxDB server; default is onos.password") | ||
113 | + protected String password = DEFAULT_PASSWORD; | ||
114 | + | ||
115 | + InfluxDB influxDB; | ||
116 | + | ||
117 | + @Activate | ||
118 | + public void activate() { | ||
119 | + cfgService.registerProperties(getClass()); | ||
120 | + coreService.registerApplication("org.onosproject.influxdbmetrics"); | ||
121 | + | ||
122 | + config(); | ||
123 | + | ||
124 | + log.info("Started"); | ||
125 | + } | ||
126 | + | ||
127 | + @Deactivate | ||
128 | + public void deactivate() { | ||
129 | + cfgService.unregisterProperties(getClass(), false); | ||
130 | + | ||
131 | + log.info("Stopped"); | ||
132 | + } | ||
133 | + | ||
134 | + @Modified | ||
135 | + public void modified(ComponentContext context) { | ||
136 | + readComponentConfiguration(context); | ||
137 | + config(); | ||
138 | + } | ||
139 | + | ||
140 | + private void config() { | ||
141 | + StringBuilder url = new StringBuilder(); | ||
142 | + url.append(DEFAULT_PROTOCOL); | ||
143 | + url.append(COLON_SEPARATOR + SLASH_SEPARATOR); | ||
144 | + url.append(address); | ||
145 | + url.append(COLON_SEPARATOR); | ||
146 | + url.append(port); | ||
147 | + | ||
148 | + influxDB = InfluxDBFactory.connect(url.toString(), username, password); | ||
149 | + } | ||
150 | + | ||
151 | + @Override | ||
152 | + public Map<NodeId, Map<String, List<InfluxMetric>>> allMetrics(int period, | ||
153 | + TimeUnit unit) { | ||
154 | + Map<NodeId, Set<String>> nameMap = allMetricNames(); | ||
155 | + Map<NodeId, Map<String, List<InfluxMetric>>> metricsMap = Maps.newHashMap(); | ||
156 | + | ||
157 | + nameMap.forEach((nodeId, metricNames) -> | ||
158 | + metricsMap.putIfAbsent(nodeId, metricsByNodeId(nodeId, period, unit)) | ||
159 | + ); | ||
160 | + | ||
161 | + return metricsMap; | ||
162 | + } | ||
163 | + | ||
164 | + @Override | ||
165 | + public Map<String, List<InfluxMetric>> metricsByNodeId(NodeId nodeId, int period, | ||
166 | + TimeUnit unit) { | ||
167 | + Map<NodeId, Set<String>> nameMap = allMetricNames(); | ||
168 | + Map<String, List<InfluxMetric>> map = Maps.newHashMap(); | ||
169 | + | ||
170 | + nameMap.get(nodeId).forEach(metricName -> { | ||
171 | + List<InfluxMetric> value = metric(nodeId, metricName, period, unit); | ||
172 | + if (value != null) { | ||
173 | + map.putIfAbsent(metricName, value); | ||
174 | + } | ||
175 | + }); | ||
176 | + | ||
177 | + return map; | ||
178 | + } | ||
179 | + | ||
180 | + @Override | ||
181 | + public Map<NodeId, List<InfluxMetric>> metricsByName(String metricName, int period, | ||
182 | + TimeUnit unit) { | ||
183 | + Map<NodeId, List<InfluxMetric>> map = Maps.newHashMap(); | ||
184 | + List<InfluxMetric> metrics = Lists.newArrayList(); | ||
185 | + String queryPrefix = new StringBuilder() | ||
186 | + .append("SELECT m1_rate FROM") | ||
187 | + .append(database) | ||
188 | + .append(METRIC_DELIMITER) | ||
189 | + .append(quote(DEFAULT_POLICY)) | ||
190 | + .append(METRIC_DELIMITER) | ||
191 | + .toString(); | ||
192 | + String querySuffix = new StringBuilder() | ||
193 | + .append(" WHERE time > now() - ") | ||
194 | + .append(period) | ||
195 | + .append(unitString(unit)) | ||
196 | + .toString(); | ||
197 | + | ||
198 | + allMetricNames().keySet().forEach(nodeId -> { | ||
199 | + String queryString = new StringBuilder() | ||
200 | + .append(queryPrefix) | ||
201 | + .append(quote(nodeId + METRIC_DELIMITER + metricName)) | ||
202 | + .append(querySuffix) | ||
203 | + .toString(); | ||
204 | + Query query = new Query(queryString, database); | ||
205 | + List<QueryResult.Result> results = influxDB.query(query).getResults(); | ||
206 | + | ||
207 | + if (results != null && results.get(0) != null | ||
208 | + && results.get(0).getSeries() != null) { | ||
209 | + | ||
210 | + results.get(0).getSeries().get(0).getValues().forEach(value -> | ||
211 | + metrics.add(new DefaultInfluxMetric.Builder() | ||
212 | + .time((String) value.get(0)) | ||
213 | + .oneMinRate((Double) value.get(1)) | ||
214 | + .build())); | ||
215 | + map.putIfAbsent(nodeId, metrics); | ||
216 | + } | ||
217 | + }); | ||
218 | + | ||
219 | + return map; | ||
220 | + } | ||
221 | + | ||
222 | + @Override | ||
223 | + public List<InfluxMetric> metric(NodeId nodeId, String metricName, | ||
224 | + int period, TimeUnit unit) { | ||
225 | + List<InfluxMetric> metrics = Lists.newArrayList(); | ||
226 | + String queryString = new StringBuilder() | ||
227 | + .append("SELECT m1_rate FROM ") | ||
228 | + .append(database) | ||
229 | + .append(METRIC_DELIMITER) | ||
230 | + .append(quote(DEFAULT_POLICY)) | ||
231 | + .append(METRIC_DELIMITER) | ||
232 | + .append(quote(nodeId + METRIC_DELIMITER + metricName)) | ||
233 | + .append(" WHERE time > now() - ") | ||
234 | + .append(period) | ||
235 | + .append(unitString(unit)) | ||
236 | + .toString(); | ||
237 | + | ||
238 | + Query query = new Query(queryString, database); | ||
239 | + List<QueryResult.Result> results = influxDB.query(query).getResults(); | ||
240 | + | ||
241 | + if (results != null && results.get(0) != null | ||
242 | + && results.get(0).getSeries() != null) { | ||
243 | + | ||
244 | + results.get(0).getSeries().get(0).getValues().forEach(value -> | ||
245 | + metrics.add(new DefaultInfluxMetric.Builder() | ||
246 | + .time((String) value.get(0)) | ||
247 | + .oneMinRate((Double) value.get(1)) | ||
248 | + .build())); | ||
249 | + return metrics; | ||
250 | + } | ||
251 | + | ||
252 | + return null; | ||
253 | + } | ||
254 | + | ||
255 | + @Override | ||
256 | + public Map<NodeId, Map<String, InfluxMetric>> allMetrics() { | ||
257 | + Map<NodeId, Set<String>> nameMap = allMetricNames(); | ||
258 | + Map<NodeId, Map<String, InfluxMetric>> metricsMap = Maps.newHashMap(); | ||
259 | + | ||
260 | + nameMap.forEach((nodeId, metricNames) -> | ||
261 | + metricsMap.putIfAbsent(nodeId, metricsByNodeId(nodeId)) | ||
262 | + ); | ||
263 | + | ||
264 | + return metricsMap; | ||
265 | + } | ||
266 | + | ||
267 | + @Override | ||
268 | + public Map<String, InfluxMetric> metricsByNodeId(NodeId nodeId) { | ||
269 | + Map<NodeId, Set<String>> nameMap = allMetricNames(); | ||
270 | + Map<String, InfluxMetric> map = Maps.newHashMap(); | ||
271 | + | ||
272 | + nameMap.get(nodeId).forEach(metricName -> { | ||
273 | + InfluxMetric value = metric(nodeId, metricName); | ||
274 | + if (value != null) { | ||
275 | + map.putIfAbsent(metricName, value); | ||
276 | + } | ||
277 | + }); | ||
278 | + | ||
279 | + return map; | ||
280 | + } | ||
281 | + | ||
282 | + @Override | ||
283 | + public Map<NodeId, InfluxMetric> metricsByName(String metricName) { | ||
284 | + Map<NodeId, InfluxMetric> map = Maps.newHashMap(); | ||
285 | + String queryPrefix = new StringBuilder() | ||
286 | + .append("SELECT m1_rate FROM") | ||
287 | + .append(database) | ||
288 | + .append(METRIC_DELIMITER) | ||
289 | + .append(quote(DEFAULT_POLICY)) | ||
290 | + .append(METRIC_DELIMITER) | ||
291 | + .toString(); | ||
292 | + String querySuffix = new StringBuilder() | ||
293 | + .append(" LIMIT 1") | ||
294 | + .toString(); | ||
295 | + | ||
296 | + allMetricNames().keySet().forEach(nodeId -> { | ||
297 | + String queryString = new StringBuilder() | ||
298 | + .append(queryPrefix) | ||
299 | + .append(quote(nodeId + METRIC_DELIMITER + metricName)) | ||
300 | + .append(querySuffix) | ||
301 | + .toString(); | ||
302 | + Query query = new Query(queryString, database); | ||
303 | + List<QueryResult.Result> results = influxDB.query(query).getResults(); | ||
304 | + | ||
305 | + if (results != null && results.get(0) != null | ||
306 | + && results.get(0).getSeries() != null) { | ||
307 | + InfluxMetric metric = new DefaultInfluxMetric.Builder() | ||
308 | + .time((String) results.get(0).getSeries().get(0).getValues().get(0).get(0)) | ||
309 | + .oneMinRate((Double) results.get(0).getSeries().get(0) | ||
310 | + .getValues().get(0).get(1)).build(); | ||
311 | + map.putIfAbsent(nodeId, metric); | ||
312 | + } | ||
313 | + }); | ||
314 | + | ||
315 | + return map; | ||
316 | + } | ||
317 | + | ||
318 | + @Override | ||
319 | + public InfluxMetric metric(NodeId nodeId, String metricName) { | ||
320 | + String queryString = new StringBuilder() | ||
321 | + .append("SELECT m1_rate FROM ") | ||
322 | + .append(database) | ||
323 | + .append(METRIC_DELIMITER) | ||
324 | + .append(quote(DEFAULT_POLICY)) | ||
325 | + .append(METRIC_DELIMITER) | ||
326 | + .append(quote(nodeId + METRIC_DELIMITER + metricName)) | ||
327 | + .append(" LIMIT 1") | ||
328 | + .toString(); | ||
329 | + | ||
330 | + Query query = new Query(queryString, database); | ||
331 | + List<QueryResult.Result> results = influxDB.query(query).getResults(); | ||
332 | + | ||
333 | + if (results != null && results.get(0) != null | ||
334 | + && results.get(0).getSeries() != null) { | ||
335 | + return new DefaultInfluxMetric.Builder() | ||
336 | + .time((String) results.get(0).getSeries().get(0).getValues().get(0).get(0)) | ||
337 | + .oneMinRate((Double) results.get(0).getSeries().get(0) | ||
338 | + .getValues().get(0).get(1)).build(); | ||
339 | + } | ||
340 | + | ||
341 | + return null; | ||
342 | + } | ||
343 | + | ||
344 | + private String unitString(TimeUnit unit) { | ||
345 | + return TIME_UNIT_MAP.get(unit) == null ? "h" : TIME_UNIT_MAP.get(unit); | ||
346 | + } | ||
347 | + | ||
348 | + private String quote(String str) { | ||
349 | + return "\"" + str + "\""; | ||
350 | + } | ||
351 | + | ||
352 | + /** | ||
353 | + * Returns all metric names that bound with node identification. | ||
354 | + * | ||
355 | + * @return all metric names | ||
356 | + */ | ||
357 | + protected Map<NodeId, Set<String>> allMetricNames() { | ||
358 | + Map<NodeId, Set<String>> metricNameMap = Maps.newHashMap(); | ||
359 | + Query query = new Query("SHOW MEASUREMENTS", database); | ||
360 | + List<QueryResult.Result> results = influxDB.query(query).getResults(); | ||
361 | + List<List<Object>> rawMetricNames = results.get(0).getSeries().get(0).getValues(); | ||
362 | + | ||
363 | + rawMetricNames.forEach(rawMetricName -> { | ||
364 | + String nodeIdStr = getNodeId(strip(rawMetricName.toString())); | ||
365 | + | ||
366 | + if (nodeIdStr != null) { | ||
367 | + NodeId nodeId = NodeId.nodeId(nodeIdStr); | ||
368 | + String metricName = getMetricName(strip(rawMetricName.toString())); | ||
369 | + | ||
370 | + if (!metricNameMap.containsKey(nodeId)) { | ||
371 | + metricNameMap.putIfAbsent(nodeId, Sets.newHashSet()); | ||
372 | + } | ||
373 | + | ||
374 | + if (metricName != null) { | ||
375 | + metricNameMap.get(nodeId).add(metricName); | ||
376 | + } | ||
377 | + } | ||
378 | + }); | ||
379 | + | ||
380 | + return metricNameMap; | ||
381 | + } | ||
382 | + | ||
383 | + /** | ||
384 | + * Strips special bracket from the full name. | ||
385 | + * | ||
386 | + * @param fullName full name | ||
387 | + * @return bracket stripped string | ||
388 | + */ | ||
389 | + private String strip(String fullName) { | ||
390 | + return StringUtils.strip(StringUtils.strip(fullName, BRACKET_START), BRACKET_END); | ||
391 | + } | ||
392 | + | ||
393 | + /** | ||
394 | + * Returns metric name from full name. | ||
395 | + * The elements in full name is split by using '.'; | ||
396 | + * We assume that the metric name always comes after the last three '.' | ||
397 | + * | ||
398 | + * @param fullName full name | ||
399 | + * @return metric name | ||
400 | + */ | ||
401 | + private String getMetricName(String fullName) { | ||
402 | + int index = StringUtils.lastOrdinalIndexOf(fullName, | ||
403 | + METRIC_DELIMITER, NUB_OF_DELIMITER); | ||
404 | + if (index != -1) { | ||
405 | + return StringUtils.substring(fullName, index + 1); | ||
406 | + } else { | ||
407 | + log.warn("Database {} contains malformed metric name.", database); | ||
408 | + return null; | ||
409 | + } | ||
410 | + } | ||
411 | + | ||
412 | + /** | ||
413 | + * Returns node id from full name. | ||
414 | + * The elements in full name is split by using '.'; | ||
415 | + * We assume that the node id always comes before the last three '.' | ||
416 | + * | ||
417 | + * @param fullName full name | ||
418 | + * @return node id | ||
419 | + */ | ||
420 | + private String getNodeId(String fullName) { | ||
421 | + int index = StringUtils.lastOrdinalIndexOf(fullName, | ||
422 | + METRIC_DELIMITER, NUB_OF_DELIMITER); | ||
423 | + if (index != -1) { | ||
424 | + return StringUtils.substring(fullName, 0, index); | ||
425 | + } else { | ||
426 | + log.warn("Database {} contains malformed node id.", database); | ||
427 | + return null; | ||
428 | + } | ||
429 | + } | ||
430 | + | ||
431 | + /** | ||
432 | + * Extracts properties from the component configuration context. | ||
433 | + * | ||
434 | + * @param context the component context | ||
435 | + */ | ||
436 | + private void readComponentConfiguration(ComponentContext context) { | ||
437 | + Dictionary<?, ?> properties = context.getProperties(); | ||
438 | + | ||
439 | + String addressStr = Tools.get(properties, "address"); | ||
440 | + address = addressStr != null ? addressStr : DEFAULT_ADDRESS; | ||
441 | + log.info("Configured. InfluxDB server address is {}", address); | ||
442 | + | ||
443 | + String databaseStr = Tools.get(properties, "database"); | ||
444 | + database = databaseStr != null ? databaseStr : DEFAULT_DATABASE; | ||
445 | + log.info("Configured. InfluxDB server database is {}", database); | ||
446 | + | ||
447 | + String usernameStr = Tools.get(properties, "username"); | ||
448 | + username = usernameStr != null ? usernameStr : DEFAULT_USERNAME; | ||
449 | + log.info("Configured. InfluxDB server username is {}", username); | ||
450 | + | ||
451 | + String passwordStr = Tools.get(properties, "password"); | ||
452 | + password = passwordStr != null ? passwordStr : DEFAULT_PASSWORD; | ||
453 | + log.info("Configured. InfluxDB server password is {}", password); | ||
454 | + | ||
455 | + Integer portConfigured = Tools.getIntegerProperty(properties, "port"); | ||
456 | + if (portConfigured == null) { | ||
457 | + port = DEFAULT_PORT; | ||
458 | + log.info("InfluxDB port is not configured, default value is {}", port); | ||
459 | + } else { | ||
460 | + port = portConfigured; | ||
461 | + log.info("Configured. InfluxDB port is configured to {}", port); | ||
462 | + } | ||
463 | + } | ||
464 | +} |
apps/influxdbmetrics/src/main/java/org/onosproject/influxdbmetrics/DefaultInfluxMetric.java
0 → 100644
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.influxdbmetrics; | ||
17 | + | ||
18 | +import org.apache.commons.lang.StringUtils; | ||
19 | +import org.joda.time.DateTime; | ||
20 | +import org.joda.time.format.DateTimeFormat; | ||
21 | +import org.joda.time.format.DateTimeFormatter; | ||
22 | + | ||
23 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
24 | + | ||
25 | +/** | ||
26 | + * Default implementation of influx metric. | ||
27 | + */ | ||
28 | +public final class DefaultInfluxMetric implements InfluxMetric { | ||
29 | + | ||
30 | + private double oneMinRate; | ||
31 | + private DateTime time; | ||
32 | + | ||
33 | + private DefaultInfluxMetric(double oneMinRate, DateTime time) { | ||
34 | + this.oneMinRate = oneMinRate; | ||
35 | + this.time = time; | ||
36 | + } | ||
37 | + | ||
38 | + @Override | ||
39 | + public double oneMinRate() { | ||
40 | + return oneMinRate; | ||
41 | + } | ||
42 | + | ||
43 | + @Override | ||
44 | + public DateTime time() { | ||
45 | + return time; | ||
46 | + } | ||
47 | + | ||
48 | + public static final class Builder implements InfluxMetric.Builder { | ||
49 | + | ||
50 | + private double oneMinRate; | ||
51 | + private String timestamp; | ||
52 | + private static final String TIMESTAMP_MSG = "Must specify a timestamp."; | ||
53 | + private static final String ONE_MIN_RATE_MSG = "Must specify one minute rate."; | ||
54 | + | ||
55 | + public Builder() { | ||
56 | + } | ||
57 | + | ||
58 | + @Override | ||
59 | + public InfluxMetric.Builder oneMinRate(double rate) { | ||
60 | + this.oneMinRate = rate; | ||
61 | + return this; | ||
62 | + } | ||
63 | + | ||
64 | + @Override | ||
65 | + public InfluxMetric.Builder time(String time) { | ||
66 | + this.timestamp = time; | ||
67 | + return this; | ||
68 | + } | ||
69 | + | ||
70 | + @Override | ||
71 | + public InfluxMetric build() { | ||
72 | + checkNotNull(oneMinRate, ONE_MIN_RATE_MSG); | ||
73 | + checkNotNull(timestamp, TIMESTAMP_MSG); | ||
74 | + | ||
75 | + return new DefaultInfluxMetric(oneMinRate, parseTime(timestamp)); | ||
76 | + } | ||
77 | + | ||
78 | + private DateTime parseTime(String time) { | ||
79 | + String reformatTime = StringUtils.replace(StringUtils.replace(time, "T", " "), "Z", ""); | ||
80 | + DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); | ||
81 | + return formatter.parseDateTime(reformatTime); | ||
82 | + } | ||
83 | + } | ||
84 | +} |
apps/influxdbmetrics/src/main/java/org/onosproject/influxdbmetrics/InfluxDbMetricsRetriever.java
0 → 100644
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.influxdbmetrics; | ||
17 | + | ||
18 | +import org.onosproject.cluster.NodeId; | ||
19 | + | ||
20 | +import java.util.List; | ||
21 | +import java.util.Map; | ||
22 | +import java.util.concurrent.TimeUnit; | ||
23 | + | ||
24 | +/** | ||
25 | + * A Metric retriever interface for querying metrics value from influxDB server. | ||
26 | + */ | ||
27 | +public interface InfluxDbMetricsRetriever { | ||
28 | + | ||
29 | + /** | ||
30 | + * Returns last metric values from all nodes. | ||
31 | + * | ||
32 | + * @return all metrics from all nodes | ||
33 | + */ | ||
34 | + Map<NodeId, Map<String, InfluxMetric>> allMetrics(); | ||
35 | + | ||
36 | + /** | ||
37 | + * Returns last metric values from a node. | ||
38 | + * | ||
39 | + * @param nodeId node identification | ||
40 | + * @return all metrics from a given node | ||
41 | + */ | ||
42 | + Map<String, InfluxMetric> metricsByNodeId(NodeId nodeId); | ||
43 | + | ||
44 | + /** | ||
45 | + * Returns a collection of last metric values from all nodes. | ||
46 | + * | ||
47 | + * @param metricName metric name | ||
48 | + * @return a collection of metrics from all nodes | ||
49 | + */ | ||
50 | + Map<NodeId, InfluxMetric> metricsByName(String metricName); | ||
51 | + | ||
52 | + /** | ||
53 | + * Returns a last metric value from a given node. | ||
54 | + * | ||
55 | + * @param nodeId node identification | ||
56 | + * @param metricName metric name | ||
57 | + * @return a metric value from a given node | ||
58 | + */ | ||
59 | + InfluxMetric metric(NodeId nodeId, String metricName); | ||
60 | + | ||
61 | + /** | ||
62 | + * Returns metric values of all nodes within a given period of time. | ||
63 | + * | ||
64 | + * @param period projected period | ||
65 | + * @param unit time unit | ||
66 | + * @return all metric values of all nodes | ||
67 | + */ | ||
68 | + Map<NodeId, Map<String, List<InfluxMetric>>> allMetrics(int period, TimeUnit unit); | ||
69 | + | ||
70 | + /** | ||
71 | + * Returns metric values of a node within a given period of time. | ||
72 | + * | ||
73 | + * @param nodeId node identification | ||
74 | + * @param period projected period | ||
75 | + * @param unit time unit | ||
76 | + * @return metric value of a node | ||
77 | + */ | ||
78 | + Map<String, List<InfluxMetric>> metricsByNodeId(NodeId nodeId, int period, TimeUnit unit); | ||
79 | + | ||
80 | + /** | ||
81 | + * Returns a collection of last metric values of all nodes within a given period of time. | ||
82 | + * | ||
83 | + * @param metricName metric name | ||
84 | + * @param period projected period | ||
85 | + * @param unit time unit | ||
86 | + * @return metric value of all nodes | ||
87 | + */ | ||
88 | + Map<NodeId, List<InfluxMetric>> metricsByName(String metricName, int period, TimeUnit unit); | ||
89 | + | ||
90 | + /** | ||
91 | + * Returns metric value of a given node within a given period of time. | ||
92 | + * | ||
93 | + * @param nodeId node identification | ||
94 | + * @param metricName metric name | ||
95 | + * @param period projected period | ||
96 | + * @param unit time unit | ||
97 | + * @return metric value of a node | ||
98 | + */ | ||
99 | + List<InfluxMetric> metric(NodeId nodeId, String metricName, int period, TimeUnit unit); | ||
100 | +} |
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.influxdbmetrics; | ||
17 | + | ||
18 | +import org.joda.time.DateTime; | ||
19 | + | ||
20 | +/** | ||
21 | + * Metric that represents all values queried from influx database. | ||
22 | + */ | ||
23 | +public interface InfluxMetric { | ||
24 | + | ||
25 | + /** | ||
26 | + * Returns one minute rate of the given metric. | ||
27 | + * | ||
28 | + * @return one minute rate of the given metric | ||
29 | + */ | ||
30 | + double oneMinRate(); | ||
31 | + | ||
32 | + | ||
33 | + /** | ||
34 | + * Returns collected timestamp of the given metric. | ||
35 | + * | ||
36 | + * @return collected timestamp of the given metric | ||
37 | + */ | ||
38 | + DateTime time(); | ||
39 | + | ||
40 | + /** | ||
41 | + * A builder of InfluxMetric. | ||
42 | + */ | ||
43 | + interface Builder { | ||
44 | + | ||
45 | + /** | ||
46 | + * Sets one minute rate. | ||
47 | + * | ||
48 | + * @param rate one minute rate | ||
49 | + * @return builder object | ||
50 | + */ | ||
51 | + Builder oneMinRate(double rate); | ||
52 | + | ||
53 | + /** | ||
54 | + * Sets collected timestamp. | ||
55 | + * | ||
56 | + * @param time timestamp | ||
57 | + * @return builder object | ||
58 | + */ | ||
59 | + Builder time(String time); | ||
60 | + | ||
61 | + /** | ||
62 | + * Builds a influx metric instance. | ||
63 | + * | ||
64 | + * @return influx metric instance | ||
65 | + */ | ||
66 | + InfluxMetric build(); | ||
67 | + } | ||
68 | +} |
-
Please register or login to post a comment