13 #define HOSTNAME_MAX 1024
14 char *hostname = "unknown";
16 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb)
18 pthread_rwlock_rdlock(&st->rwlock);
22 "\t\t\t\"id\": \"%s\",\n"
23 "\t\t\t\"name\": \"%s\",\n"
24 "\t\t\t\"type\": \"%s\",\n"
25 "\t\t\t\"family\": \"%s\",\n"
26 "\t\t\t\"title\": \"%s\",\n"
27 "\t\t\t\"priority\": %ld,\n"
28 "\t\t\t\"enabled\": %s,\n"
29 "\t\t\t\"units\": \"%s\",\n"
30 "\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
31 "\t\t\t\"chart_type\": \"%s\",\n"
32 "\t\t\t\"duration\": %ld,\n"
33 "\t\t\t\"first_entry\": %lu,\n"
34 "\t\t\t\"last_entry\": %lu,\n"
35 "\t\t\t\"update_every\": %d,\n"
36 "\t\t\t\"dimensions\": {\n"
43 , st->enabled?"true":"false"
46 , rrdset_type_name(st->chart_type)
47 , st->entries * st->update_every
48 , rrdset_first_entry_t(st)
49 , rrdset_last_entry_t(st)
53 unsigned long memory = st->memsize;
57 for(rd = st->dimensions; rd ; rd = rd->next) {
58 if(rd->flags & RRDDIM_FLAG_HIDDEN) continue;
60 memory += rd->memsize;
64 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
78 pthread_rwlock_unlock(&st->rwlock);
81 void rrd_stats_api_v1_charts(BUFFER *wb)
86 buffer_sprintf(wb, "{\n"
87 "\t\"hostname\": \"%s\""
88 ",\n\t\"update_every\": %d"
89 ",\n\t\"history\": %d"
93 , rrd_default_history_entries
96 pthread_rwlock_rdlock(&rrdset_root_rwlock);
97 for(st = rrdset_root, c = 0; st ; st = st->next) {
99 if(c) buffer_strcat(wb, ",");
100 buffer_strcat(wb, "\n\t\t\"");
101 buffer_strcat(wb, st->id);
102 buffer_strcat(wb, "\": ");
103 rrd_stats_api_v1_chart(st, wb);
107 pthread_rwlock_unlock(&rrdset_root_rwlock);
109 buffer_strcat(wb, "\n\t}\n}\n");
113 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
115 time_t now = time(NULL);
117 pthread_rwlock_rdlock(&st->rwlock);
121 "\t\t\t\"id\": \"%s\",\n"
122 "\t\t\t\"name\": \"%s\",\n"
123 "\t\t\t\"type\": \"%s\",\n"
124 "\t\t\t\"family\": \"%s\",\n"
125 "\t\t\t\"title\": \"%s\",\n"
126 "\t\t\t\"priority\": %ld,\n"
127 "\t\t\t\"enabled\": %d,\n"
128 "\t\t\t\"units\": \"%s\",\n"
129 "\t\t\t\"url\": \"/data/%s/%s\",\n"
130 "\t\t\t\"chart_type\": \"%s\",\n"
131 "\t\t\t\"counter\": %ld,\n"
132 "\t\t\t\"entries\": %ld,\n"
133 "\t\t\t\"first_entry_t\": %lu,\n"
134 "\t\t\t\"last_entry\": %ld,\n"
135 "\t\t\t\"last_entry_t\": %lu,\n"
136 "\t\t\t\"last_entry_secs_ago\": %lu,\n"
137 "\t\t\t\"update_every\": %d,\n"
138 "\t\t\t\"isdetail\": %d,\n"
139 "\t\t\t\"usec_since_last_update\": %llu,\n"
140 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
141 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
142 "\t\t\t\"dimensions\": [\n"
151 , st->name, options?options:""
152 , rrdset_type_name(st->chart_type)
155 , rrdset_first_entry_t(st)
156 , rrdset_last_slot(st)
157 , rrdset_last_entry_t(st)
158 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
161 , st->usec_since_last_update
162 , st->collected_total
163 , st->last_collected_total
166 unsigned long memory = st->memsize;
169 for(rd = st->dimensions; rd ; rd = rd->next) {
171 memory += rd->memsize;
175 "\t\t\t\t\t\"id\": \"%s\",\n"
176 "\t\t\t\t\t\"name\": \"%s\",\n"
177 "\t\t\t\t\t\"entries\": %ld,\n"
178 "\t\t\t\t\t\"isHidden\": %d,\n"
179 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
180 "\t\t\t\t\t\"multiplier\": %ld,\n"
181 "\t\t\t\t\t\"divisor\": %ld,\n"
182 "\t\t\t\t\t\"last_entry_t\": %lu,\n"
183 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
184 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
185 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
186 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
187 "\t\t\t\t\t\"memory\": %lu\n"
192 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
193 , rrddim_algorithm_name(rd->algorithm)
196 , rd->last_collected_time.tv_sec
197 , rd->collected_value
198 , rd->calculated_value
199 , rd->last_collected_value
200 , rd->last_calculated_value
208 "\t\t\t\"memory\" : %lu\n"
213 pthread_rwlock_unlock(&st->rwlock);
217 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
218 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
220 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
222 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
223 rrd_stats_one_json(st, options, wb);
224 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
227 void rrd_stats_all_json(BUFFER *wb)
229 unsigned long memory = 0;
233 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
235 pthread_rwlock_rdlock(&rrdset_root_rwlock);
236 for(st = rrdset_root, c = 0; st ; st = st->next) {
238 if(c) buffer_strcat(wb, ",\n");
239 memory += rrd_stats_one_json(st, NULL, wb);
243 pthread_rwlock_unlock(&rrdset_root_rwlock);
245 buffer_sprintf(wb, "\n\t],\n"
246 "\t\"hostname\": \"%s\",\n"
247 "\t\"update_every\": %d,\n"
248 "\t\"history\": %d,\n"
249 "\t\"memory\": %lu\n"
253 , rrd_default_history_entries
260 // ----------------------------------------------------------------------------
263 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
264 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
265 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
266 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
269 typedef struct rrdresult {
270 RRDSET *st; // the chart this result refers to
272 int group; // how many collected values were grouped for each row
273 int update_every; // what is the suggested update frequency in seconds
275 int d; // the number of dimensions
276 int n; // the number of values in the arrays
277 int rows; // the number of rows used
279 uint8_t *od; // the options for the dimensions
281 time_t *t; // array of n timestamps
282 calculated_number *v; // array n x d values
283 uint8_t *o; // array n x d options
285 int c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
287 int has_st_lock; // if st is read locked by us
290 #define rrdr_rows(r) ((r)->rows)
293 static void rrdr_dump(RRDR *r)
298 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
300 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
301 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
304 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
305 , (r->od[c] & RRDR_RESET)?"RESET ":""
306 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
307 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
312 fprintf(stderr, "RRDR does not have any values in it.\n");
316 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
318 // for each line in the array
319 for(i = 0; i < r->rows ;i++) {
320 calculated_number *cn = &r->v[ i * r->d ];
321 uint8_t *co = &r->o[ i * r->d ];
323 // print the id and the timestamp of the line
324 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
326 // for each dimension
327 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
328 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
329 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
331 if(co[c] & RRDR_EMPTY)
332 fprintf(stderr, "null ");
334 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
336 , (co[c] & RRDR_EMPTY)?"E":" "
337 , (co[c] & RRDR_RESET)?"R":" "
338 , (co[c] & RRDR_HIDDEN)?"H":" "
339 , (co[c] & RRDR_NONZERO)?"N":" "
343 fprintf(stderr, "\n");
348 void rrdr_disable_not_selected_dimensions(RRDR *r, const char *dims)
350 char b[strlen(dims) + 1];
357 // disable all of them
358 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
359 r->od[c] |= RRDR_HIDDEN;
361 while(o && *o && (tok = mystrsep(&o, ", |"))) {
364 // find it and enable it
365 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
366 if(!strcmp(d->name, tok)) {
367 r->od[c] &= ~RRDR_HIDDEN;
373 #define JSON_DATES_JS 1
374 #define JSON_DATES_TIMESTAMP 2
376 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
378 int row_annotations = 0, dates = JSON_DATES_JS, dates_with_new = 0;
379 char kq[2] = "", // key quote
380 sq[2] = "", // string quote
381 pre_label[101] = "", // before each label
382 post_label[101] = "", // after each label
383 pre_date[101] = "", // the beginning of line, to the date
384 post_date[101] = "", // closing the date
385 pre_value[101] = "", // before each value
386 post_value[101] = "", // after each value
387 post_line[101] = "", // at the end of each row
388 normal_annotation[201] = "", // default row annotation
389 overflow_annotation[201] = "", // overflow row annotation
390 data_begin[101] = "", // between labels and values
391 finish[101] = ""; // at the end of everything
394 dates = JSON_DATES_JS;
395 if( options & RRDR_OPTION_GOOGLE_JSON ) {
404 snprintf(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
405 snprintf(post_date, 100, "%s}", sq);
406 snprintf(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
407 snprintf(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
408 snprintf(pre_value, 100, ",{%sv%s:", kq, kq);
409 snprintf(post_value, 100, "}");
410 snprintf(post_line, 100, "]}");
411 snprintf(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
412 snprintf(finish, 100, "\n ]\n}\n");
414 snprintf(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
415 snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
417 buffer_sprintf(wb, "{\n %supdate_every%s: %d,\n %scols%s:\n [\n", kq, kq, r->update_every, kq, kq, kq, kq);
418 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);
419 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);
420 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);
422 // remove the valueobjects flag
423 // google wants its own keys
424 if(options & RRDR_OPTION_OBJECTSROWS)
425 options &= ~RRDR_OPTION_OBJECTSROWS;
430 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
431 dates = JSON_DATES_TIMESTAMP;
435 dates = JSON_DATES_JS;
438 if( options & RRDR_OPTION_OBJECTSROWS )
439 snprintf(pre_date, 100, " { ");
441 snprintf(pre_date, 100, " [ ");
442 snprintf(pre_label, 100, ", \"");
443 snprintf(post_label, 100, "\"");
444 snprintf(pre_value, 100, ", ");
445 if( options & RRDR_OPTION_OBJECTSROWS )
446 snprintf(post_line, 100, "}");
448 snprintf(post_line, 100, "]");
449 snprintf(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
450 snprintf(finish, 100, "\n ]\n}\n");
452 buffer_sprintf(wb, "{\n %supdate_every%s: %d,\n %slabels%s: [", kq, kq, r->update_every, kq, kq);
453 buffer_sprintf(wb, "%stime%s", sq, sq);
456 // -------------------------------------------------------------------------
457 // print the JSON header
462 // print the header lines
463 for(c = 0, i = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
464 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
465 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
467 buffer_strcat(wb, pre_label);
468 buffer_strcat(wb, rd->name);
469 buffer_strcat(wb, post_label);
473 buffer_strcat(wb, pre_label);
474 buffer_strcat(wb, "no data");
475 buffer_strcat(wb, post_label);
478 // print the begin of row data
479 buffer_strcat(wb, data_begin);
481 // if all dimensions are hidden, print a null
483 buffer_strcat(wb, pre_value);
484 if(options & RRDR_OPTION_NULL2ZERO)
485 buffer_strcat(wb, "0");
487 buffer_strcat(wb, "null");
488 buffer_strcat(wb, post_value);
491 long start = 0, end = rrdr_rows(r), step = 1;
492 if((options & RRDR_OPTION_REVERSED)) {
493 start = rrdr_rows(r) - 1;
498 // for each line in the array
499 for(i = start; i != end ;i += step) {
500 calculated_number *cn = &r->v[ i * r->d ];
501 uint8_t *co = &r->o[ i * r->d ];
503 time_t now = r->t[i];
505 if(dates == JSON_DATES_JS) {
506 // generate the local date time
507 struct tm *tm = localtime(&now);
508 if(!tm) { error("localtime() failed."); continue; }
510 if(likely(i != start)) buffer_strcat(wb, ",\n");
511 buffer_strcat(wb, pre_date);
513 if( options & RRDR_OPTION_OBJECTSROWS )
514 buffer_sprintf(wb, "%stime%s: ", kq, kq);
517 buffer_strcat(wb, "new ");
519 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
521 buffer_strcat(wb, post_date);
523 if(row_annotations) {
524 // google supports one annotation per row
525 int annotation_found = 0;
526 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
527 if(co[c] & RRDR_RESET) {
528 buffer_strcat(wb, overflow_annotation);
529 annotation_found = 1;
533 if(!annotation_found)
534 buffer_strcat(wb, normal_annotation);
538 // print the timestamp of the line
539 if(likely(i != start)) buffer_strcat(wb, ",\n");
540 buffer_strcat(wb, pre_date);
542 if( options & RRDR_OPTION_OBJECTSROWS )
543 buffer_sprintf(wb, "%stime%s: ", kq, kq);
545 buffer_rrd_value(wb, (calculated_number)r->t[i]);
547 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
549 buffer_strcat(wb, post_date);
552 // for each dimension
553 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
554 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
555 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
557 calculated_number n = cn[c];
559 buffer_strcat(wb, pre_value);
561 if( options & RRDR_OPTION_OBJECTSROWS )
562 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
564 if(co[c] & RRDR_EMPTY) {
565 if(options & RRDR_OPTION_NULL2ZERO)
566 buffer_strcat(wb, "0");
568 buffer_strcat(wb, "null");
570 else if((options & RRDR_OPTION_ABSOLUTE))
571 buffer_rrd_value(wb, (n<0)?-n:n);
573 buffer_rrd_value(wb, n);
575 buffer_strcat(wb, post_value);
578 buffer_strcat(wb, post_line);
581 buffer_strcat(wb, finish);
584 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline)
589 // print the csv header
590 for(c = 0, i = 0, d = r->st->dimensions; d ;c++, d = d->next) {
591 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
592 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
595 buffer_strcat(wb, startline);
596 buffer_strcat(wb, "time");
598 buffer_strcat(wb, separator);
599 buffer_strcat(wb, d->name);
602 buffer_strcat(wb, endline);
605 // no dimensions present
609 long start = 0, end = rrdr_rows(r), step = 1;
610 if((options & RRDR_OPTION_REVERSED)) {
611 start = rrdr_rows(r) - 1;
616 // for each line in the array
617 for(i = start; i != end ;i += step) {
618 calculated_number *cn = &r->v[ i * r->d ];
619 uint8_t *co = &r->o[ i * r->d ];
621 buffer_strcat(wb, startline);
623 time_t now = r->t[i];
625 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
626 // print the timestamp of the line
627 buffer_rrd_value(wb, (calculated_number)now);
629 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
632 // generate the local date time
633 struct tm *tm = localtime(&now);
634 if(!tm) { error("localtime() failed."); continue; }
635 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
638 // for each dimension
639 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
640 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
641 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
643 buffer_strcat(wb, separator);
645 calculated_number n = cn[c];
647 if(co[c] & RRDR_EMPTY) {
648 if(options & RRDR_OPTION_NULL2ZERO)
649 buffer_strcat(wb, "0");
651 buffer_strcat(wb, "null");
653 else if((options & RRDR_OPTION_ABSOLUTE))
654 buffer_rrd_value(wb, (n<0)?-n:n);
656 buffer_rrd_value(wb, n);
659 buffer_strcat(wb, endline);
663 static void rrdr2ssv(RRDR *r, BUFFER *out, uint32_t options, const char *prefix, const char *separator, const char *suffix)
668 buffer_strcat(out, prefix);
669 long start = 0, end = rrdr_rows(r), step = 1;
670 if((options & RRDR_OPTION_REVERSED)) {
671 start = rrdr_rows(r) - 1;
676 // for each line in the array
677 for(i = start; i != end ;i += step) {
679 calculated_number *cn = &r->v[ i * r->d ];
680 uint8_t *co = &r->o[ i * r->d ];
682 calculated_number sum = 0, min = 0, max = 0;
683 int all_null = 1, init = 1;
685 // for each dimension
686 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
687 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
688 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
690 calculated_number n = cn[c];
704 if(likely(!(co[c] & RRDR_EMPTY))) {
706 if((options & RRDR_OPTION_ABSOLUTE) && n < 0) n = -n;
714 if(likely(i != start))
715 buffer_strcat(out, separator);
718 if(options & RRDR_OPTION_NULL2ZERO)
719 buffer_strcat(out, "0");
721 buffer_strcat(out, "null");
723 else if(options & RRDR_OPTION_MIN2MAX)
724 buffer_rrd_value(out, max - min);
726 buffer_rrd_value(out, sum);
728 buffer_strcat(out, suffix);
731 inline static calculated_number *rrdr_line_values(RRDR *r)
733 return &r->v[ r->c * r->d ];
736 inline static uint8_t *rrdr_line_options(RRDR *r)
738 return &r->o[ r->c * r->d ];
741 inline static int rrdr_line_init(RRDR *r, time_t t)
745 if(unlikely(r->c >= r->n)) {
746 error("requested to step above RRDR size for chart %s", r->st->name);
756 inline static void rrdr_lock_rrdset(RRDR *r) {
758 error("NULL value given!");
762 pthread_rwlock_rdlock(&r->st->rwlock);
766 inline static void rrdr_unlock_rrdset(RRDR *r) {
768 error("NULL value given!");
772 if(likely(r->has_st_lock)) {
773 pthread_rwlock_unlock(&r->st->rwlock);
778 inline static void rrdr_free(RRDR *r)
781 error("NULL value given!");
785 rrdr_unlock_rrdset(r);
786 if(likely(r->t)) free(r->t);
787 if(likely(r->v)) free(r->v);
788 if(likely(r->o)) free(r->o);
789 if(likely(r->od)) free(r->od);
793 inline void rrdr_done(RRDR *r)
799 static RRDR *rrdr_create(RRDSET *st, int n)
802 error("NULL value given!");
806 RRDR *r = calloc(1, sizeof(RRDR));
807 if(unlikely(!r)) goto cleanup;
814 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
818 r->t = calloc(n, sizeof(time_t));
819 if(unlikely(!r->t)) goto cleanup;
821 r->v = calloc(n * r->d, sizeof(calculated_number));
822 if(unlikely(!r->v)) goto cleanup;
824 r->o = calloc(n * r->d, sizeof(uint8_t));
825 if(unlikely(!r->o)) goto cleanup;
827 r->od = calloc(r->d, sizeof(uint8_t));
828 if(unlikely(!r->od)) goto cleanup;
836 error("Cannot allocate memory");
837 if(likely(r)) rrdr_free(r);
841 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method)
843 int debug = st->debug;
845 time_t first_entry_t = rrdset_first_entry_t(st);
846 time_t last_entry_t = rrdset_last_entry_t(st);
848 if(before == 0 && after == 0) {
849 before = last_entry_t;
850 after = first_entry_t;
853 // allow relative for before and after
854 if(before <= st->update_every * st->entries) before = last_entry_t + before;
855 if(after <= st->update_every * st->entries) after = last_entry_t + after;
857 // make sure they are within our timeframe
858 if(before > last_entry_t) before = last_entry_t;
859 if(before < first_entry_t) before = first_entry_t;
861 if(after > last_entry_t) after = last_entry_t;
862 if(after < first_entry_t) after = first_entry_t;
864 // check if they are upside down
871 // the duration of the chart
872 time_t duration = before - after;
873 if(duration <= 0) return NULL;
875 // check the required points
876 if(points > duration / st->update_every) points = 0;
877 if(points <= 0) points = duration / st->update_every;
879 // calculate proper grouping of source data
880 long group = duration / points;
881 if(group <= 0) group = 1;
882 if(duration / group > points) group++;
884 // error("NEW: points=%d after=%d before=%d group=%d, duration=%d", points, after, before, group, duration);
887 // before = the end time of the calculation
888 // after = the start time of the calculation
889 // duration = the duration of the calculation
890 // group = the number of source points to aggregate / group together
891 // method = the method of grouping source points
892 // points = the number of points to generate
895 // -------------------------------------------------------------------------
896 // initialize our result set
898 RRDR *r = rrdr_create(st, points);
905 // find how many dimensions we have
906 long dimensions = r->d;
909 // -------------------------------------------------------------------------
910 // checks for debugging
912 if(debug) debug(D_RRD_STATS, "INFO %s first_t: %lu, last_t: %lu, all_duration: %lu, after: %lu, before: %lu, duration: %lu, points: %ld, group: %ld"
916 , last_entry_t - first_entry_t
925 // -------------------------------------------------------------------------
926 // temp arrays for keeping values per dimension
928 calculated_number group_values[dimensions]; // keep sums when grouping
929 long group_counts[dimensions]; // keep the number of values added to group_values
930 uint8_t group_options[dimensions];
931 uint8_t found_non_zero[dimensions];
937 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
940 group_options[c] = 0;
941 found_non_zero[c] = 0;
945 // -------------------------------------------------------------------------
948 long start_at_slot = rrdset_time2slot(st, before), // rrdset_last_slot(st),
949 stop_at_slot = rrdset_time2slot(st, after);
951 time_t now = rrdset_slot2time(st, start_at_slot),
952 dt = st->update_every,
955 if(unlikely(debug)) debug(D_RRD_STATS, "INFO %s after_t: %lu (stop_at_t: %ld), before_t: %lu (start_at_t: %ld), start_t(now): %lu, current_entry: %ld, entries: %ld"
966 // align to group for proper panning of data
967 start_at_slot -= start_at_slot % group;
968 stop_at_slot -= stop_at_slot % group;
969 now = rrdset_slot2time(st, start_at_slot);
971 if(unlikely(debug)) debug(D_RRD_STATS, "BEGIN %s after_t: %lu (stop_at_t: %ld), before_t: %lu (start_at_t: %ld), start_t(now): %lu, current_entry: %ld, entries: %ld"
983 r->update_every = group * st->update_every;
985 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
986 for(; !stop_now ; now -= dt, slot--, counter++) {
987 if(unlikely(slot < 0)) slot = st->entries - 1;
988 if(unlikely(slot == stop_at_slot)) stop_now = counter;
990 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %lu, %s %s"
997 , (group_count + 1 == group)?"PRINT":" - "
998 , (now >= after && now <= before)?"RANGE":" - "
1001 // make sure we return data in the proper time range
1002 if(unlikely(now > before)) continue;
1003 if(unlikely(now < after)) break;
1005 if(unlikely(group_count == 0)) group_start_t = now;
1008 if(unlikely(group_count == group)) {
1009 if(unlikely(added >= points)) break;
1013 // do the calculations
1014 for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
1015 storage_number n = rd->values[slot];
1016 if(unlikely(!does_storage_number_exist(n))) continue;
1020 calculated_number value = unpack_storage_number(n);
1021 if(likely(value != 0.0)) {
1022 group_options[c] |= RRDR_NONZERO;
1023 found_non_zero[c] = 1;
1026 if(unlikely(did_storage_number_reset(n)))
1027 group_options[c] |= RRDR_RESET;
1029 switch(group_method) {
1031 if(unlikely(abs(value) > abs(group_values[c])))
1032 group_values[c] = value;
1038 group_values[c] += value;
1044 if(unlikely(add_this)) {
1045 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1047 calculated_number *cn = rrdr_line_values(r);
1048 uint8_t *co = rrdr_line_options(r);
1050 for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
1052 // update the dimension options
1053 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1054 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] |= RRDR_HIDDEN;
1056 // store the specific point options
1057 co[c] = group_options[c];
1060 if(unlikely(group_counts[c] == 0)) {
1062 co[c] |= RRDR_EMPTY;
1064 else if(unlikely(group_method == GROUP_AVERAGE)) {
1065 cn[c] = group_values[c] / group_counts[c];
1068 cn[c] = group_values[c];
1071 // reset them for the next loop
1072 group_values[c] = 0;
1073 group_counts[c] = 0;
1074 group_options[c] = 0;
1087 int rrd2format(RRDSET *st, BUFFER *out, BUFFER *dimensions, uint32_t format, long points, long long after, long long before, int group_method, uint32_t options, time_t *latest_timestamp)
1089 RRDR *rrdr = rrd2rrdr(st, points, after, before, group_method);
1091 buffer_strcat(out, "Cannot generate output with these parameters on this chart.");
1096 rrdr_disable_not_selected_dimensions(rrdr, buffer_tostring(dimensions));
1098 if(latest_timestamp && rrdr_rows(rrdr) > 0)
1099 *latest_timestamp = rrdr->t[rrdr_rows(rrdr) - 1];
1102 case DATASOURCE_SSV:
1103 out->contenttype = CT_TEXT_PLAIN;
1104 rrdr2ssv(rrdr, out, options, "", " ", "");
1107 case DATASOURCE_SSV_COMMA:
1108 out->contenttype = CT_TEXT_PLAIN;
1109 rrdr2ssv(rrdr, out, options, "", ",", "");
1112 case DATASOURCE_JS_ARRAY:
1113 out->contenttype = CT_APPLICATION_JSON;
1114 rrdr2ssv(rrdr, out, options, "[", ",", "]");
1117 case DATASOURCE_CSV:
1118 out->contenttype = CT_TEXT_PLAIN;
1119 rrdr2csv(rrdr, out, options, "", ",", "\r\n");
1122 case DATASOURCE_TSV:
1123 out->contenttype = CT_TEXT_PLAIN;
1124 rrdr2csv(rrdr, out, options, "", "\t", "\r\n");
1127 case DATASOURCE_HTML:
1128 out->contenttype = CT_TEXT_HTML;
1129 buffer_strcat(out, "<html>\n<center><table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">");
1130 rrdr2csv(rrdr, out, options, "<tr><td>", "</td><td>", "</td></tr>\n");
1131 buffer_strcat(out, "</table>\n</center>\n</html>\n");
1134 case DATASOURCE_DATATABLE_JSONP:
1135 out->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1136 rrdr2json(rrdr, out, options, 1);
1139 case DATASOURCE_DATATABLE_JSON:
1140 out->contenttype = CT_APPLICATION_JSON;
1141 rrdr2json(rrdr, out, options, 1);
1144 case DATASOURCE_JSONP:
1145 out->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1146 rrdr2json(rrdr, out, options, 0);
1149 case DATASOURCE_JSON:
1151 out->contenttype = CT_APPLICATION_JSON;
1152 rrdr2json(rrdr, out, options, 0);
1160 unsigned long rrd_stats_json(int type, RRDSET *st, BUFFER *wb, int points, int group, int group_method, time_t after, time_t before, int only_non_zero)
1163 pthread_rwlock_rdlock(&st->rwlock);
1166 // -------------------------------------------------------------------------
1167 // switch from JSON to google JSON
1172 case DATASOURCE_DATATABLE_JSON:
1173 case DATASOURCE_DATATABLE_JSONP:
1178 case DATASOURCE_JSON:
1184 // -------------------------------------------------------------------------
1185 // validate the parameters
1187 if(points < 1) points = 1;
1188 if(group < 1) group = 1;
1190 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1191 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1195 // our return value (the last timestamp printed)
1196 // this is required to detect re-transmit in google JSONP
1197 time_t last_timestamp = 0;
1200 // -------------------------------------------------------------------------
1201 // find how many dimensions we have
1205 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1207 pthread_rwlock_unlock(&st->rwlock);
1208 buffer_strcat(wb, "No dimensions yet.");
1213 // -------------------------------------------------------------------------
1214 // prepare various strings, to speed up the loop
1216 char overflow_annotation[201]; snprintf(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
1217 char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1218 char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1219 char post_date[21]; snprintf(post_date, 20, "%s}", sq);
1220 char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
1221 char post_value[21]; snprintf(post_value, 20, "}");
1224 // -------------------------------------------------------------------------
1225 // checks for debugging
1228 debug(D_RRD_STATS, "%s first_entry_t = %lu, last_entry_t = %lu, duration = %lu, after = %lu, before = %lu, duration = %lu, entries_to_show = %lu, group = %lu"
1230 , rrdset_first_entry_t(st)
1231 , rrdset_last_entry_t(st)
1232 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1241 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
1243 if((before - after) > st->entries * st->update_every)
1244 debug(D_RRD_STATS, "WARNING: %s The time difference between the oldest and the newest entries (%lu) is higher than the capacity of the database (%lu)", st->name, before - after, st->entries * st->update_every);
1248 // -------------------------------------------------------------------------
1249 // temp arrays for keeping values per dimension
1251 calculated_number group_values[dimensions]; // keep sums when grouping
1252 int print_hidden[dimensions]; // keep hidden flags
1253 int found_non_zero[dimensions];
1254 int found_non_existing[dimensions];
1257 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1258 group_values[c] = 0;
1259 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1260 found_non_zero[c] = 0;
1261 found_non_existing[c] = 0;
1265 // 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);
1266 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1267 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1269 // -------------------------------------------------------------------------
1270 // remove dimensions that contain only zeros
1273 if(only_non_zero) max_loop = 2;
1275 for(; max_loop ; max_loop--) {
1277 // -------------------------------------------------------------------------
1278 // print the JSON header
1280 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1281 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);
1282 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);
1283 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);
1285 // print the header for each dimension
1286 // and update the print_hidden array for the dimensions that should be hidden
1288 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1289 if(!print_hidden[c]) {
1291 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);
1295 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);
1298 // print the begin of row data
1299 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1302 // -------------------------------------------------------------------------
1305 int annotate_reset = 0;
1306 int annotation_count = 0;
1308 long t = rrdset_time2slot(st, before),
1309 stop_at_t = rrdset_time2slot(st, after),
1314 time_t now = rrdset_slot2time(st, t),
1315 dt = st->update_every;
1317 long count = 0, printed = 0, group_count = 0;
1320 if(st->debug) debug(D_RRD_STATS, "%s: REQUEST after:%lu before:%lu, points:%d, group:%d, CHART cur:%ld first: %lu last:%lu, CALC start_t:%ld, stop_t:%ld"
1327 , rrdset_first_entry_t(st)
1328 , rrdset_last_entry_t(st)
1333 for(; !stop_now ; now -= dt, t--) {
1334 if(t < 0) t = st->entries - 1;
1335 if(t == stop_at_t) stop_now = 1;
1339 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
1346 , (group_count + 1 == group)?"PRINT":" - "
1347 , (now >= after && now <= before)?"RANGE":" - "
1351 // make sure we return data in the proper time range
1352 if(now > before) continue;
1353 if(now < after) break;
1355 //if(rrdset_slot2time(st, t) != now)
1356 // 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));
1361 // check if we have to print this now
1362 if(group_count == group) {
1363 if(printed >= points) {
1364 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
1368 // generate the local date time
1369 struct tm *tm = localtime(&now);
1370 if(!tm) { error("localtime() failed."); continue; }
1371 if(now > last_timestamp) last_timestamp = now;
1373 if(printed) buffer_strcat(wb, "]},\n");
1374 buffer_strcat(wb, pre_date);
1375 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1376 buffer_strcat(wb, post_date);
1381 // do the calculations
1382 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1383 storage_number n = rd->values[t];
1384 calculated_number value = unpack_storage_number(n);
1386 if(!does_storage_number_exist(n)) {
1388 found_non_existing[c]++;
1390 if(did_storage_number_reset(n)) annotate_reset = 1;
1392 switch(group_method) {
1394 if(abs(value) > abs(group_values[c])) group_values[c] = value;
1398 group_values[c] += value;
1403 group_values[c] += value;
1404 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
1410 if(annotate_reset) {
1412 buffer_strcat(wb, overflow_annotation);
1416 buffer_strcat(wb, normal_annotation);
1419 for(c = 0 ; c < dimensions ; c++) {
1420 if(found_non_existing[c] == group_count) {
1421 // all entries are non-existing
1423 buffer_strcat(wb, pre_value);
1424 buffer_strcat(wb, "null");
1425 buffer_strcat(wb, post_value);
1427 else if(!print_hidden[c]) {
1429 buffer_strcat(wb, pre_value);
1430 buffer_rrd_value(wb, group_values[c]);
1431 buffer_strcat(wb, post_value);
1433 if(group_values[c]) found_non_zero[c]++;
1436 // reset them for the next loop
1437 group_values[c] = 0;
1438 found_non_existing[c] = 0;
1441 // if all dimensions are hidden, print a null
1443 buffer_strcat(wb, pre_value);
1444 buffer_strcat(wb, "null");
1445 buffer_strcat(wb, post_value);
1453 if(printed) buffer_strcat(wb, "]}");
1454 buffer_strcat(wb, "\n ]\n}\n");
1456 if(only_non_zero && max_loop > 1) {
1458 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1459 group_values[c] = 0;
1460 found_non_existing[c] = 0;
1462 if(!print_hidden[c] && !found_non_zero[c]) {
1464 print_hidden[c] = 1;
1468 if(changed) buffer_flush(wb);
1475 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->len);
1477 pthread_rwlock_unlock(&st->rwlock);
1478 return last_timestamp;