@cryptotaxi247 / netdata-1 / commits / 14c7c7206

added allmetrics API call to return JSON data; fixes #1910

Costa Tsaousis (ktsaou) committed Mar 9, 2017 at 00:35 UTC 14c7c7206eef181f536464bf4e206d568466728f
3 files changed +79 -1
src/rrd2json.c
+68
@@ -323,6 +323,74 @@ void rrd_stats_api_v1_charts_allmetrics_shell(RRDHOST *host, BUFFER *wb) {
323
324 // ----------------------------------------------------------------------------
325
326 +void rrd_stats_api_v1_charts_allmetrics_json(RRDHOST *host, BUFFER *wb) {
327 + rrdhost_rdlock(host);
328 +
329 + buffer_strcat(wb, "{");
330 +
331 + size_t chart_counter = 0;
332 + size_t dimension_counter = 0;
333 +
334 + // for each chart
335 + RRDSET *st;
336 + rrdset_foreach_read(st, host) {
337 + if(rrdset_is_available_for_viewers(st)) {
338 + rrdset_rdlock(st);
339 +
340 + buffer_sprintf(wb, "%s\n"
341 + "\t\"%s\": {\n"
342 + "\t\t\"name\":\"%s\",\n"
343 + "\t\t\"context\":\"%s\",\n"
344 + "\t\t\"units\":\"%s\",\n"
345 + "\t\t\"last_updated\": %ld,\n"
346 + "\t\t\"dimensions\": {"
347 + , chart_counter?",":""
348 + , st->id
349 + , st->name
350 + , st->context
351 + , st->units
352 + , rrdset_last_entry_t(st)
353 + );
354 +
355 + chart_counter++;
356 + dimension_counter = 0;
357 +
358 + // for each dimension
359 + RRDDIM *rd;
360 + rrddim_foreach_read(rd, st) {
361 + if(rd->collections_counter) {
362 +
363 + buffer_sprintf(wb, "%s\n"
364 + "\t\t\t\"%s\": {\n"
365 + "\t\t\t\t\"name\": \"%s\",\n"
366 + "\t\t\t\t\"value\": "
367 + , dimension_counter?",":""
368 + , rd->id
369 + , rd->name
370 + );
371 +
372 + if(isnan(rd->last_stored_value))
373 + buffer_strcat(wb, "null");
374 + else
375 + buffer_sprintf(wb, CALCULATED_NUMBER_FORMAT, rd->last_stored_value);
376 +
377 + buffer_strcat(wb, "\n\t\t\t}");
378 +
379 + dimension_counter++;
380 + }
381 + }
382 +
383 + buffer_strcat(wb, "\n\t\t}\n\t}");
384 + rrdset_unlock(st);
385 + }
386 + }
387 +
388 + buffer_strcat(wb, "\n}");
389 + rrdhost_unlock(host);
390 +}
391 +
392 +// ----------------------------------------------------------------------------
393 +
394 // RRDR dimension options
395 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
396 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
src/rrd2json.h
+3
@@ -33,9 +33,11 @@
33
34 #define ALLMETRICS_FORMAT_SHELL "shell"
35 #define ALLMETRICS_FORMAT_PROMETHEUS "prometheus"
36 +#define ALLMETRICS_FORMAT_JSON "json"
37
38 #define ALLMETRICS_SHELL 1
39 #define ALLMETRICS_PROMETHEUS 2
40 +#define ALLMETRICS_JSON 3
41
42 #define GROUP_UNDEFINED 0
43 #define GROUP_AVERAGE 1
@@ -61,6 +63,7 @@
63 extern void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb);
64 extern void rrd_stats_api_v1_charts(RRDHOST *host, BUFFER *wb);
65
66 +extern void rrd_stats_api_v1_charts_allmetrics_json(RRDHOST *host, BUFFER *wb);
67 extern void rrd_stats_api_v1_charts_allmetrics_shell(RRDHOST *host, BUFFER *wb);
68 extern void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER *wb);
69
src/web_api_v1.c
+8 -1
@@ -222,6 +222,8 @@ inline int web_client_api_request_v1_allmetrics(RRDHOST *host, struct web_client
222 format = ALLMETRICS_SHELL;
223 else if(!strcmp(value, ALLMETRICS_FORMAT_PROMETHEUS))
224 format = ALLMETRICS_PROMETHEUS;
225 + else if(!strcmp(value, ALLMETRICS_FORMAT_JSON))
226 + format = ALLMETRICS_JSON;
227 else
228 format = 0;
229 }
@@ -231,6 +233,11 @@ inline int web_client_api_request_v1_allmetrics(RRDHOST *host, struct web_client
233 buffer_no_cacheable(w->response.data);
234
235 switch(format) {
236 + case ALLMETRICS_JSON:
237 + w->response.data->contenttype = CT_APPLICATION_JSON;
238 + rrd_stats_api_v1_charts_allmetrics_json(host, w->response.data);
239 + return 200;
240 +
241 case ALLMETRICS_SHELL:
242 w->response.data->contenttype = CT_TEXT_PLAIN;
243 rrd_stats_api_v1_charts_allmetrics_shell(host, w->response.data);
@@ -243,7 +250,7 @@ inline int web_client_api_request_v1_allmetrics(RRDHOST *host, struct web_client
250
251 default:
252 w->response.data->contenttype = CT_TEXT_PLAIN;
246 - buffer_strcat(w->response.data, "Which format? Only '" ALLMETRICS_FORMAT_SHELL "' and '" ALLMETRICS_FORMAT_PROMETHEUS "' is currently supported.");
253 + buffer_strcat(w->response.data, "Which format? '" ALLMETRICS_FORMAT_SHELL "', '" ALLMETRICS_FORMAT_PROMETHEUS "' and '" ALLMETRICS_FORMAT_JSON "' are currently supported.");
254 return 400;
255 }
256 }