13 #define HOSTNAME_MAX 1024
14 char *hostname = "unknown";
16 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb)
18 pthread_rwlock_rdlock(&st->rwlock);
22 "\t\t\t\"id\": \"%s\",\n"
23 "\t\t\t\"name\": \"%s\",\n"
24 "\t\t\t\"type\": \"%s\",\n"
25 "\t\t\t\"family\": \"%s\",\n"
26 "\t\t\t\"title\": \"%s\",\n"
27 "\t\t\t\"priority\": %ld,\n"
28 "\t\t\t\"enabled\": %s,\n"
29 "\t\t\t\"units\": \"%s\",\n"
30 "\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
31 "\t\t\t\"chart_type\": \"%s\",\n"
32 "\t\t\t\"duration\": %ld,\n"
33 "\t\t\t\"first_entry\": %lu,\n"
34 "\t\t\t\"last_entry\": %lu,\n"
35 "\t\t\t\"update_every\": %d,\n"
36 "\t\t\t\"dimensions\": {\n"
43 , st->enabled?"true":"false"
46 , rrdset_type_name(st->chart_type)
47 , st->entries * st->update_every
48 , rrdset_first_entry_t(st)
49 , rrdset_last_entry_t(st)
53 unsigned long memory = st->memsize;
57 for(rd = st->dimensions; rd ; rd = rd->next) {
58 if(rd->flags & RRDDIM_FLAG_HIDDEN) continue;
60 memory += rd->memsize;
64 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
78 pthread_rwlock_unlock(&st->rwlock);
81 void rrd_stats_api_v1_charts(BUFFER *wb)
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(&rrdset_root_rwlock);
97 for(st = rrdset_root, c = 0; st ; st = st->next) {
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(st, wb);
107 pthread_rwlock_unlock(&rrdset_root_rwlock);
109 buffer_strcat(wb, "\n\t}\n}\n");
113 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
115 time_t now = time(NULL);
117 pthread_rwlock_rdlock(&st->rwlock);
121 "\t\t\t\"id\": \"%s\",\n"
122 "\t\t\t\"name\": \"%s\",\n"
123 "\t\t\t\"type\": \"%s\",\n"
124 "\t\t\t\"family\": \"%s\",\n"
125 "\t\t\t\"title\": \"%s\",\n"
126 "\t\t\t\"priority\": %ld,\n"
127 "\t\t\t\"enabled\": %d,\n"
128 "\t\t\t\"units\": \"%s\",\n"
129 "\t\t\t\"url\": \"/data/%s/%s\",\n"
130 "\t\t\t\"chart_type\": \"%s\",\n"
131 "\t\t\t\"counter\": %ld,\n"
132 "\t\t\t\"entries\": %ld,\n"
133 "\t\t\t\"first_entry_t\": %lu,\n"
134 "\t\t\t\"last_entry\": %ld,\n"
135 "\t\t\t\"last_entry_t\": %lu,\n"
136 "\t\t\t\"last_entry_secs_ago\": %lu,\n"
137 "\t\t\t\"update_every\": %d,\n"
138 "\t\t\t\"isdetail\": %d,\n"
139 "\t\t\t\"usec_since_last_update\": %llu,\n"
140 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
141 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
142 "\t\t\t\"dimensions\": [\n"
151 , st->name, options?options:""
152 , rrdset_type_name(st->chart_type)
155 , rrdset_first_entry_t(st)
156 , rrdset_last_slot(st)
157 , rrdset_last_entry_t(st)
158 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
161 , st->usec_since_last_update
162 , st->collected_total
163 , st->last_collected_total
166 unsigned long memory = st->memsize;
169 for(rd = st->dimensions; rd ; rd = rd->next) {
171 memory += rd->memsize;
175 "\t\t\t\t\t\"id\": \"%s\",\n"
176 "\t\t\t\t\t\"name\": \"%s\",\n"
177 "\t\t\t\t\t\"entries\": %ld,\n"
178 "\t\t\t\t\t\"isHidden\": %d,\n"
179 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
180 "\t\t\t\t\t\"multiplier\": %ld,\n"
181 "\t\t\t\t\t\"divisor\": %ld,\n"
182 "\t\t\t\t\t\"last_entry_t\": %lu,\n"
183 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
184 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
185 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
186 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
187 "\t\t\t\t\t\"memory\": %lu\n"
192 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
193 , rrddim_algorithm_name(rd->algorithm)
196 , rd->last_collected_time.tv_sec
197 , rd->collected_value
198 , rd->calculated_value
199 , rd->last_collected_value
200 , rd->last_calculated_value
208 "\t\t\t\"memory\" : %lu\n"
213 pthread_rwlock_unlock(&st->rwlock);
217 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
218 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
220 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
222 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
223 rrd_stats_one_json(st, options, wb);
224 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
227 void rrd_stats_all_json(BUFFER *wb)
229 unsigned long memory = 0;
233 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
235 pthread_rwlock_rdlock(&rrdset_root_rwlock);
236 for(st = rrdset_root, c = 0; st ; st = st->next) {
238 if(c) buffer_strcat(wb, ",\n");
239 memory += rrd_stats_one_json(st, NULL, wb);
243 pthread_rwlock_unlock(&rrdset_root_rwlock);
245 buffer_sprintf(wb, "\n\t],\n"
246 "\t\"hostname\": \"%s\",\n"
247 "\t\"update_every\": %d,\n"
248 "\t\"history\": %d,\n"
249 "\t\"memory\": %lu\n"
253 , rrd_default_history_entries
260 // ----------------------------------------------------------------------------
263 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
264 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
265 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
266 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
269 typedef struct rrdresult {
270 RRDSET *st; // the chart this result refers to
272 int d; // the number of dimensions
273 int n; // the number of values in the arrays
274 int rows; // the number of rows used
276 uint8_t *od; // the options for the dimensions
278 time_t *t; // array of n timestamps
279 calculated_number *v; // array n x d values
280 uint8_t *o; // array n x d options
282 int c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
284 int group; // how many collected values were grouped for each row
285 int update_every; // what is the suggested update frequency in seconds
287 calculated_number min;
288 calculated_number max;
293 int has_st_lock; // if st is read locked by us
296 #define rrdr_rows(r) ((r)->rows)
299 static void rrdr_dump(RRDR *r)
304 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
306 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
307 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
310 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
311 , (r->od[c] & RRDR_RESET)?"RESET ":""
312 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
313 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
318 fprintf(stderr, "RRDR does not have any values in it.\n");
322 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
324 // for each line in the array
325 for(i = 0; i < r->rows ;i++) {
326 calculated_number *cn = &r->v[ i * r->d ];
327 uint8_t *co = &r->o[ i * r->d ];
329 // print the id and the timestamp of the line
330 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
332 // for each dimension
333 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
334 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
335 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
337 if(co[c] & RRDR_EMPTY)
338 fprintf(stderr, "null ");
340 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
342 , (co[c] & RRDR_EMPTY)?"E":" "
343 , (co[c] & RRDR_RESET)?"R":" "
344 , (co[c] & RRDR_HIDDEN)?"H":" "
345 , (co[c] & RRDR_NONZERO)?"N":" "
349 fprintf(stderr, "\n");
354 void rrdr_disable_not_selected_dimensions(RRDR *r, const char *dims)
356 char b[strlen(dims) + 1];
363 // disable all of them
364 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
365 r->od[c] |= RRDR_HIDDEN;
367 while(o && *o && (tok = mystrsep(&o, ", |"))) {
370 // find it and enable it
371 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
372 if(!strcmp(d->name, tok)) {
373 r->od[c] &= ~RRDR_HIDDEN;
379 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
382 case DATASOURCE_JSON:
383 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
386 case DATASOURCE_DATATABLE_JSON:
387 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
390 case DATASOURCE_DATATABLE_JSONP:
391 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
394 case DATASOURCE_JSONP:
395 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
399 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
403 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
407 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
410 case DATASOURCE_HTML:
411 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
414 case DATASOURCE_JS_ARRAY:
415 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
418 case DATASOURCE_SSV_COMMA:
419 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
423 buffer_strcat(wb, "unknown");
428 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
430 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
431 char kq[2] = "", // key quote
432 sq[2] = ""; // string quote
434 if( options & RRDR_OPTION_GOOGLE_JSON ) {
443 buffer_sprintf(wb, "{\n"
445 " %sname%s: %s%s%s,\n"
446 " %sview_update_every%s: %d,\n"
447 " %supdate_every%s: %d,\n"
448 " %sfirst_entry_t%s: %u, \n"
449 " %slast_entry_t%s: %u, \n"
451 , kq, kq, sq, r->st->id, sq
452 , kq, kq, sq, r->st->name, sq
453 , kq, kq, r->update_every
454 , kq, kq, r->st->update_every
455 , kq, kq, rrdset_first_entry_t(r->st)
456 , kq, kq, rrdset_last_entry_t(r->st)
459 buffer_rrd_value(wb, r->min);
460 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
461 buffer_rrd_value(wb, r->max);
462 buffer_sprintf(wb, ",\n"
465 " %sdimension_names%s: ["
473 for(c = 0, i = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
474 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
475 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
477 if(i) buffer_strcat(wb, ", ");
478 buffer_strcat(wb, sq);
479 buffer_strcat(wb, rd->name);
480 buffer_strcat(wb, sq);
483 buffer_sprintf(wb, "],\n"
484 " %sdimension_ids%s: ["
487 for(c = 0, i = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
488 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
489 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
491 if(i) buffer_strcat(wb, ", ");
492 buffer_strcat(wb, sq);
493 buffer_strcat(wb, rd->id);
494 buffer_strcat(wb, sq);
497 buffer_sprintf(wb, "],\n"
498 " %slatest_values%s: ["
501 for(c = 0, i = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
502 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
503 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
505 if(i) buffer_strcat(wb, ", ");
508 storage_number n = rd->values[rrdset_last_slot(r->st)];
510 if(!does_storage_number_exist(n))
511 buffer_strcat(wb, "null");
513 buffer_rrd_value(wb, unpack_storage_number(n));
515 buffer_sprintf(wb, "],\n"
516 " %sresult_latest_values%s: ["
520 for(c = 0, i = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
521 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
522 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
524 if(i) buffer_strcat(wb, ", ");
527 calculated_number *cn = &r->v[ (0) * r->d ];
528 uint8_t *co = &r->o[ (0) * r->d ];
530 if(co[c] & RRDR_EMPTY)
531 buffer_strcat(wb, "null");
533 buffer_rrd_value(wb, cn[c]);
536 buffer_sprintf(wb, "],\n"
537 " %sdimensions%s: %d,\n"
541 , kq, kq, rrdr_rows(r)
545 rrdr_buffer_print_format(wb, format);
547 buffer_sprintf(wb, "%s,\n"
553 if(string_value) buffer_strcat(wb, sq);
554 //info("JSONWRAPPER(): %s: END", r->st->id);
557 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
562 char sq[2] = ""; // string quote
564 if( options & RRDR_OPTION_GOOGLE_JSON )
569 if(string_value) buffer_strcat(wb, sq);
570 buffer_strcat(wb, "\n}\n");
573 #define JSON_DATES_JS 1
574 #define JSON_DATES_TIMESTAMP 2
576 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
578 //info("RRD2JSON(): %s: BEGIN", r->st->id);
579 int row_annotations = 0, dates = JSON_DATES_JS, dates_with_new = 0;
580 char kq[2] = "", // key quote
581 sq[2] = "", // string quote
582 pre_label[101] = "", // before each label
583 post_label[101] = "", // after each label
584 pre_date[101] = "", // the beginning of line, to the date
585 post_date[101] = "", // closing the date
586 pre_value[101] = "", // before each value
587 post_value[101] = "", // after each value
588 post_line[101] = "", // at the end of each row
589 normal_annotation[201] = "", // default row annotation
590 overflow_annotation[201] = "", // overflow row annotation
591 data_begin[101] = "", // between labels and values
592 finish[101] = ""; // at the end of everything
595 dates = JSON_DATES_JS;
596 if( options & RRDR_OPTION_GOOGLE_JSON ) {
605 snprintf(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
606 snprintf(post_date, 100, "%s}", sq);
607 snprintf(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
608 snprintf(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
609 snprintf(pre_value, 100, ",{%sv%s:", kq, kq);
610 snprintf(post_value, 100, "}");
611 snprintf(post_line, 100, "]}");
612 snprintf(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
613 snprintf(finish, 100, "\n ]\n}\n");
615 snprintf(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);
616 snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
618 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq, kq, kq);
619 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);
620 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);
621 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);
623 // remove the valueobjects flag
624 // google wants its own keys
625 if(options & RRDR_OPTION_OBJECTSROWS)
626 options &= ~RRDR_OPTION_OBJECTSROWS;
631 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
632 dates = JSON_DATES_TIMESTAMP;
636 dates = JSON_DATES_JS;
639 if( options & RRDR_OPTION_OBJECTSROWS )
640 snprintf(pre_date, 100, " { ");
642 snprintf(pre_date, 100, " [ ");
643 snprintf(pre_label, 100, ", \"");
644 snprintf(post_label, 100, "\"");
645 snprintf(pre_value, 100, ", ");
646 if( options & RRDR_OPTION_OBJECTSROWS )
647 snprintf(post_line, 100, "}");
649 snprintf(post_line, 100, "]");
650 snprintf(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
651 snprintf(finish, 100, "\n ]\n}\n");
653 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
654 buffer_sprintf(wb, "%stime%s", sq, sq);
657 // -------------------------------------------------------------------------
658 // print the JSON header
663 // print the header lines
664 for(c = 0, i = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
665 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
666 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
668 buffer_strcat(wb, pre_label);
669 buffer_strcat(wb, rd->name);
670 buffer_strcat(wb, post_label);
674 buffer_strcat(wb, pre_label);
675 buffer_strcat(wb, "no data");
676 buffer_strcat(wb, post_label);
679 // print the begin of row data
680 buffer_strcat(wb, data_begin);
682 // if all dimensions are hidden, print a null
684 buffer_strcat(wb, finish);
688 long start = 0, end = rrdr_rows(r), step = 1;
689 if((options & RRDR_OPTION_REVERSED)) {
690 start = rrdr_rows(r) - 1;
695 // for each line in the array
696 for(i = start; i != end ;i += step) {
697 calculated_number *cn = &r->v[ i * r->d ];
698 uint8_t *co = &r->o[ i * r->d ];
700 time_t now = r->t[i];
702 if(dates == JSON_DATES_JS) {
703 // generate the local date time
704 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
705 if(!tm) { error("localtime_r() failed."); continue; }
707 if(likely(i != start)) buffer_strcat(wb, ",\n");
708 buffer_strcat(wb, pre_date);
710 if( options & RRDR_OPTION_OBJECTSROWS )
711 buffer_sprintf(wb, "%stime%s: ", kq, kq);
714 buffer_strcat(wb, "new ");
716 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
718 buffer_strcat(wb, post_date);
720 if(row_annotations) {
721 // google supports one annotation per row
722 int annotation_found = 0;
723 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
724 if(co[c] & RRDR_RESET) {
725 buffer_strcat(wb, overflow_annotation);
726 annotation_found = 1;
730 if(!annotation_found)
731 buffer_strcat(wb, normal_annotation);
735 // print the timestamp of the line
736 if(likely(i != start)) buffer_strcat(wb, ",\n");
737 buffer_strcat(wb, pre_date);
739 if( options & RRDR_OPTION_OBJECTSROWS )
740 buffer_sprintf(wb, "%stime%s: ", kq, kq);
742 buffer_rrd_value(wb, (calculated_number)r->t[i]);
744 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
746 buffer_strcat(wb, post_date);
749 // for each dimension
750 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
751 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
752 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
754 calculated_number n = cn[c];
756 buffer_strcat(wb, pre_value);
758 if( options & RRDR_OPTION_OBJECTSROWS )
759 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
761 if(co[c] & RRDR_EMPTY) {
762 if(options & RRDR_OPTION_NULL2ZERO)
763 buffer_strcat(wb, "0");
765 buffer_strcat(wb, "null");
767 else if((options & RRDR_OPTION_ABSOLUTE))
768 buffer_rrd_value(wb, (n<0)?-n:n);
770 buffer_rrd_value(wb, n);
772 buffer_strcat(wb, post_value);
775 buffer_strcat(wb, post_line);
778 buffer_strcat(wb, finish);
779 //info("RRD2JSON(): %s: END", r->st->id);
782 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline)
784 //info("RRD2CSV(): %s: BEGIN", r->st->id);
788 // print the csv header
789 for(c = 0, i = 0, d = r->st->dimensions; d ;c++, d = d->next) {
790 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
791 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
794 buffer_strcat(wb, startline);
795 buffer_strcat(wb, "time");
797 buffer_strcat(wb, separator);
798 buffer_strcat(wb, d->name);
801 buffer_strcat(wb, endline);
804 // no dimensions present
808 long start = 0, end = rrdr_rows(r), step = 1;
809 if((options & RRDR_OPTION_REVERSED)) {
810 start = rrdr_rows(r) - 1;
815 // for each line in the array
816 for(i = start; i != end ;i += step) {
817 calculated_number *cn = &r->v[ i * r->d ];
818 uint8_t *co = &r->o[ i * r->d ];
820 buffer_strcat(wb, startline);
822 time_t now = r->t[i];
824 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
825 // print the timestamp of the line
826 buffer_rrd_value(wb, (calculated_number)now);
828 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
831 // generate the local date time
832 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
833 if(!tm) { error("localtime() failed."); continue; }
834 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
837 // for each dimension
838 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
839 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
840 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
842 buffer_strcat(wb, separator);
844 calculated_number n = cn[c];
846 if(co[c] & RRDR_EMPTY) {
847 if(options & RRDR_OPTION_NULL2ZERO)
848 buffer_strcat(wb, "0");
850 buffer_strcat(wb, "null");
852 else if((options & RRDR_OPTION_ABSOLUTE))
853 buffer_rrd_value(wb, (n<0)?-n:n);
855 buffer_rrd_value(wb, n);
858 buffer_strcat(wb, endline);
860 //info("RRD2CSV(): %s: END", r->st->id);
863 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
865 //info("RRD2SSV(): %s: BEGIN", r->st->id);
869 buffer_strcat(wb, prefix);
870 long start = 0, end = rrdr_rows(r), step = 1;
871 if((options & RRDR_OPTION_REVERSED)) {
872 start = rrdr_rows(r) - 1;
877 // for each line in the array
878 for(i = start; i != end ;i += step) {
880 calculated_number *cn = &r->v[ i * r->d ];
881 uint8_t *co = &r->o[ i * r->d ];
883 calculated_number sum = 0, min = 0, max = 0;
884 int all_null = 1, init = 1;
886 // for each dimension
887 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
888 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
889 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
891 calculated_number n = cn[c];
905 if(likely(!(co[c] & RRDR_EMPTY))) {
907 if((options & RRDR_OPTION_ABSOLUTE) && n < 0) n = -n;
915 if(likely(i != start))
916 buffer_strcat(wb, separator);
919 if(options & RRDR_OPTION_NULL2ZERO)
920 buffer_strcat(wb, "0");
922 buffer_strcat(wb, "null");
924 else if(options & RRDR_OPTION_MIN2MAX)
925 buffer_rrd_value(wb, max - min);
927 buffer_rrd_value(wb, sum);
929 buffer_strcat(wb, suffix);
930 //info("RRD2SSV(): %s: END", r->st->id);
933 inline static calculated_number *rrdr_line_values(RRDR *r)
935 return &r->v[ r->c * r->d ];
938 inline static uint8_t *rrdr_line_options(RRDR *r)
940 return &r->o[ r->c * r->d ];
943 inline static int rrdr_line_init(RRDR *r, time_t t)
947 if(unlikely(r->c >= r->n)) {
948 error("requested to step above RRDR size for chart %s", r->st->name);
958 inline static void rrdr_lock_rrdset(RRDR *r) {
960 error("NULL value given!");
964 pthread_rwlock_rdlock(&r->st->rwlock);
968 inline static void rrdr_unlock_rrdset(RRDR *r) {
970 error("NULL value given!");
974 if(likely(r->has_st_lock)) {
975 pthread_rwlock_unlock(&r->st->rwlock);
980 inline static void rrdr_free(RRDR *r)
983 error("NULL value given!");
987 rrdr_unlock_rrdset(r);
988 if(likely(r->t)) free(r->t);
989 if(likely(r->v)) free(r->v);
990 if(likely(r->o)) free(r->o);
991 if(likely(r->od)) free(r->od);
995 inline void rrdr_done(RRDR *r)
1001 static RRDR *rrdr_create(RRDSET *st, int n)
1004 error("NULL value given!");
1008 RRDR *r = calloc(1, sizeof(RRDR));
1009 if(unlikely(!r)) goto cleanup;
1013 rrdr_lock_rrdset(r);
1016 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
1020 r->t = malloc(n * sizeof(time_t));
1021 if(unlikely(!r->t)) goto cleanup;
1023 r->v = malloc(n * r->d * sizeof(calculated_number));
1024 if(unlikely(!r->v)) goto cleanup;
1026 r->o = malloc(n * r->d * sizeof(uint8_t));
1027 if(unlikely(!r->o)) goto cleanup;
1029 r->od = malloc(r->d * sizeof(uint8_t));
1030 if(unlikely(!r->od)) goto cleanup;
1035 r->update_every = 1;
1040 error("Cannot allocate RRDR memory for %d entries", n);
1041 if(likely(r)) rrdr_free(r);
1045 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method)
1047 int debug = st->debug;
1049 time_t first_entry_t = rrdset_first_entry_t(st);
1050 time_t last_entry_t = rrdset_last_entry_t(st);
1052 if(before == 0 && after == 0) {
1053 before = last_entry_t;
1054 after = first_entry_t;
1057 // allow relative for before and after
1058 if(before <= st->update_every * st->entries)
1059 before = last_entry_t + before;
1061 if(after <= st->update_every * st->entries)
1062 after = last_entry_t + after;
1064 // make sure they are within our timeframe
1065 if(before > last_entry_t) before = last_entry_t;
1066 if(before < first_entry_t) before = first_entry_t;
1068 if(after > last_entry_t) after = last_entry_t;
1069 if(after < first_entry_t) after = first_entry_t;
1071 // check if they are upside down
1072 if(after > before) {
1073 time_t tmp = before;
1078 // the duration of the chart
1079 time_t duration = before - after;
1080 long available_points = duration / st->update_every;
1082 if(duration <= 0 || available_points <= 0)
1083 return rrdr_create(st, 1);
1085 // check the wanted points
1086 if(points < 0) points = -points;
1087 if(points > available_points) points = available_points;
1088 if(points == 0) points = available_points;
1090 // calculate proper grouping of source data
1091 long group = available_points / points;
1092 if(group <= 0) group = 1;
1094 // round group to the closest integer
1095 if(available_points % points > points / 2) group++;
1097 time_t after_new = after - (after % (group * st->update_every));
1098 time_t before_new = before - (before % (group * st->update_every));
1099 long points_new = (before_new - after_new) / st->update_every / group;
1101 // find the starting and ending slots in our round robin db
1102 long start_at_slot = rrdset_time2slot(st, before_new),
1103 stop_at_slot = rrdset_time2slot(st, after_new);
1105 #ifdef NETDATA_INTERNAL_CHECKS
1106 if(after_new < first_entry_t) {
1107 error("after_new %u is too small, minimum %u", after_new, first_entry_t);
1109 if(after_new > last_entry_t) {
1110 error("after_new %u is too big, maximum %u", after_new, last_entry_t);
1112 if(before_new < first_entry_t) {
1113 error("before_new %u is too small, minimum %u", before_new, first_entry_t);
1115 if(before_new > last_entry_t) {
1116 error("before_new %u is too big, maximum %u", before_new, last_entry_t);
1118 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1119 error("start_at_slot is invalid %ld, expected %ld to %ld", start_at_slot, 0, st->entries - 1);
1121 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1122 error("stop_at_slot is invalid %ld, expected %ld to %ld", stop_at_slot, 0, st->entries - 1);
1124 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1125 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1129 //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);
1132 before = before_new;
1133 duration = before - after;
1134 points = points_new;
1137 // before = the end time of the calculation
1138 // after = the start time of the calculation
1139 // duration = the duration of the calculation
1140 // group = the number of source points to aggregate / group together
1141 // method = the method of grouping source points
1142 // points = the number of points to generate
1145 // -------------------------------------------------------------------------
1146 // initialize our result set
1148 RRDR *r = rrdr_create(st, points);
1152 // find how many dimensions we have
1153 long dimensions = r->d;
1156 // -------------------------------------------------------------------------
1157 // checks for debugging
1159 if(debug) debug(D_RRD_STATS, "INFO %s first_t: %lu, last_t: %lu, all_duration: %lu, after: %lu, before: %lu, duration: %lu, points: %ld, group: %ld"
1163 , last_entry_t - first_entry_t
1172 // -------------------------------------------------------------------------
1173 // temp arrays for keeping values per dimension
1175 calculated_number group_values[dimensions]; // keep sums when grouping
1176 long group_counts[dimensions]; // keep the number of values added to group_values
1177 uint8_t group_options[dimensions];
1178 uint8_t found_non_zero[dimensions];
1184 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1185 group_values[c] = 0;
1186 group_counts[c] = 0;
1187 group_options[c] = 0;
1188 found_non_zero[c] = 0;
1192 // -------------------------------------------------------------------------
1195 time_t now = rrdset_slot2time(st, start_at_slot),
1196 dt = st->update_every,
1199 if(unlikely(debug)) debug(D_RRD_STATS, "BEGIN %s after_t: %lu (stop_at_t: %ld), before_t: %lu (start_at_t: %ld), start_t(now): %lu, current_entry: %ld, entries: %ld"
1211 r->update_every = group * st->update_every;
1215 //info("RRD2RRDR(): %s: STARTING", st->id);
1217 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1218 for(; !stop_now ; now -= dt, slot--, counter++) {
1219 if(unlikely(slot < 0)) slot = st->entries - 1;
1220 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1222 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %lu, %s %s"
1229 , (group_count + 1 == group)?"PRINT":" - "
1230 , (now >= after && now <= before)?"RANGE":" - "
1233 // make sure we return data in the proper time range
1234 if(unlikely(now > before)) continue;
1235 if(unlikely(now < after)) break;
1237 if(unlikely(group_count == 0)) {
1238 group_start_t = now;
1242 if(unlikely(group_count == group)) {
1243 if(unlikely(added >= points)) break;
1247 // do the calculations
1248 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1249 storage_number n = rd->values[slot];
1250 if(unlikely(!does_storage_number_exist(n))) continue;
1254 calculated_number value = unpack_storage_number(n);
1255 if(likely(value != 0.0)) {
1256 group_options[c] |= RRDR_NONZERO;
1257 found_non_zero[c] = 1;
1260 if(unlikely(did_storage_number_reset(n)))
1261 group_options[c] |= RRDR_RESET;
1263 switch(group_method) {
1265 if(unlikely(abs(value) > abs(group_values[c])))
1266 group_values[c] = value;
1272 group_values[c] += value;
1278 if(unlikely(add_this)) {
1279 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1283 calculated_number *cn = rrdr_line_values(r);
1284 uint8_t *co = rrdr_line_options(r);
1286 for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
1288 // update the dimension options
1289 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1290 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] |= RRDR_HIDDEN;
1292 // store the specific point options
1293 co[c] = group_options[c];
1296 if(unlikely(group_counts[c] == 0)) {
1298 co[c] |= RRDR_EMPTY;
1300 else if(unlikely(group_method == GROUP_AVERAGE)) {
1301 cn[c] = group_values[c] / group_counts[c];
1304 cn[c] = group_values[c];
1307 if(cn[c] < r->min) r->min = cn[c];
1308 if(cn[c] > r->max) r->max = cn[c];
1310 // reset them for the next loop
1311 group_values[c] = 0;
1312 group_counts[c] = 0;
1313 group_options[c] = 0;
1323 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1324 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1328 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)
1330 RRDR *r = rrd2rrdr(st, points, after, before, group_method);
1332 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1337 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1339 if(latest_timestamp && rrdr_rows(r) > 0)
1340 *latest_timestamp = r->before;
1343 case DATASOURCE_SSV:
1344 if(options & RRDR_OPTION_JSON_WRAP) {
1345 wb->contenttype = CT_APPLICATION_JSON;
1346 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1347 rrdr2ssv(r, wb, options, "", " ", "");
1348 rrdr_json_wrapper_end(r, wb, format, options, 1);
1351 wb->contenttype = CT_TEXT_PLAIN;
1352 rrdr2ssv(r, wb, options, "", " ", "");
1356 case DATASOURCE_SSV_COMMA:
1357 if(options & RRDR_OPTION_JSON_WRAP) {
1358 wb->contenttype = CT_APPLICATION_JSON;
1359 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1360 rrdr2ssv(r, wb, options, "", ",", "");
1361 rrdr_json_wrapper_end(r, wb, format, options, 1);
1364 wb->contenttype = CT_TEXT_PLAIN;
1365 rrdr2ssv(r, wb, options, "", ",", "");
1369 case DATASOURCE_JS_ARRAY:
1370 if(options & RRDR_OPTION_JSON_WRAP) {
1371 wb->contenttype = CT_APPLICATION_JSON;
1372 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1373 rrdr2ssv(r, wb, options, "[", ",", "]");
1374 rrdr_json_wrapper_end(r, wb, format, options, 0);
1377 wb->contenttype = CT_APPLICATION_JSON;
1378 rrdr2ssv(r, wb, options, "[", ",", "]");
1382 case DATASOURCE_CSV:
1383 if(options & RRDR_OPTION_JSON_WRAP) {
1384 wb->contenttype = CT_APPLICATION_JSON;
1385 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1386 rrdr2csv(r, wb, options, "", ",", "\\n");
1387 rrdr_json_wrapper_end(r, wb, format, options, 1);
1390 wb->contenttype = CT_TEXT_PLAIN;
1391 rrdr2csv(r, wb, options, "", ",", "\r\n");
1395 case DATASOURCE_TSV:
1396 if(options & RRDR_OPTION_JSON_WRAP) {
1397 wb->contenttype = CT_APPLICATION_JSON;
1398 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1399 rrdr2csv(r, wb, options, "", "\t", "\\n");
1400 rrdr_json_wrapper_end(r, wb, format, options, 1);
1403 wb->contenttype = CT_TEXT_PLAIN;
1404 rrdr2csv(r, wb, options, "", "\t", "\r\n");
1408 case DATASOURCE_HTML:
1409 if(options & RRDR_OPTION_JSON_WRAP) {
1410 wb->contenttype = CT_APPLICATION_JSON;
1411 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1412 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1413 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n");
1414 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1415 rrdr_json_wrapper_end(r, wb, format, options, 1);
1418 wb->contenttype = CT_TEXT_HTML;
1419 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1420 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n");
1421 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1425 case DATASOURCE_DATATABLE_JSONP:
1426 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1428 if(options & RRDR_OPTION_JSON_WRAP)
1429 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1431 rrdr2json(r, wb, options, 1);
1433 if(options & RRDR_OPTION_JSON_WRAP)
1434 rrdr_json_wrapper_end(r, wb, format, options, 0);
1437 case DATASOURCE_DATATABLE_JSON:
1438 wb->contenttype = CT_APPLICATION_JSON;
1440 if(options & RRDR_OPTION_JSON_WRAP)
1441 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1443 rrdr2json(r, wb, options, 1);
1445 if(options & RRDR_OPTION_JSON_WRAP)
1446 rrdr_json_wrapper_end(r, wb, format, options, 0);
1449 case DATASOURCE_JSONP:
1450 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1451 if(options & RRDR_OPTION_JSON_WRAP)
1452 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1454 rrdr2json(r, wb, options, 0);
1456 if(options & RRDR_OPTION_JSON_WRAP)
1457 rrdr_json_wrapper_end(r, wb, format, options, 0);
1460 case DATASOURCE_JSON:
1462 wb->contenttype = CT_APPLICATION_JSON;
1464 if(options & RRDR_OPTION_JSON_WRAP)
1465 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1467 rrdr2json(r, wb, options, 0);
1469 if(options & RRDR_OPTION_JSON_WRAP)
1470 rrdr_json_wrapper_end(r, wb, format, options, 0);
1478 unsigned long rrd_stats_json(int type, RRDSET *st, BUFFER *wb, int points, int group, int group_method, time_t after, time_t before, int only_non_zero)
1481 pthread_rwlock_rdlock(&st->rwlock);
1484 // -------------------------------------------------------------------------
1485 // switch from JSON to google JSON
1490 case DATASOURCE_DATATABLE_JSON:
1491 case DATASOURCE_DATATABLE_JSONP:
1496 case DATASOURCE_JSON:
1502 // -------------------------------------------------------------------------
1503 // validate the parameters
1505 if(points < 1) points = 1;
1506 if(group < 1) group = 1;
1508 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1509 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1513 // our return value (the last timestamp printed)
1514 // this is required to detect re-transmit in google JSONP
1515 time_t last_timestamp = 0;
1518 // -------------------------------------------------------------------------
1519 // find how many dimensions we have
1523 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1525 pthread_rwlock_unlock(&st->rwlock);
1526 buffer_strcat(wb, "No dimensions yet.");
1531 // -------------------------------------------------------------------------
1532 // prepare various strings, to speed up the loop
1534 char overflow_annotation[201]; snprintf(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);
1535 char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1536 char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1537 char post_date[21]; snprintf(post_date, 20, "%s}", sq);
1538 char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
1539 char post_value[21]; snprintf(post_value, 20, "}");
1542 // -------------------------------------------------------------------------
1543 // checks for debugging
1546 debug(D_RRD_STATS, "%s first_entry_t = %lu, last_entry_t = %lu, duration = %lu, after = %lu, before = %lu, duration = %lu, entries_to_show = %lu, group = %lu"
1548 , rrdset_first_entry_t(st)
1549 , rrdset_last_entry_t(st)
1550 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1559 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
1561 if((before - after) > st->entries * st->update_every)
1562 debug(D_RRD_STATS, "WARNING: %s The time difference between the oldest and the newest entries (%lu) is higher than the capacity of the database (%lu)", st->name, before - after, st->entries * st->update_every);
1566 // -------------------------------------------------------------------------
1567 // temp arrays for keeping values per dimension
1569 calculated_number group_values[dimensions]; // keep sums when grouping
1570 int print_hidden[dimensions]; // keep hidden flags
1571 int found_non_zero[dimensions];
1572 int found_non_existing[dimensions];
1575 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1576 group_values[c] = 0;
1577 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1578 found_non_zero[c] = 0;
1579 found_non_existing[c] = 0;
1583 // 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);
1584 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1585 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1587 // -------------------------------------------------------------------------
1588 // remove dimensions that contain only zeros
1591 if(only_non_zero) max_loop = 2;
1593 for(; max_loop ; max_loop--) {
1595 // -------------------------------------------------------------------------
1596 // print the JSON header
1598 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1599 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);
1600 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);
1601 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);
1603 // print the header for each dimension
1604 // and update the print_hidden array for the dimensions that should be hidden
1606 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1607 if(!print_hidden[c]) {
1609 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);
1613 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);
1616 // print the begin of row data
1617 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1620 // -------------------------------------------------------------------------
1623 int annotate_reset = 0;
1624 int annotation_count = 0;
1626 long t = rrdset_time2slot(st, before),
1627 stop_at_t = rrdset_time2slot(st, after),
1632 time_t now = rrdset_slot2time(st, t),
1633 dt = st->update_every;
1635 long count = 0, printed = 0, group_count = 0;
1638 if(st->debug) debug(D_RRD_STATS, "%s: REQUEST after:%lu before:%lu, points:%d, group:%d, CHART cur:%ld first: %lu last:%lu, CALC start_t:%ld, stop_t:%ld"
1645 , rrdset_first_entry_t(st)
1646 , rrdset_last_entry_t(st)
1652 for(; !stop_now ; now -= dt, t--, counter++) {
1653 if(t < 0) t = st->entries - 1;
1654 if(t == stop_at_t) stop_now = counter;
1658 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
1665 , (group_count + 1 == group)?"PRINT":" - "
1666 , (now >= after && now <= before)?"RANGE":" - "
1670 // make sure we return data in the proper time range
1671 if(now > before) continue;
1672 if(now < after) break;
1674 //if(rrdset_slot2time(st, t) != now)
1675 // 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));
1680 // check if we have to print this now
1681 if(group_count == group) {
1682 if(printed >= points) {
1683 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
1687 // generate the local date time
1688 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1689 if(!tm) { error("localtime() failed."); continue; }
1690 if(now > last_timestamp) last_timestamp = now;
1692 if(printed) buffer_strcat(wb, "]},\n");
1693 buffer_strcat(wb, pre_date);
1694 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1695 buffer_strcat(wb, post_date);
1700 // do the calculations
1701 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1702 storage_number n = rd->values[t];
1703 calculated_number value = unpack_storage_number(n);
1705 if(!does_storage_number_exist(n)) {
1707 found_non_existing[c]++;
1709 if(did_storage_number_reset(n)) annotate_reset = 1;
1711 switch(group_method) {
1713 if(abs(value) > abs(group_values[c])) group_values[c] = value;
1717 group_values[c] += value;
1722 group_values[c] += value;
1723 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
1729 if(annotate_reset) {
1731 buffer_strcat(wb, overflow_annotation);
1735 buffer_strcat(wb, normal_annotation);
1738 for(c = 0 ; c < dimensions ; c++) {
1739 if(found_non_existing[c] == group_count) {
1740 // all entries are non-existing
1742 buffer_strcat(wb, pre_value);
1743 buffer_strcat(wb, "null");
1744 buffer_strcat(wb, post_value);
1746 else if(!print_hidden[c]) {
1748 buffer_strcat(wb, pre_value);
1749 buffer_rrd_value(wb, group_values[c]);
1750 buffer_strcat(wb, post_value);
1752 if(group_values[c]) found_non_zero[c]++;
1755 // reset them for the next loop
1756 group_values[c] = 0;
1757 found_non_existing[c] = 0;
1760 // if all dimensions are hidden, print a null
1762 buffer_strcat(wb, pre_value);
1763 buffer_strcat(wb, "null");
1764 buffer_strcat(wb, post_value);
1772 if(printed) buffer_strcat(wb, "]}");
1773 buffer_strcat(wb, "\n ]\n}\n");
1775 if(only_non_zero && max_loop > 1) {
1777 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1778 group_values[c] = 0;
1779 found_non_existing[c] = 0;
1781 if(!print_hidden[c] && !found_non_zero[c]) {
1783 print_hidden[c] = 1;
1787 if(changed) buffer_flush(wb);
1794 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->len);
1796 pthread_rwlock_unlock(&st->rwlock);
1797 return last_timestamp;