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)
855 before = last_entry_t + before;
857 if(after <= st->update_every * st->entries)
858 after = last_entry_t + after;
860 // make sure they are within our timeframe
861 if(before > last_entry_t) before = last_entry_t;
862 if(before < first_entry_t) before = first_entry_t;
864 if(after > last_entry_t) after = last_entry_t;
865 if(after < first_entry_t) after = first_entry_t;
867 // check if they are upside down
874 // the duration of the chart
875 time_t duration = before - after;
876 if(duration <= 0) return NULL;
878 // how many points does the chart has?
879 long available_points = duration / st->update_every;
881 // check the wanted points
882 if(points < 0) points = -points;
883 if(points > available_points) points = available_points;
884 if(points == 0) points = available_points;
886 // calculate proper grouping of source data
887 long group = available_points / points;
888 if(group <= 0) group = 1;
890 // round group to the closest integer
891 if(available_points % points > points / 2) group++;
893 // find the starting and ending slots in our round robin db
894 long start_at_slot = rrdset_time2slot(st, before),
895 stop_at_slot = rrdset_time2slot(st, after);
897 // align them, so that panning on the data
898 // will always produce the same results
899 start_at_slot -= start_at_slot % group;
900 stop_at_slot -= stop_at_slot % group;
902 time_t after_new = rrdset_slot2time(st, stop_at_slot);
903 time_t before_new = rrdset_slot2time(st, start_at_slot);
904 long points_new = (before_new - after_new) / st->update_every / group;
906 if(after_new < first_entry_t) {
907 error("after_new %u is before min %u", after_new, first_entry_t);
908 after_new = first_entry_t;
909 points_new = (before_new - after_new) / st->update_every / group;
911 if(after_new > last_entry_t) {
912 error("after_new %u is after max %u", after_new, last_entry_t);
913 after_new = last_entry_t;
914 points_new = (before_new - after_new) / st->update_every / group;
916 if(before_new < first_entry_t) {
917 error("before_new %u is before min %u", before_new, first_entry_t);
918 before_new = first_entry_t;
919 points_new = (before_new - after_new) / st->update_every / group;
921 if(before_new > last_entry_t) {
922 error("before_new %u is after max %u", before_new, last_entry_t);
923 before_new = last_entry_t;
924 points_new = (before_new - after_new) / st->update_every / group;
926 if(start_at_slot < 0 || start_at_slot >= st->entries) {
927 error("start_at_slot is invalid %ld, expected %ld to %ld", start_at_slot, 0, st->entries - 1);
928 start_at_slot = st->current_entry;
929 points_new = (before_new - after_new) / st->update_every / group;
931 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
932 error("stop_at_slot is invalid %ld, expected %ld to %ld", stop_at_slot, 0, st->entries - 1);
934 points_new = (before_new - after_new) / st->update_every / group;
936 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
937 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
940 //error("SHIFT: %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);
944 duration = before - after;
948 // before = the end time of the calculation
949 // after = the start time of the calculation
950 // duration = the duration of the calculation
951 // group = the number of source points to aggregate / group together
952 // method = the method of grouping source points
953 // points = the number of points to generate
956 // -------------------------------------------------------------------------
957 // initialize our result set
959 RRDR *r = rrdr_create(st, points);
966 // find how many dimensions we have
967 long dimensions = r->d;
970 // -------------------------------------------------------------------------
971 // checks for debugging
973 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"
977 , last_entry_t - first_entry_t
986 // -------------------------------------------------------------------------
987 // temp arrays for keeping values per dimension
989 calculated_number group_values[dimensions]; // keep sums when grouping
990 long group_counts[dimensions]; // keep the number of values added to group_values
991 uint8_t group_options[dimensions];
992 uint8_t found_non_zero[dimensions];
998 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1000 group_counts[c] = 0;
1001 group_options[c] = 0;
1002 found_non_zero[c] = 0;
1006 // -------------------------------------------------------------------------
1009 time_t now = rrdset_slot2time(st, start_at_slot),
1010 dt = st->update_every,
1013 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"
1025 r->update_every = group * st->update_every;
1027 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1028 for(; !stop_now ; now -= dt, slot--, counter++) {
1029 if(unlikely(slot < 0)) slot = st->entries - 1;
1030 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1032 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %lu, %s %s"
1039 , (group_count + 1 == group)?"PRINT":" - "
1040 , (now >= after && now <= before)?"RANGE":" - "
1043 // make sure we return data in the proper time range
1044 if(unlikely(now > before)) continue;
1045 if(unlikely(now < after)) break;
1047 if(unlikely(group_count == 0)) group_start_t = now;
1050 if(unlikely(group_count == group)) {
1051 if(unlikely(added >= points)) break;
1055 // do the calculations
1056 for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
1057 storage_number n = rd->values[slot];
1058 if(unlikely(!does_storage_number_exist(n))) continue;
1062 calculated_number value = unpack_storage_number(n);
1063 if(likely(value != 0.0)) {
1064 group_options[c] |= RRDR_NONZERO;
1065 found_non_zero[c] = 1;
1068 if(unlikely(did_storage_number_reset(n)))
1069 group_options[c] |= RRDR_RESET;
1071 switch(group_method) {
1073 if(unlikely(abs(value) > abs(group_values[c])))
1074 group_values[c] = value;
1080 group_values[c] += value;
1086 if(unlikely(add_this)) {
1087 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1089 calculated_number *cn = rrdr_line_values(r);
1090 uint8_t *co = rrdr_line_options(r);
1092 for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
1094 // update the dimension options
1095 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1096 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] |= RRDR_HIDDEN;
1098 // store the specific point options
1099 co[c] = group_options[c];
1102 if(unlikely(group_counts[c] == 0)) {
1104 co[c] |= RRDR_EMPTY;
1106 else if(unlikely(group_method == GROUP_AVERAGE)) {
1107 cn[c] = group_values[c] / group_counts[c];
1110 cn[c] = group_values[c];
1113 // reset them for the next loop
1114 group_values[c] = 0;
1115 group_counts[c] = 0;
1116 group_options[c] = 0;
1126 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1130 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)
1132 RRDR *rrdr = rrd2rrdr(st, points, after, before, group_method);
1134 buffer_strcat(out, "Cannot generate output with these parameters on this chart.");
1139 rrdr_disable_not_selected_dimensions(rrdr, buffer_tostring(dimensions));
1141 if(latest_timestamp && rrdr_rows(rrdr) > 0)
1142 *latest_timestamp = rrdr->t[rrdr_rows(rrdr) - 1];
1145 case DATASOURCE_SSV:
1146 out->contenttype = CT_TEXT_PLAIN;
1147 rrdr2ssv(rrdr, out, options, "", " ", "");
1150 case DATASOURCE_SSV_COMMA:
1151 out->contenttype = CT_TEXT_PLAIN;
1152 rrdr2ssv(rrdr, out, options, "", ",", "");
1155 case DATASOURCE_JS_ARRAY:
1156 out->contenttype = CT_APPLICATION_JSON;
1157 rrdr2ssv(rrdr, out, options, "[", ",", "]");
1160 case DATASOURCE_CSV:
1161 out->contenttype = CT_TEXT_PLAIN;
1162 rrdr2csv(rrdr, out, options, "", ",", "\r\n");
1165 case DATASOURCE_TSV:
1166 out->contenttype = CT_TEXT_PLAIN;
1167 rrdr2csv(rrdr, out, options, "", "\t", "\r\n");
1170 case DATASOURCE_HTML:
1171 out->contenttype = CT_TEXT_HTML;
1172 buffer_strcat(out, "<html>\n<center><table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">");
1173 rrdr2csv(rrdr, out, options, "<tr><td>", "</td><td>", "</td></tr>\n");
1174 buffer_strcat(out, "</table>\n</center>\n</html>\n");
1177 case DATASOURCE_DATATABLE_JSONP:
1178 out->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1179 rrdr2json(rrdr, out, options, 1);
1182 case DATASOURCE_DATATABLE_JSON:
1183 out->contenttype = CT_APPLICATION_JSON;
1184 rrdr2json(rrdr, out, options, 1);
1187 case DATASOURCE_JSONP:
1188 out->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1189 rrdr2json(rrdr, out, options, 0);
1192 case DATASOURCE_JSON:
1194 out->contenttype = CT_APPLICATION_JSON;
1195 rrdr2json(rrdr, out, options, 0);
1203 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)
1206 pthread_rwlock_rdlock(&st->rwlock);
1209 // -------------------------------------------------------------------------
1210 // switch from JSON to google JSON
1215 case DATASOURCE_DATATABLE_JSON:
1216 case DATASOURCE_DATATABLE_JSONP:
1221 case DATASOURCE_JSON:
1227 // -------------------------------------------------------------------------
1228 // validate the parameters
1230 if(points < 1) points = 1;
1231 if(group < 1) group = 1;
1233 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1234 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1238 // our return value (the last timestamp printed)
1239 // this is required to detect re-transmit in google JSONP
1240 time_t last_timestamp = 0;
1243 // -------------------------------------------------------------------------
1244 // find how many dimensions we have
1248 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1250 pthread_rwlock_unlock(&st->rwlock);
1251 buffer_strcat(wb, "No dimensions yet.");
1256 // -------------------------------------------------------------------------
1257 // prepare various strings, to speed up the loop
1259 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);
1260 char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1261 char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1262 char post_date[21]; snprintf(post_date, 20, "%s}", sq);
1263 char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
1264 char post_value[21]; snprintf(post_value, 20, "}");
1267 // -------------------------------------------------------------------------
1268 // checks for debugging
1271 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"
1273 , rrdset_first_entry_t(st)
1274 , rrdset_last_entry_t(st)
1275 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1284 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
1286 if((before - after) > st->entries * st->update_every)
1287 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);
1291 // -------------------------------------------------------------------------
1292 // temp arrays for keeping values per dimension
1294 calculated_number group_values[dimensions]; // keep sums when grouping
1295 int print_hidden[dimensions]; // keep hidden flags
1296 int found_non_zero[dimensions];
1297 int found_non_existing[dimensions];
1300 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1301 group_values[c] = 0;
1302 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1303 found_non_zero[c] = 0;
1304 found_non_existing[c] = 0;
1308 // 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);
1309 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1310 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1312 // -------------------------------------------------------------------------
1313 // remove dimensions that contain only zeros
1316 if(only_non_zero) max_loop = 2;
1318 for(; max_loop ; max_loop--) {
1320 // -------------------------------------------------------------------------
1321 // print the JSON header
1323 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1324 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);
1325 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);
1326 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);
1328 // print the header for each dimension
1329 // and update the print_hidden array for the dimensions that should be hidden
1331 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1332 if(!print_hidden[c]) {
1334 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);
1338 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);
1341 // print the begin of row data
1342 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1345 // -------------------------------------------------------------------------
1348 int annotate_reset = 0;
1349 int annotation_count = 0;
1351 long t = rrdset_time2slot(st, before),
1352 stop_at_t = rrdset_time2slot(st, after),
1357 time_t now = rrdset_slot2time(st, t),
1358 dt = st->update_every;
1360 long count = 0, printed = 0, group_count = 0;
1363 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"
1370 , rrdset_first_entry_t(st)
1371 , rrdset_last_entry_t(st)
1376 for(; !stop_now ; now -= dt, t--) {
1377 if(t < 0) t = st->entries - 1;
1378 if(t == stop_at_t) stop_now = 1;
1382 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
1389 , (group_count + 1 == group)?"PRINT":" - "
1390 , (now >= after && now <= before)?"RANGE":" - "
1394 // make sure we return data in the proper time range
1395 if(now > before) continue;
1396 if(now < after) break;
1398 //if(rrdset_slot2time(st, t) != now)
1399 // 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));
1404 // check if we have to print this now
1405 if(group_count == group) {
1406 if(printed >= points) {
1407 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
1411 // generate the local date time
1412 struct tm *tm = localtime(&now);
1413 if(!tm) { error("localtime() failed."); continue; }
1414 if(now > last_timestamp) last_timestamp = now;
1416 if(printed) buffer_strcat(wb, "]},\n");
1417 buffer_strcat(wb, pre_date);
1418 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1419 buffer_strcat(wb, post_date);
1424 // do the calculations
1425 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1426 storage_number n = rd->values[t];
1427 calculated_number value = unpack_storage_number(n);
1429 if(!does_storage_number_exist(n)) {
1431 found_non_existing[c]++;
1433 if(did_storage_number_reset(n)) annotate_reset = 1;
1435 switch(group_method) {
1437 if(abs(value) > abs(group_values[c])) group_values[c] = value;
1441 group_values[c] += value;
1446 group_values[c] += value;
1447 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
1453 if(annotate_reset) {
1455 buffer_strcat(wb, overflow_annotation);
1459 buffer_strcat(wb, normal_annotation);
1462 for(c = 0 ; c < dimensions ; c++) {
1463 if(found_non_existing[c] == group_count) {
1464 // all entries are non-existing
1466 buffer_strcat(wb, pre_value);
1467 buffer_strcat(wb, "null");
1468 buffer_strcat(wb, post_value);
1470 else if(!print_hidden[c]) {
1472 buffer_strcat(wb, pre_value);
1473 buffer_rrd_value(wb, group_values[c]);
1474 buffer_strcat(wb, post_value);
1476 if(group_values[c]) found_non_zero[c]++;
1479 // reset them for the next loop
1480 group_values[c] = 0;
1481 found_non_existing[c] = 0;
1484 // if all dimensions are hidden, print a null
1486 buffer_strcat(wb, pre_value);
1487 buffer_strcat(wb, "null");
1488 buffer_strcat(wb, post_value);
1496 if(printed) buffer_strcat(wb, "]}");
1497 buffer_strcat(wb, "\n ]\n}\n");
1499 if(only_non_zero && max_loop > 1) {
1501 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1502 group_values[c] = 0;
1503 found_non_existing[c] = 0;
1505 if(!print_hidden[c] && !found_non_zero[c]) {
1507 print_hidden[c] = 1;
1511 if(changed) buffer_flush(wb);
1518 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->len);
1520 pthread_rwlock_unlock(&st->rwlock);
1521 return last_timestamp;