3 void rrd_stats_api_v1_chart_with_data(RRDSET *st, BUFFER *wb, size_t *dimensions_count, size_t *memory_used)
5 pthread_rwlock_rdlock(&st->rwlock);
9 "\t\t\t\"id\": \"%s\",\n"
10 "\t\t\t\"name\": \"%s\",\n"
11 "\t\t\t\"type\": \"%s\",\n"
12 "\t\t\t\"family\": \"%s\",\n"
13 "\t\t\t\"context\": \"%s\",\n"
14 "\t\t\t\"title\": \"%s\",\n"
15 "\t\t\t\"priority\": %ld,\n"
16 "\t\t\t\"enabled\": %s,\n"
17 "\t\t\t\"units\": \"%s\",\n"
18 "\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
19 "\t\t\t\"chart_type\": \"%s\",\n"
20 "\t\t\t\"duration\": %ld,\n"
21 "\t\t\t\"first_entry\": %ld,\n"
22 "\t\t\t\"last_entry\": %ld,\n"
23 "\t\t\t\"update_every\": %d,\n"
24 "\t\t\t\"dimensions\": {\n"
32 , st->enabled?"true":"false"
35 , rrdset_type_name(st->chart_type)
36 , st->entries * st->update_every
37 , rrdset_first_entry_t(st)
38 , rrdset_last_entry_t(st)
42 unsigned long memory = st->memsize;
44 size_t dimensions = 0;
46 for(rd = st->dimensions; rd ; rd = rd->next) {
47 if(rd->flags & RRDDIM_FLAG_HIDDEN) continue;
49 memory += rd->memsize;
53 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
62 if(dimensions_count) *dimensions_count += dimensions;
63 if(memory_used) *memory_used += memory;
65 buffer_strcat(wb, "\n\t\t\t},\n\t\t\t\"green\": ");
66 buffer_rrd_value(wb, st->green);
67 buffer_strcat(wb, ",\n\t\t\t\"red\": ");
68 buffer_rrd_value(wb, st->red);
74 pthread_rwlock_unlock(&st->rwlock);
77 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb) {
78 rrd_stats_api_v1_chart_with_data(st, wb, NULL, NULL);
81 void rrd_stats_api_v1_charts(BUFFER *wb)
83 size_t c, dimensions = 0, memory = 0, alarms = 0;
86 buffer_sprintf(wb, "{\n"
87 "\t\"hostname\": \"%s\""
88 ",\n\t\"update_every\": %d"
89 ",\n\t\"history\": %d"
93 , rrd_default_history_entries
96 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
97 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
98 if(st->enabled && st->dimensions) {
99 if(c) buffer_strcat(wb, ",");
100 buffer_strcat(wb, "\n\t\t\"");
101 buffer_strcat(wb, st->id);
102 buffer_strcat(wb, "\": ");
103 rrd_stats_api_v1_chart_with_data(st, wb, &dimensions, &memory);
109 for(rc = localhost.alarms; rc ; rc = rc->next) {
113 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
115 buffer_sprintf(wb, "\n\t}"
116 ",\n\t\"charts_count\": %zu"
117 ",\n\t\"dimensions_count\": %zu"
118 ",\n\t\"alarms_count\": %zu"
119 ",\n\t\"rrd_memory_bytes\": %zu"
128 // ----------------------------------------------------------------------------
130 // /api/v1/allmetrics?format=prometheus
132 static inline size_t prometheus_name_copy(char *d, const char *s, size_t usable) {
135 for(n = 0; *s && n < usable ; d++, s++, n++) {
136 register char c = *s;
138 if(unlikely(!isalnum(c))) *d = '_';
146 #define PROMETHEUS_ELEMENT_MAX 256
148 void rrd_stats_api_v1_charts_allmetrics_prometheus(BUFFER *wb)
150 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
152 char host[PROMETHEUS_ELEMENT_MAX + 1];
153 prometheus_name_copy(host, config_get("global", "hostname", "localhost"), PROMETHEUS_ELEMENT_MAX);
157 for(st = localhost.rrdset_root; st ; st = st->next) {
158 char chart[PROMETHEUS_ELEMENT_MAX + 1];
159 prometheus_name_copy(chart, st->id, PROMETHEUS_ELEMENT_MAX);
161 buffer_strcat(wb, "\n");
162 if(st->enabled && st->dimensions) {
163 pthread_rwlock_rdlock(&st->rwlock);
165 // for each dimension
167 for(rd = st->dimensions; rd ; rd = rd->next) {
169 char dimension[PROMETHEUS_ELEMENT_MAX + 1];
170 prometheus_name_copy(dimension, rd->id, PROMETHEUS_ELEMENT_MAX);
172 // buffer_sprintf(wb, "# HELP %s.%s %s\n", st->id, rd->id, st->units);
174 switch(rd->algorithm) {
175 case RRDDIM_INCREMENTAL:
176 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
177 buffer_sprintf(wb, "# TYPE %s_%s counter\n", chart, dimension);
181 buffer_sprintf(wb, "# TYPE %s_%s gauge\n", chart, dimension);
185 // calculated_number n = (calculated_number)rd->last_collected_value * (calculated_number)(abs(rd->multiplier)) / (calculated_number)(abs(rd->divisor));
186 // buffer_sprintf(wb, "%s.%s " CALCULATED_NUMBER_FORMAT " %llu\n", st->id, rd->id, n,
187 // (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
189 buffer_sprintf(wb, "%s_%s{instance=\"%s\"} " COLLECTED_NUMBER_FORMAT " %llu\n",
190 chart, dimension, host, rd->last_collected_value,
191 (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
196 pthread_rwlock_unlock(&st->rwlock);
200 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
203 // ----------------------------------------------------------------------------
205 // /api/v1/allmetrics?format=bash
207 static inline size_t shell_name_copy(char *d, const char *s, size_t usable) {
210 for(n = 0; *s && n < usable ; d++, s++, n++) {
211 register char c = *s;
213 if(unlikely(!isalnum(c))) *d = '_';
214 else *d = (char)toupper(c);
221 #define SHELL_ELEMENT_MAX 100
223 void rrd_stats_api_v1_charts_allmetrics_shell(BUFFER *wb)
225 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
227 char host[SHELL_ELEMENT_MAX + 1];
228 shell_name_copy(host, config_get("global", "hostname", "localhost"), SHELL_ELEMENT_MAX);
232 for(st = localhost.rrdset_root; st ; st = st->next) {
233 calculated_number total = 0.0;
234 char chart[SHELL_ELEMENT_MAX + 1];
235 shell_name_copy(chart, st->id, SHELL_ELEMENT_MAX);
237 buffer_sprintf(wb, "\n# chart: %s (name: %s)\n", st->id, st->name);
238 if(st->enabled && st->dimensions) {
239 pthread_rwlock_rdlock(&st->rwlock);
241 // for each dimension
243 for(rd = st->dimensions; rd ; rd = rd->next) {
245 char dimension[SHELL_ELEMENT_MAX + 1];
246 shell_name_copy(dimension, rd->id, SHELL_ELEMENT_MAX);
248 calculated_number n = rd->last_stored_value;
250 if(isnan(n) || isinf(n))
251 buffer_sprintf(wb, "NETDATA_%s_%s=\"\" # %s\n", chart, dimension, st->units);
253 if(rd->multiplier < 0 || rd->divisor < 0) n = -n;
255 if(!(rd->flags & RRDDIM_FLAG_HIDDEN)) total += n;
256 buffer_sprintf(wb, "NETDATA_%s_%s=\"%0.0Lf\" # %s\n", chart, dimension, n, st->units);
261 total = roundl(total);
262 buffer_sprintf(wb, "NETDATA_%s_VISIBLETOTAL=\"%0.0Lf\" # %s\n", chart, total, st->units);
263 pthread_rwlock_unlock(&st->rwlock);
267 buffer_strcat(wb, "\n# NETDATA ALARMS RUNNING\n");
270 for(rc = localhost.alarms; rc ;rc = rc->next) {
271 if(!rc->rrdset) continue;
273 char chart[SHELL_ELEMENT_MAX + 1];
274 shell_name_copy(chart, rc->rrdset->id, SHELL_ELEMENT_MAX);
276 char alarm[SHELL_ELEMENT_MAX + 1];
277 shell_name_copy(alarm, rc->name, SHELL_ELEMENT_MAX);
279 calculated_number n = rc->value;
281 if(isnan(n) || isinf(n))
282 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_VALUE=\"\" # %s\n", chart, alarm, rc->units);
285 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_VALUE=\"%0.0Lf\" # %s\n", chart, alarm, n, rc->units);
288 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_STATUS=\"%s\"\n", chart, alarm, rrdcalc_status2string(rc->status));
291 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
294 // ----------------------------------------------------------------------------
296 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
298 time_t now = now_realtime_sec();
300 pthread_rwlock_rdlock(&st->rwlock);
304 "\t\t\t\"id\": \"%s\",\n"
305 "\t\t\t\"name\": \"%s\",\n"
306 "\t\t\t\"type\": \"%s\",\n"
307 "\t\t\t\"family\": \"%s\",\n"
308 "\t\t\t\"context\": \"%s\",\n"
309 "\t\t\t\"title\": \"%s\",\n"
310 "\t\t\t\"priority\": %ld,\n"
311 "\t\t\t\"enabled\": %d,\n"
312 "\t\t\t\"units\": \"%s\",\n"
313 "\t\t\t\"url\": \"/data/%s/%s\",\n"
314 "\t\t\t\"chart_type\": \"%s\",\n"
315 "\t\t\t\"counter\": %lu,\n"
316 "\t\t\t\"entries\": %ld,\n"
317 "\t\t\t\"first_entry_t\": %ld,\n"
318 "\t\t\t\"last_entry\": %lu,\n"
319 "\t\t\t\"last_entry_t\": %ld,\n"
320 "\t\t\t\"last_entry_secs_ago\": %ld,\n"
321 "\t\t\t\"update_every\": %d,\n"
322 "\t\t\t\"isdetail\": %d,\n"
323 "\t\t\t\"usec_since_last_update\": %llu,\n"
324 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
325 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
326 "\t\t\t\"dimensions\": [\n"
336 , st->name, options?options:""
337 , rrdset_type_name(st->chart_type)
340 , rrdset_first_entry_t(st)
341 , rrdset_last_slot(st)
342 , rrdset_last_entry_t(st)
343 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
346 , st->usec_since_last_update
347 , st->collected_total
348 , st->last_collected_total
351 unsigned long memory = st->memsize;
354 for(rd = st->dimensions; rd ; rd = rd->next) {
356 memory += rd->memsize;
360 "\t\t\t\t\t\"id\": \"%s\",\n"
361 "\t\t\t\t\t\"name\": \"%s\",\n"
362 "\t\t\t\t\t\"entries\": %ld,\n"
363 "\t\t\t\t\t\"isHidden\": %d,\n"
364 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
365 "\t\t\t\t\t\"multiplier\": %ld,\n"
366 "\t\t\t\t\t\"divisor\": %ld,\n"
367 "\t\t\t\t\t\"last_entry_t\": %ld,\n"
368 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
369 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
370 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
371 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
372 "\t\t\t\t\t\"memory\": %lu\n"
377 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
378 , rrddim_algorithm_name(rd->algorithm)
381 , rd->last_collected_time.tv_sec
382 , rd->collected_value
383 , rd->calculated_value
384 , rd->last_collected_value
385 , rd->last_calculated_value
393 "\t\t\t\"memory\" : %lu\n"
398 pthread_rwlock_unlock(&st->rwlock);
402 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
403 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
405 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
407 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
408 rrd_stats_one_json(st, options, wb);
409 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
412 void rrd_stats_all_json(BUFFER *wb)
414 unsigned long memory = 0;
418 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
420 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
421 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
422 if(st->enabled && st->dimensions) {
423 if(c) buffer_strcat(wb, ",\n");
424 memory += rrd_stats_one_json(st, NULL, wb);
428 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
430 buffer_sprintf(wb, "\n\t],\n"
431 "\t\"hostname\": \"%s\",\n"
432 "\t\"update_every\": %d,\n"
433 "\t\"history\": %d,\n"
434 "\t\"memory\": %lu\n"
438 , rrd_default_history_entries
445 // ----------------------------------------------------------------------------
447 // RRDR dimension options
448 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
449 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
450 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
451 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
453 // RRDR result options
454 #define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
455 #define RRDR_RESULT_OPTION_RELATIVE 0x00000002
457 typedef struct rrdresult {
458 RRDSET *st; // the chart this result refers to
460 uint32_t result_options; // RRDR_RESULT_OPTION_*
462 int d; // the number of dimensions
463 long n; // the number of values in the arrays
464 long rows; // the number of rows used
466 uint8_t *od; // the options for the dimensions
468 time_t *t; // array of n timestamps
469 calculated_number *v; // array n x d values
470 uint8_t *o; // array n x d options
472 long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
474 long group; // how many collected values were grouped for each row
475 int update_every; // what is the suggested update frequency in seconds
477 calculated_number min;
478 calculated_number max;
483 int has_st_lock; // if st is read locked by us
486 #define rrdr_rows(r) ((r)->rows)
489 static void rrdr_dump(RRDR *r)
494 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
496 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
497 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
500 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
501 , (r->od[c] & RRDR_RESET)?"RESET ":""
502 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
503 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
508 fprintf(stderr, "RRDR does not have any values in it.\n");
512 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
514 // for each line in the array
515 for(i = 0; i < r->rows ;i++) {
516 calculated_number *cn = &r->v[ i * r->d ];
517 uint8_t *co = &r->o[ i * r->d ];
519 // print the id and the timestamp of the line
520 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
522 // for each dimension
523 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
524 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
525 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
527 if(co[c] & RRDR_EMPTY)
528 fprintf(stderr, "null ");
530 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
532 , (co[c] & RRDR_EMPTY)?"E":" "
533 , (co[c] & RRDR_RESET)?"R":" "
534 , (co[c] & RRDR_HIDDEN)?"H":" "
535 , (co[c] & RRDR_NONZERO)?"N":" "
539 fprintf(stderr, "\n");
544 void rrdr_disable_not_selected_dimensions(RRDR *r, const char *dims)
546 char b[strlen(dims) + 1];
553 // disable all of them
554 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
555 r->od[c] |= RRDR_HIDDEN;
557 while(o && *o && (tok = mystrsep(&o, ",|"))) {
560 uint32_t hash = simple_hash(tok);
562 // find it and enable it
563 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
564 if(unlikely((hash == d->hash && !strcmp(d->id, tok)) || !strcmp(d->name, tok))) {
565 r->od[c] &= ~RRDR_HIDDEN;
567 // since the user needs this dimension
568 // make it appear as NONZERO, to return it
569 // even if the dimension has only zeros
570 r->od[c] |= RRDR_NONZERO;
576 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
579 case DATASOURCE_JSON:
580 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
583 case DATASOURCE_DATATABLE_JSON:
584 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
587 case DATASOURCE_DATATABLE_JSONP:
588 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
591 case DATASOURCE_JSONP:
592 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
596 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
600 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
604 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
607 case DATASOURCE_HTML:
608 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
611 case DATASOURCE_JS_ARRAY:
612 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
615 case DATASOURCE_SSV_COMMA:
616 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
620 buffer_strcat(wb, "unknown");
625 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
629 if(options & RRDR_OPTION_NONZERO) {
632 // commented due to #1514
634 //if(dims && *dims) {
635 // the caller wants specific dimensions
636 // disable NONZERO option
637 // to make sure we don't accidentally prevent
638 // the specific dimensions from being returned
642 // find how many dimensions are not zero
645 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
646 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
647 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
652 // if with nonzero we get i = 0 (no dimensions will be returned)
653 // disable nonzero to show all dimensions
654 if(!i) options &= ~RRDR_OPTION_NONZERO;
660 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
662 long rows = rrdr_rows(r);
666 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
667 char kq[2] = "", // key quote
668 sq[2] = ""; // string quote
670 if( options & RRDR_OPTION_GOOGLE_JSON ) {
679 buffer_sprintf(wb, "{\n"
682 " %sname%s: %s%s%s,\n"
683 " %sview_update_every%s: %d,\n"
684 " %supdate_every%s: %d,\n"
685 " %sfirst_entry%s: %u,\n"
686 " %slast_entry%s: %u,\n"
689 " %sdimension_names%s: ["
691 , kq, kq, sq, r->st->id, sq
692 , kq, kq, sq, r->st->name, sq
693 , kq, kq, r->update_every
694 , kq, kq, r->st->update_every
695 , kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
696 , kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
697 , kq, kq, (uint32_t)r->before
698 , kq, kq, (uint32_t)r->after
701 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
702 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
703 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
705 if(i) buffer_strcat(wb, ", ");
706 buffer_strcat(wb, sq);
707 buffer_strcat(wb, rd->name);
708 buffer_strcat(wb, sq);
712 #ifdef NETDATA_INTERNAL_CHECKS
713 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
716 buffer_strcat(wb, sq);
717 buffer_strcat(wb, "no data");
718 buffer_strcat(wb, sq);
721 buffer_sprintf(wb, "],\n"
722 " %sdimension_ids%s: ["
725 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
726 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
727 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
729 if(i) buffer_strcat(wb, ", ");
730 buffer_strcat(wb, sq);
731 buffer_strcat(wb, rd->id);
732 buffer_strcat(wb, sq);
737 buffer_strcat(wb, sq);
738 buffer_strcat(wb, "no data");
739 buffer_strcat(wb, sq);
742 buffer_sprintf(wb, "],\n"
743 " %slatest_values%s: ["
746 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
747 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
748 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
750 if(i) buffer_strcat(wb, ", ");
753 storage_number n = rd->values[rrdset_last_slot(r->st)];
755 if(!does_storage_number_exist(n))
756 buffer_strcat(wb, "null");
758 buffer_rrd_value(wb, unpack_storage_number(n));
762 buffer_strcat(wb, "null");
765 buffer_sprintf(wb, "],\n"
766 " %sview_latest_values%s: ["
771 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
772 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
773 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
775 if(i) buffer_strcat(wb, ", ");
778 calculated_number *cn = &r->v[ (0) * r->d ];
779 uint8_t *co = &r->o[ (0) * r->d ];
781 if(co[c] & RRDR_EMPTY)
782 buffer_strcat(wb, "null");
784 buffer_rrd_value(wb, cn[c]);
789 buffer_strcat(wb, "null");
792 buffer_sprintf(wb, "],\n"
793 " %sdimensions%s: %ld,\n"
794 " %spoints%s: %ld,\n"
801 rrdr_buffer_print_format(wb, format);
803 buffer_sprintf(wb, "%s,\n"
809 if(string_value) buffer_strcat(wb, sq);
810 //info("JSONWRAPPER(): %s: END", r->st->id);
813 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
817 char kq[2] = "", // key quote
818 sq[2] = ""; // string quote
820 if( options & RRDR_OPTION_GOOGLE_JSON ) {
829 if(string_value) buffer_strcat(wb, sq);
831 buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
832 buffer_rrd_value(wb, r->min);
833 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
834 buffer_rrd_value(wb, r->max);
835 buffer_strcat(wb, "\n}\n");
838 #define JSON_DATES_JS 1
839 #define JSON_DATES_TIMESTAMP 2
841 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
843 //info("RRD2JSON(): %s: BEGIN", r->st->id);
844 int row_annotations = 0, dates, dates_with_new = 0;
845 char kq[2] = "", // key quote
846 sq[2] = "", // string quote
847 pre_label[101] = "", // before each label
848 post_label[101] = "", // after each label
849 pre_date[101] = "", // the beginning of line, to the date
850 post_date[101] = "", // closing the date
851 pre_value[101] = "", // before each value
852 post_value[101] = "", // after each value
853 post_line[101] = "", // at the end of each row
854 normal_annotation[201] = "", // default row annotation
855 overflow_annotation[201] = "", // overflow row annotation
856 data_begin[101] = "", // between labels and values
857 finish[101] = ""; // at the end of everything
860 dates = JSON_DATES_JS;
861 if( options & RRDR_OPTION_GOOGLE_JSON ) {
870 snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
871 snprintfz(post_date, 100, "%s}", sq);
872 snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
873 snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
874 snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
875 strcpy(post_value, "}");
876 strcpy(post_line, "]}");
877 snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
878 strcpy(finish, "\n ]\n}");
880 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);
881 snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
883 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
884 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);
885 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);
886 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);
888 // remove the valueobjects flag
889 // google wants its own keys
890 if(options & RRDR_OPTION_OBJECTSROWS)
891 options &= ~RRDR_OPTION_OBJECTSROWS;
896 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
897 dates = JSON_DATES_TIMESTAMP;
901 dates = JSON_DATES_JS;
904 if( options & RRDR_OPTION_OBJECTSROWS )
905 strcpy(pre_date, " { ");
907 strcpy(pre_date, " [ ");
908 strcpy(pre_label, ", \"");
909 strcpy(post_label, "\"");
910 strcpy(pre_value, ", ");
911 if( options & RRDR_OPTION_OBJECTSROWS )
912 strcpy(post_line, "}");
914 strcpy(post_line, "]");
915 snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
916 strcpy(finish, "\n ]\n}");
918 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
919 buffer_sprintf(wb, "%stime%s", sq, sq);
922 // -------------------------------------------------------------------------
923 // print the JSON header
928 // print the header lines
929 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
930 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
931 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
933 buffer_strcat(wb, pre_label);
934 buffer_strcat(wb, rd->name);
935 buffer_strcat(wb, post_label);
939 buffer_strcat(wb, pre_label);
940 buffer_strcat(wb, "no data");
941 buffer_strcat(wb, post_label);
944 // print the begin of row data
945 buffer_strcat(wb, data_begin);
947 // if all dimensions are hidden, print a null
949 buffer_strcat(wb, finish);
953 long start = 0, end = rrdr_rows(r), step = 1;
954 if((options & RRDR_OPTION_REVERSED)) {
955 start = rrdr_rows(r) - 1;
960 // for each line in the array
961 calculated_number total = 1;
962 for(i = start; i != end ;i += step) {
963 calculated_number *cn = &r->v[ i * r->d ];
964 uint8_t *co = &r->o[ i * r->d ];
966 time_t now = r->t[i];
968 if(dates == JSON_DATES_JS) {
969 // generate the local date time
970 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
971 if(!tm) { error("localtime_r() failed."); continue; }
973 if(likely(i != start)) buffer_strcat(wb, ",\n");
974 buffer_strcat(wb, pre_date);
976 if( options & RRDR_OPTION_OBJECTSROWS )
977 buffer_sprintf(wb, "%stime%s: ", kq, kq);
980 buffer_strcat(wb, "new ");
982 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
984 buffer_strcat(wb, post_date);
986 if(row_annotations) {
987 // google supports one annotation per row
988 int annotation_found = 0;
989 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
990 if(co[c] & RRDR_RESET) {
991 buffer_strcat(wb, overflow_annotation);
992 annotation_found = 1;
996 if(!annotation_found)
997 buffer_strcat(wb, normal_annotation);
1001 // print the timestamp of the line
1002 if(likely(i != start)) buffer_strcat(wb, ",\n");
1003 buffer_strcat(wb, pre_date);
1005 if( options & RRDR_OPTION_OBJECTSROWS )
1006 buffer_sprintf(wb, "%stime%s: ", kq, kq);
1008 buffer_rrd_value(wb, (calculated_number)r->t[i]);
1010 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
1012 buffer_strcat(wb, post_date);
1015 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1017 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
1018 calculated_number n = cn[c];
1020 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1025 // prevent a division by zero
1026 if(total == 0) total = 1;
1029 // for each dimension
1030 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
1031 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1032 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1034 calculated_number n = cn[c];
1036 buffer_strcat(wb, pre_value);
1038 if( options & RRDR_OPTION_OBJECTSROWS )
1039 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
1041 if(co[c] & RRDR_EMPTY) {
1042 if(options & RRDR_OPTION_NULL2ZERO)
1043 buffer_strcat(wb, "0");
1045 buffer_strcat(wb, "null");
1048 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1051 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1052 n = n * 100 / total;
1054 buffer_rrd_value(wb, n);
1057 buffer_strcat(wb, post_value);
1060 buffer_strcat(wb, post_line);
1063 buffer_strcat(wb, finish);
1064 //info("RRD2JSON(): %s: END", r->st->id);
1067 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
1069 //info("RRD2CSV(): %s: BEGIN", r->st->id);
1073 // print the csv header
1074 for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1075 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1076 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1079 buffer_strcat(wb, startline);
1080 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1081 buffer_strcat(wb, "time");
1082 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1084 buffer_strcat(wb, separator);
1085 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1086 buffer_strcat(wb, d->name);
1087 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1090 buffer_strcat(wb, endline);
1093 // no dimensions present
1097 long start = 0, end = rrdr_rows(r), step = 1;
1098 if((options & RRDR_OPTION_REVERSED)) {
1099 start = rrdr_rows(r) - 1;
1104 // for each line in the array
1105 calculated_number total = 1;
1106 for(i = start; i != end ;i += step) {
1107 calculated_number *cn = &r->v[ i * r->d ];
1108 uint8_t *co = &r->o[ i * r->d ];
1110 buffer_strcat(wb, betweenlines);
1111 buffer_strcat(wb, startline);
1113 time_t now = r->t[i];
1115 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
1116 // print the timestamp of the line
1117 buffer_rrd_value(wb, (calculated_number)now);
1119 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
1122 // generate the local date time
1123 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1124 if(!tm) { error("localtime() failed."); continue; }
1125 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1128 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1130 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1131 calculated_number n = cn[c];
1133 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1138 // prevent a division by zero
1139 if(total == 0) total = 1;
1142 // for each dimension
1143 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1144 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1145 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1147 buffer_strcat(wb, separator);
1149 calculated_number n = cn[c];
1151 if(co[c] & RRDR_EMPTY) {
1152 if(options & RRDR_OPTION_NULL2ZERO)
1153 buffer_strcat(wb, "0");
1155 buffer_strcat(wb, "null");
1158 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1161 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1162 n = n * 100 / total;
1164 buffer_rrd_value(wb, n);
1168 buffer_strcat(wb, endline);
1170 //info("RRD2CSV(): %s: END", r->st->id);
1173 inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
1177 calculated_number *cn = &r->v[ i * r->d ];
1178 uint8_t *co = &r->o[ i * r->d ];
1180 calculated_number sum = 0, min = 0, max = 0, v;
1181 int all_null = 1, init = 1;
1183 calculated_number total = 1;
1184 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1186 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1187 calculated_number n = cn[c];
1189 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1194 // prevent a division by zero
1195 if(total == 0) total = 1;
1198 // for each dimension
1199 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1200 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1201 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1203 calculated_number n = cn[c];
1205 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1208 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1209 n = n * 100 / total;
1211 if(unlikely(init)) {
1223 if(likely(!(co[c] & RRDR_EMPTY))) {
1228 if(n < min) min = n;
1229 if(n > max) max = n;
1232 if(unlikely(all_null)) {
1233 if(likely(all_values_are_null))
1234 *all_values_are_null = 1;
1238 if(likely(all_values_are_null))
1239 *all_values_are_null = 0;
1242 if(options & RRDR_OPTION_MIN2MAX)
1250 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
1252 //info("RRD2SSV(): %s: BEGIN", r->st->id);
1255 buffer_strcat(wb, prefix);
1256 long start = 0, end = rrdr_rows(r), step = 1;
1257 if((options & RRDR_OPTION_REVERSED)) {
1258 start = rrdr_rows(r) - 1;
1263 // for each line in the array
1264 for(i = start; i != end ;i += step) {
1265 int all_values_are_null = 0;
1266 calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
1268 if(likely(i != start)) {
1269 if(r->min > v) r->min = v;
1270 if(r->max < v) r->max = v;
1277 if(likely(i != start))
1278 buffer_strcat(wb, separator);
1280 if(all_values_are_null) {
1281 if(options & RRDR_OPTION_NULL2ZERO)
1282 buffer_strcat(wb, "0");
1284 buffer_strcat(wb, "null");
1287 buffer_rrd_value(wb, v);
1289 buffer_strcat(wb, suffix);
1290 //info("RRD2SSV(): %s: END", r->st->id);
1293 inline static calculated_number *rrdr_line_values(RRDR *r)
1295 return &r->v[ r->c * r->d ];
1298 inline static uint8_t *rrdr_line_options(RRDR *r)
1300 return &r->o[ r->c * r->d ];
1303 inline static int rrdr_line_init(RRDR *r, time_t t)
1307 if(unlikely(r->c >= r->n)) {
1308 error("requested to step above RRDR size for chart %s", r->st->name);
1318 inline static void rrdr_lock_rrdset(RRDR *r) {
1320 error("NULL value given!");
1324 pthread_rwlock_rdlock(&r->st->rwlock);
1328 inline static void rrdr_unlock_rrdset(RRDR *r) {
1330 error("NULL value given!");
1334 if(likely(r->has_st_lock)) {
1335 pthread_rwlock_unlock(&r->st->rwlock);
1340 inline static void rrdr_free(RRDR *r)
1343 error("NULL value given!");
1347 rrdr_unlock_rrdset(r);
1355 static inline void rrdr_done(RRDR *r)
1361 static RRDR *rrdr_create(RRDSET *st, long n)
1364 error("NULL value given!");
1368 RRDR *r = callocz(1, sizeof(RRDR));
1371 rrdr_lock_rrdset(r);
1374 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
1378 r->t = mallocz(n * sizeof(time_t));
1379 r->v = mallocz(n * r->d * sizeof(calculated_number));
1380 r->o = mallocz(n * r->d * sizeof(uint8_t));
1381 r->od = mallocz(r->d * sizeof(uint8_t));
1383 // set the hidden flag on hidden dimensions
1385 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1386 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] = RRDR_HIDDEN;
1392 r->update_every = 1;
1397 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
1399 int debug = st->debug;
1400 int absolute_period_requested = -1;
1402 time_t first_entry_t = rrdset_first_entry_t(st);
1403 time_t last_entry_t = rrdset_last_entry_t(st);
1405 if(before == 0 && after == 0) {
1406 // dump the all the data
1407 before = last_entry_t;
1408 after = first_entry_t;
1409 absolute_period_requested = 0;
1412 // allow relative for before (smaller than API_RELATIVE_TIME_MAX)
1413 if(((before < 0)?-before:before) <= API_RELATIVE_TIME_MAX) {
1414 if(abs(before) % st->update_every) {
1415 // make sure it is multiple of st->update_every
1416 if(before < 0) before = before - st->update_every - before % st->update_every;
1417 else before = before + st->update_every - before % st->update_every;
1419 if(before > 0) before = first_entry_t + before;
1420 else before = last_entry_t + before;
1421 absolute_period_requested = 0;
1424 // allow relative for after (smaller than API_RELATIVE_TIME_MAX)
1425 if(((after < 0)?-after:after) <= API_RELATIVE_TIME_MAX) {
1426 if(after == 0) after = -st->update_every;
1427 if(abs(after) % st->update_every) {
1428 // make sure it is multiple of st->update_every
1429 if(after < 0) after = after - st->update_every - after % st->update_every;
1430 else after = after + st->update_every - after % st->update_every;
1432 after = before + after;
1433 absolute_period_requested = 0;
1436 if(absolute_period_requested == -1)
1437 absolute_period_requested = 1;
1439 // make sure they are within our timeframe
1440 if(before > last_entry_t) before = last_entry_t;
1441 if(before < first_entry_t) before = first_entry_t;
1443 if(after > last_entry_t) after = last_entry_t;
1444 if(after < first_entry_t) after = first_entry_t;
1446 // check if they are upside down
1447 if(after > before) {
1448 time_t tmp = before;
1453 // the duration of the chart
1454 time_t duration = before - after;
1455 long available_points = duration / st->update_every;
1457 if(duration <= 0 || available_points <= 0)
1458 return rrdr_create(st, 1);
1460 // check the wanted points
1461 if(points < 0) points = -points;
1462 if(points > available_points) points = available_points;
1463 if(points == 0) points = available_points;
1465 // calculate proper grouping of source data
1466 long group = available_points / points;
1467 if(group <= 0) group = 1;
1469 // round group to the closest integer
1470 if(available_points % points > points / 2) group++;
1472 time_t after_new = (aligned) ? (after - (after % (group * st->update_every))) : after;
1473 time_t before_new = (aligned) ? (before - (before % (group * st->update_every))) : before;
1474 long points_new = (before_new - after_new) / st->update_every / group;
1476 // find the starting and ending slots in our round robin db
1477 long start_at_slot = rrdset_time2slot(st, before_new),
1478 stop_at_slot = rrdset_time2slot(st, after_new);
1480 #ifdef NETDATA_INTERNAL_CHECKS
1481 if(after_new < first_entry_t) {
1482 error("after_new %u is too small, minimum %u", (uint32_t)after_new, (uint32_t)first_entry_t);
1484 if(after_new > last_entry_t) {
1485 error("after_new %u is too big, maximum %u", (uint32_t)after_new, (uint32_t)last_entry_t);
1487 if(before_new < first_entry_t) {
1488 error("before_new %u is too small, minimum %u", (uint32_t)before_new, (uint32_t)first_entry_t);
1490 if(before_new > last_entry_t) {
1491 error("before_new %u is too big, maximum %u", (uint32_t)before_new, (uint32_t)last_entry_t);
1493 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1494 error("start_at_slot is invalid %ld, expected 0 to %ld", start_at_slot, st->entries - 1);
1496 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1497 error("stop_at_slot is invalid %ld, expected 0 to %ld", stop_at_slot, st->entries - 1);
1499 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1500 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1504 //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);
1507 before = before_new;
1508 duration = before - after;
1509 points = points_new;
1512 // before = the end time of the calculation
1513 // after = the start time of the calculation
1514 // duration = the duration of the calculation
1515 // group = the number of source points to aggregate / group together
1516 // method = the method of grouping source points
1517 // points = the number of points to generate
1520 // -------------------------------------------------------------------------
1521 // initialize our result set
1523 RRDR *r = rrdr_create(st, points);
1525 #ifdef NETDATA_INTERNAL_CHECKS
1526 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);
1531 #ifdef NETDATA_INTERNAL_CHECKS
1532 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);
1537 if(absolute_period_requested == 1)
1538 r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1540 r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1542 // find how many dimensions we have
1543 long dimensions = r->d;
1546 // -------------------------------------------------------------------------
1547 // checks for debugging
1549 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"
1551 , (uint32_t)first_entry_t
1552 , (uint32_t)last_entry_t
1553 , (uint32_t)(last_entry_t - first_entry_t)
1556 , (uint32_t)duration
1562 // -------------------------------------------------------------------------
1563 // temp arrays for keeping values per dimension
1565 calculated_number last_values[dimensions]; // keep the last value of each dimension
1566 calculated_number group_values[dimensions]; // keep sums when grouping
1567 long group_counts[dimensions]; // keep the number of values added to group_values
1568 uint8_t group_options[dimensions];
1569 uint8_t found_non_zero[dimensions];
1575 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1577 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1578 group_counts[c] = 0;
1579 group_options[c] = 0;
1580 found_non_zero[c] = 0;
1584 // -------------------------------------------------------------------------
1587 time_t now = rrdset_slot2time(st, start_at_slot),
1588 dt = st->update_every,
1591 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"
1603 r->update_every = group * st->update_every;
1607 //info("RRD2RRDR(): %s: STARTING", st->id);
1609 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1610 for(; !stop_now ; now -= dt, slot--, counter++) {
1611 if(unlikely(slot < 0)) slot = st->entries - 1;
1612 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1614 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %ld, %s %s"
1621 , (group_count + 1 == group)?"PRINT":" - "
1622 , (now >= after && now <= before)?"RANGE":" - "
1625 // make sure we return data in the proper time range
1626 if(unlikely(now > before)) continue;
1627 if(unlikely(now < after)) break;
1629 if(unlikely(group_count == 0)) {
1630 group_start_t = now;
1634 if(unlikely(group_count == group)) {
1635 if(unlikely(added >= points)) break;
1639 // do the calculations
1640 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1641 storage_number n = rd->values[slot];
1642 if(unlikely(!does_storage_number_exist(n))) continue;
1646 calculated_number value = unpack_storage_number(n);
1647 if(likely(value != 0.0)) {
1648 group_options[c] |= RRDR_NONZERO;
1649 found_non_zero[c] = 1;
1652 if(unlikely(did_storage_number_reset(n)))
1653 group_options[c] |= RRDR_RESET;
1655 switch(group_method) {
1657 if(unlikely(isnan(group_values[c])) ||
1658 fabsl(value) < fabsl(group_values[c]))
1659 group_values[c] = value;
1663 if(unlikely(isnan(group_values[c])) ||
1664 fabsl(value) > fabsl(group_values[c]))
1665 group_values[c] = value;
1671 case GROUP_UNDEFINED:
1672 group_values[c] += value;
1675 case GROUP_INCREMENTAL_SUM:
1676 if(unlikely(slot == start_at_slot))
1677 last_values[c] = value;
1679 group_values[c] += last_values[c] - value;
1680 last_values[c] = value;
1686 if(unlikely(add_this)) {
1687 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1691 calculated_number *cn = rrdr_line_values(r);
1692 uint8_t *co = rrdr_line_options(r);
1694 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1696 // update the dimension options
1697 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1699 // store the specific point options
1700 co[c] = group_options[c];
1703 if(unlikely(group_counts[c] == 0)) {
1705 co[c] |= RRDR_EMPTY;
1706 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1709 switch(group_method) {
1712 if(unlikely(isnan(group_values[c])))
1715 cn[c] = group_values[c];
1716 group_values[c] = NAN;
1721 case GROUP_INCREMENTAL_SUM:
1722 cn[c] = group_values[c];
1723 group_values[c] = 0;
1728 case GROUP_UNDEFINED:
1729 cn[c] = group_values[c] / group_counts[c];
1730 group_values[c] = 0;
1734 if(cn[c] < r->min) r->min = cn[c];
1735 if(cn[c] > r->max) r->max = cn[c];
1738 // reset for the next loop
1739 group_counts[c] = 0;
1740 group_options[c] = 0;
1750 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1751 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1755 int rrd2value(RRDSET *st, BUFFER *wb, calculated_number *n, const char *dimensions, long points, long long after, long long before, int group_method, uint32_t options, time_t *db_after, time_t *db_before, int *value_is_null)
1757 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1759 if(value_is_null) *value_is_null = 1;
1763 if(rrdr_rows(r) == 0) {
1766 if(db_after) *db_after = 0;
1767 if(db_before) *db_before = 0;
1768 if(value_is_null) *value_is_null = 1;
1773 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1774 buffer_no_cacheable(wb);
1775 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1776 buffer_cacheable(wb);
1778 options = rrdr_check_options(r, options, dimensions);
1781 rrdr_disable_not_selected_dimensions(r, dimensions);
1783 if(db_after) *db_after = r->after;
1784 if(db_before) *db_before = r->before;
1786 long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
1787 *n = rrdr2value(r, i, options, value_is_null);
1793 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)
1795 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1797 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1801 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1802 buffer_no_cacheable(wb);
1803 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1804 buffer_cacheable(wb);
1806 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1809 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1811 if(latest_timestamp && rrdr_rows(r) > 0)
1812 *latest_timestamp = r->before;
1815 case DATASOURCE_SSV:
1816 if(options & RRDR_OPTION_JSON_WRAP) {
1817 wb->contenttype = CT_APPLICATION_JSON;
1818 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1819 rrdr2ssv(r, wb, options, "", " ", "");
1820 rrdr_json_wrapper_end(r, wb, format, options, 1);
1823 wb->contenttype = CT_TEXT_PLAIN;
1824 rrdr2ssv(r, wb, options, "", " ", "");
1828 case DATASOURCE_SSV_COMMA:
1829 if(options & RRDR_OPTION_JSON_WRAP) {
1830 wb->contenttype = CT_APPLICATION_JSON;
1831 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1832 rrdr2ssv(r, wb, options, "", ",", "");
1833 rrdr_json_wrapper_end(r, wb, format, options, 1);
1836 wb->contenttype = CT_TEXT_PLAIN;
1837 rrdr2ssv(r, wb, options, "", ",", "");
1841 case DATASOURCE_JS_ARRAY:
1842 if(options & RRDR_OPTION_JSON_WRAP) {
1843 wb->contenttype = CT_APPLICATION_JSON;
1844 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1845 rrdr2ssv(r, wb, options, "[", ",", "]");
1846 rrdr_json_wrapper_end(r, wb, format, options, 0);
1849 wb->contenttype = CT_APPLICATION_JSON;
1850 rrdr2ssv(r, wb, options, "[", ",", "]");
1854 case DATASOURCE_CSV:
1855 if(options & RRDR_OPTION_JSON_WRAP) {
1856 wb->contenttype = CT_APPLICATION_JSON;
1857 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1858 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1859 rrdr_json_wrapper_end(r, wb, format, options, 1);
1862 wb->contenttype = CT_TEXT_PLAIN;
1863 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1867 case DATASOURCE_CSV_JSON_ARRAY:
1868 wb->contenttype = CT_APPLICATION_JSON;
1869 if(options & RRDR_OPTION_JSON_WRAP) {
1870 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1871 buffer_strcat(wb, "[\n");
1872 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1873 buffer_strcat(wb, "\n]");
1874 rrdr_json_wrapper_end(r, wb, format, options, 0);
1877 wb->contenttype = CT_TEXT_PLAIN;
1878 buffer_strcat(wb, "[\n");
1879 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1880 buffer_strcat(wb, "\n]");
1884 case DATASOURCE_TSV:
1885 if(options & RRDR_OPTION_JSON_WRAP) {
1886 wb->contenttype = CT_APPLICATION_JSON;
1887 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1888 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1889 rrdr_json_wrapper_end(r, wb, format, options, 1);
1892 wb->contenttype = CT_TEXT_PLAIN;
1893 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1897 case DATASOURCE_HTML:
1898 if(options & RRDR_OPTION_JSON_WRAP) {
1899 wb->contenttype = CT_APPLICATION_JSON;
1900 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1901 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1902 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1903 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1904 rrdr_json_wrapper_end(r, wb, format, options, 1);
1907 wb->contenttype = CT_TEXT_HTML;
1908 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1909 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1910 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1914 case DATASOURCE_DATATABLE_JSONP:
1915 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1917 if(options & RRDR_OPTION_JSON_WRAP)
1918 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1920 rrdr2json(r, wb, options, 1);
1922 if(options & RRDR_OPTION_JSON_WRAP)
1923 rrdr_json_wrapper_end(r, wb, format, options, 0);
1926 case DATASOURCE_DATATABLE_JSON:
1927 wb->contenttype = CT_APPLICATION_JSON;
1929 if(options & RRDR_OPTION_JSON_WRAP)
1930 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1932 rrdr2json(r, wb, options, 1);
1934 if(options & RRDR_OPTION_JSON_WRAP)
1935 rrdr_json_wrapper_end(r, wb, format, options, 0);
1938 case DATASOURCE_JSONP:
1939 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1940 if(options & RRDR_OPTION_JSON_WRAP)
1941 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1943 rrdr2json(r, wb, options, 0);
1945 if(options & RRDR_OPTION_JSON_WRAP)
1946 rrdr_json_wrapper_end(r, wb, format, options, 0);
1949 case DATASOURCE_JSON:
1951 wb->contenttype = CT_APPLICATION_JSON;
1953 if(options & RRDR_OPTION_JSON_WRAP)
1954 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1956 rrdr2json(r, wb, options, 0);
1958 if(options & RRDR_OPTION_JSON_WRAP)
1959 rrdr_json_wrapper_end(r, wb, format, options, 0);
1967 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)
1970 pthread_rwlock_rdlock(&st->rwlock);
1973 // -------------------------------------------------------------------------
1974 // switch from JSON to google JSON
1979 case DATASOURCE_DATATABLE_JSON:
1980 case DATASOURCE_DATATABLE_JSONP:
1985 case DATASOURCE_JSON:
1991 // -------------------------------------------------------------------------
1992 // validate the parameters
1994 if(points < 1) points = 1;
1995 if(group < 1) group = 1;
1997 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1998 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
2002 // our return value (the last timestamp printed)
2003 // this is required to detect re-transmit in google JSONP
2004 time_t last_timestamp = 0;
2007 // -------------------------------------------------------------------------
2008 // find how many dimensions we have
2012 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
2014 pthread_rwlock_unlock(&st->rwlock);
2015 buffer_strcat(wb, "No dimensions yet.");
2020 // -------------------------------------------------------------------------
2021 // prepare various strings, to speed up the loop
2023 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);
2024 char normal_annotation[201]; snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
2025 char pre_date[51]; snprintfz(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
2026 char post_date[21]; snprintfz(post_date, 20, "%s}", sq);
2027 char pre_value[21]; snprintfz(pre_value, 20, ",{%sv%s:", kq, kq);
2028 char post_value[21]; strcpy(post_value, "}");
2031 // -------------------------------------------------------------------------
2032 // checks for debugging
2035 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"
2037 , rrdset_first_entry_t(st)
2038 , rrdset_last_entry_t(st)
2039 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
2048 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%ld) is earlier than the oldest (%ld)", st->name, before, after);
2050 if((before - after) > st->entries * st->update_every)
2051 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);
2055 // -------------------------------------------------------------------------
2056 // temp arrays for keeping values per dimension
2058 calculated_number group_values[dimensions]; // keep sums when grouping
2059 int print_hidden[dimensions]; // keep hidden flags
2060 int found_non_zero[dimensions];
2061 int found_non_existing[dimensions];
2064 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2065 group_values[c] = 0;
2066 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
2067 found_non_zero[c] = 0;
2068 found_non_existing[c] = 0;
2072 // 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);
2073 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
2074 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
2076 // -------------------------------------------------------------------------
2077 // remove dimensions that contain only zeros
2080 if(only_non_zero) max_loop = 2;
2082 for(; max_loop ; max_loop--) {
2084 // -------------------------------------------------------------------------
2085 // print the JSON header
2087 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
2088 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);
2089 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);
2090 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);
2092 // print the header for each dimension
2093 // and update the print_hidden array for the dimensions that should be hidden
2095 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2096 if(!print_hidden[c]) {
2098 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);
2102 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);
2105 // print the begin of row data
2106 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
2109 // -------------------------------------------------------------------------
2112 int annotate_reset = 0;
2113 int annotation_count = 0;
2115 long t = rrdset_time2slot(st, before),
2116 stop_at_t = rrdset_time2slot(st, after),
2121 time_t now = rrdset_slot2time(st, t),
2122 dt = st->update_every;
2124 long count = 0, printed = 0, group_count = 0;
2127 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"
2134 , (uint32_t)rrdset_first_entry_t(st)
2135 , (uint32_t)rrdset_last_entry_t(st)
2141 for(; !stop_now ; now -= dt, t--, counter++) {
2142 if(t < 0) t = st->entries - 1;
2143 if(t == stop_at_t) stop_now = counter;
2147 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %ld, %s %s"
2154 , (group_count + 1 == group)?"PRINT":" - "
2155 , (now >= after && now <= before)?"RANGE":" - "
2159 // make sure we return data in the proper time range
2160 if(now > before) continue;
2161 if(now < after) break;
2163 //if(rrdset_slot2time(st, t) != now)
2164 // 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));
2169 // check if we have to print this now
2170 if(group_count == group) {
2171 if(printed >= points) {
2172 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
2176 // generate the local date time
2177 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
2178 if(!tm) { error("localtime() failed."); continue; }
2179 if(now > last_timestamp) last_timestamp = now;
2181 if(printed) buffer_strcat(wb, "]},\n");
2182 buffer_strcat(wb, pre_date);
2183 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
2184 buffer_strcat(wb, post_date);
2189 // do the calculations
2190 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2191 storage_number n = rd->values[t];
2192 calculated_number value = unpack_storage_number(n);
2194 if(!does_storage_number_exist(n)) {
2196 found_non_existing[c]++;
2198 if(did_storage_number_reset(n)) annotate_reset = 1;
2200 switch(group_method) {
2202 if(abs(value) > abs(group_values[c])) group_values[c] = value;
2206 group_values[c] += value;
2211 group_values[c] += value;
2212 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
2218 if(annotate_reset) {
2220 buffer_strcat(wb, overflow_annotation);
2224 buffer_strcat(wb, normal_annotation);
2227 for(c = 0 ; c < dimensions ; c++) {
2228 if(found_non_existing[c] == group_count) {
2229 // all entries are non-existing
2231 buffer_strcat(wb, pre_value);
2232 buffer_strcat(wb, "null");
2233 buffer_strcat(wb, post_value);
2235 else if(!print_hidden[c]) {
2237 buffer_strcat(wb, pre_value);
2238 buffer_rrd_value(wb, group_values[c]);
2239 buffer_strcat(wb, post_value);
2241 if(group_values[c]) found_non_zero[c]++;
2244 // reset them for the next loop
2245 group_values[c] = 0;
2246 found_non_existing[c] = 0;
2249 // if all dimensions are hidden, print a null
2251 buffer_strcat(wb, pre_value);
2252 buffer_strcat(wb, "null");
2253 buffer_strcat(wb, post_value);
2261 if(printed) buffer_strcat(wb, "]}");
2262 buffer_strcat(wb, "\n ]\n}\n");
2264 if(only_non_zero && max_loop > 1) {
2266 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2267 group_values[c] = 0;
2268 found_non_existing[c] = 0;
2270 if(!print_hidden[c] && !found_non_zero[c]) {
2272 print_hidden[c] = 1;
2276 if(changed) buffer_flush(wb);
2283 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %lu bytes", st->name, wb->len);
2285 pthread_rwlock_unlock(&st->rwlock);
2286 return last_timestamp;