13 #define HOSTNAME_MAX 1024
14 char *hostname = "unknown";
16 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb)
18 pthread_rwlock_rdlock(&st->rwlock);
22 "\t\t\t\"id\": \"%s\",\n"
23 "\t\t\t\"name\": \"%s\",\n"
24 "\t\t\t\"type\": \"%s\",\n"
25 "\t\t\t\"family\": \"%s\",\n"
26 "\t\t\t\"title\": \"%s\",\n"
27 "\t\t\t\"priority\": %ld,\n"
28 "\t\t\t\"enabled\": %s,\n"
29 "\t\t\t\"units\": \"%s\",\n"
30 "\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
31 "\t\t\t\"chart_type\": \"%s\",\n"
32 "\t\t\t\"duration\": %ld,\n"
33 "\t\t\t\"first_entry\": %lu,\n"
34 "\t\t\t\"last_entry\": %lu,\n"
35 "\t\t\t\"update_every\": %d,\n"
36 "\t\t\t\"dimensions\": {\n"
43 , st->enabled?"true":"false"
46 , rrdset_type_name(st->chart_type)
47 , st->entries * st->update_every
48 , rrdset_first_entry_t(st)
49 , rrdset_last_entry_t(st)
53 unsigned long memory = st->memsize;
57 for(rd = st->dimensions; rd ; rd = rd->next) {
58 if(rd->flags & RRDDIM_FLAG_HIDDEN) continue;
60 memory += rd->memsize;
64 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
78 pthread_rwlock_unlock(&st->rwlock);
81 void rrd_stats_api_v1_charts(BUFFER *wb)
86 buffer_sprintf(wb, "{\n"
87 "\t\"hostname\": \"%s\""
88 ",\n\t\"update_every\": %d"
89 ",\n\t\"history\": %d"
93 , rrd_default_history_entries
96 pthread_rwlock_rdlock(&rrdset_root_rwlock);
97 for(st = rrdset_root, c = 0; st ; st = st->next) {
99 if(c) buffer_strcat(wb, ",");
100 buffer_strcat(wb, "\n\t\t\"");
101 buffer_strcat(wb, st->id);
102 buffer_strcat(wb, "\": ");
103 rrd_stats_api_v1_chart(st, wb);
107 pthread_rwlock_unlock(&rrdset_root_rwlock);
109 buffer_strcat(wb, "\n\t}\n}\n");
113 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
115 time_t now = time(NULL);
117 pthread_rwlock_rdlock(&st->rwlock);
121 "\t\t\t\"id\": \"%s\",\n"
122 "\t\t\t\"name\": \"%s\",\n"
123 "\t\t\t\"type\": \"%s\",\n"
124 "\t\t\t\"family\": \"%s\",\n"
125 "\t\t\t\"title\": \"%s\",\n"
126 "\t\t\t\"priority\": %ld,\n"
127 "\t\t\t\"enabled\": %d,\n"
128 "\t\t\t\"units\": \"%s\",\n"
129 "\t\t\t\"url\": \"/data/%s/%s\",\n"
130 "\t\t\t\"chart_type\": \"%s\",\n"
131 "\t\t\t\"counter\": %ld,\n"
132 "\t\t\t\"entries\": %ld,\n"
133 "\t\t\t\"first_entry_t\": %lu,\n"
134 "\t\t\t\"last_entry\": %ld,\n"
135 "\t\t\t\"last_entry_t\": %lu,\n"
136 "\t\t\t\"last_entry_secs_ago\": %lu,\n"
137 "\t\t\t\"update_every\": %d,\n"
138 "\t\t\t\"isdetail\": %d,\n"
139 "\t\t\t\"usec_since_last_update\": %llu,\n"
140 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
141 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
142 "\t\t\t\"dimensions\": [\n"
151 , st->name, options?options:""
152 , rrdset_type_name(st->chart_type)
155 , rrdset_first_entry_t(st)
156 , rrdset_last_slot(st)
157 , rrdset_last_entry_t(st)
158 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
161 , st->usec_since_last_update
162 , st->collected_total
163 , st->last_collected_total
166 unsigned long memory = st->memsize;
169 for(rd = st->dimensions; rd ; rd = rd->next) {
171 memory += rd->memsize;
175 "\t\t\t\t\t\"id\": \"%s\",\n"
176 "\t\t\t\t\t\"name\": \"%s\",\n"
177 "\t\t\t\t\t\"entries\": %ld,\n"
178 "\t\t\t\t\t\"isHidden\": %d,\n"
179 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
180 "\t\t\t\t\t\"multiplier\": %ld,\n"
181 "\t\t\t\t\t\"divisor\": %ld,\n"
182 "\t\t\t\t\t\"last_entry_t\": %lu,\n"
183 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
184 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
185 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
186 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
187 "\t\t\t\t\t\"memory\": %lu\n"
192 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
193 , rrddim_algorithm_name(rd->algorithm)
196 , rd->last_collected_time.tv_sec
197 , rd->collected_value
198 , rd->calculated_value
199 , rd->last_collected_value
200 , rd->last_calculated_value
208 "\t\t\t\"memory\" : %lu\n"
213 pthread_rwlock_unlock(&st->rwlock);
217 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
218 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
220 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
222 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
223 rrd_stats_one_json(st, options, wb);
224 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
227 void rrd_stats_all_json(BUFFER *wb)
229 unsigned long memory = 0;
233 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
235 pthread_rwlock_rdlock(&rrdset_root_rwlock);
236 for(st = rrdset_root, c = 0; st ; st = st->next) {
238 if(c) buffer_strcat(wb, ",\n");
239 memory += rrd_stats_one_json(st, NULL, wb);
243 pthread_rwlock_unlock(&rrdset_root_rwlock);
245 buffer_sprintf(wb, "\n\t],\n"
246 "\t\"hostname\": \"%s\",\n"
247 "\t\"update_every\": %d,\n"
248 "\t\"history\": %d,\n"
249 "\t\"memory\": %lu\n"
253 , rrd_default_history_entries
260 // ----------------------------------------------------------------------------
263 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
264 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
265 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
266 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
269 typedef struct rrdresult {
270 RRDSET *st; // the chart this result refers to
272 int d; // the number of dimensions
273 int n; // the number of values in the arrays
275 uint8_t *od; // the options for the dimensions
277 time_t *t; // array of n timestamps
278 calculated_number *v; // array n x d values
279 uint8_t *o; // array n x d options
281 int c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
283 int has_st_lock; // if st is read locked by us
286 #define rrdr_rows(r) ((r)->c + 1)
289 static void rrdr_dump(RRDR *r)
294 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
296 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
297 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
300 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
301 , (r->od[c] & RRDR_RESET)?"RESET ":""
302 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
303 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
308 fprintf(stderr, "RRDR does not have any values in it.\n");
312 fprintf(stderr, "RRDR includes %d values in it:\n", r->c + 1);
314 // for each line in the array
315 for(i = 0; i <= r->c ;i++) {
316 calculated_number *cn = &r->v[ i * r->d ];
317 uint8_t *co = &r->o[ i * r->d ];
319 // print the id and the timestamp of the line
320 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
322 // for each dimension
323 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
324 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
325 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
327 if(co[c] & RRDR_EMPTY)
328 fprintf(stderr, "null ");
330 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
332 , (co[c] & RRDR_EMPTY)?"E":" "
333 , (co[c] & RRDR_RESET)?"R":" "
334 , (co[c] & RRDR_HIDDEN)?"H":" "
335 , (co[c] & RRDR_NONZERO)?"N":" "
339 fprintf(stderr, "\n");
344 void rrdr_disable_not_selected_dimensions(RRDR *r, const char *dims)
346 char b[strlen(dims) + 1];
353 // disable all of them
354 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
355 r->od[c] |= RRDR_HIDDEN;
357 while(o && *o && (tok = mystrsep(&o, ", |"))) {
360 // find it and enable it
361 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
362 if(!strcmp(d->name, tok)) {
363 r->od[c] &= ~RRDR_HIDDEN;
369 #define JSON_DATES_JS 1
370 #define JSON_DATES_TIMESTAMP 2
372 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int google)
374 int annotations = 0, dates = JSON_DATES_JS;
375 char kq[2] = "", // key quote
376 sq[2] = "", // string quote
377 pre_label[101] = "", // before each label
378 post_label[101] = "", // after each label
379 pre_date[101] = "", // the beginning of line, to the date
380 post_date[101] = "", // closing the date
381 pre_value[101] = "", // before each value
382 post_value[101] = "", // after each value
383 post_line[101] = "", // at the end of each row
384 normal_annotation[201] = "", // default row annotation
385 overflow_annotation[201] = "", // overflow row annotation
386 data_begin[101] = "", // between labels and values
387 finish[101] = ""; // at the end of everything
390 dates = JSON_DATES_JS;
394 snprintf(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
395 snprintf(post_date, 100, "%s}", sq);
396 snprintf(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
397 snprintf(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
398 snprintf(pre_value, 100, ",{%sv%s:", kq, kq);
399 snprintf(post_value, 100, "}");
400 snprintf(post_line, 100, "]}");
401 snprintf(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
402 snprintf(finish, 100, "\n ]\n}\n");
404 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);
405 snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
407 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
408 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);
409 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);
410 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);
413 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
414 dates = JSON_DATES_TIMESTAMP;
415 snprintf(pre_date, 100, " [");
418 dates = JSON_DATES_JS;
419 snprintf(pre_date, 100, " [new ");
423 snprintf(pre_label, 100, ", \"");
424 snprintf(post_label, 100, "\"");
425 snprintf(pre_value, 100, ", ");
426 snprintf(post_line, 100, "]");
427 snprintf(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
428 snprintf(finish, 100, "\n ]\n}\n");
430 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
431 buffer_sprintf(wb, "%stime%s", sq, sq);
434 // -------------------------------------------------------------------------
435 // print the JSON header
440 // print the csv header
441 for(c = 0, i = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
442 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
443 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
445 buffer_strcat(wb, pre_label);
446 buffer_strcat(wb, rd->name);
447 buffer_strcat(wb, post_label);
451 buffer_strcat(wb, pre_label);
452 buffer_strcat(wb, "no data");
453 buffer_strcat(wb, post_label);
456 // print the begin of row data
457 buffer_strcat(wb, data_begin);
459 // if all dimensions are hidden, print a null
461 buffer_strcat(wb, pre_value);
462 if(options & RRDR_OPTION_NULL2ZERO)
463 buffer_strcat(wb, "0");
465 buffer_strcat(wb, "null");
466 buffer_strcat(wb, post_value);
469 long start = 0, end = rrdr_rows(r), step = 1;
470 if((options & RRDR_OPTION_REVERSED)) {
471 start = rrdr_rows(r) - 1;
476 // for each line in the array
477 for(i = start; i != end ;i += step) {
478 calculated_number *cn = &r->v[ i * r->d ];
479 uint8_t *co = &r->o[ i * r->d ];
481 time_t now = r->t[i];
483 if(dates == JSON_DATES_JS) {
484 // generate the local date time
485 struct tm *tm = localtime(&now);
486 if(!tm) { error("localtime() failed."); continue; }
488 if(likely(i != start)) buffer_strcat(wb, ",\n");
489 buffer_strcat(wb, pre_date);
490 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
491 buffer_strcat(wb, post_date);
494 if(co[c] & RRDR_RESET)
495 buffer_strcat(wb, overflow_annotation);
497 buffer_strcat(wb, normal_annotation);
501 // print the timestamp of the line
502 if(likely(i != start)) buffer_strcat(wb, ",\n");
503 buffer_strcat(wb, pre_date);
504 buffer_rrd_value(wb, (calculated_number)r->t[i]);
506 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
507 buffer_strcat(wb, post_date);
510 // for each dimension
511 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
512 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
513 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
515 calculated_number n = cn[c];
517 if(co[c] & RRDR_EMPTY) {
518 buffer_strcat(wb, pre_value);
519 if(options & RRDR_OPTION_NULL2ZERO)
520 buffer_strcat(wb, "0");
522 buffer_strcat(wb, "null");
523 buffer_strcat(wb, post_value);
525 else if((options & RRDR_OPTION_ABSOLUTE)) {
526 buffer_strcat(wb, pre_value);
527 buffer_rrd_value(wb, (n<0)?-n:n);
528 buffer_strcat(wb, post_value);
531 buffer_strcat(wb, pre_value);
532 buffer_rrd_value(wb, n);
533 buffer_strcat(wb, post_value);
537 buffer_strcat(wb, post_line);
540 buffer_strcat(wb, finish);
543 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline)
548 // print the csv header
549 for(c = 0, i = 0, d = r->st->dimensions; d ;c++, d = d->next) {
550 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
551 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
554 buffer_strcat(wb, startline);
555 buffer_strcat(wb, "time");
557 buffer_strcat(wb, separator);
558 buffer_strcat(wb, d->name);
561 buffer_strcat(wb, endline);
564 // no dimensions present
568 long start = 0, end = rrdr_rows(r), step = 1;
569 if((options & RRDR_OPTION_REVERSED)) {
570 start = rrdr_rows(r) - 1;
575 // for each line in the array
576 for(i = start; i != end ;i += step) {
577 calculated_number *cn = &r->v[ i * r->d ];
578 uint8_t *co = &r->o[ i * r->d ];
580 buffer_strcat(wb, startline);
582 time_t now = r->t[i];
584 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
585 // print the timestamp of the line
586 buffer_rrd_value(wb, (calculated_number)now);
588 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
591 // generate the local date time
592 struct tm *tm = localtime(&now);
593 if(!tm) { error("localtime() failed."); continue; }
594 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
597 // for each dimension
598 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
599 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
600 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
602 buffer_strcat(wb, separator);
604 calculated_number n = cn[c];
606 if(co[c] & RRDR_EMPTY) {
607 if(options & RRDR_OPTION_NULL2ZERO)
608 buffer_strcat(wb, "0");
610 buffer_strcat(wb, "null");
612 else if((options & RRDR_OPTION_ABSOLUTE))
613 buffer_rrd_value(wb, (n<0)?-n:n);
615 buffer_rrd_value(wb, n);
618 buffer_strcat(wb, endline);
622 static void rrdr2ssv(RRDR *r, BUFFER *out, uint32_t options, const char *prefix, const char *separator, const char *suffix)
627 buffer_strcat(out, prefix);
628 long start = 0, end = rrdr_rows(r), step = 1;
629 if((options & RRDR_OPTION_REVERSED)) {
630 start = rrdr_rows(r) - 1;
635 // for each line in the array
636 for(i = start; i != end ;i += step) {
638 calculated_number *cn = &r->v[ i * r->d ];
639 uint8_t *co = &r->o[ i * r->d ];
641 calculated_number sum = 0, min = 0, max = 0;
642 int all_null = 1, init = 1;
644 // for each dimension
645 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
646 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
647 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
649 calculated_number n = cn[c];
663 if(likely(!(co[c] & RRDR_EMPTY))) {
665 if((options & RRDR_OPTION_ABSOLUTE) && n < 0) n = -n;
673 if(likely(i != start))
674 buffer_strcat(out, separator);
677 if(options & RRDR_OPTION_NULL2ZERO)
678 buffer_strcat(out, "0");
680 buffer_strcat(out, "null");
682 else if(options & RRDR_OPTION_MIN2MAX)
683 buffer_rrd_value(out, max - min);
685 buffer_rrd_value(out, sum);
687 buffer_strcat(out, suffix);
690 inline static calculated_number *rrdr_line_values(RRDR *r)
692 return &r->v[ r->c * r->d ];
695 inline static uint8_t *rrdr_line_options(RRDR *r)
697 return &r->o[ r->c * r->d ];
700 inline static int rrdr_line_init(RRDR *r, time_t t)
703 if(unlikely(r->c >= r->n)) {
714 inline static void rrdr_lock_rrdset(RRDR *r) {
716 error("NULL value given!");
720 pthread_rwlock_rdlock(&r->st->rwlock);
724 inline static void rrdr_unlock_rrdset(RRDR *r) {
726 error("NULL value given!");
730 if(likely(r->has_st_lock)) {
731 pthread_rwlock_unlock(&r->st->rwlock);
736 inline static void rrdr_free(RRDR *r)
739 error("NULL value given!");
743 rrdr_unlock_rrdset(r);
744 if(likely(r->t)) free(r->t);
745 if(likely(r->v)) free(r->v);
746 if(likely(r->o)) free(r->o);
747 if(likely(r->od)) free(r->od);
751 static RRDR *rrdr_create(RRDSET *st, int n)
754 error("NULL value given!");
758 RRDR *r = calloc(1, sizeof(RRDR));
759 if(unlikely(!r)) goto cleanup;
766 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
769 r->t = malloc(n * sizeof(time_t));
770 if(unlikely(!r->t)) goto cleanup;
772 r->t = malloc(n * sizeof(time_t));
773 if(unlikely(!r->t)) goto cleanup;
775 r->v = malloc(n * r->d * sizeof(calculated_number));
776 if(unlikely(!r->v)) goto cleanup;
778 r->o = malloc(n * r->d * sizeof(uint8_t));
779 if(unlikely(!r->o)) goto cleanup;
781 r->od = calloc(r->d, sizeof(uint8_t));
782 if(unlikely(!r->od)) goto cleanup;
789 error("Cannot allocate memory");
790 if(likely(r)) rrdr_free(r);
794 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method)
796 int debug = st->debug;
798 time_t first_entry_t = rrdset_first_entry_t(st);
799 time_t last_entry_t = rrdset_last_entry_t(st);
801 if(before == 0 && after == 0) {
802 before = last_entry_t;
803 after = first_entry_t;
806 // allow relative for before and after
807 if(before <= st->update_every * st->entries) before = last_entry_t + before;
808 if(after <= st->update_every * st->entries) after = last_entry_t + after;
810 // make sure they are within our timeframe
811 if(before > last_entry_t) before = last_entry_t;
812 if(before < first_entry_t) before = first_entry_t;
814 if(after > last_entry_t) after = last_entry_t;
815 if(after < first_entry_t) after = first_entry_t;
817 // check if they are upside down
824 // the duration of the chart
825 time_t duration = before - after;
826 if(duration <= 0) return NULL;
828 // check the required points
829 if(points > duration / st->update_every) points = 0;
830 if(points <= 0) points = duration / st->update_every;
832 // calculate proper grouping of source data
833 long group = duration / points;
834 if(group <= 0) group = 1;
835 if(duration / group > points) group++;
837 // error("NEW: points=%d after=%d before=%d group=%d, duration=%d", points, after, before, group, duration);
840 // before = the end time of the calculation
841 // after = the start time of the calculation
842 // duration = the duration of the calculation
843 // group = the number of source points to aggregate / group together
844 // method = the method of grouping source points
845 // points = the number of points to generate
848 // -------------------------------------------------------------------------
849 // initialize our result set
851 RRDR *r = rrdr_create(st, points);
858 // find how many dimensions we have
859 long dimensions = r->d;
862 // -------------------------------------------------------------------------
863 // checks for debugging
865 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"
869 , last_entry_t - first_entry_t
878 // -------------------------------------------------------------------------
879 // temp arrays for keeping values per dimension
881 calculated_number group_values[dimensions]; // keep sums when grouping
882 long group_counts[dimensions]; // keep the number of values added to group_values
883 uint8_t group_options[dimensions];
884 uint8_t found_non_zero[dimensions];
890 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
893 group_options[c] = 0;
894 found_non_zero[c] = 0;
898 // -------------------------------------------------------------------------
901 long start_at_slot = rrdset_time2slot(st, before), // rrdset_last_slot(st),
902 stop_at_slot = rrdset_time2slot(st, after);
904 time_t now = rrdset_slot2time(st, start_at_slot),
905 dt = st->update_every,
908 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"
919 // align to group for proper panning of data
920 start_at_slot -= start_at_slot % group;
921 stop_at_slot -= stop_at_slot % group;
922 now = rrdset_slot2time(st, start_at_slot);
924 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"
935 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
936 for(; !stop_now ; now -= dt, slot--, counter++) {
937 if(unlikely(slot < 0)) slot = st->entries - 1;
938 if(unlikely(slot == stop_at_slot)) stop_now = counter;
940 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %lu, %s %s"
947 , (group_count + 1 == group)?"PRINT":" - "
948 , (now >= after && now <= before)?"RANGE":" - "
951 // make sure we return data in the proper time range
952 if(unlikely(now > before)) continue;
953 if(unlikely(now < after)) break;
955 if(unlikely(group_count == 0)) group_start_t = now;
958 if(unlikely(group_count == group)) {
959 if(unlikely(added >= points)) break;
963 // do the calculations
964 for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
965 storage_number n = rd->values[slot];
966 if(unlikely(!does_storage_number_exist(n))) continue;
970 calculated_number value = unpack_storage_number(n);
971 if(likely(value != 0.0)) {
972 group_options[c] |= RRDR_NONZERO;
973 found_non_zero[c] = 1;
976 if(unlikely(did_storage_number_reset(n)))
977 group_options[c] |= RRDR_RESET;
979 switch(group_method) {
981 if(unlikely(abs(value) > abs(group_values[c])))
982 group_values[c] = value;
988 group_values[c] += value;
994 if(unlikely(add_this)) {
995 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
997 calculated_number *cn = rrdr_line_values(r);
998 uint8_t *co = rrdr_line_options(r);
1000 for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
1002 // update the dimension options
1003 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1004 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] |= RRDR_HIDDEN;
1006 // store the specific point options
1007 co[c] = group_options[c];
1010 if(unlikely(group_counts[c] == 0)) {
1012 co[c] |= RRDR_EMPTY;
1014 else if(unlikely(group_method == GROUP_AVERAGE)) {
1015 cn[c] = group_values[c] / group_counts[c];
1018 cn[c] = group_values[c];
1021 // reset them for the next loop
1022 group_values[c] = 0;
1023 group_counts[c] = 0;
1024 group_options[c] = 0;
1036 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)
1038 RRDR *rrdr = rrd2rrdr(st, points, after, before, group_method);
1040 buffer_strcat(out, "Cannot generate output with these parameters on this chart.");
1045 rrdr_disable_not_selected_dimensions(rrdr, buffer_tostring(dimensions));
1047 if(latest_timestamp && rrdr_rows(rrdr) > 0)
1048 *latest_timestamp = rrdr->t[rrdr_rows(rrdr) - 1];
1051 case DATASOURCE_SSV:
1052 out->contenttype = CT_TEXT_PLAIN;
1053 rrdr2ssv(rrdr, out, options, "", " ", "");
1056 case DATASOURCE_SSV_COMMA:
1057 out->contenttype = CT_TEXT_PLAIN;
1058 rrdr2ssv(rrdr, out, options, "", ",", "");
1061 case DATASOURCE_JS_ARRAY:
1062 out->contenttype = CT_APPLICATION_JSON;
1063 rrdr2ssv(rrdr, out, options, "[", ",", "]");
1066 case DATASOURCE_CSV:
1067 out->contenttype = CT_TEXT_PLAIN;
1068 rrdr2csv(rrdr, out, options, "", ",", "\r\n");
1071 case DATASOURCE_TSV:
1072 out->contenttype = CT_TEXT_PLAIN;
1073 rrdr2csv(rrdr, out, options, "", "\t", "\r\n");
1076 case DATASOURCE_HTML:
1077 out->contenttype = CT_TEXT_HTML;
1078 buffer_strcat(out, "<html>\n<center><table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">");
1079 rrdr2csv(rrdr, out, options, "<tr><td>", "</td><td>", "</td></tr>\n");
1080 buffer_strcat(out, "</table>\n</center>\n</html>\n");
1083 case DATASOURCE_GOOGLE_JSONP:
1084 out->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1085 rrdr2json(rrdr, out, options, 1);
1088 case DATASOURCE_GOOGLE_JSON:
1089 out->contenttype = CT_APPLICATION_JSON;
1090 rrdr2json(rrdr, out, options, 1);
1093 case DATASOURCE_JSONP:
1094 out->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1095 rrdr2json(rrdr, out, options, 0);
1098 case DATASOURCE_JSON:
1100 out->contenttype = CT_APPLICATION_JSON;
1101 rrdr2json(rrdr, out, options, 0);
1109 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)
1112 pthread_rwlock_rdlock(&st->rwlock);
1115 // -------------------------------------------------------------------------
1116 // switch from JSON to google JSON
1121 case DATASOURCE_GOOGLE_JSON:
1122 case DATASOURCE_GOOGLE_JSONP:
1127 case DATASOURCE_JSON:
1133 // -------------------------------------------------------------------------
1134 // validate the parameters
1136 if(points < 1) points = 1;
1137 if(group < 1) group = 1;
1139 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1140 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1144 // our return value (the last timestamp printed)
1145 // this is required to detect re-transmit in google JSONP
1146 time_t last_timestamp = 0;
1149 // -------------------------------------------------------------------------
1150 // find how many dimensions we have
1154 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1156 pthread_rwlock_unlock(&st->rwlock);
1157 buffer_strcat(wb, "No dimensions yet.");
1162 // -------------------------------------------------------------------------
1163 // prepare various strings, to speed up the loop
1165 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);
1166 char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1167 char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1168 char post_date[21]; snprintf(post_date, 20, "%s}", sq);
1169 char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
1170 char post_value[21]; snprintf(post_value, 20, "}");
1173 // -------------------------------------------------------------------------
1174 // checks for debugging
1177 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"
1179 , rrdset_first_entry_t(st)
1180 , rrdset_last_entry_t(st)
1181 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1190 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
1192 if((before - after) > st->entries * st->update_every)
1193 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);
1197 // -------------------------------------------------------------------------
1198 // temp arrays for keeping values per dimension
1200 calculated_number group_values[dimensions]; // keep sums when grouping
1201 int print_hidden[dimensions]; // keep hidden flags
1202 int found_non_zero[dimensions];
1203 int found_non_existing[dimensions];
1206 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1207 group_values[c] = 0;
1208 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1209 found_non_zero[c] = 0;
1210 found_non_existing[c] = 0;
1214 // 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);
1215 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1216 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1218 // -------------------------------------------------------------------------
1219 // remove dimensions that contain only zeros
1222 if(only_non_zero) max_loop = 2;
1224 for(; max_loop ; max_loop--) {
1226 // -------------------------------------------------------------------------
1227 // print the JSON header
1229 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1230 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);
1231 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);
1232 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);
1234 // print the header for each dimension
1235 // and update the print_hidden array for the dimensions that should be hidden
1237 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1238 if(!print_hidden[c]) {
1240 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);
1244 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);
1247 // print the begin of row data
1248 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1251 // -------------------------------------------------------------------------
1254 int annotate_reset = 0;
1255 int annotation_count = 0;
1257 long t = rrdset_time2slot(st, before),
1258 stop_at_t = rrdset_time2slot(st, after),
1263 time_t now = rrdset_slot2time(st, t),
1264 dt = st->update_every;
1266 long count = 0, printed = 0, group_count = 0;
1269 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"
1276 , rrdset_first_entry_t(st)
1277 , rrdset_last_entry_t(st)
1282 for(; !stop_now ; now -= dt, t--) {
1283 if(t < 0) t = st->entries - 1;
1284 if(t == stop_at_t) stop_now = 1;
1288 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
1295 , (group_count + 1 == group)?"PRINT":" - "
1296 , (now >= after && now <= before)?"RANGE":" - "
1300 // make sure we return data in the proper time range
1301 if(now > before) continue;
1302 if(now < after) break;
1304 //if(rrdset_slot2time(st, t) != now)
1305 // 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));
1310 // check if we have to print this now
1311 if(group_count == group) {
1312 if(printed >= points) {
1313 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
1317 // generate the local date time
1318 struct tm *tm = localtime(&now);
1319 if(!tm) { error("localtime() failed."); continue; }
1320 if(now > last_timestamp) last_timestamp = now;
1322 if(printed) buffer_strcat(wb, "]},\n");
1323 buffer_strcat(wb, pre_date);
1324 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1325 buffer_strcat(wb, post_date);
1330 // do the calculations
1331 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1332 storage_number n = rd->values[t];
1333 calculated_number value = unpack_storage_number(n);
1335 if(!does_storage_number_exist(n)) {
1337 found_non_existing[c]++;
1339 if(did_storage_number_reset(n)) annotate_reset = 1;
1341 switch(group_method) {
1343 if(abs(value) > abs(group_values[c])) group_values[c] = value;
1347 group_values[c] += value;
1352 group_values[c] += value;
1353 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
1359 if(annotate_reset) {
1361 buffer_strcat(wb, overflow_annotation);
1365 buffer_strcat(wb, normal_annotation);
1368 for(c = 0 ; c < dimensions ; c++) {
1369 if(found_non_existing[c] == group_count) {
1370 // all entries are non-existing
1372 buffer_strcat(wb, pre_value);
1373 buffer_strcat(wb, "null");
1374 buffer_strcat(wb, post_value);
1376 else if(!print_hidden[c]) {
1378 buffer_strcat(wb, pre_value);
1379 buffer_rrd_value(wb, group_values[c]);
1380 buffer_strcat(wb, post_value);
1382 if(group_values[c]) found_non_zero[c]++;
1385 // reset them for the next loop
1386 group_values[c] = 0;
1387 found_non_existing[c] = 0;
1390 // if all dimensions are hidden, print a null
1392 buffer_strcat(wb, pre_value);
1393 buffer_strcat(wb, "null");
1394 buffer_strcat(wb, post_value);
1402 if(printed) buffer_strcat(wb, "]}");
1403 buffer_strcat(wb, "\n ]\n}\n");
1405 if(only_non_zero && max_loop > 1) {
1407 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1408 group_values[c] = 0;
1409 found_non_existing[c] = 0;
1411 if(!print_hidden[c] && !found_non_zero[c]) {
1413 print_hidden[c] = 1;
1417 if(changed) buffer_flush(wb);
1424 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->len);
1426 pthread_rwlock_unlock(&st->rwlock);
1427 return last_timestamp;