3 void rrd_stats_api_v1_chart_with_data(RRDSET *st, BUFFER *wb, size_t *dimensions_count, size_t *memory_used)
5 pthread_rwlock_rdlock(&st->rwlock);
9 "\t\t\t\"id\": \"%s\",\n"
10 "\t\t\t\"name\": \"%s\",\n"
11 "\t\t\t\"type\": \"%s\",\n"
12 "\t\t\t\"family\": \"%s\",\n"
13 "\t\t\t\"context\": \"%s\",\n"
14 "\t\t\t\"title\": \"%s\",\n"
15 "\t\t\t\"priority\": %ld,\n"
16 "\t\t\t\"enabled\": %s,\n"
17 "\t\t\t\"units\": \"%s\",\n"
18 "\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
19 "\t\t\t\"chart_type\": \"%s\",\n"
20 "\t\t\t\"duration\": %ld,\n"
21 "\t\t\t\"first_entry\": %ld,\n"
22 "\t\t\t\"last_entry\": %ld,\n"
23 "\t\t\t\"update_every\": %d,\n"
24 "\t\t\t\"dimensions\": {\n"
32 , st->enabled?"true":"false"
35 , rrdset_type_name(st->chart_type)
36 , st->entries * st->update_every
37 , rrdset_first_entry_t(st)
38 , rrdset_last_entry_t(st)
42 unsigned long memory = st->memsize;
44 size_t dimensions = 0;
46 for(rd = st->dimensions; rd ; rd = rd->next) {
47 if(rd->flags & RRDDIM_FLAG_HIDDEN) continue;
49 memory += rd->memsize;
53 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
62 if(dimensions_count) *dimensions_count += dimensions;
63 if(memory_used) *memory_used += memory;
65 buffer_strcat(wb, "\n\t\t\t},\n\t\t\t\"green\": ");
66 buffer_rrd_value(wb, st->green);
67 buffer_strcat(wb, ",\n\t\t\t\"red\": ");
68 buffer_rrd_value(wb, st->red);
74 pthread_rwlock_unlock(&st->rwlock);
77 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb) {
78 rrd_stats_api_v1_chart_with_data(st, wb, NULL, NULL);
81 void rrd_stats_api_v1_charts(BUFFER *wb)
83 size_t c, dimensions = 0, memory = 0, alarms = 0;
86 buffer_sprintf(wb, "{\n"
87 "\t\"hostname\": \"%s\""
88 ",\n\t\"update_every\": %d"
89 ",\n\t\"history\": %d"
93 , rrd_default_history_entries
96 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
97 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
98 if(st->enabled && st->dimensions) {
99 if(c) buffer_strcat(wb, ",");
100 buffer_strcat(wb, "\n\t\t\"");
101 buffer_strcat(wb, st->id);
102 buffer_strcat(wb, "\": ");
103 rrd_stats_api_v1_chart_with_data(st, wb, &dimensions, &memory);
109 for(rc = localhost.alarms; rc ; rc = rc->next) {
112 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
114 buffer_sprintf(wb, "\n\t}"
115 ",\n\t\"charts_count\": %zu"
116 ",\n\t\"dimensions_count\": %zu"
117 ",\n\t\"alarms_count\": %zu"
118 ",\n\t\"rrd_memory_bytes\": %zu"
127 static inline size_t prometheus_name_copy(char *d, const char *s, size_t usable) {
130 for(n = 0; *s && n < usable ; d++, s++, n++) {
131 register char c = *s;
133 if(unlikely(!isalnum(c))) *d = '_';
141 #define PROMETHEUS_ELEMENT_MAX 256
143 void rrd_stats_api_v1_charts_allmetrics_prometheus(BUFFER *wb)
145 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
147 char host[PROMETHEUS_ELEMENT_MAX + 1];
148 prometheus_name_copy(host, config_get("global", "hostname", "localhost"), PROMETHEUS_ELEMENT_MAX);
152 for(st = localhost.rrdset_root; st ; st = st->next) {
153 char chart[PROMETHEUS_ELEMENT_MAX + 1];
154 prometheus_name_copy(chart, st->id, PROMETHEUS_ELEMENT_MAX);
156 buffer_strcat(wb, "\n");
157 if(st->enabled && st->dimensions) {
158 // for each dimension
160 for(rd = st->dimensions; rd ; rd = rd->next) {
163 char dimension[PROMETHEUS_ELEMENT_MAX + 1];
164 prometheus_name_copy(dimension, rd->id, PROMETHEUS_ELEMENT_MAX);
166 // buffer_sprintf(wb, "# HELP %s.%s %s\n", st->id, rd->id, st->units);
168 switch(rd->algorithm) {
169 case RRDDIM_INCREMENTAL:
170 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
171 buffer_sprintf(wb, "# TYPE %s_%s counter\n", chart, dimension);
175 buffer_sprintf(wb, "# TYPE %s_%s gauge\n", chart, dimension);
179 // calculated_number n = (calculated_number)rd->last_collected_value * (calculated_number)(abs(rd->multiplier)) / (calculated_number)(abs(rd->divisor));
180 // buffer_sprintf(wb, "%s.%s " CALCULATED_NUMBER_FORMAT " %llu\n", st->id, rd->id, n,
181 // (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
183 buffer_sprintf(wb, "%s_%s{instance=\"%s\"} " COLLECTED_NUMBER_FORMAT " %llu\n",
184 chart, dimension, host, rd->last_collected_value,
185 (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
191 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
194 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
196 time_t now = now_realtime_sec();
198 pthread_rwlock_rdlock(&st->rwlock);
202 "\t\t\t\"id\": \"%s\",\n"
203 "\t\t\t\"name\": \"%s\",\n"
204 "\t\t\t\"type\": \"%s\",\n"
205 "\t\t\t\"family\": \"%s\",\n"
206 "\t\t\t\"context\": \"%s\",\n"
207 "\t\t\t\"title\": \"%s\",\n"
208 "\t\t\t\"priority\": %ld,\n"
209 "\t\t\t\"enabled\": %d,\n"
210 "\t\t\t\"units\": \"%s\",\n"
211 "\t\t\t\"url\": \"/data/%s/%s\",\n"
212 "\t\t\t\"chart_type\": \"%s\",\n"
213 "\t\t\t\"counter\": %lu,\n"
214 "\t\t\t\"entries\": %ld,\n"
215 "\t\t\t\"first_entry_t\": %ld,\n"
216 "\t\t\t\"last_entry\": %lu,\n"
217 "\t\t\t\"last_entry_t\": %ld,\n"
218 "\t\t\t\"last_entry_secs_ago\": %ld,\n"
219 "\t\t\t\"update_every\": %d,\n"
220 "\t\t\t\"isdetail\": %d,\n"
221 "\t\t\t\"usec_since_last_update\": %llu,\n"
222 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
223 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
224 "\t\t\t\"dimensions\": [\n"
234 , st->name, options?options:""
235 , rrdset_type_name(st->chart_type)
238 , rrdset_first_entry_t(st)
239 , rrdset_last_slot(st)
240 , rrdset_last_entry_t(st)
241 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
244 , st->usec_since_last_update
245 , st->collected_total
246 , st->last_collected_total
249 unsigned long memory = st->memsize;
252 for(rd = st->dimensions; rd ; rd = rd->next) {
254 memory += rd->memsize;
258 "\t\t\t\t\t\"id\": \"%s\",\n"
259 "\t\t\t\t\t\"name\": \"%s\",\n"
260 "\t\t\t\t\t\"entries\": %ld,\n"
261 "\t\t\t\t\t\"isHidden\": %d,\n"
262 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
263 "\t\t\t\t\t\"multiplier\": %ld,\n"
264 "\t\t\t\t\t\"divisor\": %ld,\n"
265 "\t\t\t\t\t\"last_entry_t\": %ld,\n"
266 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
267 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
268 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
269 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
270 "\t\t\t\t\t\"memory\": %lu\n"
275 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
276 , rrddim_algorithm_name(rd->algorithm)
279 , rd->last_collected_time.tv_sec
280 , rd->collected_value
281 , rd->calculated_value
282 , rd->last_collected_value
283 , rd->last_calculated_value
291 "\t\t\t\"memory\" : %lu\n"
296 pthread_rwlock_unlock(&st->rwlock);
300 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
301 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
303 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
305 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
306 rrd_stats_one_json(st, options, wb);
307 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
310 void rrd_stats_all_json(BUFFER *wb)
312 unsigned long memory = 0;
316 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
318 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
319 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
320 if(st->enabled && st->dimensions) {
321 if(c) buffer_strcat(wb, ",\n");
322 memory += rrd_stats_one_json(st, NULL, wb);
326 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
328 buffer_sprintf(wb, "\n\t],\n"
329 "\t\"hostname\": \"%s\",\n"
330 "\t\"update_every\": %d,\n"
331 "\t\"history\": %d,\n"
332 "\t\"memory\": %lu\n"
336 , rrd_default_history_entries
343 // ----------------------------------------------------------------------------
345 // RRDR dimension options
346 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
347 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
348 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
349 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
351 // RRDR result options
352 #define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
353 #define RRDR_RESULT_OPTION_RELATIVE 0x00000002
355 typedef struct rrdresult {
356 RRDSET *st; // the chart this result refers to
358 uint32_t result_options; // RRDR_RESULT_OPTION_*
360 int d; // the number of dimensions
361 long n; // the number of values in the arrays
362 long rows; // the number of rows used
364 uint8_t *od; // the options for the dimensions
366 time_t *t; // array of n timestamps
367 calculated_number *v; // array n x d values
368 uint8_t *o; // array n x d options
370 long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
372 long group; // how many collected values were grouped for each row
373 int update_every; // what is the suggested update frequency in seconds
375 calculated_number min;
376 calculated_number max;
381 int has_st_lock; // if st is read locked by us
384 #define rrdr_rows(r) ((r)->rows)
387 static void rrdr_dump(RRDR *r)
392 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
394 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
395 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
398 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
399 , (r->od[c] & RRDR_RESET)?"RESET ":""
400 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
401 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
406 fprintf(stderr, "RRDR does not have any values in it.\n");
410 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
412 // for each line in the array
413 for(i = 0; i < r->rows ;i++) {
414 calculated_number *cn = &r->v[ i * r->d ];
415 uint8_t *co = &r->o[ i * r->d ];
417 // print the id and the timestamp of the line
418 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
420 // for each dimension
421 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
422 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
423 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
425 if(co[c] & RRDR_EMPTY)
426 fprintf(stderr, "null ");
428 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
430 , (co[c] & RRDR_EMPTY)?"E":" "
431 , (co[c] & RRDR_RESET)?"R":" "
432 , (co[c] & RRDR_HIDDEN)?"H":" "
433 , (co[c] & RRDR_NONZERO)?"N":" "
437 fprintf(stderr, "\n");
442 void rrdr_disable_not_selected_dimensions(RRDR *r, const char *dims)
444 char b[strlen(dims) + 1];
451 // disable all of them
452 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
453 r->od[c] |= RRDR_HIDDEN;
455 while(o && *o && (tok = mystrsep(&o, ",|"))) {
458 uint32_t hash = simple_hash(tok);
460 // find it and enable it
461 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
462 if(unlikely((hash == d->hash && !strcmp(d->id, tok)) || !strcmp(d->name, tok))) {
463 r->od[c] &= ~RRDR_HIDDEN;
465 // since the user needs this dimension
466 // make it appear as NONZERO, to return it
467 // even if the dimension has only zeros
468 r->od[c] |= RRDR_NONZERO;
474 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
477 case DATASOURCE_JSON:
478 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
481 case DATASOURCE_DATATABLE_JSON:
482 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
485 case DATASOURCE_DATATABLE_JSONP:
486 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
489 case DATASOURCE_JSONP:
490 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
494 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
498 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
502 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
505 case DATASOURCE_HTML:
506 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
509 case DATASOURCE_JS_ARRAY:
510 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
513 case DATASOURCE_SSV_COMMA:
514 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
518 buffer_strcat(wb, "unknown");
523 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
525 if(options & RRDR_OPTION_NONZERO) {
529 // the caller wants specific dimensions
530 // disable NONZERO option
531 // to make sure we don't accidentally prevent
532 // the specific dimensions from being returned
536 // find how many dimensions are not zero
539 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
540 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
541 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
546 // if with nonzero we get i = 0 (no dimensions will be returned)
547 // disable nonzero to show all dimensions
548 if(!i) options &= ~RRDR_OPTION_NONZERO;
554 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
556 long rows = rrdr_rows(r);
560 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
561 char kq[2] = "", // key quote
562 sq[2] = ""; // string quote
564 if( options & RRDR_OPTION_GOOGLE_JSON ) {
573 buffer_sprintf(wb, "{\n"
576 " %sname%s: %s%s%s,\n"
577 " %sview_update_every%s: %d,\n"
578 " %supdate_every%s: %d,\n"
579 " %sfirst_entry%s: %u,\n"
580 " %slast_entry%s: %u,\n"
583 " %sdimension_names%s: ["
585 , kq, kq, sq, r->st->id, sq
586 , kq, kq, sq, r->st->name, sq
587 , kq, kq, r->update_every
588 , kq, kq, r->st->update_every
589 , kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
590 , kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
591 , kq, kq, (uint32_t)r->before
592 , kq, kq, (uint32_t)r->after
595 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
596 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
597 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
599 if(i) buffer_strcat(wb, ", ");
600 buffer_strcat(wb, sq);
601 buffer_strcat(wb, rd->name);
602 buffer_strcat(wb, sq);
606 #ifdef NETDATA_INTERNAL_CHECKS
607 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
610 buffer_strcat(wb, sq);
611 buffer_strcat(wb, "no data");
612 buffer_strcat(wb, sq);
615 buffer_sprintf(wb, "],\n"
616 " %sdimension_ids%s: ["
619 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
620 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
621 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
623 if(i) buffer_strcat(wb, ", ");
624 buffer_strcat(wb, sq);
625 buffer_strcat(wb, rd->id);
626 buffer_strcat(wb, sq);
631 buffer_strcat(wb, sq);
632 buffer_strcat(wb, "no data");
633 buffer_strcat(wb, sq);
636 buffer_sprintf(wb, "],\n"
637 " %slatest_values%s: ["
640 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
641 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
642 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
644 if(i) buffer_strcat(wb, ", ");
647 storage_number n = rd->values[rrdset_last_slot(r->st)];
649 if(!does_storage_number_exist(n))
650 buffer_strcat(wb, "null");
652 buffer_rrd_value(wb, unpack_storage_number(n));
656 buffer_strcat(wb, "null");
659 buffer_sprintf(wb, "],\n"
660 " %sview_latest_values%s: ["
665 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
666 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
667 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
669 if(i) buffer_strcat(wb, ", ");
672 calculated_number *cn = &r->v[ (0) * r->d ];
673 uint8_t *co = &r->o[ (0) * r->d ];
675 if(co[c] & RRDR_EMPTY)
676 buffer_strcat(wb, "null");
678 buffer_rrd_value(wb, cn[c]);
683 buffer_strcat(wb, "null");
686 buffer_sprintf(wb, "],\n"
687 " %sdimensions%s: %ld,\n"
688 " %spoints%s: %ld,\n"
695 rrdr_buffer_print_format(wb, format);
697 buffer_sprintf(wb, "%s,\n"
703 if(string_value) buffer_strcat(wb, sq);
704 //info("JSONWRAPPER(): %s: END", r->st->id);
707 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
711 char kq[2] = "", // key quote
712 sq[2] = ""; // string quote
714 if( options & RRDR_OPTION_GOOGLE_JSON ) {
723 if(string_value) buffer_strcat(wb, sq);
725 buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
726 buffer_rrd_value(wb, r->min);
727 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
728 buffer_rrd_value(wb, r->max);
729 buffer_strcat(wb, "\n}\n");
732 #define JSON_DATES_JS 1
733 #define JSON_DATES_TIMESTAMP 2
735 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
737 //info("RRD2JSON(): %s: BEGIN", r->st->id);
738 int row_annotations = 0, dates, dates_with_new = 0;
739 char kq[2] = "", // key quote
740 sq[2] = "", // string quote
741 pre_label[101] = "", // before each label
742 post_label[101] = "", // after each label
743 pre_date[101] = "", // the beginning of line, to the date
744 post_date[101] = "", // closing the date
745 pre_value[101] = "", // before each value
746 post_value[101] = "", // after each value
747 post_line[101] = "", // at the end of each row
748 normal_annotation[201] = "", // default row annotation
749 overflow_annotation[201] = "", // overflow row annotation
750 data_begin[101] = "", // between labels and values
751 finish[101] = ""; // at the end of everything
754 dates = JSON_DATES_JS;
755 if( options & RRDR_OPTION_GOOGLE_JSON ) {
764 snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
765 snprintfz(post_date, 100, "%s}", sq);
766 snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
767 snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
768 snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
769 strcpy(post_value, "}");
770 strcpy(post_line, "]}");
771 snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
772 strcpy(finish, "\n ]\n}");
774 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);
775 snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
777 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
778 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);
779 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);
780 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);
782 // remove the valueobjects flag
783 // google wants its own keys
784 if(options & RRDR_OPTION_OBJECTSROWS)
785 options &= ~RRDR_OPTION_OBJECTSROWS;
790 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
791 dates = JSON_DATES_TIMESTAMP;
795 dates = JSON_DATES_JS;
798 if( options & RRDR_OPTION_OBJECTSROWS )
799 strcpy(pre_date, " { ");
801 strcpy(pre_date, " [ ");
802 strcpy(pre_label, ", \"");
803 strcpy(post_label, "\"");
804 strcpy(pre_value, ", ");
805 if( options & RRDR_OPTION_OBJECTSROWS )
806 strcpy(post_line, "}");
808 strcpy(post_line, "]");
809 snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
810 strcpy(finish, "\n ]\n}");
812 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
813 buffer_sprintf(wb, "%stime%s", sq, sq);
816 // -------------------------------------------------------------------------
817 // print the JSON header
822 // print the header lines
823 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
824 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
825 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
827 buffer_strcat(wb, pre_label);
828 buffer_strcat(wb, rd->name);
829 buffer_strcat(wb, post_label);
833 buffer_strcat(wb, pre_label);
834 buffer_strcat(wb, "no data");
835 buffer_strcat(wb, post_label);
838 // print the begin of row data
839 buffer_strcat(wb, data_begin);
841 // if all dimensions are hidden, print a null
843 buffer_strcat(wb, finish);
847 long start = 0, end = rrdr_rows(r), step = 1;
848 if((options & RRDR_OPTION_REVERSED)) {
849 start = rrdr_rows(r) - 1;
854 // for each line in the array
855 calculated_number total = 1;
856 for(i = start; i != end ;i += step) {
857 calculated_number *cn = &r->v[ i * r->d ];
858 uint8_t *co = &r->o[ i * r->d ];
860 time_t now = r->t[i];
862 if(dates == JSON_DATES_JS) {
863 // generate the local date time
864 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
865 if(!tm) { error("localtime_r() failed."); continue; }
867 if(likely(i != start)) buffer_strcat(wb, ",\n");
868 buffer_strcat(wb, pre_date);
870 if( options & RRDR_OPTION_OBJECTSROWS )
871 buffer_sprintf(wb, "%stime%s: ", kq, kq);
874 buffer_strcat(wb, "new ");
876 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
878 buffer_strcat(wb, post_date);
880 if(row_annotations) {
881 // google supports one annotation per row
882 int annotation_found = 0;
883 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
884 if(co[c] & RRDR_RESET) {
885 buffer_strcat(wb, overflow_annotation);
886 annotation_found = 1;
890 if(!annotation_found)
891 buffer_strcat(wb, normal_annotation);
895 // print the timestamp of the line
896 if(likely(i != start)) buffer_strcat(wb, ",\n");
897 buffer_strcat(wb, pre_date);
899 if( options & RRDR_OPTION_OBJECTSROWS )
900 buffer_sprintf(wb, "%stime%s: ", kq, kq);
902 buffer_rrd_value(wb, (calculated_number)r->t[i]);
904 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
906 buffer_strcat(wb, post_date);
909 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
911 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
912 calculated_number n = cn[c];
914 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
919 // prevent a division by zero
920 if(total == 0) total = 1;
923 // for each dimension
924 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
925 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
926 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
928 calculated_number n = cn[c];
930 buffer_strcat(wb, pre_value);
932 if( options & RRDR_OPTION_OBJECTSROWS )
933 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
935 if(co[c] & RRDR_EMPTY) {
936 if(options & RRDR_OPTION_NULL2ZERO)
937 buffer_strcat(wb, "0");
939 buffer_strcat(wb, "null");
942 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
945 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
948 buffer_rrd_value(wb, n);
951 buffer_strcat(wb, post_value);
954 buffer_strcat(wb, post_line);
957 buffer_strcat(wb, finish);
958 //info("RRD2JSON(): %s: END", r->st->id);
961 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
963 //info("RRD2CSV(): %s: BEGIN", r->st->id);
967 // print the csv header
968 for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
969 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
970 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
973 buffer_strcat(wb, startline);
974 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
975 buffer_strcat(wb, "time");
976 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
978 buffer_strcat(wb, separator);
979 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
980 buffer_strcat(wb, d->name);
981 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
984 buffer_strcat(wb, endline);
987 // no dimensions present
991 long start = 0, end = rrdr_rows(r), step = 1;
992 if((options & RRDR_OPTION_REVERSED)) {
993 start = rrdr_rows(r) - 1;
998 // for each line in the array
999 calculated_number total = 1;
1000 for(i = start; i != end ;i += step) {
1001 calculated_number *cn = &r->v[ i * r->d ];
1002 uint8_t *co = &r->o[ i * r->d ];
1004 buffer_strcat(wb, betweenlines);
1005 buffer_strcat(wb, startline);
1007 time_t now = r->t[i];
1009 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
1010 // print the timestamp of the line
1011 buffer_rrd_value(wb, (calculated_number)now);
1013 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
1016 // generate the local date time
1017 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1018 if(!tm) { error("localtime() failed."); continue; }
1019 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1022 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1024 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1025 calculated_number n = cn[c];
1027 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1032 // prevent a division by zero
1033 if(total == 0) total = 1;
1036 // for each dimension
1037 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1038 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1039 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1041 buffer_strcat(wb, separator);
1043 calculated_number n = cn[c];
1045 if(co[c] & RRDR_EMPTY) {
1046 if(options & RRDR_OPTION_NULL2ZERO)
1047 buffer_strcat(wb, "0");
1049 buffer_strcat(wb, "null");
1052 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1055 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1056 n = n * 100 / total;
1058 buffer_rrd_value(wb, n);
1062 buffer_strcat(wb, endline);
1064 //info("RRD2CSV(): %s: END", r->st->id);
1067 inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
1071 calculated_number *cn = &r->v[ i * r->d ];
1072 uint8_t *co = &r->o[ i * r->d ];
1074 calculated_number sum = 0, min = 0, max = 0, v;
1075 int all_null = 1, init = 1;
1077 calculated_number total = 1;
1078 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1080 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1081 calculated_number n = cn[c];
1083 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1088 // prevent a division by zero
1089 if(total == 0) total = 1;
1092 // for each dimension
1093 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1094 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1095 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1097 calculated_number n = cn[c];
1099 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1102 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1103 n = n * 100 / total;
1105 if(unlikely(init)) {
1117 if(likely(!(co[c] & RRDR_EMPTY))) {
1122 if(n < min) min = n;
1123 if(n > max) max = n;
1126 if(unlikely(all_null)) {
1127 if(likely(all_values_are_null))
1128 *all_values_are_null = 1;
1132 if(likely(all_values_are_null))
1133 *all_values_are_null = 0;
1136 if(options & RRDR_OPTION_MIN2MAX)
1144 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
1146 //info("RRD2SSV(): %s: BEGIN", r->st->id);
1149 buffer_strcat(wb, prefix);
1150 long start = 0, end = rrdr_rows(r), step = 1;
1151 if((options & RRDR_OPTION_REVERSED)) {
1152 start = rrdr_rows(r) - 1;
1157 // for each line in the array
1158 for(i = start; i != end ;i += step) {
1159 int all_values_are_null = 0;
1160 calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
1162 if(likely(i != start)) {
1163 if(r->min > v) r->min = v;
1164 if(r->max < v) r->max = v;
1171 if(likely(i != start))
1172 buffer_strcat(wb, separator);
1174 if(all_values_are_null) {
1175 if(options & RRDR_OPTION_NULL2ZERO)
1176 buffer_strcat(wb, "0");
1178 buffer_strcat(wb, "null");
1181 buffer_rrd_value(wb, v);
1183 buffer_strcat(wb, suffix);
1184 //info("RRD2SSV(): %s: END", r->st->id);
1187 inline static calculated_number *rrdr_line_values(RRDR *r)
1189 return &r->v[ r->c * r->d ];
1192 inline static uint8_t *rrdr_line_options(RRDR *r)
1194 return &r->o[ r->c * r->d ];
1197 inline static int rrdr_line_init(RRDR *r, time_t t)
1201 if(unlikely(r->c >= r->n)) {
1202 error("requested to step above RRDR size for chart %s", r->st->name);
1212 inline static void rrdr_lock_rrdset(RRDR *r) {
1214 error("NULL value given!");
1218 pthread_rwlock_rdlock(&r->st->rwlock);
1222 inline static void rrdr_unlock_rrdset(RRDR *r) {
1224 error("NULL value given!");
1228 if(likely(r->has_st_lock)) {
1229 pthread_rwlock_unlock(&r->st->rwlock);
1234 inline static void rrdr_free(RRDR *r)
1237 error("NULL value given!");
1241 rrdr_unlock_rrdset(r);
1249 static inline void rrdr_done(RRDR *r)
1255 static RRDR *rrdr_create(RRDSET *st, long n)
1258 error("NULL value given!");
1262 RRDR *r = callocz(1, sizeof(RRDR));
1265 rrdr_lock_rrdset(r);
1268 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
1272 r->t = mallocz(n * sizeof(time_t));
1273 r->v = mallocz(n * r->d * sizeof(calculated_number));
1274 r->o = mallocz(n * r->d * sizeof(uint8_t));
1275 r->od = mallocz(r->d * sizeof(uint8_t));
1277 // set the hidden flag on hidden dimensions
1279 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1280 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] = RRDR_HIDDEN;
1286 r->update_every = 1;
1291 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
1293 int debug = st->debug;
1294 int absolute_period_requested = -1;
1296 time_t first_entry_t = rrdset_first_entry_t(st);
1297 time_t last_entry_t = rrdset_last_entry_t(st);
1299 if(before == 0 && after == 0) {
1300 // dump the all the data
1301 before = last_entry_t;
1302 after = first_entry_t;
1303 absolute_period_requested = 0;
1306 // allow relative for before (smaller than API_RELATIVE_TIME_MAX)
1307 if(((before < 0)?-before:before) <= API_RELATIVE_TIME_MAX) {
1308 if(abs(before) % st->update_every) {
1309 // make sure it is multiple of st->update_every
1310 if(before < 0) before = before - st->update_every - before % st->update_every;
1311 else before = before + st->update_every - before % st->update_every;
1313 if(before > 0) before = first_entry_t + before;
1314 else before = last_entry_t + before;
1315 absolute_period_requested = 0;
1318 // allow relative for after (smaller than API_RELATIVE_TIME_MAX)
1319 if(((after < 0)?-after:after) <= API_RELATIVE_TIME_MAX) {
1320 if(after == 0) after = -st->update_every;
1321 if(abs(after) % st->update_every) {
1322 // make sure it is multiple of st->update_every
1323 if(after < 0) after = after - st->update_every - after % st->update_every;
1324 else after = after + st->update_every - after % st->update_every;
1326 after = before + after;
1327 absolute_period_requested = 0;
1330 if(absolute_period_requested == -1)
1331 absolute_period_requested = 1;
1333 // make sure they are within our timeframe
1334 if(before > last_entry_t) before = last_entry_t;
1335 if(before < first_entry_t) before = first_entry_t;
1337 if(after > last_entry_t) after = last_entry_t;
1338 if(after < first_entry_t) after = first_entry_t;
1340 // check if they are upside down
1341 if(after > before) {
1342 time_t tmp = before;
1347 // the duration of the chart
1348 time_t duration = before - after;
1349 long available_points = duration / st->update_every;
1351 if(duration <= 0 || available_points <= 0)
1352 return rrdr_create(st, 1);
1354 // check the wanted points
1355 if(points < 0) points = -points;
1356 if(points > available_points) points = available_points;
1357 if(points == 0) points = available_points;
1359 // calculate proper grouping of source data
1360 long group = available_points / points;
1361 if(group <= 0) group = 1;
1363 // round group to the closest integer
1364 if(available_points % points > points / 2) group++;
1366 time_t after_new = (aligned) ? (after - (after % (group * st->update_every))) : after;
1367 time_t before_new = (aligned) ? (before - (before % (group * st->update_every))) : before;
1368 long points_new = (before_new - after_new) / st->update_every / group;
1370 // find the starting and ending slots in our round robin db
1371 long start_at_slot = rrdset_time2slot(st, before_new),
1372 stop_at_slot = rrdset_time2slot(st, after_new);
1374 #ifdef NETDATA_INTERNAL_CHECKS
1375 if(after_new < first_entry_t) {
1376 error("after_new %u is too small, minimum %u", (uint32_t)after_new, (uint32_t)first_entry_t);
1378 if(after_new > last_entry_t) {
1379 error("after_new %u is too big, maximum %u", (uint32_t)after_new, (uint32_t)last_entry_t);
1381 if(before_new < first_entry_t) {
1382 error("before_new %u is too small, minimum %u", (uint32_t)before_new, (uint32_t)first_entry_t);
1384 if(before_new > last_entry_t) {
1385 error("before_new %u is too big, maximum %u", (uint32_t)before_new, (uint32_t)last_entry_t);
1387 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1388 error("start_at_slot is invalid %ld, expected 0 to %ld", start_at_slot, st->entries - 1);
1390 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1391 error("stop_at_slot is invalid %ld, expected 0 to %ld", stop_at_slot, st->entries - 1);
1393 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1394 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1398 //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);
1401 before = before_new;
1402 duration = before - after;
1403 points = points_new;
1406 // before = the end time of the calculation
1407 // after = the start time of the calculation
1408 // duration = the duration of the calculation
1409 // group = the number of source points to aggregate / group together
1410 // method = the method of grouping source points
1411 // points = the number of points to generate
1414 // -------------------------------------------------------------------------
1415 // initialize our result set
1417 RRDR *r = rrdr_create(st, points);
1419 #ifdef NETDATA_INTERNAL_CHECKS
1420 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);
1425 #ifdef NETDATA_INTERNAL_CHECKS
1426 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);
1431 if(absolute_period_requested == 1)
1432 r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1434 r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1436 // find how many dimensions we have
1437 long dimensions = r->d;
1440 // -------------------------------------------------------------------------
1441 // checks for debugging
1443 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"
1445 , (uint32_t)first_entry_t
1446 , (uint32_t)last_entry_t
1447 , (uint32_t)(last_entry_t - first_entry_t)
1450 , (uint32_t)duration
1456 // -------------------------------------------------------------------------
1457 // temp arrays for keeping values per dimension
1459 calculated_number last_values[dimensions]; // keep the last value of each dimension
1460 calculated_number group_values[dimensions]; // keep sums when grouping
1461 long group_counts[dimensions]; // keep the number of values added to group_values
1462 uint8_t group_options[dimensions];
1463 uint8_t found_non_zero[dimensions];
1469 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1471 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1472 group_counts[c] = 0;
1473 group_options[c] = 0;
1474 found_non_zero[c] = 0;
1478 // -------------------------------------------------------------------------
1481 time_t now = rrdset_slot2time(st, start_at_slot),
1482 dt = st->update_every,
1485 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"
1497 r->update_every = group * st->update_every;
1501 //info("RRD2RRDR(): %s: STARTING", st->id);
1503 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1504 for(; !stop_now ; now -= dt, slot--, counter++) {
1505 if(unlikely(slot < 0)) slot = st->entries - 1;
1506 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1508 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %ld, %s %s"
1515 , (group_count + 1 == group)?"PRINT":" - "
1516 , (now >= after && now <= before)?"RANGE":" - "
1519 // make sure we return data in the proper time range
1520 if(unlikely(now > before)) continue;
1521 if(unlikely(now < after)) break;
1523 if(unlikely(group_count == 0)) {
1524 group_start_t = now;
1528 if(unlikely(group_count == group)) {
1529 if(unlikely(added >= points)) break;
1533 // do the calculations
1534 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1535 storage_number n = rd->values[slot];
1536 if(unlikely(!does_storage_number_exist(n))) continue;
1540 calculated_number value = unpack_storage_number(n);
1541 if(likely(value != 0.0)) {
1542 group_options[c] |= RRDR_NONZERO;
1543 found_non_zero[c] = 1;
1546 if(unlikely(did_storage_number_reset(n)))
1547 group_options[c] |= RRDR_RESET;
1549 switch(group_method) {
1551 if(unlikely(isnan(group_values[c])) ||
1552 fabsl(value) < fabsl(group_values[c]))
1553 group_values[c] = value;
1557 if(unlikely(isnan(group_values[c])) ||
1558 fabsl(value) > fabsl(group_values[c]))
1559 group_values[c] = value;
1565 case GROUP_UNDEFINED:
1566 group_values[c] += value;
1569 case GROUP_INCREMENTAL_SUM:
1570 if(unlikely(slot == start_at_slot))
1571 last_values[c] = value;
1573 group_values[c] += last_values[c] - value;
1574 last_values[c] = value;
1580 if(unlikely(add_this)) {
1581 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1585 calculated_number *cn = rrdr_line_values(r);
1586 uint8_t *co = rrdr_line_options(r);
1588 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1590 // update the dimension options
1591 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1593 // store the specific point options
1594 co[c] = group_options[c];
1597 if(unlikely(group_counts[c] == 0)) {
1599 co[c] |= RRDR_EMPTY;
1600 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1603 switch(group_method) {
1606 if(unlikely(isnan(group_values[c])))
1609 cn[c] = group_values[c];
1610 group_values[c] = NAN;
1615 case GROUP_INCREMENTAL_SUM:
1616 cn[c] = group_values[c];
1617 group_values[c] = 0;
1622 case GROUP_UNDEFINED:
1623 cn[c] = group_values[c] / group_counts[c];
1624 group_values[c] = 0;
1628 if(cn[c] < r->min) r->min = cn[c];
1629 if(cn[c] > r->max) r->max = cn[c];
1632 // reset for the next loop
1633 group_counts[c] = 0;
1634 group_options[c] = 0;
1644 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1645 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1649 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)
1651 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1653 if(value_is_null) *value_is_null = 1;
1657 if(rrdr_rows(r) == 0) {
1660 if(db_after) *db_after = 0;
1661 if(db_before) *db_before = 0;
1662 if(value_is_null) *value_is_null = 1;
1667 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1668 buffer_no_cacheable(wb);
1669 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1670 buffer_cacheable(wb);
1672 options = rrdr_check_options(r, options, dimensions);
1675 rrdr_disable_not_selected_dimensions(r, dimensions);
1677 if(db_after) *db_after = r->after;
1678 if(db_before) *db_before = r->before;
1680 long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
1681 *n = rrdr2value(r, i, options, value_is_null);
1687 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)
1689 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1691 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1695 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1696 buffer_no_cacheable(wb);
1697 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1698 buffer_cacheable(wb);
1700 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1703 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1705 if(latest_timestamp && rrdr_rows(r) > 0)
1706 *latest_timestamp = r->before;
1709 case DATASOURCE_SSV:
1710 if(options & RRDR_OPTION_JSON_WRAP) {
1711 wb->contenttype = CT_APPLICATION_JSON;
1712 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1713 rrdr2ssv(r, wb, options, "", " ", "");
1714 rrdr_json_wrapper_end(r, wb, format, options, 1);
1717 wb->contenttype = CT_TEXT_PLAIN;
1718 rrdr2ssv(r, wb, options, "", " ", "");
1722 case DATASOURCE_SSV_COMMA:
1723 if(options & RRDR_OPTION_JSON_WRAP) {
1724 wb->contenttype = CT_APPLICATION_JSON;
1725 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1726 rrdr2ssv(r, wb, options, "", ",", "");
1727 rrdr_json_wrapper_end(r, wb, format, options, 1);
1730 wb->contenttype = CT_TEXT_PLAIN;
1731 rrdr2ssv(r, wb, options, "", ",", "");
1735 case DATASOURCE_JS_ARRAY:
1736 if(options & RRDR_OPTION_JSON_WRAP) {
1737 wb->contenttype = CT_APPLICATION_JSON;
1738 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1739 rrdr2ssv(r, wb, options, "[", ",", "]");
1740 rrdr_json_wrapper_end(r, wb, format, options, 0);
1743 wb->contenttype = CT_APPLICATION_JSON;
1744 rrdr2ssv(r, wb, options, "[", ",", "]");
1748 case DATASOURCE_CSV:
1749 if(options & RRDR_OPTION_JSON_WRAP) {
1750 wb->contenttype = CT_APPLICATION_JSON;
1751 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1752 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1753 rrdr_json_wrapper_end(r, wb, format, options, 1);
1756 wb->contenttype = CT_TEXT_PLAIN;
1757 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1761 case DATASOURCE_CSV_JSON_ARRAY:
1762 wb->contenttype = CT_APPLICATION_JSON;
1763 if(options & RRDR_OPTION_JSON_WRAP) {
1764 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1765 buffer_strcat(wb, "[\n");
1766 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1767 buffer_strcat(wb, "\n]");
1768 rrdr_json_wrapper_end(r, wb, format, options, 0);
1771 wb->contenttype = CT_TEXT_PLAIN;
1772 buffer_strcat(wb, "[\n");
1773 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1774 buffer_strcat(wb, "\n]");
1778 case DATASOURCE_TSV:
1779 if(options & RRDR_OPTION_JSON_WRAP) {
1780 wb->contenttype = CT_APPLICATION_JSON;
1781 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1782 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1783 rrdr_json_wrapper_end(r, wb, format, options, 1);
1786 wb->contenttype = CT_TEXT_PLAIN;
1787 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1791 case DATASOURCE_HTML:
1792 if(options & RRDR_OPTION_JSON_WRAP) {
1793 wb->contenttype = CT_APPLICATION_JSON;
1794 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1795 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1796 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1797 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1798 rrdr_json_wrapper_end(r, wb, format, options, 1);
1801 wb->contenttype = CT_TEXT_HTML;
1802 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1803 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1804 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1808 case DATASOURCE_DATATABLE_JSONP:
1809 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1811 if(options & RRDR_OPTION_JSON_WRAP)
1812 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1814 rrdr2json(r, wb, options, 1);
1816 if(options & RRDR_OPTION_JSON_WRAP)
1817 rrdr_json_wrapper_end(r, wb, format, options, 0);
1820 case DATASOURCE_DATATABLE_JSON:
1821 wb->contenttype = CT_APPLICATION_JSON;
1823 if(options & RRDR_OPTION_JSON_WRAP)
1824 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1826 rrdr2json(r, wb, options, 1);
1828 if(options & RRDR_OPTION_JSON_WRAP)
1829 rrdr_json_wrapper_end(r, wb, format, options, 0);
1832 case DATASOURCE_JSONP:
1833 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1834 if(options & RRDR_OPTION_JSON_WRAP)
1835 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1837 rrdr2json(r, wb, options, 0);
1839 if(options & RRDR_OPTION_JSON_WRAP)
1840 rrdr_json_wrapper_end(r, wb, format, options, 0);
1843 case DATASOURCE_JSON:
1845 wb->contenttype = CT_APPLICATION_JSON;
1847 if(options & RRDR_OPTION_JSON_WRAP)
1848 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1850 rrdr2json(r, wb, options, 0);
1852 if(options & RRDR_OPTION_JSON_WRAP)
1853 rrdr_json_wrapper_end(r, wb, format, options, 0);
1861 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)
1864 pthread_rwlock_rdlock(&st->rwlock);
1867 // -------------------------------------------------------------------------
1868 // switch from JSON to google JSON
1873 case DATASOURCE_DATATABLE_JSON:
1874 case DATASOURCE_DATATABLE_JSONP:
1879 case DATASOURCE_JSON:
1885 // -------------------------------------------------------------------------
1886 // validate the parameters
1888 if(points < 1) points = 1;
1889 if(group < 1) group = 1;
1891 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1892 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1896 // our return value (the last timestamp printed)
1897 // this is required to detect re-transmit in google JSONP
1898 time_t last_timestamp = 0;
1901 // -------------------------------------------------------------------------
1902 // find how many dimensions we have
1906 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1908 pthread_rwlock_unlock(&st->rwlock);
1909 buffer_strcat(wb, "No dimensions yet.");
1914 // -------------------------------------------------------------------------
1915 // prepare various strings, to speed up the loop
1917 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);
1918 char normal_annotation[201]; snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1919 char pre_date[51]; snprintfz(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1920 char post_date[21]; snprintfz(post_date, 20, "%s}", sq);
1921 char pre_value[21]; snprintfz(pre_value, 20, ",{%sv%s:", kq, kq);
1922 char post_value[21]; strcpy(post_value, "}");
1925 // -------------------------------------------------------------------------
1926 // checks for debugging
1929 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"
1931 , rrdset_first_entry_t(st)
1932 , rrdset_last_entry_t(st)
1933 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1942 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%ld) is earlier than the oldest (%ld)", st->name, before, after);
1944 if((before - after) > st->entries * st->update_every)
1945 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);
1949 // -------------------------------------------------------------------------
1950 // temp arrays for keeping values per dimension
1952 calculated_number group_values[dimensions]; // keep sums when grouping
1953 int print_hidden[dimensions]; // keep hidden flags
1954 int found_non_zero[dimensions];
1955 int found_non_existing[dimensions];
1958 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1959 group_values[c] = 0;
1960 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1961 found_non_zero[c] = 0;
1962 found_non_existing[c] = 0;
1966 // 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);
1967 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1968 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1970 // -------------------------------------------------------------------------
1971 // remove dimensions that contain only zeros
1974 if(only_non_zero) max_loop = 2;
1976 for(; max_loop ; max_loop--) {
1978 // -------------------------------------------------------------------------
1979 // print the JSON header
1981 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1982 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);
1983 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);
1984 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);
1986 // print the header for each dimension
1987 // and update the print_hidden array for the dimensions that should be hidden
1989 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1990 if(!print_hidden[c]) {
1992 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);
1996 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);
1999 // print the begin of row data
2000 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
2003 // -------------------------------------------------------------------------
2006 int annotate_reset = 0;
2007 int annotation_count = 0;
2009 long t = rrdset_time2slot(st, before),
2010 stop_at_t = rrdset_time2slot(st, after),
2015 time_t now = rrdset_slot2time(st, t),
2016 dt = st->update_every;
2018 long count = 0, printed = 0, group_count = 0;
2021 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"
2028 , (uint32_t)rrdset_first_entry_t(st)
2029 , (uint32_t)rrdset_last_entry_t(st)
2035 for(; !stop_now ; now -= dt, t--, counter++) {
2036 if(t < 0) t = st->entries - 1;
2037 if(t == stop_at_t) stop_now = counter;
2041 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %ld, %s %s"
2048 , (group_count + 1 == group)?"PRINT":" - "
2049 , (now >= after && now <= before)?"RANGE":" - "
2053 // make sure we return data in the proper time range
2054 if(now > before) continue;
2055 if(now < after) break;
2057 //if(rrdset_slot2time(st, t) != now)
2058 // 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));
2063 // check if we have to print this now
2064 if(group_count == group) {
2065 if(printed >= points) {
2066 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
2070 // generate the local date time
2071 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
2072 if(!tm) { error("localtime() failed."); continue; }
2073 if(now > last_timestamp) last_timestamp = now;
2075 if(printed) buffer_strcat(wb, "]},\n");
2076 buffer_strcat(wb, pre_date);
2077 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
2078 buffer_strcat(wb, post_date);
2083 // do the calculations
2084 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2085 storage_number n = rd->values[t];
2086 calculated_number value = unpack_storage_number(n);
2088 if(!does_storage_number_exist(n)) {
2090 found_non_existing[c]++;
2092 if(did_storage_number_reset(n)) annotate_reset = 1;
2094 switch(group_method) {
2096 if(abs(value) > abs(group_values[c])) group_values[c] = value;
2100 group_values[c] += value;
2105 group_values[c] += value;
2106 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
2112 if(annotate_reset) {
2114 buffer_strcat(wb, overflow_annotation);
2118 buffer_strcat(wb, normal_annotation);
2121 for(c = 0 ; c < dimensions ; c++) {
2122 if(found_non_existing[c] == group_count) {
2123 // all entries are non-existing
2125 buffer_strcat(wb, pre_value);
2126 buffer_strcat(wb, "null");
2127 buffer_strcat(wb, post_value);
2129 else if(!print_hidden[c]) {
2131 buffer_strcat(wb, pre_value);
2132 buffer_rrd_value(wb, group_values[c]);
2133 buffer_strcat(wb, post_value);
2135 if(group_values[c]) found_non_zero[c]++;
2138 // reset them for the next loop
2139 group_values[c] = 0;
2140 found_non_existing[c] = 0;
2143 // if all dimensions are hidden, print a null
2145 buffer_strcat(wb, pre_value);
2146 buffer_strcat(wb, "null");
2147 buffer_strcat(wb, post_value);
2155 if(printed) buffer_strcat(wb, "]}");
2156 buffer_strcat(wb, "\n ]\n}\n");
2158 if(only_non_zero && max_loop > 1) {
2160 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2161 group_values[c] = 0;
2162 found_non_existing[c] = 0;
2164 if(!print_hidden[c] && !found_non_zero[c]) {
2166 print_hidden[c] = 1;
2170 if(changed) buffer_flush(wb);
2177 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %lu bytes", st->name, wb->len);
2179 pthread_rwlock_unlock(&st->rwlock);
2180 return last_timestamp;