3 #define HOSTNAME_MAX 1024
4 char *hostname = "unknown";
6 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb)
8 pthread_rwlock_rdlock(&st->rwlock);
12 "\t\t\t\"id\": \"%s\",\n"
13 "\t\t\t\"name\": \"%s\",\n"
14 "\t\t\t\"type\": \"%s\",\n"
15 "\t\t\t\"family\": \"%s\",\n"
16 "\t\t\t\"context\": \"%s\",\n"
17 "\t\t\t\"title\": \"%s\",\n"
18 "\t\t\t\"priority\": %ld,\n"
19 "\t\t\t\"enabled\": %s,\n"
20 "\t\t\t\"units\": \"%s\",\n"
21 "\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
22 "\t\t\t\"chart_type\": \"%s\",\n"
23 "\t\t\t\"duration\": %ld,\n"
24 "\t\t\t\"first_entry\": %ld,\n"
25 "\t\t\t\"last_entry\": %ld,\n"
26 "\t\t\t\"update_every\": %d,\n"
27 "\t\t\t\"dimensions\": {\n"
35 , st->enabled?"true":"false"
38 , rrdset_type_name(st->chart_type)
39 , st->entries * st->update_every
40 , rrdset_first_entry_t(st)
41 , rrdset_last_entry_t(st)
45 unsigned long memory = st->memsize;
49 for(rd = st->dimensions; rd ; rd = rd->next) {
50 if(rd->flags & RRDDIM_FLAG_HIDDEN) continue;
52 memory += rd->memsize;
56 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
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_charts(BUFFER *wb)
82 buffer_sprintf(wb, "{\n"
83 "\t\"hostname\": \"%s\""
84 ",\n\t\"update_every\": %d"
85 ",\n\t\"history\": %d"
89 , rrd_default_history_entries
92 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
93 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
94 if(st->enabled && st->dimensions) {
95 if(c) buffer_strcat(wb, ",");
96 buffer_strcat(wb, "\n\t\t\"");
97 buffer_strcat(wb, st->id);
98 buffer_strcat(wb, "\": ");
99 rrd_stats_api_v1_chart(st, wb);
103 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
105 buffer_strcat(wb, "\n\t}\n}\n");
109 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
111 time_t now = time(NULL);
113 pthread_rwlock_rdlock(&st->rwlock);
117 "\t\t\t\"id\": \"%s\",\n"
118 "\t\t\t\"name\": \"%s\",\n"
119 "\t\t\t\"type\": \"%s\",\n"
120 "\t\t\t\"family\": \"%s\",\n"
121 "\t\t\t\"context\": \"%s\",\n"
122 "\t\t\t\"title\": \"%s\",\n"
123 "\t\t\t\"priority\": %ld,\n"
124 "\t\t\t\"enabled\": %d,\n"
125 "\t\t\t\"units\": \"%s\",\n"
126 "\t\t\t\"url\": \"/data/%s/%s\",\n"
127 "\t\t\t\"chart_type\": \"%s\",\n"
128 "\t\t\t\"counter\": %lu,\n"
129 "\t\t\t\"entries\": %ld,\n"
130 "\t\t\t\"first_entry_t\": %ld,\n"
131 "\t\t\t\"last_entry\": %lu,\n"
132 "\t\t\t\"last_entry_t\": %ld,\n"
133 "\t\t\t\"last_entry_secs_ago\": %ld,\n"
134 "\t\t\t\"update_every\": %d,\n"
135 "\t\t\t\"isdetail\": %d,\n"
136 "\t\t\t\"usec_since_last_update\": %llu,\n"
137 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
138 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
139 "\t\t\t\"dimensions\": [\n"
149 , st->name, options?options:""
150 , rrdset_type_name(st->chart_type)
153 , rrdset_first_entry_t(st)
154 , rrdset_last_slot(st)
155 , rrdset_last_entry_t(st)
156 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
159 , st->usec_since_last_update
160 , st->collected_total
161 , st->last_collected_total
164 unsigned long memory = st->memsize;
167 for(rd = st->dimensions; rd ; rd = rd->next) {
169 memory += rd->memsize;
173 "\t\t\t\t\t\"id\": \"%s\",\n"
174 "\t\t\t\t\t\"name\": \"%s\",\n"
175 "\t\t\t\t\t\"entries\": %ld,\n"
176 "\t\t\t\t\t\"isHidden\": %d,\n"
177 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
178 "\t\t\t\t\t\"multiplier\": %ld,\n"
179 "\t\t\t\t\t\"divisor\": %ld,\n"
180 "\t\t\t\t\t\"last_entry_t\": %ld,\n"
181 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
182 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
183 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
184 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
185 "\t\t\t\t\t\"memory\": %lu\n"
190 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
191 , rrddim_algorithm_name(rd->algorithm)
194 , rd->last_collected_time.tv_sec
195 , rd->collected_value
196 , rd->calculated_value
197 , rd->last_collected_value
198 , rd->last_calculated_value
206 "\t\t\t\"memory\" : %lu\n"
211 pthread_rwlock_unlock(&st->rwlock);
215 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
216 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
218 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
220 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
221 rrd_stats_one_json(st, options, wb);
222 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
225 void rrd_stats_all_json(BUFFER *wb)
227 unsigned long memory = 0;
231 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
233 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
234 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
235 if(st->enabled && st->dimensions) {
236 if(c) buffer_strcat(wb, ",\n");
237 memory += rrd_stats_one_json(st, NULL, wb);
241 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
243 buffer_sprintf(wb, "\n\t],\n"
244 "\t\"hostname\": \"%s\",\n"
245 "\t\"update_every\": %d,\n"
246 "\t\"history\": %d,\n"
247 "\t\"memory\": %lu\n"
251 , rrd_default_history_entries
258 // ----------------------------------------------------------------------------
260 // RRDR dimension options
261 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
262 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
263 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
264 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
266 // RRDR result options
267 #define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
268 #define RRDR_RESULT_OPTION_RELATIVE 0x00000002
270 typedef struct rrdresult {
271 RRDSET *st; // the chart this result refers to
273 uint32_t result_options; // RRDR_RESULT_OPTION_*
275 int d; // the number of dimensions
276 long n; // the number of values in the arrays
277 long rows; // the number of rows used
279 uint8_t *od; // the options for the dimensions
281 time_t *t; // array of n timestamps
282 calculated_number *v; // array n x d values
283 uint8_t *o; // array n x d options
285 long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
287 long group; // how many collected values were grouped for each row
288 int update_every; // what is the suggested update frequency in seconds
290 calculated_number min;
291 calculated_number max;
296 int has_st_lock; // if st is read locked by us
299 #define rrdr_rows(r) ((r)->rows)
302 static void rrdr_dump(RRDR *r)
307 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
309 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
310 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
313 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
314 , (r->od[c] & RRDR_RESET)?"RESET ":""
315 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
316 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
321 fprintf(stderr, "RRDR does not have any values in it.\n");
325 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
327 // for each line in the array
328 for(i = 0; i < r->rows ;i++) {
329 calculated_number *cn = &r->v[ i * r->d ];
330 uint8_t *co = &r->o[ i * r->d ];
332 // print the id and the timestamp of the line
333 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
335 // for each dimension
336 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
337 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
338 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
340 if(co[c] & RRDR_EMPTY)
341 fprintf(stderr, "null ");
343 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
345 , (co[c] & RRDR_EMPTY)?"E":" "
346 , (co[c] & RRDR_RESET)?"R":" "
347 , (co[c] & RRDR_HIDDEN)?"H":" "
348 , (co[c] & RRDR_NONZERO)?"N":" "
352 fprintf(stderr, "\n");
357 void rrdr_disable_not_selected_dimensions(RRDR *r, const char *dims)
359 char b[strlen(dims) + 1];
366 // disable all of them
367 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
368 r->od[c] |= RRDR_HIDDEN;
370 while(o && *o && (tok = mystrsep(&o, ",|"))) {
373 uint32_t hash = simple_hash(tok);
375 // find it and enable it
376 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
377 if(unlikely((hash == d->hash && !strcmp(d->id, tok)) || !strcmp(d->name, tok))) {
378 r->od[c] &= ~RRDR_HIDDEN;
380 // since the user needs this dimension
381 // make it appear as NONZERO, to return it
382 // even if the dimension has only zeros
383 r->od[c] |= RRDR_NONZERO;
389 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
392 case DATASOURCE_JSON:
393 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
396 case DATASOURCE_DATATABLE_JSON:
397 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
400 case DATASOURCE_DATATABLE_JSONP:
401 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
404 case DATASOURCE_JSONP:
405 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
409 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
413 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
417 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
420 case DATASOURCE_HTML:
421 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
424 case DATASOURCE_JS_ARRAY:
425 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
428 case DATASOURCE_SSV_COMMA:
429 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
433 buffer_strcat(wb, "unknown");
438 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
440 if(options & RRDR_OPTION_NONZERO) {
444 // the caller wants specific dimensions
445 // disable NONZERO option
446 // to make sure we don't accidentally prevent
447 // the specific dimensions from being returned
451 // find how many dimensions are not zero
454 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
455 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
456 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
461 // if with nonzero we get i = 0 (no dimensions will be returned)
462 // disable nonzero to show all dimensions
463 if(!i) options &= ~RRDR_OPTION_NONZERO;
469 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
471 long rows = rrdr_rows(r);
475 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
476 char kq[2] = "", // key quote
477 sq[2] = ""; // string quote
479 if( options & RRDR_OPTION_GOOGLE_JSON ) {
488 buffer_sprintf(wb, "{\n"
491 " %sname%s: %s%s%s,\n"
492 " %sview_update_every%s: %d,\n"
493 " %supdate_every%s: %d,\n"
494 " %sfirst_entry%s: %u,\n"
495 " %slast_entry%s: %u,\n"
498 " %sdimension_names%s: ["
500 , kq, kq, sq, r->st->id, sq
501 , kq, kq, sq, r->st->name, sq
502 , kq, kq, r->update_every
503 , kq, kq, r->st->update_every
504 , kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
505 , kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
506 , kq, kq, (uint32_t)r->before
507 , kq, kq, (uint32_t)r->after
510 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
511 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
512 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
514 if(i) buffer_strcat(wb, ", ");
515 buffer_strcat(wb, sq);
516 buffer_strcat(wb, rd->name);
517 buffer_strcat(wb, sq);
521 #ifdef NETDATA_INTERNAL_CHECKS
522 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
525 buffer_strcat(wb, sq);
526 buffer_strcat(wb, "no data");
527 buffer_strcat(wb, sq);
530 buffer_sprintf(wb, "],\n"
531 " %sdimension_ids%s: ["
534 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
535 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
536 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
538 if(i) buffer_strcat(wb, ", ");
539 buffer_strcat(wb, sq);
540 buffer_strcat(wb, rd->id);
541 buffer_strcat(wb, sq);
546 buffer_strcat(wb, sq);
547 buffer_strcat(wb, "no data");
548 buffer_strcat(wb, sq);
551 buffer_sprintf(wb, "],\n"
552 " %slatest_values%s: ["
555 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
556 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
557 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
559 if(i) buffer_strcat(wb, ", ");
562 storage_number n = rd->values[rrdset_last_slot(r->st)];
564 if(!does_storage_number_exist(n))
565 buffer_strcat(wb, "null");
567 buffer_rrd_value(wb, unpack_storage_number(n));
571 buffer_strcat(wb, "null");
574 buffer_sprintf(wb, "],\n"
575 " %sview_latest_values%s: ["
580 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
581 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
582 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
584 if(i) buffer_strcat(wb, ", ");
587 calculated_number *cn = &r->v[ (0) * r->d ];
588 uint8_t *co = &r->o[ (0) * r->d ];
590 if(co[c] & RRDR_EMPTY)
591 buffer_strcat(wb, "null");
593 buffer_rrd_value(wb, cn[c]);
598 buffer_strcat(wb, "null");
601 buffer_sprintf(wb, "],\n"
602 " %sdimensions%s: %ld,\n"
603 " %spoints%s: %ld,\n"
610 rrdr_buffer_print_format(wb, format);
612 buffer_sprintf(wb, "%s,\n"
618 if(string_value) buffer_strcat(wb, sq);
619 //info("JSONWRAPPER(): %s: END", r->st->id);
622 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
626 char kq[2] = "", // key quote
627 sq[2] = ""; // string quote
629 if( options & RRDR_OPTION_GOOGLE_JSON ) {
638 if(string_value) buffer_strcat(wb, sq);
640 buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
641 buffer_rrd_value(wb, r->min);
642 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
643 buffer_rrd_value(wb, r->max);
644 buffer_strcat(wb, "\n}\n");
647 #define JSON_DATES_JS 1
648 #define JSON_DATES_TIMESTAMP 2
650 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
652 //info("RRD2JSON(): %s: BEGIN", r->st->id);
653 int row_annotations = 0, dates, dates_with_new = 0;
654 char kq[2] = "", // key quote
655 sq[2] = "", // string quote
656 pre_label[101] = "", // before each label
657 post_label[101] = "", // after each label
658 pre_date[101] = "", // the beginning of line, to the date
659 post_date[101] = "", // closing the date
660 pre_value[101] = "", // before each value
661 post_value[101] = "", // after each value
662 post_line[101] = "", // at the end of each row
663 normal_annotation[201] = "", // default row annotation
664 overflow_annotation[201] = "", // overflow row annotation
665 data_begin[101] = "", // between labels and values
666 finish[101] = ""; // at the end of everything
669 dates = JSON_DATES_JS;
670 if( options & RRDR_OPTION_GOOGLE_JSON ) {
679 snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
680 snprintfz(post_date, 100, "%s}", sq);
681 snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
682 snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
683 snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
684 strcpy(post_value, "}");
685 strcpy(post_line, "]}");
686 snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
687 strcpy(finish, "\n ]\n}");
689 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);
690 snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
692 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
693 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);
694 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);
695 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);
697 // remove the valueobjects flag
698 // google wants its own keys
699 if(options & RRDR_OPTION_OBJECTSROWS)
700 options &= ~RRDR_OPTION_OBJECTSROWS;
705 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
706 dates = JSON_DATES_TIMESTAMP;
710 dates = JSON_DATES_JS;
713 if( options & RRDR_OPTION_OBJECTSROWS )
714 strcpy(pre_date, " { ");
716 strcpy(pre_date, " [ ");
717 strcpy(pre_label, ", \"");
718 strcpy(post_label, "\"");
719 strcpy(pre_value, ", ");
720 if( options & RRDR_OPTION_OBJECTSROWS )
721 strcpy(post_line, "}");
723 strcpy(post_line, "]");
724 snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
725 strcpy(finish, "\n ]\n}");
727 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
728 buffer_sprintf(wb, "%stime%s", sq, sq);
731 // -------------------------------------------------------------------------
732 // print the JSON header
737 // print the header lines
738 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
739 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
740 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
742 buffer_strcat(wb, pre_label);
743 buffer_strcat(wb, rd->name);
744 buffer_strcat(wb, post_label);
748 buffer_strcat(wb, pre_label);
749 buffer_strcat(wb, "no data");
750 buffer_strcat(wb, post_label);
753 // print the begin of row data
754 buffer_strcat(wb, data_begin);
756 // if all dimensions are hidden, print a null
758 buffer_strcat(wb, finish);
762 long start = 0, end = rrdr_rows(r), step = 1;
763 if((options & RRDR_OPTION_REVERSED)) {
764 start = rrdr_rows(r) - 1;
769 // for each line in the array
770 calculated_number total = 1;
771 for(i = start; i != end ;i += step) {
772 calculated_number *cn = &r->v[ i * r->d ];
773 uint8_t *co = &r->o[ i * r->d ];
775 time_t now = r->t[i];
777 if(dates == JSON_DATES_JS) {
778 // generate the local date time
779 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
780 if(!tm) { error("localtime_r() failed."); continue; }
782 if(likely(i != start)) buffer_strcat(wb, ",\n");
783 buffer_strcat(wb, pre_date);
785 if( options & RRDR_OPTION_OBJECTSROWS )
786 buffer_sprintf(wb, "%stime%s: ", kq, kq);
789 buffer_strcat(wb, "new ");
791 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
793 buffer_strcat(wb, post_date);
795 if(row_annotations) {
796 // google supports one annotation per row
797 int annotation_found = 0;
798 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
799 if(co[c] & RRDR_RESET) {
800 buffer_strcat(wb, overflow_annotation);
801 annotation_found = 1;
805 if(!annotation_found)
806 buffer_strcat(wb, normal_annotation);
810 // print the timestamp of the line
811 if(likely(i != start)) buffer_strcat(wb, ",\n");
812 buffer_strcat(wb, pre_date);
814 if( options & RRDR_OPTION_OBJECTSROWS )
815 buffer_sprintf(wb, "%stime%s: ", kq, kq);
817 buffer_rrd_value(wb, (calculated_number)r->t[i]);
819 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
821 buffer_strcat(wb, post_date);
824 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
826 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
827 calculated_number n = cn[c];
829 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
834 // prevent a division by zero
835 if(total == 0) total = 1;
838 // for each dimension
839 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
840 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
841 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
843 calculated_number n = cn[c];
845 buffer_strcat(wb, pre_value);
847 if( options & RRDR_OPTION_OBJECTSROWS )
848 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
850 if(co[c] & RRDR_EMPTY) {
851 if(options & RRDR_OPTION_NULL2ZERO)
852 buffer_strcat(wb, "0");
854 buffer_strcat(wb, "null");
857 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
860 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
863 buffer_rrd_value(wb, n);
866 buffer_strcat(wb, post_value);
869 buffer_strcat(wb, post_line);
872 buffer_strcat(wb, finish);
873 //info("RRD2JSON(): %s: END", r->st->id);
876 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
878 //info("RRD2CSV(): %s: BEGIN", r->st->id);
882 // print the csv header
883 for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
884 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
885 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
888 buffer_strcat(wb, startline);
889 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
890 buffer_strcat(wb, "time");
891 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
893 buffer_strcat(wb, separator);
894 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
895 buffer_strcat(wb, d->name);
896 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
899 buffer_strcat(wb, endline);
902 // no dimensions present
906 long start = 0, end = rrdr_rows(r), step = 1;
907 if((options & RRDR_OPTION_REVERSED)) {
908 start = rrdr_rows(r) - 1;
913 // for each line in the array
914 calculated_number total = 1;
915 for(i = start; i != end ;i += step) {
916 calculated_number *cn = &r->v[ i * r->d ];
917 uint8_t *co = &r->o[ i * r->d ];
919 buffer_strcat(wb, betweenlines);
920 buffer_strcat(wb, startline);
922 time_t now = r->t[i];
924 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
925 // print the timestamp of the line
926 buffer_rrd_value(wb, (calculated_number)now);
928 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
931 // generate the local date time
932 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
933 if(!tm) { error("localtime() failed."); continue; }
934 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
937 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
939 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
940 calculated_number n = cn[c];
942 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
947 // prevent a division by zero
948 if(total == 0) total = 1;
951 // for each dimension
952 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
953 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
954 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
956 buffer_strcat(wb, separator);
958 calculated_number n = cn[c];
960 if(co[c] & RRDR_EMPTY) {
961 if(options & RRDR_OPTION_NULL2ZERO)
962 buffer_strcat(wb, "0");
964 buffer_strcat(wb, "null");
967 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
970 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
973 buffer_rrd_value(wb, n);
977 buffer_strcat(wb, endline);
979 //info("RRD2CSV(): %s: END", r->st->id);
982 inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
986 calculated_number *cn = &r->v[ i * r->d ];
987 uint8_t *co = &r->o[ i * r->d ];
989 calculated_number sum = 0, min = 0, max = 0, v;
990 int all_null = 1, init = 1;
992 calculated_number total = 1;
993 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
995 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
996 calculated_number n = cn[c];
998 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1003 // prevent a division by zero
1004 if(total == 0) total = 1;
1007 // for each dimension
1008 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1009 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1010 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1012 calculated_number n = cn[c];
1014 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1017 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1018 n = n * 100 / total;
1020 if(unlikely(init)) {
1032 if(likely(!(co[c] & RRDR_EMPTY))) {
1037 if(n < min) min = n;
1038 if(n > max) max = n;
1041 if(unlikely(all_null)) {
1042 if(likely(*all_values_are_null))
1043 *all_values_are_null = 1;
1047 if(likely(*all_values_are_null))
1048 *all_values_are_null = 0;
1051 if(options & RRDR_OPTION_MIN2MAX)
1059 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
1061 //info("RRD2SSV(): %s: BEGIN", r->st->id);
1064 buffer_strcat(wb, prefix);
1065 long start = 0, end = rrdr_rows(r), step = 1;
1066 if((options & RRDR_OPTION_REVERSED)) {
1067 start = rrdr_rows(r) - 1;
1072 // for each line in the array
1073 for(i = start; i != end ;i += step) {
1074 int all_values_are_null = 0;
1075 calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
1077 if(likely(i != start)) {
1078 if(r->min > v) r->min = v;
1079 if(r->max < v) r->max = v;
1086 if(likely(i != start))
1087 buffer_strcat(wb, separator);
1089 if(all_values_are_null) {
1090 if(options & RRDR_OPTION_NULL2ZERO)
1091 buffer_strcat(wb, "0");
1093 buffer_strcat(wb, "null");
1096 buffer_rrd_value(wb, v);
1098 buffer_strcat(wb, suffix);
1099 //info("RRD2SSV(): %s: END", r->st->id);
1102 inline static calculated_number *rrdr_line_values(RRDR *r)
1104 return &r->v[ r->c * r->d ];
1107 inline static uint8_t *rrdr_line_options(RRDR *r)
1109 return &r->o[ r->c * r->d ];
1112 inline static int rrdr_line_init(RRDR *r, time_t t)
1116 if(unlikely(r->c >= r->n)) {
1117 error("requested to step above RRDR size for chart %s", r->st->name);
1127 inline static void rrdr_lock_rrdset(RRDR *r) {
1129 error("NULL value given!");
1133 pthread_rwlock_rdlock(&r->st->rwlock);
1137 inline static void rrdr_unlock_rrdset(RRDR *r) {
1139 error("NULL value given!");
1143 if(likely(r->has_st_lock)) {
1144 pthread_rwlock_unlock(&r->st->rwlock);
1149 inline static void rrdr_free(RRDR *r)
1152 error("NULL value given!");
1156 rrdr_unlock_rrdset(r);
1164 inline void rrdr_done(RRDR *r)
1170 static RRDR *rrdr_create(RRDSET *st, long n)
1173 error("NULL value given!");
1177 RRDR *r = callocz(1, sizeof(RRDR));
1180 rrdr_lock_rrdset(r);
1183 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
1187 r->t = mallocz(n * sizeof(time_t));
1188 r->v = mallocz(n * r->d * sizeof(calculated_number));
1189 r->o = mallocz(n * r->d * sizeof(uint8_t));
1190 r->od = mallocz(r->d * sizeof(uint8_t));
1192 // set the hidden flag on hidden dimensions
1194 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1195 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] = RRDR_HIDDEN;
1201 r->update_every = 1;
1206 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
1208 int debug = st->debug;
1209 int absolute_period_requested = -1;
1211 time_t first_entry_t = rrdset_first_entry_t(st);
1212 time_t last_entry_t = rrdset_last_entry_t(st);
1214 if(before == 0 && after == 0) {
1215 before = last_entry_t;
1216 after = first_entry_t;
1217 absolute_period_requested = 0;
1220 // allow relative for before and after
1221 if(((before < 0)?-before:before) <= (st->update_every * st->entries)) {
1222 before = last_entry_t + before;
1223 absolute_period_requested = 0;
1226 if(((after < 0)?-after:after) <= (st->update_every * st->entries)) {
1227 if(after == 0) after = -st->update_every;
1228 after = before + after;
1229 absolute_period_requested = 0;
1232 if(absolute_period_requested == -1)
1233 absolute_period_requested = 1;
1235 // make sure they are within our timeframe
1236 if(before > last_entry_t) before = last_entry_t;
1237 if(before < first_entry_t) before = first_entry_t;
1239 if(after > last_entry_t) after = last_entry_t;
1240 if(after < first_entry_t) after = first_entry_t;
1242 // check if they are upside down
1243 if(after > before) {
1244 time_t tmp = before;
1249 // the duration of the chart
1250 time_t duration = before - after;
1251 long available_points = duration / st->update_every;
1253 if(duration <= 0 || available_points <= 0)
1254 return rrdr_create(st, 1);
1256 // check the wanted points
1257 if(points < 0) points = -points;
1258 if(points > available_points) points = available_points;
1259 if(points == 0) points = available_points;
1261 // calculate proper grouping of source data
1262 long group = available_points / points;
1263 if(group <= 0) group = 1;
1265 // round group to the closest integer
1266 if(available_points % points > points / 2) group++;
1268 time_t after_new = (aligned) ? (after - (after % (group * st->update_every))) : after;
1269 time_t before_new = (aligned) ? (before - (before % (group * st->update_every))) : before;
1270 long points_new = (before_new - after_new) / st->update_every / group;
1272 // find the starting and ending slots in our round robin db
1273 long start_at_slot = rrdset_time2slot(st, before_new),
1274 stop_at_slot = rrdset_time2slot(st, after_new);
1276 #ifdef NETDATA_INTERNAL_CHECKS
1277 if(after_new < first_entry_t) {
1278 error("after_new %u is too small, minimum %u", (uint32_t)after_new, (uint32_t)first_entry_t);
1280 if(after_new > last_entry_t) {
1281 error("after_new %u is too big, maximum %u", (uint32_t)after_new, (uint32_t)last_entry_t);
1283 if(before_new < first_entry_t) {
1284 error("before_new %u is too small, minimum %u", (uint32_t)before_new, (uint32_t)first_entry_t);
1286 if(before_new > last_entry_t) {
1287 error("before_new %u is too big, maximum %u", (uint32_t)before_new, (uint32_t)last_entry_t);
1289 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1290 error("start_at_slot is invalid %ld, expected 0 to %ld", start_at_slot, st->entries - 1);
1292 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1293 error("stop_at_slot is invalid %ld, expected 0 to %ld", stop_at_slot, st->entries - 1);
1295 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1296 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1300 //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);
1303 before = before_new;
1304 duration = before - after;
1305 points = points_new;
1308 // before = the end time of the calculation
1309 // after = the start time of the calculation
1310 // duration = the duration of the calculation
1311 // group = the number of source points to aggregate / group together
1312 // method = the method of grouping source points
1313 // points = the number of points to generate
1316 // -------------------------------------------------------------------------
1317 // initialize our result set
1319 RRDR *r = rrdr_create(st, points);
1321 #ifdef NETDATA_INTERNAL_CHECKS
1322 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);
1327 #ifdef NETDATA_INTERNAL_CHECKS
1328 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);
1333 if(absolute_period_requested == 1)
1334 r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1336 r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1338 // find how many dimensions we have
1339 long dimensions = r->d;
1342 // -------------------------------------------------------------------------
1343 // checks for debugging
1345 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"
1347 , (uint32_t)first_entry_t
1348 , (uint32_t)last_entry_t
1349 , (uint32_t)(last_entry_t - first_entry_t)
1352 , (uint32_t)duration
1358 // -------------------------------------------------------------------------
1359 // temp arrays for keeping values per dimension
1361 calculated_number last_values[dimensions]; // keep the last value of each dimension
1362 calculated_number group_values[dimensions]; // keep sums when grouping
1363 long group_counts[dimensions]; // keep the number of values added to group_values
1364 uint8_t group_options[dimensions];
1365 uint8_t found_non_zero[dimensions];
1371 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1373 group_values[c] = 0;
1374 group_counts[c] = 0;
1375 group_options[c] = 0;
1376 found_non_zero[c] = 0;
1380 // -------------------------------------------------------------------------
1383 time_t now = rrdset_slot2time(st, start_at_slot),
1384 dt = st->update_every,
1387 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"
1399 r->update_every = group * st->update_every;
1403 //info("RRD2RRDR(): %s: STARTING", st->id);
1405 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1406 for(; !stop_now ; now -= dt, slot--, counter++) {
1407 if(unlikely(slot < 0)) slot = st->entries - 1;
1408 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1410 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %ld, %s %s"
1417 , (group_count + 1 == group)?"PRINT":" - "
1418 , (now >= after && now <= before)?"RANGE":" - "
1421 // make sure we return data in the proper time range
1422 if(unlikely(now > before)) continue;
1423 if(unlikely(now < after)) break;
1425 if(unlikely(group_count == 0)) {
1426 group_start_t = now;
1430 if(unlikely(group_count == group)) {
1431 if(unlikely(added >= points)) break;
1435 // do the calculations
1436 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1437 storage_number n = rd->values[slot];
1438 if(unlikely(!does_storage_number_exist(n))) continue;
1442 calculated_number value = unpack_storage_number(n);
1443 if(likely(value != 0.0)) {
1444 group_options[c] |= RRDR_NONZERO;
1445 found_non_zero[c] = 1;
1448 if(unlikely(did_storage_number_reset(n)))
1449 group_options[c] |= RRDR_RESET;
1451 switch(group_method) {
1453 if(unlikely(fabsl(value) > fabsl(group_values[c])))
1454 group_values[c] = value;
1460 group_values[c] += value;
1463 case GROUP_INCREMENTAL_SUM:
1464 if(unlikely(slot == start_at_slot))
1465 last_values[c] = value;
1467 group_values[c] += last_values[c] - value;
1468 last_values[c] = value;
1474 if(unlikely(add_this)) {
1475 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1479 calculated_number *cn = rrdr_line_values(r);
1480 uint8_t *co = rrdr_line_options(r);
1482 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1484 // update the dimension options
1485 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1487 // store the specific point options
1488 co[c] = group_options[c];
1491 if(unlikely(group_counts[c] == 0)) {
1493 co[c] |= RRDR_EMPTY;
1495 else if(unlikely(group_method == GROUP_AVERAGE)) {
1497 cn[c] = group_values[c] / group_counts[c];
1502 // GROUP_INCREMENTAL_SUM
1503 cn[c] = group_values[c];
1506 if(cn[c] < r->min) r->min = cn[c];
1507 if(cn[c] > r->max) r->max = cn[c];
1509 // reset them for the next loop
1510 group_values[c] = 0;
1511 group_counts[c] = 0;
1512 group_options[c] = 0;
1522 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1523 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1527 int rrd2value(RRDSET *st, BUFFER *wb, calculated_number *n, BUFFER *dimensions, long points, long long after, long long before, int group_method, uint32_t options, time_t *latest_timestamp, int *value_is_null)
1529 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1531 if(value_is_null) *value_is_null = 1;
1535 if(rrdr_rows(r) == 0) {
1537 if(value_is_null) *value_is_null = 1;
1541 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1542 wb->options |= WB_CONTENT_NO_CACHEABLE;
1543 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1544 wb->options |= WB_CONTENT_CACHEABLE;
1546 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1549 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1551 if(latest_timestamp)
1552 *latest_timestamp = r->before;
1554 long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
1555 *n = rrdr2value(r, i, options, value_is_null);
1561 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)
1563 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1565 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1569 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1570 wb->options |= WB_CONTENT_NO_CACHEABLE;
1571 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1572 wb->options |= WB_CONTENT_CACHEABLE;
1574 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1577 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1579 if(latest_timestamp && rrdr_rows(r) > 0)
1580 *latest_timestamp = r->before;
1583 case DATASOURCE_SSV:
1584 if(options & RRDR_OPTION_JSON_WRAP) {
1585 wb->contenttype = CT_APPLICATION_JSON;
1586 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1587 rrdr2ssv(r, wb, options, "", " ", "");
1588 rrdr_json_wrapper_end(r, wb, format, options, 1);
1591 wb->contenttype = CT_TEXT_PLAIN;
1592 rrdr2ssv(r, wb, options, "", " ", "");
1596 case DATASOURCE_SSV_COMMA:
1597 if(options & RRDR_OPTION_JSON_WRAP) {
1598 wb->contenttype = CT_APPLICATION_JSON;
1599 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1600 rrdr2ssv(r, wb, options, "", ",", "");
1601 rrdr_json_wrapper_end(r, wb, format, options, 1);
1604 wb->contenttype = CT_TEXT_PLAIN;
1605 rrdr2ssv(r, wb, options, "", ",", "");
1609 case DATASOURCE_JS_ARRAY:
1610 if(options & RRDR_OPTION_JSON_WRAP) {
1611 wb->contenttype = CT_APPLICATION_JSON;
1612 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1613 rrdr2ssv(r, wb, options, "[", ",", "]");
1614 rrdr_json_wrapper_end(r, wb, format, options, 0);
1617 wb->contenttype = CT_APPLICATION_JSON;
1618 rrdr2ssv(r, wb, options, "[", ",", "]");
1622 case DATASOURCE_CSV:
1623 if(options & RRDR_OPTION_JSON_WRAP) {
1624 wb->contenttype = CT_APPLICATION_JSON;
1625 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1626 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1627 rrdr_json_wrapper_end(r, wb, format, options, 1);
1630 wb->contenttype = CT_TEXT_PLAIN;
1631 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1635 case DATASOURCE_CSV_JSON_ARRAY:
1636 wb->contenttype = CT_APPLICATION_JSON;
1637 if(options & RRDR_OPTION_JSON_WRAP) {
1638 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1639 buffer_strcat(wb, "[\n");
1640 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1641 buffer_strcat(wb, "\n]");
1642 rrdr_json_wrapper_end(r, wb, format, options, 0);
1645 wb->contenttype = CT_TEXT_PLAIN;
1646 buffer_strcat(wb, "[\n");
1647 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1648 buffer_strcat(wb, "\n]");
1652 case DATASOURCE_TSV:
1653 if(options & RRDR_OPTION_JSON_WRAP) {
1654 wb->contenttype = CT_APPLICATION_JSON;
1655 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1656 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1657 rrdr_json_wrapper_end(r, wb, format, options, 1);
1660 wb->contenttype = CT_TEXT_PLAIN;
1661 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1665 case DATASOURCE_HTML:
1666 if(options & RRDR_OPTION_JSON_WRAP) {
1667 wb->contenttype = CT_APPLICATION_JSON;
1668 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1669 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1670 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1671 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1672 rrdr_json_wrapper_end(r, wb, format, options, 1);
1675 wb->contenttype = CT_TEXT_HTML;
1676 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1677 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1678 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1682 case DATASOURCE_DATATABLE_JSONP:
1683 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1685 if(options & RRDR_OPTION_JSON_WRAP)
1686 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1688 rrdr2json(r, wb, options, 1);
1690 if(options & RRDR_OPTION_JSON_WRAP)
1691 rrdr_json_wrapper_end(r, wb, format, options, 0);
1694 case DATASOURCE_DATATABLE_JSON:
1695 wb->contenttype = CT_APPLICATION_JSON;
1697 if(options & RRDR_OPTION_JSON_WRAP)
1698 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1700 rrdr2json(r, wb, options, 1);
1702 if(options & RRDR_OPTION_JSON_WRAP)
1703 rrdr_json_wrapper_end(r, wb, format, options, 0);
1706 case DATASOURCE_JSONP:
1707 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1708 if(options & RRDR_OPTION_JSON_WRAP)
1709 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1711 rrdr2json(r, wb, options, 0);
1713 if(options & RRDR_OPTION_JSON_WRAP)
1714 rrdr_json_wrapper_end(r, wb, format, options, 0);
1717 case DATASOURCE_JSON:
1719 wb->contenttype = CT_APPLICATION_JSON;
1721 if(options & RRDR_OPTION_JSON_WRAP)
1722 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1724 rrdr2json(r, wb, options, 0);
1726 if(options & RRDR_OPTION_JSON_WRAP)
1727 rrdr_json_wrapper_end(r, wb, format, options, 0);
1735 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)
1738 pthread_rwlock_rdlock(&st->rwlock);
1741 // -------------------------------------------------------------------------
1742 // switch from JSON to google JSON
1747 case DATASOURCE_DATATABLE_JSON:
1748 case DATASOURCE_DATATABLE_JSONP:
1753 case DATASOURCE_JSON:
1759 // -------------------------------------------------------------------------
1760 // validate the parameters
1762 if(points < 1) points = 1;
1763 if(group < 1) group = 1;
1765 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1766 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1770 // our return value (the last timestamp printed)
1771 // this is required to detect re-transmit in google JSONP
1772 time_t last_timestamp = 0;
1775 // -------------------------------------------------------------------------
1776 // find how many dimensions we have
1780 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1782 pthread_rwlock_unlock(&st->rwlock);
1783 buffer_strcat(wb, "No dimensions yet.");
1788 // -------------------------------------------------------------------------
1789 // prepare various strings, to speed up the loop
1791 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);
1792 char normal_annotation[201]; snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1793 char pre_date[51]; snprintfz(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1794 char post_date[21]; snprintfz(post_date, 20, "%s}", sq);
1795 char pre_value[21]; snprintfz(pre_value, 20, ",{%sv%s:", kq, kq);
1796 char post_value[21]; strcpy(post_value, "}");
1799 // -------------------------------------------------------------------------
1800 // checks for debugging
1803 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"
1805 , rrdset_first_entry_t(st)
1806 , rrdset_last_entry_t(st)
1807 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1816 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%ld) is earlier than the oldest (%ld)", st->name, before, after);
1818 if((before - after) > st->entries * st->update_every)
1819 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);
1823 // -------------------------------------------------------------------------
1824 // temp arrays for keeping values per dimension
1826 calculated_number group_values[dimensions]; // keep sums when grouping
1827 int print_hidden[dimensions]; // keep hidden flags
1828 int found_non_zero[dimensions];
1829 int found_non_existing[dimensions];
1832 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1833 group_values[c] = 0;
1834 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1835 found_non_zero[c] = 0;
1836 found_non_existing[c] = 0;
1840 // 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);
1841 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1842 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1844 // -------------------------------------------------------------------------
1845 // remove dimensions that contain only zeros
1848 if(only_non_zero) max_loop = 2;
1850 for(; max_loop ; max_loop--) {
1852 // -------------------------------------------------------------------------
1853 // print the JSON header
1855 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1856 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);
1857 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);
1858 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);
1860 // print the header for each dimension
1861 // and update the print_hidden array for the dimensions that should be hidden
1863 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1864 if(!print_hidden[c]) {
1866 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);
1870 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);
1873 // print the begin of row data
1874 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1877 // -------------------------------------------------------------------------
1880 int annotate_reset = 0;
1881 int annotation_count = 0;
1883 long t = rrdset_time2slot(st, before),
1884 stop_at_t = rrdset_time2slot(st, after),
1889 time_t now = rrdset_slot2time(st, t),
1890 dt = st->update_every;
1892 long count = 0, printed = 0, group_count = 0;
1895 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"
1902 , (uint32_t)rrdset_first_entry_t(st)
1903 , (uint32_t)rrdset_last_entry_t(st)
1909 for(; !stop_now ; now -= dt, t--, counter++) {
1910 if(t < 0) t = st->entries - 1;
1911 if(t == stop_at_t) stop_now = counter;
1915 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %ld, %s %s"
1922 , (group_count + 1 == group)?"PRINT":" - "
1923 , (now >= after && now <= before)?"RANGE":" - "
1927 // make sure we return data in the proper time range
1928 if(now > before) continue;
1929 if(now < after) break;
1931 //if(rrdset_slot2time(st, t) != now)
1932 // 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));
1937 // check if we have to print this now
1938 if(group_count == group) {
1939 if(printed >= points) {
1940 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
1944 // generate the local date time
1945 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1946 if(!tm) { error("localtime() failed."); continue; }
1947 if(now > last_timestamp) last_timestamp = now;
1949 if(printed) buffer_strcat(wb, "]},\n");
1950 buffer_strcat(wb, pre_date);
1951 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1952 buffer_strcat(wb, post_date);
1957 // do the calculations
1958 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1959 storage_number n = rd->values[t];
1960 calculated_number value = unpack_storage_number(n);
1962 if(!does_storage_number_exist(n)) {
1964 found_non_existing[c]++;
1966 if(did_storage_number_reset(n)) annotate_reset = 1;
1968 switch(group_method) {
1970 if(abs(value) > abs(group_values[c])) group_values[c] = value;
1974 group_values[c] += value;
1979 group_values[c] += value;
1980 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
1986 if(annotate_reset) {
1988 buffer_strcat(wb, overflow_annotation);
1992 buffer_strcat(wb, normal_annotation);
1995 for(c = 0 ; c < dimensions ; c++) {
1996 if(found_non_existing[c] == group_count) {
1997 // all entries are non-existing
1999 buffer_strcat(wb, pre_value);
2000 buffer_strcat(wb, "null");
2001 buffer_strcat(wb, post_value);
2003 else if(!print_hidden[c]) {
2005 buffer_strcat(wb, pre_value);
2006 buffer_rrd_value(wb, group_values[c]);
2007 buffer_strcat(wb, post_value);
2009 if(group_values[c]) found_non_zero[c]++;
2012 // reset them for the next loop
2013 group_values[c] = 0;
2014 found_non_existing[c] = 0;
2017 // if all dimensions are hidden, print a null
2019 buffer_strcat(wb, pre_value);
2020 buffer_strcat(wb, "null");
2021 buffer_strcat(wb, post_value);
2029 if(printed) buffer_strcat(wb, "]}");
2030 buffer_strcat(wb, "\n ]\n}\n");
2032 if(only_non_zero && max_loop > 1) {
2034 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2035 group_values[c] = 0;
2036 found_non_existing[c] = 0;
2038 if(!print_hidden[c] && !found_non_zero[c]) {
2040 print_hidden[c] = 1;
2044 if(changed) buffer_flush(wb);
2051 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %lu bytes", st->name, wb->len);
2053 pthread_rwlock_unlock(&st->rwlock);
2054 return last_timestamp;