3 void rrd_stats_api_v1_chart_with_data(RRDSET *st, BUFFER *wb, size_t *dimensions_count, size_t *memory_used)
5 pthread_rwlock_rdlock(&st->rwlock);
9 "\t\t\t\"id\": \"%s\",\n"
10 "\t\t\t\"name\": \"%s\",\n"
11 "\t\t\t\"type\": \"%s\",\n"
12 "\t\t\t\"family\": \"%s\",\n"
13 "\t\t\t\"context\": \"%s\",\n"
14 "\t\t\t\"title\": \"%s\",\n"
15 "\t\t\t\"priority\": %ld,\n"
16 "\t\t\t\"enabled\": %s,\n"
17 "\t\t\t\"units\": \"%s\",\n"
18 "\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
19 "\t\t\t\"chart_type\": \"%s\",\n"
20 "\t\t\t\"duration\": %ld,\n"
21 "\t\t\t\"first_entry\": %ld,\n"
22 "\t\t\t\"last_entry\": %ld,\n"
23 "\t\t\t\"update_every\": %d,\n"
24 "\t\t\t\"dimensions\": {\n"
32 , st->enabled?"true":"false"
35 , rrdset_type_name(st->chart_type)
36 , st->entries * st->update_every
37 , rrdset_first_entry_t(st)
38 , rrdset_last_entry_t(st)
42 unsigned long memory = st->memsize;
44 size_t dimensions = 0;
46 for(rd = st->dimensions; rd ; rd = rd->next) {
47 if(rd->flags & RRDDIM_FLAG_HIDDEN) continue;
49 memory += rd->memsize;
53 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
62 if(dimensions_count) *dimensions_count += dimensions;
63 if(memory_used) *memory_used += memory;
65 buffer_strcat(wb, "\n\t\t\t},\n\t\t\t\"green\": ");
66 buffer_rrd_value(wb, st->green);
67 buffer_strcat(wb, ",\n\t\t\t\"red\": ");
68 buffer_rrd_value(wb, st->red);
74 pthread_rwlock_unlock(&st->rwlock);
77 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb) {
78 rrd_stats_api_v1_chart_with_data(st, wb, NULL, NULL);
81 void rrd_stats_api_v1_charts(BUFFER *wb)
83 size_t c, dimensions = 0, memory = 0, alarms = 0;
86 buffer_sprintf(wb, "{\n"
87 "\t\"hostname\": \"%s\""
88 ",\n\t\"update_every\": %d"
89 ",\n\t\"history\": %d"
93 , rrd_default_history_entries
96 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
97 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
98 if(st->enabled && st->dimensions) {
99 if(c) buffer_strcat(wb, ",");
100 buffer_strcat(wb, "\n\t\t\"");
101 buffer_strcat(wb, st->id);
102 buffer_strcat(wb, "\": ");
103 rrd_stats_api_v1_chart_with_data(st, wb, &dimensions, &memory);
109 for(rc = localhost.alarms; rc ; rc = rc->next) {
112 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
114 buffer_sprintf(wb, "\n\t}"
115 ",\n\t\"charts_count\": %zu"
116 ",\n\t\"dimensions_count\": %zu"
117 ",\n\t\"alarms_count\": %zu"
118 ",\n\t\"rrd_memory_bytes\": %zu"
127 // ----------------------------------------------------------------------------
129 // /api/v1/allmetrics?format=prometheus
131 static inline size_t prometheus_name_copy(char *d, const char *s, size_t usable) {
134 for(n = 0; *s && n < usable ; d++, s++, n++) {
135 register char c = *s;
137 if(unlikely(!isalnum(c))) *d = '_';
145 #define PROMETHEUS_ELEMENT_MAX 256
147 void rrd_stats_api_v1_charts_allmetrics_prometheus(BUFFER *wb)
149 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
151 char host[PROMETHEUS_ELEMENT_MAX + 1];
152 prometheus_name_copy(host, config_get("global", "hostname", "localhost"), PROMETHEUS_ELEMENT_MAX);
156 for(st = localhost.rrdset_root; st ; st = st->next) {
157 char chart[PROMETHEUS_ELEMENT_MAX + 1];
158 prometheus_name_copy(chart, st->id, PROMETHEUS_ELEMENT_MAX);
160 buffer_strcat(wb, "\n");
161 if(st->enabled && st->dimensions) {
162 pthread_rwlock_rdlock(&st->rwlock);
164 // for each dimension
166 for(rd = st->dimensions; rd ; rd = rd->next) {
168 char dimension[PROMETHEUS_ELEMENT_MAX + 1];
169 prometheus_name_copy(dimension, rd->id, PROMETHEUS_ELEMENT_MAX);
171 // buffer_sprintf(wb, "# HELP %s.%s %s\n", st->id, rd->id, st->units);
173 switch(rd->algorithm) {
174 case RRDDIM_INCREMENTAL:
175 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
176 buffer_sprintf(wb, "# TYPE %s_%s counter\n", chart, dimension);
180 buffer_sprintf(wb, "# TYPE %s_%s gauge\n", chart, dimension);
184 // calculated_number n = (calculated_number)rd->last_collected_value * (calculated_number)(abs(rd->multiplier)) / (calculated_number)(abs(rd->divisor));
185 // buffer_sprintf(wb, "%s.%s " CALCULATED_NUMBER_FORMAT " %llu\n", st->id, rd->id, n,
186 // (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
188 buffer_sprintf(wb, "%s_%s{instance=\"%s\"} " COLLECTED_NUMBER_FORMAT " %llu\n",
189 chart, dimension, host, rd->last_collected_value,
190 (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
195 pthread_rwlock_unlock(&st->rwlock);
199 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
202 // ----------------------------------------------------------------------------
204 // /api/v1/allmetrics?format=bash
206 static inline size_t shell_name_copy(char *d, const char *s, size_t usable) {
209 for(n = 0; *s && n < usable ; d++, s++, n++) {
210 register char c = *s;
212 if(unlikely(!isalnum(c))) *d = '_';
213 else *d = (char)toupper(c);
220 #define BASH_ELEMENT_MAX 100
222 void rrd_stats_api_v1_charts_allmetrics_shell(BUFFER *wb)
224 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
226 char host[BASH_ELEMENT_MAX + 1];
227 shell_name_copy(host, config_get("global", "hostname", "localhost"), BASH_ELEMENT_MAX);
231 for(st = localhost.rrdset_root; st ; st = st->next) {
232 calculated_number total = 0.0;
233 char chart[BASH_ELEMENT_MAX + 1];
234 shell_name_copy(chart, st->id, BASH_ELEMENT_MAX);
236 buffer_sprintf(wb, "\n# chart: %s (name: %s)\n", st->id, st->name);
237 if(st->enabled && st->dimensions) {
238 pthread_rwlock_rdlock(&st->rwlock);
240 // for each dimension
242 for(rd = st->dimensions; rd ; rd = rd->next) {
244 char dimension[BASH_ELEMENT_MAX + 1];
245 shell_name_copy(dimension, rd->id, BASH_ELEMENT_MAX);
247 calculated_number n = rd->last_stored_value;
249 if(isnan(n) || isinf(n))
250 buffer_sprintf(wb, "NETDATA_%s_%s=\"\" # %s\n", chart, dimension, st->units);
252 if(rd->multiplier < 0 || rd->divisor < 0) n = -n;
254 if(!(rd->flags & RRDDIM_FLAG_HIDDEN)) total += n;
255 buffer_sprintf(wb, "NETDATA_%s_%s=\"%0.0Lf\" # %s\n", chart, dimension, n, st->units);
260 total = roundl(total);
261 buffer_sprintf(wb, "NETDATA_%s_VISIBLETOTAL=\"%0.0Lf\" # %s\n", chart, total, st->units);
262 pthread_rwlock_unlock(&st->rwlock);
266 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
269 // ----------------------------------------------------------------------------
271 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
273 time_t now = now_realtime_sec();
275 pthread_rwlock_rdlock(&st->rwlock);
279 "\t\t\t\"id\": \"%s\",\n"
280 "\t\t\t\"name\": \"%s\",\n"
281 "\t\t\t\"type\": \"%s\",\n"
282 "\t\t\t\"family\": \"%s\",\n"
283 "\t\t\t\"context\": \"%s\",\n"
284 "\t\t\t\"title\": \"%s\",\n"
285 "\t\t\t\"priority\": %ld,\n"
286 "\t\t\t\"enabled\": %d,\n"
287 "\t\t\t\"units\": \"%s\",\n"
288 "\t\t\t\"url\": \"/data/%s/%s\",\n"
289 "\t\t\t\"chart_type\": \"%s\",\n"
290 "\t\t\t\"counter\": %lu,\n"
291 "\t\t\t\"entries\": %ld,\n"
292 "\t\t\t\"first_entry_t\": %ld,\n"
293 "\t\t\t\"last_entry\": %lu,\n"
294 "\t\t\t\"last_entry_t\": %ld,\n"
295 "\t\t\t\"last_entry_secs_ago\": %ld,\n"
296 "\t\t\t\"update_every\": %d,\n"
297 "\t\t\t\"isdetail\": %d,\n"
298 "\t\t\t\"usec_since_last_update\": %llu,\n"
299 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
300 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
301 "\t\t\t\"dimensions\": [\n"
311 , st->name, options?options:""
312 , rrdset_type_name(st->chart_type)
315 , rrdset_first_entry_t(st)
316 , rrdset_last_slot(st)
317 , rrdset_last_entry_t(st)
318 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
321 , st->usec_since_last_update
322 , st->collected_total
323 , st->last_collected_total
326 unsigned long memory = st->memsize;
329 for(rd = st->dimensions; rd ; rd = rd->next) {
331 memory += rd->memsize;
335 "\t\t\t\t\t\"id\": \"%s\",\n"
336 "\t\t\t\t\t\"name\": \"%s\",\n"
337 "\t\t\t\t\t\"entries\": %ld,\n"
338 "\t\t\t\t\t\"isHidden\": %d,\n"
339 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
340 "\t\t\t\t\t\"multiplier\": %ld,\n"
341 "\t\t\t\t\t\"divisor\": %ld,\n"
342 "\t\t\t\t\t\"last_entry_t\": %ld,\n"
343 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
344 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
345 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
346 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
347 "\t\t\t\t\t\"memory\": %lu\n"
352 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
353 , rrddim_algorithm_name(rd->algorithm)
356 , rd->last_collected_time.tv_sec
357 , rd->collected_value
358 , rd->calculated_value
359 , rd->last_collected_value
360 , rd->last_calculated_value
368 "\t\t\t\"memory\" : %lu\n"
373 pthread_rwlock_unlock(&st->rwlock);
377 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
378 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
380 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
382 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
383 rrd_stats_one_json(st, options, wb);
384 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
387 void rrd_stats_all_json(BUFFER *wb)
389 unsigned long memory = 0;
393 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
395 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
396 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
397 if(st->enabled && st->dimensions) {
398 if(c) buffer_strcat(wb, ",\n");
399 memory += rrd_stats_one_json(st, NULL, wb);
403 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
405 buffer_sprintf(wb, "\n\t],\n"
406 "\t\"hostname\": \"%s\",\n"
407 "\t\"update_every\": %d,\n"
408 "\t\"history\": %d,\n"
409 "\t\"memory\": %lu\n"
413 , rrd_default_history_entries
420 // ----------------------------------------------------------------------------
422 // RRDR dimension options
423 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
424 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
425 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
426 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
428 // RRDR result options
429 #define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
430 #define RRDR_RESULT_OPTION_RELATIVE 0x00000002
432 typedef struct rrdresult {
433 RRDSET *st; // the chart this result refers to
435 uint32_t result_options; // RRDR_RESULT_OPTION_*
437 int d; // the number of dimensions
438 long n; // the number of values in the arrays
439 long rows; // the number of rows used
441 uint8_t *od; // the options for the dimensions
443 time_t *t; // array of n timestamps
444 calculated_number *v; // array n x d values
445 uint8_t *o; // array n x d options
447 long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
449 long group; // how many collected values were grouped for each row
450 int update_every; // what is the suggested update frequency in seconds
452 calculated_number min;
453 calculated_number max;
458 int has_st_lock; // if st is read locked by us
461 #define rrdr_rows(r) ((r)->rows)
464 static void rrdr_dump(RRDR *r)
469 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
471 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
472 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
475 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
476 , (r->od[c] & RRDR_RESET)?"RESET ":""
477 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
478 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
483 fprintf(stderr, "RRDR does not have any values in it.\n");
487 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
489 // for each line in the array
490 for(i = 0; i < r->rows ;i++) {
491 calculated_number *cn = &r->v[ i * r->d ];
492 uint8_t *co = &r->o[ i * r->d ];
494 // print the id and the timestamp of the line
495 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
497 // for each dimension
498 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
499 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
500 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
502 if(co[c] & RRDR_EMPTY)
503 fprintf(stderr, "null ");
505 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
507 , (co[c] & RRDR_EMPTY)?"E":" "
508 , (co[c] & RRDR_RESET)?"R":" "
509 , (co[c] & RRDR_HIDDEN)?"H":" "
510 , (co[c] & RRDR_NONZERO)?"N":" "
514 fprintf(stderr, "\n");
519 void rrdr_disable_not_selected_dimensions(RRDR *r, const char *dims)
521 char b[strlen(dims) + 1];
528 // disable all of them
529 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
530 r->od[c] |= RRDR_HIDDEN;
532 while(o && *o && (tok = mystrsep(&o, ",|"))) {
535 uint32_t hash = simple_hash(tok);
537 // find it and enable it
538 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
539 if(unlikely((hash == d->hash && !strcmp(d->id, tok)) || !strcmp(d->name, tok))) {
540 r->od[c] &= ~RRDR_HIDDEN;
542 // since the user needs this dimension
543 // make it appear as NONZERO, to return it
544 // even if the dimension has only zeros
545 r->od[c] |= RRDR_NONZERO;
551 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
554 case DATASOURCE_JSON:
555 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
558 case DATASOURCE_DATATABLE_JSON:
559 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
562 case DATASOURCE_DATATABLE_JSONP:
563 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
566 case DATASOURCE_JSONP:
567 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
571 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
575 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
579 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
582 case DATASOURCE_HTML:
583 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
586 case DATASOURCE_JS_ARRAY:
587 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
590 case DATASOURCE_SSV_COMMA:
591 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
595 buffer_strcat(wb, "unknown");
600 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
602 if(options & RRDR_OPTION_NONZERO) {
606 // the caller wants specific dimensions
607 // disable NONZERO option
608 // to make sure we don't accidentally prevent
609 // the specific dimensions from being returned
613 // find how many dimensions are not zero
616 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
617 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
618 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
623 // if with nonzero we get i = 0 (no dimensions will be returned)
624 // disable nonzero to show all dimensions
625 if(!i) options &= ~RRDR_OPTION_NONZERO;
631 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
633 long rows = rrdr_rows(r);
637 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
638 char kq[2] = "", // key quote
639 sq[2] = ""; // string quote
641 if( options & RRDR_OPTION_GOOGLE_JSON ) {
650 buffer_sprintf(wb, "{\n"
653 " %sname%s: %s%s%s,\n"
654 " %sview_update_every%s: %d,\n"
655 " %supdate_every%s: %d,\n"
656 " %sfirst_entry%s: %u,\n"
657 " %slast_entry%s: %u,\n"
660 " %sdimension_names%s: ["
662 , kq, kq, sq, r->st->id, sq
663 , kq, kq, sq, r->st->name, sq
664 , kq, kq, r->update_every
665 , kq, kq, r->st->update_every
666 , kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
667 , kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
668 , kq, kq, (uint32_t)r->before
669 , kq, kq, (uint32_t)r->after
672 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
673 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
674 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
676 if(i) buffer_strcat(wb, ", ");
677 buffer_strcat(wb, sq);
678 buffer_strcat(wb, rd->name);
679 buffer_strcat(wb, sq);
683 #ifdef NETDATA_INTERNAL_CHECKS
684 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
687 buffer_strcat(wb, sq);
688 buffer_strcat(wb, "no data");
689 buffer_strcat(wb, sq);
692 buffer_sprintf(wb, "],\n"
693 " %sdimension_ids%s: ["
696 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
697 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
698 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
700 if(i) buffer_strcat(wb, ", ");
701 buffer_strcat(wb, sq);
702 buffer_strcat(wb, rd->id);
703 buffer_strcat(wb, sq);
708 buffer_strcat(wb, sq);
709 buffer_strcat(wb, "no data");
710 buffer_strcat(wb, sq);
713 buffer_sprintf(wb, "],\n"
714 " %slatest_values%s: ["
717 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
718 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
719 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
721 if(i) buffer_strcat(wb, ", ");
724 storage_number n = rd->values[rrdset_last_slot(r->st)];
726 if(!does_storage_number_exist(n))
727 buffer_strcat(wb, "null");
729 buffer_rrd_value(wb, unpack_storage_number(n));
733 buffer_strcat(wb, "null");
736 buffer_sprintf(wb, "],\n"
737 " %sview_latest_values%s: ["
742 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
743 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
744 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
746 if(i) buffer_strcat(wb, ", ");
749 calculated_number *cn = &r->v[ (0) * r->d ];
750 uint8_t *co = &r->o[ (0) * r->d ];
752 if(co[c] & RRDR_EMPTY)
753 buffer_strcat(wb, "null");
755 buffer_rrd_value(wb, cn[c]);
760 buffer_strcat(wb, "null");
763 buffer_sprintf(wb, "],\n"
764 " %sdimensions%s: %ld,\n"
765 " %spoints%s: %ld,\n"
772 rrdr_buffer_print_format(wb, format);
774 buffer_sprintf(wb, "%s,\n"
780 if(string_value) buffer_strcat(wb, sq);
781 //info("JSONWRAPPER(): %s: END", r->st->id);
784 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
788 char kq[2] = "", // key quote
789 sq[2] = ""; // string quote
791 if( options & RRDR_OPTION_GOOGLE_JSON ) {
800 if(string_value) buffer_strcat(wb, sq);
802 buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
803 buffer_rrd_value(wb, r->min);
804 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
805 buffer_rrd_value(wb, r->max);
806 buffer_strcat(wb, "\n}\n");
809 #define JSON_DATES_JS 1
810 #define JSON_DATES_TIMESTAMP 2
812 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
814 //info("RRD2JSON(): %s: BEGIN", r->st->id);
815 int row_annotations = 0, dates, dates_with_new = 0;
816 char kq[2] = "", // key quote
817 sq[2] = "", // string quote
818 pre_label[101] = "", // before each label
819 post_label[101] = "", // after each label
820 pre_date[101] = "", // the beginning of line, to the date
821 post_date[101] = "", // closing the date
822 pre_value[101] = "", // before each value
823 post_value[101] = "", // after each value
824 post_line[101] = "", // at the end of each row
825 normal_annotation[201] = "", // default row annotation
826 overflow_annotation[201] = "", // overflow row annotation
827 data_begin[101] = "", // between labels and values
828 finish[101] = ""; // at the end of everything
831 dates = JSON_DATES_JS;
832 if( options & RRDR_OPTION_GOOGLE_JSON ) {
841 snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
842 snprintfz(post_date, 100, "%s}", sq);
843 snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
844 snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
845 snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
846 strcpy(post_value, "}");
847 strcpy(post_line, "]}");
848 snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
849 strcpy(finish, "\n ]\n}");
851 snprintfz(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
852 snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
854 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
855 buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%stime%s,%spattern%s:%s%s,%stype%s:%sdatetime%s},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq);
856 buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%s%s,%spattern%s:%s%s,%stype%s:%sstring%s,%sp%s:{%srole%s:%sannotation%s}},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, kq, kq, sq, sq);
857 buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%s%s,%spattern%s:%s%s,%stype%s:%sstring%s,%sp%s:{%srole%s:%sannotationText%s}}", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, kq, kq, sq, sq);
859 // remove the valueobjects flag
860 // google wants its own keys
861 if(options & RRDR_OPTION_OBJECTSROWS)
862 options &= ~RRDR_OPTION_OBJECTSROWS;
867 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
868 dates = JSON_DATES_TIMESTAMP;
872 dates = JSON_DATES_JS;
875 if( options & RRDR_OPTION_OBJECTSROWS )
876 strcpy(pre_date, " { ");
878 strcpy(pre_date, " [ ");
879 strcpy(pre_label, ", \"");
880 strcpy(post_label, "\"");
881 strcpy(pre_value, ", ");
882 if( options & RRDR_OPTION_OBJECTSROWS )
883 strcpy(post_line, "}");
885 strcpy(post_line, "]");
886 snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
887 strcpy(finish, "\n ]\n}");
889 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
890 buffer_sprintf(wb, "%stime%s", sq, sq);
893 // -------------------------------------------------------------------------
894 // print the JSON header
899 // print the header lines
900 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
901 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
902 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
904 buffer_strcat(wb, pre_label);
905 buffer_strcat(wb, rd->name);
906 buffer_strcat(wb, post_label);
910 buffer_strcat(wb, pre_label);
911 buffer_strcat(wb, "no data");
912 buffer_strcat(wb, post_label);
915 // print the begin of row data
916 buffer_strcat(wb, data_begin);
918 // if all dimensions are hidden, print a null
920 buffer_strcat(wb, finish);
924 long start = 0, end = rrdr_rows(r), step = 1;
925 if((options & RRDR_OPTION_REVERSED)) {
926 start = rrdr_rows(r) - 1;
931 // for each line in the array
932 calculated_number total = 1;
933 for(i = start; i != end ;i += step) {
934 calculated_number *cn = &r->v[ i * r->d ];
935 uint8_t *co = &r->o[ i * r->d ];
937 time_t now = r->t[i];
939 if(dates == JSON_DATES_JS) {
940 // generate the local date time
941 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
942 if(!tm) { error("localtime_r() failed."); continue; }
944 if(likely(i != start)) buffer_strcat(wb, ",\n");
945 buffer_strcat(wb, pre_date);
947 if( options & RRDR_OPTION_OBJECTSROWS )
948 buffer_sprintf(wb, "%stime%s: ", kq, kq);
951 buffer_strcat(wb, "new ");
953 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
955 buffer_strcat(wb, post_date);
957 if(row_annotations) {
958 // google supports one annotation per row
959 int annotation_found = 0;
960 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
961 if(co[c] & RRDR_RESET) {
962 buffer_strcat(wb, overflow_annotation);
963 annotation_found = 1;
967 if(!annotation_found)
968 buffer_strcat(wb, normal_annotation);
972 // print the timestamp of the line
973 if(likely(i != start)) buffer_strcat(wb, ",\n");
974 buffer_strcat(wb, pre_date);
976 if( options & RRDR_OPTION_OBJECTSROWS )
977 buffer_sprintf(wb, "%stime%s: ", kq, kq);
979 buffer_rrd_value(wb, (calculated_number)r->t[i]);
981 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
983 buffer_strcat(wb, post_date);
986 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
988 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
989 calculated_number n = cn[c];
991 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
996 // prevent a division by zero
997 if(total == 0) total = 1;
1000 // for each dimension
1001 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
1002 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1003 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1005 calculated_number n = cn[c];
1007 buffer_strcat(wb, pre_value);
1009 if( options & RRDR_OPTION_OBJECTSROWS )
1010 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
1012 if(co[c] & RRDR_EMPTY) {
1013 if(options & RRDR_OPTION_NULL2ZERO)
1014 buffer_strcat(wb, "0");
1016 buffer_strcat(wb, "null");
1019 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1022 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1023 n = n * 100 / total;
1025 buffer_rrd_value(wb, n);
1028 buffer_strcat(wb, post_value);
1031 buffer_strcat(wb, post_line);
1034 buffer_strcat(wb, finish);
1035 //info("RRD2JSON(): %s: END", r->st->id);
1038 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
1040 //info("RRD2CSV(): %s: BEGIN", r->st->id);
1044 // print the csv header
1045 for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1046 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1047 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1050 buffer_strcat(wb, startline);
1051 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1052 buffer_strcat(wb, "time");
1053 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1055 buffer_strcat(wb, separator);
1056 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1057 buffer_strcat(wb, d->name);
1058 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1061 buffer_strcat(wb, endline);
1064 // no dimensions present
1068 long start = 0, end = rrdr_rows(r), step = 1;
1069 if((options & RRDR_OPTION_REVERSED)) {
1070 start = rrdr_rows(r) - 1;
1075 // for each line in the array
1076 calculated_number total = 1;
1077 for(i = start; i != end ;i += step) {
1078 calculated_number *cn = &r->v[ i * r->d ];
1079 uint8_t *co = &r->o[ i * r->d ];
1081 buffer_strcat(wb, betweenlines);
1082 buffer_strcat(wb, startline);
1084 time_t now = r->t[i];
1086 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
1087 // print the timestamp of the line
1088 buffer_rrd_value(wb, (calculated_number)now);
1090 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
1093 // generate the local date time
1094 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1095 if(!tm) { error("localtime() failed."); continue; }
1096 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1099 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1101 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1102 calculated_number n = cn[c];
1104 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1109 // prevent a division by zero
1110 if(total == 0) total = 1;
1113 // for each dimension
1114 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1115 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1116 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1118 buffer_strcat(wb, separator);
1120 calculated_number n = cn[c];
1122 if(co[c] & RRDR_EMPTY) {
1123 if(options & RRDR_OPTION_NULL2ZERO)
1124 buffer_strcat(wb, "0");
1126 buffer_strcat(wb, "null");
1129 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1132 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1133 n = n * 100 / total;
1135 buffer_rrd_value(wb, n);
1139 buffer_strcat(wb, endline);
1141 //info("RRD2CSV(): %s: END", r->st->id);
1144 inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
1148 calculated_number *cn = &r->v[ i * r->d ];
1149 uint8_t *co = &r->o[ i * r->d ];
1151 calculated_number sum = 0, min = 0, max = 0, v;
1152 int all_null = 1, init = 1;
1154 calculated_number total = 1;
1155 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1157 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1158 calculated_number n = cn[c];
1160 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1165 // prevent a division by zero
1166 if(total == 0) total = 1;
1169 // for each dimension
1170 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1171 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1172 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1174 calculated_number n = cn[c];
1176 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1179 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1180 n = n * 100 / total;
1182 if(unlikely(init)) {
1194 if(likely(!(co[c] & RRDR_EMPTY))) {
1199 if(n < min) min = n;
1200 if(n > max) max = n;
1203 if(unlikely(all_null)) {
1204 if(likely(all_values_are_null))
1205 *all_values_are_null = 1;
1209 if(likely(all_values_are_null))
1210 *all_values_are_null = 0;
1213 if(options & RRDR_OPTION_MIN2MAX)
1221 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
1223 //info("RRD2SSV(): %s: BEGIN", r->st->id);
1226 buffer_strcat(wb, prefix);
1227 long start = 0, end = rrdr_rows(r), step = 1;
1228 if((options & RRDR_OPTION_REVERSED)) {
1229 start = rrdr_rows(r) - 1;
1234 // for each line in the array
1235 for(i = start; i != end ;i += step) {
1236 int all_values_are_null = 0;
1237 calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
1239 if(likely(i != start)) {
1240 if(r->min > v) r->min = v;
1241 if(r->max < v) r->max = v;
1248 if(likely(i != start))
1249 buffer_strcat(wb, separator);
1251 if(all_values_are_null) {
1252 if(options & RRDR_OPTION_NULL2ZERO)
1253 buffer_strcat(wb, "0");
1255 buffer_strcat(wb, "null");
1258 buffer_rrd_value(wb, v);
1260 buffer_strcat(wb, suffix);
1261 //info("RRD2SSV(): %s: END", r->st->id);
1264 inline static calculated_number *rrdr_line_values(RRDR *r)
1266 return &r->v[ r->c * r->d ];
1269 inline static uint8_t *rrdr_line_options(RRDR *r)
1271 return &r->o[ r->c * r->d ];
1274 inline static int rrdr_line_init(RRDR *r, time_t t)
1278 if(unlikely(r->c >= r->n)) {
1279 error("requested to step above RRDR size for chart %s", r->st->name);
1289 inline static void rrdr_lock_rrdset(RRDR *r) {
1291 error("NULL value given!");
1295 pthread_rwlock_rdlock(&r->st->rwlock);
1299 inline static void rrdr_unlock_rrdset(RRDR *r) {
1301 error("NULL value given!");
1305 if(likely(r->has_st_lock)) {
1306 pthread_rwlock_unlock(&r->st->rwlock);
1311 inline static void rrdr_free(RRDR *r)
1314 error("NULL value given!");
1318 rrdr_unlock_rrdset(r);
1326 static inline void rrdr_done(RRDR *r)
1332 static RRDR *rrdr_create(RRDSET *st, long n)
1335 error("NULL value given!");
1339 RRDR *r = callocz(1, sizeof(RRDR));
1342 rrdr_lock_rrdset(r);
1345 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
1349 r->t = mallocz(n * sizeof(time_t));
1350 r->v = mallocz(n * r->d * sizeof(calculated_number));
1351 r->o = mallocz(n * r->d * sizeof(uint8_t));
1352 r->od = mallocz(r->d * sizeof(uint8_t));
1354 // set the hidden flag on hidden dimensions
1356 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1357 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] = RRDR_HIDDEN;
1363 r->update_every = 1;
1368 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
1370 int debug = st->debug;
1371 int absolute_period_requested = -1;
1373 time_t first_entry_t = rrdset_first_entry_t(st);
1374 time_t last_entry_t = rrdset_last_entry_t(st);
1376 if(before == 0 && after == 0) {
1377 // dump the all the data
1378 before = last_entry_t;
1379 after = first_entry_t;
1380 absolute_period_requested = 0;
1383 // allow relative for before (smaller than API_RELATIVE_TIME_MAX)
1384 if(((before < 0)?-before:before) <= API_RELATIVE_TIME_MAX) {
1385 if(abs(before) % st->update_every) {
1386 // make sure it is multiple of st->update_every
1387 if(before < 0) before = before - st->update_every - before % st->update_every;
1388 else before = before + st->update_every - before % st->update_every;
1390 if(before > 0) before = first_entry_t + before;
1391 else before = last_entry_t + before;
1392 absolute_period_requested = 0;
1395 // allow relative for after (smaller than API_RELATIVE_TIME_MAX)
1396 if(((after < 0)?-after:after) <= API_RELATIVE_TIME_MAX) {
1397 if(after == 0) after = -st->update_every;
1398 if(abs(after) % st->update_every) {
1399 // make sure it is multiple of st->update_every
1400 if(after < 0) after = after - st->update_every - after % st->update_every;
1401 else after = after + st->update_every - after % st->update_every;
1403 after = before + after;
1404 absolute_period_requested = 0;
1407 if(absolute_period_requested == -1)
1408 absolute_period_requested = 1;
1410 // make sure they are within our timeframe
1411 if(before > last_entry_t) before = last_entry_t;
1412 if(before < first_entry_t) before = first_entry_t;
1414 if(after > last_entry_t) after = last_entry_t;
1415 if(after < first_entry_t) after = first_entry_t;
1417 // check if they are upside down
1418 if(after > before) {
1419 time_t tmp = before;
1424 // the duration of the chart
1425 time_t duration = before - after;
1426 long available_points = duration / st->update_every;
1428 if(duration <= 0 || available_points <= 0)
1429 return rrdr_create(st, 1);
1431 // check the wanted points
1432 if(points < 0) points = -points;
1433 if(points > available_points) points = available_points;
1434 if(points == 0) points = available_points;
1436 // calculate proper grouping of source data
1437 long group = available_points / points;
1438 if(group <= 0) group = 1;
1440 // round group to the closest integer
1441 if(available_points % points > points / 2) group++;
1443 time_t after_new = (aligned) ? (after - (after % (group * st->update_every))) : after;
1444 time_t before_new = (aligned) ? (before - (before % (group * st->update_every))) : before;
1445 long points_new = (before_new - after_new) / st->update_every / group;
1447 // find the starting and ending slots in our round robin db
1448 long start_at_slot = rrdset_time2slot(st, before_new),
1449 stop_at_slot = rrdset_time2slot(st, after_new);
1451 #ifdef NETDATA_INTERNAL_CHECKS
1452 if(after_new < first_entry_t) {
1453 error("after_new %u is too small, minimum %u", (uint32_t)after_new, (uint32_t)first_entry_t);
1455 if(after_new > last_entry_t) {
1456 error("after_new %u is too big, maximum %u", (uint32_t)after_new, (uint32_t)last_entry_t);
1458 if(before_new < first_entry_t) {
1459 error("before_new %u is too small, minimum %u", (uint32_t)before_new, (uint32_t)first_entry_t);
1461 if(before_new > last_entry_t) {
1462 error("before_new %u is too big, maximum %u", (uint32_t)before_new, (uint32_t)last_entry_t);
1464 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1465 error("start_at_slot is invalid %ld, expected 0 to %ld", start_at_slot, st->entries - 1);
1467 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1468 error("stop_at_slot is invalid %ld, expected 0 to %ld", stop_at_slot, st->entries - 1);
1470 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1471 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1475 //info("RRD2RRDR(): %s: wanted %ld points, got %ld - group=%ld, wanted duration=%u, got %u - wanted %ld - %ld, got %ld - %ld", st->id, points, points_new, group, before - after, before_new - after_new, after, before, after_new, before_new);
1478 before = before_new;
1479 duration = before - after;
1480 points = points_new;
1483 // before = the end time of the calculation
1484 // after = the start time of the calculation
1485 // duration = the duration of the calculation
1486 // group = the number of source points to aggregate / group together
1487 // method = the method of grouping source points
1488 // points = the number of points to generate
1491 // -------------------------------------------------------------------------
1492 // initialize our result set
1494 RRDR *r = rrdr_create(st, points);
1496 #ifdef NETDATA_INTERNAL_CHECKS
1497 error("Cannot create RRDR for %s, after=%u, before=%u, duration=%u, points=%ld", st->id, (uint32_t)after, (uint32_t)before, (uint32_t)duration, points);
1502 #ifdef NETDATA_INTERNAL_CHECKS
1503 error("Returning empty RRDR (no dimensions in RRDSET) for %s, after=%u, before=%u, duration=%u, points=%ld", st->id, (uint32_t)after, (uint32_t)before, (uint32_t)duration, points);
1508 if(absolute_period_requested == 1)
1509 r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1511 r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1513 // find how many dimensions we have
1514 long dimensions = r->d;
1517 // -------------------------------------------------------------------------
1518 // checks for debugging
1520 if(debug) debug(D_RRD_STATS, "INFO %s first_t: %u, last_t: %u, all_duration: %u, after: %u, before: %u, duration: %u, points: %ld, group: %ld"
1522 , (uint32_t)first_entry_t
1523 , (uint32_t)last_entry_t
1524 , (uint32_t)(last_entry_t - first_entry_t)
1527 , (uint32_t)duration
1533 // -------------------------------------------------------------------------
1534 // temp arrays for keeping values per dimension
1536 calculated_number last_values[dimensions]; // keep the last value of each dimension
1537 calculated_number group_values[dimensions]; // keep sums when grouping
1538 long group_counts[dimensions]; // keep the number of values added to group_values
1539 uint8_t group_options[dimensions];
1540 uint8_t found_non_zero[dimensions];
1546 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1548 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1549 group_counts[c] = 0;
1550 group_options[c] = 0;
1551 found_non_zero[c] = 0;
1555 // -------------------------------------------------------------------------
1558 time_t now = rrdset_slot2time(st, start_at_slot),
1559 dt = st->update_every,
1562 if(unlikely(debug)) debug(D_RRD_STATS, "BEGIN %s after_t: %u (stop_at_t: %ld), before_t: %u (start_at_t: %ld), start_t(now): %u, current_entry: %ld, entries: %ld"
1574 r->update_every = group * st->update_every;
1578 //info("RRD2RRDR(): %s: STARTING", st->id);
1580 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1581 for(; !stop_now ; now -= dt, slot--, counter++) {
1582 if(unlikely(slot < 0)) slot = st->entries - 1;
1583 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1585 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %ld, %s %s"
1592 , (group_count + 1 == group)?"PRINT":" - "
1593 , (now >= after && now <= before)?"RANGE":" - "
1596 // make sure we return data in the proper time range
1597 if(unlikely(now > before)) continue;
1598 if(unlikely(now < after)) break;
1600 if(unlikely(group_count == 0)) {
1601 group_start_t = now;
1605 if(unlikely(group_count == group)) {
1606 if(unlikely(added >= points)) break;
1610 // do the calculations
1611 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1612 storage_number n = rd->values[slot];
1613 if(unlikely(!does_storage_number_exist(n))) continue;
1617 calculated_number value = unpack_storage_number(n);
1618 if(likely(value != 0.0)) {
1619 group_options[c] |= RRDR_NONZERO;
1620 found_non_zero[c] = 1;
1623 if(unlikely(did_storage_number_reset(n)))
1624 group_options[c] |= RRDR_RESET;
1626 switch(group_method) {
1628 if(unlikely(isnan(group_values[c])) ||
1629 fabsl(value) < fabsl(group_values[c]))
1630 group_values[c] = value;
1634 if(unlikely(isnan(group_values[c])) ||
1635 fabsl(value) > fabsl(group_values[c]))
1636 group_values[c] = value;
1642 case GROUP_UNDEFINED:
1643 group_values[c] += value;
1646 case GROUP_INCREMENTAL_SUM:
1647 if(unlikely(slot == start_at_slot))
1648 last_values[c] = value;
1650 group_values[c] += last_values[c] - value;
1651 last_values[c] = value;
1657 if(unlikely(add_this)) {
1658 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1662 calculated_number *cn = rrdr_line_values(r);
1663 uint8_t *co = rrdr_line_options(r);
1665 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1667 // update the dimension options
1668 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1670 // store the specific point options
1671 co[c] = group_options[c];
1674 if(unlikely(group_counts[c] == 0)) {
1676 co[c] |= RRDR_EMPTY;
1677 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1680 switch(group_method) {
1683 if(unlikely(isnan(group_values[c])))
1686 cn[c] = group_values[c];
1687 group_values[c] = NAN;
1692 case GROUP_INCREMENTAL_SUM:
1693 cn[c] = group_values[c];
1694 group_values[c] = 0;
1699 case GROUP_UNDEFINED:
1700 cn[c] = group_values[c] / group_counts[c];
1701 group_values[c] = 0;
1705 if(cn[c] < r->min) r->min = cn[c];
1706 if(cn[c] > r->max) r->max = cn[c];
1709 // reset for the next loop
1710 group_counts[c] = 0;
1711 group_options[c] = 0;
1721 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1722 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1726 int rrd2value(RRDSET *st, BUFFER *wb, calculated_number *n, const char *dimensions, long points, long long after, long long before, int group_method, uint32_t options, time_t *db_after, time_t *db_before, int *value_is_null)
1728 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1730 if(value_is_null) *value_is_null = 1;
1734 if(rrdr_rows(r) == 0) {
1737 if(db_after) *db_after = 0;
1738 if(db_before) *db_before = 0;
1739 if(value_is_null) *value_is_null = 1;
1744 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1745 buffer_no_cacheable(wb);
1746 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1747 buffer_cacheable(wb);
1749 options = rrdr_check_options(r, options, dimensions);
1752 rrdr_disable_not_selected_dimensions(r, dimensions);
1754 if(db_after) *db_after = r->after;
1755 if(db_before) *db_before = r->before;
1757 long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
1758 *n = rrdr2value(r, i, options, value_is_null);
1764 int rrd2format(RRDSET *st, BUFFER *wb, BUFFER *dimensions, uint32_t format, long points, long long after, long long before, int group_method, uint32_t options, time_t *latest_timestamp)
1766 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1768 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1772 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1773 buffer_no_cacheable(wb);
1774 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1775 buffer_cacheable(wb);
1777 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1780 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1782 if(latest_timestamp && rrdr_rows(r) > 0)
1783 *latest_timestamp = r->before;
1786 case DATASOURCE_SSV:
1787 if(options & RRDR_OPTION_JSON_WRAP) {
1788 wb->contenttype = CT_APPLICATION_JSON;
1789 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1790 rrdr2ssv(r, wb, options, "", " ", "");
1791 rrdr_json_wrapper_end(r, wb, format, options, 1);
1794 wb->contenttype = CT_TEXT_PLAIN;
1795 rrdr2ssv(r, wb, options, "", " ", "");
1799 case DATASOURCE_SSV_COMMA:
1800 if(options & RRDR_OPTION_JSON_WRAP) {
1801 wb->contenttype = CT_APPLICATION_JSON;
1802 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1803 rrdr2ssv(r, wb, options, "", ",", "");
1804 rrdr_json_wrapper_end(r, wb, format, options, 1);
1807 wb->contenttype = CT_TEXT_PLAIN;
1808 rrdr2ssv(r, wb, options, "", ",", "");
1812 case DATASOURCE_JS_ARRAY:
1813 if(options & RRDR_OPTION_JSON_WRAP) {
1814 wb->contenttype = CT_APPLICATION_JSON;
1815 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1816 rrdr2ssv(r, wb, options, "[", ",", "]");
1817 rrdr_json_wrapper_end(r, wb, format, options, 0);
1820 wb->contenttype = CT_APPLICATION_JSON;
1821 rrdr2ssv(r, wb, options, "[", ",", "]");
1825 case DATASOURCE_CSV:
1826 if(options & RRDR_OPTION_JSON_WRAP) {
1827 wb->contenttype = CT_APPLICATION_JSON;
1828 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1829 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1830 rrdr_json_wrapper_end(r, wb, format, options, 1);
1833 wb->contenttype = CT_TEXT_PLAIN;
1834 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1838 case DATASOURCE_CSV_JSON_ARRAY:
1839 wb->contenttype = CT_APPLICATION_JSON;
1840 if(options & RRDR_OPTION_JSON_WRAP) {
1841 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1842 buffer_strcat(wb, "[\n");
1843 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1844 buffer_strcat(wb, "\n]");
1845 rrdr_json_wrapper_end(r, wb, format, options, 0);
1848 wb->contenttype = CT_TEXT_PLAIN;
1849 buffer_strcat(wb, "[\n");
1850 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1851 buffer_strcat(wb, "\n]");
1855 case DATASOURCE_TSV:
1856 if(options & RRDR_OPTION_JSON_WRAP) {
1857 wb->contenttype = CT_APPLICATION_JSON;
1858 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1859 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1860 rrdr_json_wrapper_end(r, wb, format, options, 1);
1863 wb->contenttype = CT_TEXT_PLAIN;
1864 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1868 case DATASOURCE_HTML:
1869 if(options & RRDR_OPTION_JSON_WRAP) {
1870 wb->contenttype = CT_APPLICATION_JSON;
1871 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1872 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1873 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1874 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1875 rrdr_json_wrapper_end(r, wb, format, options, 1);
1878 wb->contenttype = CT_TEXT_HTML;
1879 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1880 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1881 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1885 case DATASOURCE_DATATABLE_JSONP:
1886 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1888 if(options & RRDR_OPTION_JSON_WRAP)
1889 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1891 rrdr2json(r, wb, options, 1);
1893 if(options & RRDR_OPTION_JSON_WRAP)
1894 rrdr_json_wrapper_end(r, wb, format, options, 0);
1897 case DATASOURCE_DATATABLE_JSON:
1898 wb->contenttype = CT_APPLICATION_JSON;
1900 if(options & RRDR_OPTION_JSON_WRAP)
1901 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1903 rrdr2json(r, wb, options, 1);
1905 if(options & RRDR_OPTION_JSON_WRAP)
1906 rrdr_json_wrapper_end(r, wb, format, options, 0);
1909 case DATASOURCE_JSONP:
1910 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1911 if(options & RRDR_OPTION_JSON_WRAP)
1912 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1914 rrdr2json(r, wb, options, 0);
1916 if(options & RRDR_OPTION_JSON_WRAP)
1917 rrdr_json_wrapper_end(r, wb, format, options, 0);
1920 case DATASOURCE_JSON:
1922 wb->contenttype = CT_APPLICATION_JSON;
1924 if(options & RRDR_OPTION_JSON_WRAP)
1925 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1927 rrdr2json(r, wb, options, 0);
1929 if(options & RRDR_OPTION_JSON_WRAP)
1930 rrdr_json_wrapper_end(r, wb, format, options, 0);
1938 time_t rrd_stats_json(int type, RRDSET *st, BUFFER *wb, long points, long group, int group_method, time_t after, time_t before, int only_non_zero)
1941 pthread_rwlock_rdlock(&st->rwlock);
1944 // -------------------------------------------------------------------------
1945 // switch from JSON to google JSON
1950 case DATASOURCE_DATATABLE_JSON:
1951 case DATASOURCE_DATATABLE_JSONP:
1956 case DATASOURCE_JSON:
1962 // -------------------------------------------------------------------------
1963 // validate the parameters
1965 if(points < 1) points = 1;
1966 if(group < 1) group = 1;
1968 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1969 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1973 // our return value (the last timestamp printed)
1974 // this is required to detect re-transmit in google JSONP
1975 time_t last_timestamp = 0;
1978 // -------------------------------------------------------------------------
1979 // find how many dimensions we have
1983 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1985 pthread_rwlock_unlock(&st->rwlock);
1986 buffer_strcat(wb, "No dimensions yet.");
1991 // -------------------------------------------------------------------------
1992 // prepare various strings, to speed up the loop
1994 char overflow_annotation[201]; snprintfz(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
1995 char normal_annotation[201]; snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1996 char pre_date[51]; snprintfz(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1997 char post_date[21]; snprintfz(post_date, 20, "%s}", sq);
1998 char pre_value[21]; snprintfz(pre_value, 20, ",{%sv%s:", kq, kq);
1999 char post_value[21]; strcpy(post_value, "}");
2002 // -------------------------------------------------------------------------
2003 // checks for debugging
2006 debug(D_RRD_STATS, "%s first_entry_t = %ld, last_entry_t = %ld, duration = %ld, after = %ld, before = %ld, duration = %ld, entries_to_show = %ld, group = %ld"
2008 , rrdset_first_entry_t(st)
2009 , rrdset_last_entry_t(st)
2010 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
2019 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%ld) is earlier than the oldest (%ld)", st->name, before, after);
2021 if((before - after) > st->entries * st->update_every)
2022 debug(D_RRD_STATS, "WARNING: %s The time difference between the oldest and the newest entries (%ld) is higher than the capacity of the database (%ld)", st->name, before - after, st->entries * st->update_every);
2026 // -------------------------------------------------------------------------
2027 // temp arrays for keeping values per dimension
2029 calculated_number group_values[dimensions]; // keep sums when grouping
2030 int print_hidden[dimensions]; // keep hidden flags
2031 int found_non_zero[dimensions];
2032 int found_non_existing[dimensions];
2035 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2036 group_values[c] = 0;
2037 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
2038 found_non_zero[c] = 0;
2039 found_non_existing[c] = 0;
2043 // error("OLD: points=%d after=%d before=%d group=%d, duration=%d", entries_to_show, before - (st->update_every * group * entries_to_show), before, group, before - after + 1);
2044 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
2045 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
2047 // -------------------------------------------------------------------------
2048 // remove dimensions that contain only zeros
2051 if(only_non_zero) max_loop = 2;
2053 for(; max_loop ; max_loop--) {
2055 // -------------------------------------------------------------------------
2056 // print the JSON header
2058 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
2059 buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%stime%s,%spattern%s:%s%s,%stype%s:%sdatetime%s},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq);
2060 buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%s%s,%spattern%s:%s%s,%stype%s:%sstring%s,%sp%s:{%srole%s:%sannotation%s}},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, kq, kq, sq, sq);
2061 buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%s%s,%spattern%s:%s%s,%stype%s:%sstring%s,%sp%s:{%srole%s:%sannotationText%s}}", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, kq, kq, sq, sq);
2063 // print the header for each dimension
2064 // and update the print_hidden array for the dimensions that should be hidden
2066 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2067 if(!print_hidden[c]) {
2069 buffer_sprintf(wb, ",\n {%sid%s:%s%s,%slabel%s:%s%s%s,%spattern%s:%s%s,%stype%s:%snumber%s}", kq, kq, sq, sq, kq, kq, sq, rd->name, sq, kq, kq, sq, sq, kq, kq, sq, sq);
2073 buffer_sprintf(wb, ",\n {%sid%s:%s%s,%slabel%s:%s%s%s,%spattern%s:%s%s,%stype%s:%snumber%s}", kq, kq, sq, sq, kq, kq, sq, "no data", sq, kq, kq, sq, sq, kq, kq, sq, sq);
2076 // print the begin of row data
2077 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
2080 // -------------------------------------------------------------------------
2083 int annotate_reset = 0;
2084 int annotation_count = 0;
2086 long t = rrdset_time2slot(st, before),
2087 stop_at_t = rrdset_time2slot(st, after),
2092 time_t now = rrdset_slot2time(st, t),
2093 dt = st->update_every;
2095 long count = 0, printed = 0, group_count = 0;
2098 if(st->debug) debug(D_RRD_STATS, "%s: REQUEST after:%u before:%u, points:%ld, group:%ld, CHART cur:%ld first: %u last:%u, CALC start_t:%ld, stop_t:%ld"
2105 , (uint32_t)rrdset_first_entry_t(st)
2106 , (uint32_t)rrdset_last_entry_t(st)
2112 for(; !stop_now ; now -= dt, t--, counter++) {
2113 if(t < 0) t = st->entries - 1;
2114 if(t == stop_at_t) stop_now = counter;
2118 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %ld, %s %s"
2125 , (group_count + 1 == group)?"PRINT":" - "
2126 , (now >= after && now <= before)?"RANGE":" - "
2130 // make sure we return data in the proper time range
2131 if(now > before) continue;
2132 if(now < after) break;
2134 //if(rrdset_slot2time(st, t) != now)
2135 // error("%s: slot=%ld, now=%ld, slot2time=%ld, diff=%ld, last_entry_t=%ld, rrdset_last_slot=%ld", st->id, t, now, rrdset_slot2time(st,t), now - rrdset_slot2time(st,t), rrdset_last_entry_t(st), rrdset_last_slot(st));
2140 // check if we have to print this now
2141 if(group_count == group) {
2142 if(printed >= points) {
2143 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
2147 // generate the local date time
2148 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
2149 if(!tm) { error("localtime() failed."); continue; }
2150 if(now > last_timestamp) last_timestamp = now;
2152 if(printed) buffer_strcat(wb, "]},\n");
2153 buffer_strcat(wb, pre_date);
2154 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
2155 buffer_strcat(wb, post_date);
2160 // do the calculations
2161 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2162 storage_number n = rd->values[t];
2163 calculated_number value = unpack_storage_number(n);
2165 if(!does_storage_number_exist(n)) {
2167 found_non_existing[c]++;
2169 if(did_storage_number_reset(n)) annotate_reset = 1;
2171 switch(group_method) {
2173 if(abs(value) > abs(group_values[c])) group_values[c] = value;
2177 group_values[c] += value;
2182 group_values[c] += value;
2183 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
2189 if(annotate_reset) {
2191 buffer_strcat(wb, overflow_annotation);
2195 buffer_strcat(wb, normal_annotation);
2198 for(c = 0 ; c < dimensions ; c++) {
2199 if(found_non_existing[c] == group_count) {
2200 // all entries are non-existing
2202 buffer_strcat(wb, pre_value);
2203 buffer_strcat(wb, "null");
2204 buffer_strcat(wb, post_value);
2206 else if(!print_hidden[c]) {
2208 buffer_strcat(wb, pre_value);
2209 buffer_rrd_value(wb, group_values[c]);
2210 buffer_strcat(wb, post_value);
2212 if(group_values[c]) found_non_zero[c]++;
2215 // reset them for the next loop
2216 group_values[c] = 0;
2217 found_non_existing[c] = 0;
2220 // if all dimensions are hidden, print a null
2222 buffer_strcat(wb, pre_value);
2223 buffer_strcat(wb, "null");
2224 buffer_strcat(wb, post_value);
2232 if(printed) buffer_strcat(wb, "]}");
2233 buffer_strcat(wb, "\n ]\n}\n");
2235 if(only_non_zero && max_loop > 1) {
2237 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2238 group_values[c] = 0;
2239 found_non_existing[c] = 0;
2241 if(!print_hidden[c] && !found_non_zero[c]) {
2243 print_hidden[c] = 1;
2247 if(changed) buffer_flush(wb);
2254 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %lu bytes", st->name, wb->len);
2256 pthread_rwlock_unlock(&st->rwlock);
2257 return last_timestamp;