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
278 uint8_t *od; // the options for the dimensions
280 time_t *t; // array of n timestamps
281 calculated_number *v; // array n x d values
282 uint8_t *o; // array n x d options
284 int c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
286 int has_st_lock; // if st is read locked by us
289 #define rrdr_rows(r) ((r)->c + 1)
292 static void rrdr_dump(RRDR *r)
297 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
299 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
300 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
303 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
304 , (r->od[c] & RRDR_RESET)?"RESET ":""
305 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
306 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
311 fprintf(stderr, "RRDR does not have any values in it.\n");
315 fprintf(stderr, "RRDR includes %d values in it:\n", r->c + 1);
317 // for each line in the array
318 for(i = 0; i <= r->c ;i++) {
319 calculated_number *cn = &r->v[ i * r->d ];
320 uint8_t *co = &r->o[ i * r->d ];
322 // print the id and the timestamp of the line
323 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
325 // for each dimension
326 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
327 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
328 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
330 if(co[c] & RRDR_EMPTY)
331 fprintf(stderr, "null ");
333 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
335 , (co[c] & RRDR_EMPTY)?"E":" "
336 , (co[c] & RRDR_RESET)?"R":" "
337 , (co[c] & RRDR_HIDDEN)?"H":" "
338 , (co[c] & RRDR_NONZERO)?"N":" "
342 fprintf(stderr, "\n");
347 void rrdr_disable_not_selected_dimensions(RRDR *r, const char *dims)
349 char b[strlen(dims) + 1];
356 // disable all of them
357 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
358 r->od[c] |= RRDR_HIDDEN;
360 while(o && *o && (tok = mystrsep(&o, ", |"))) {
363 // find it and enable it
364 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
365 if(!strcmp(d->name, tok)) {
366 r->od[c] &= ~RRDR_HIDDEN;
372 #define JSON_DATES_JS 1
373 #define JSON_DATES_TIMESTAMP 2
375 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
377 int annotations = 0, dates = JSON_DATES_JS, dates_with_new = 0;
378 char kq[2] = "", // key quote
379 sq[2] = "", // string quote
380 pre_label[101] = "", // before each label
381 post_label[101] = "", // after each label
382 pre_date[101] = "", // the beginning of line, to the date
383 post_date[101] = "", // closing the date
384 pre_value[101] = "", // before each value
385 post_value[101] = "", // after each value
386 post_line[101] = "", // at the end of each row
387 normal_annotation[201] = "", // default row annotation
388 overflow_annotation[201] = "", // overflow row annotation
389 data_begin[101] = "", // between labels and values
390 finish[101] = ""; // at the end of everything
393 dates = JSON_DATES_JS;
394 if( options & RRDR_OPTION_GOOGLE_JSON ) {
403 snprintf(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
404 snprintf(post_date, 100, "%s}", sq);
405 snprintf(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
406 snprintf(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
407 snprintf(pre_value, 100, ",{%sv%s:", kq, kq);
408 snprintf(post_value, 100, "}");
409 snprintf(post_line, 100, "]}");
410 snprintf(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
411 snprintf(finish, 100, "\n ]\n}\n");
413 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);
414 snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
416 buffer_sprintf(wb, "{\n %supdate_every%s: %d,\n %scols%s:\n [\n", kq, kq, r->update_every, kq, kq, kq, kq);
417 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);
418 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);
419 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);
421 // remove the valueobjects flag
422 // google wants its own keys
423 if(options & RRDR_OPTION_OBJECTSROWS)
424 options &= ~RRDR_OPTION_OBJECTSROWS;
429 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
430 dates = JSON_DATES_TIMESTAMP;
434 dates = JSON_DATES_JS;
437 if( options & RRDR_OPTION_OBJECTSROWS )
438 snprintf(pre_date, 100, " { ");
440 snprintf(pre_date, 100, " [ ");
441 snprintf(pre_label, 100, ", \"");
442 snprintf(post_label, 100, "\"");
443 snprintf(pre_value, 100, ", ");
444 if( options & RRDR_OPTION_OBJECTSROWS )
445 snprintf(post_line, 100, "}");
447 snprintf(post_line, 100, "]");
448 snprintf(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
449 snprintf(finish, 100, "\n ]\n}\n");
451 buffer_sprintf(wb, "{\n %supdate_every%s: %d,\n %slabels%s: [", kq, kq, r->update_every, kq, kq);
452 buffer_sprintf(wb, "%stime%s", sq, sq);
455 // -------------------------------------------------------------------------
456 // print the JSON header
461 // print the csv header
462 for(c = 0, i = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
463 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
464 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
466 buffer_strcat(wb, pre_label);
467 buffer_strcat(wb, rd->name);
468 buffer_strcat(wb, post_label);
472 buffer_strcat(wb, pre_label);
473 buffer_strcat(wb, "no data");
474 buffer_strcat(wb, post_label);
477 // print the begin of row data
478 buffer_strcat(wb, data_begin);
480 // if all dimensions are hidden, print a null
482 buffer_strcat(wb, pre_value);
483 if(options & RRDR_OPTION_NULL2ZERO)
484 buffer_strcat(wb, "0");
486 buffer_strcat(wb, "null");
487 buffer_strcat(wb, post_value);
490 long start = 0, end = rrdr_rows(r), step = 1;
491 if((options & RRDR_OPTION_REVERSED)) {
492 start = rrdr_rows(r) - 1;
497 // for each line in the array
498 for(i = start; i != end ;i += step) {
499 calculated_number *cn = &r->v[ i * r->d ];
500 uint8_t *co = &r->o[ i * r->d ];
502 time_t now = r->t[i];
504 if(dates == JSON_DATES_JS) {
505 // generate the local date time
506 struct tm *tm = localtime(&now);
507 if(!tm) { error("localtime() failed."); continue; }
509 if(likely(i != start)) buffer_strcat(wb, ",\n");
510 buffer_strcat(wb, pre_date);
512 if( options & RRDR_OPTION_OBJECTSROWS )
513 buffer_sprintf(wb, "%stime%s: ", kq, kq);
516 buffer_strcat(wb, "new ");
518 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
520 buffer_strcat(wb, post_date);
523 if(co[c] & RRDR_RESET)
524 buffer_strcat(wb, overflow_annotation);
526 buffer_strcat(wb, normal_annotation);
530 // print the timestamp of the line
531 if(likely(i != start)) buffer_strcat(wb, ",\n");
532 buffer_strcat(wb, pre_date);
534 if( options & RRDR_OPTION_OBJECTSROWS )
535 buffer_sprintf(wb, "%stime%s: ", kq, kq);
537 buffer_rrd_value(wb, (calculated_number)r->t[i]);
539 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
541 buffer_strcat(wb, post_date);
544 // for each dimension
545 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
546 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
547 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
549 calculated_number n = cn[c];
551 buffer_strcat(wb, pre_value);
553 if( options & RRDR_OPTION_OBJECTSROWS )
554 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
556 if(co[c] & RRDR_EMPTY) {
557 if(options & RRDR_OPTION_NULL2ZERO)
558 buffer_strcat(wb, "0");
560 buffer_strcat(wb, "null");
562 else if((options & RRDR_OPTION_ABSOLUTE))
563 buffer_rrd_value(wb, (n<0)?-n:n);
565 buffer_rrd_value(wb, n);
567 buffer_strcat(wb, post_value);
570 buffer_strcat(wb, post_line);
573 buffer_strcat(wb, finish);
576 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline)
581 // print the csv header
582 for(c = 0, i = 0, d = r->st->dimensions; d ;c++, d = d->next) {
583 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
584 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
587 buffer_strcat(wb, startline);
588 buffer_strcat(wb, "time");
590 buffer_strcat(wb, separator);
591 buffer_strcat(wb, d->name);
594 buffer_strcat(wb, endline);
597 // no dimensions present
601 long start = 0, end = rrdr_rows(r), step = 1;
602 if((options & RRDR_OPTION_REVERSED)) {
603 start = rrdr_rows(r) - 1;
608 // for each line in the array
609 for(i = start; i != end ;i += step) {
610 calculated_number *cn = &r->v[ i * r->d ];
611 uint8_t *co = &r->o[ i * r->d ];
613 buffer_strcat(wb, startline);
615 time_t now = r->t[i];
617 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
618 // print the timestamp of the line
619 buffer_rrd_value(wb, (calculated_number)now);
621 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
624 // generate the local date time
625 struct tm *tm = localtime(&now);
626 if(!tm) { error("localtime() failed."); continue; }
627 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
630 // for each dimension
631 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
632 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
633 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
635 buffer_strcat(wb, separator);
637 calculated_number n = cn[c];
639 if(co[c] & RRDR_EMPTY) {
640 if(options & RRDR_OPTION_NULL2ZERO)
641 buffer_strcat(wb, "0");
643 buffer_strcat(wb, "null");
645 else if((options & RRDR_OPTION_ABSOLUTE))
646 buffer_rrd_value(wb, (n<0)?-n:n);
648 buffer_rrd_value(wb, n);
651 buffer_strcat(wb, endline);
655 static void rrdr2ssv(RRDR *r, BUFFER *out, uint32_t options, const char *prefix, const char *separator, const char *suffix)
660 buffer_strcat(out, prefix);
661 long start = 0, end = rrdr_rows(r), step = 1;
662 if((options & RRDR_OPTION_REVERSED)) {
663 start = rrdr_rows(r) - 1;
668 // for each line in the array
669 for(i = start; i != end ;i += step) {
671 calculated_number *cn = &r->v[ i * r->d ];
672 uint8_t *co = &r->o[ i * r->d ];
674 calculated_number sum = 0, min = 0, max = 0;
675 int all_null = 1, init = 1;
677 // for each dimension
678 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
679 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
680 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
682 calculated_number n = cn[c];
696 if(likely(!(co[c] & RRDR_EMPTY))) {
698 if((options & RRDR_OPTION_ABSOLUTE) && n < 0) n = -n;
706 if(likely(i != start))
707 buffer_strcat(out, separator);
710 if(options & RRDR_OPTION_NULL2ZERO)
711 buffer_strcat(out, "0");
713 buffer_strcat(out, "null");
715 else if(options & RRDR_OPTION_MIN2MAX)
716 buffer_rrd_value(out, max - min);
718 buffer_rrd_value(out, sum);
720 buffer_strcat(out, suffix);
723 inline static calculated_number *rrdr_line_values(RRDR *r)
725 return &r->v[ r->c * r->d ];
728 inline static uint8_t *rrdr_line_options(RRDR *r)
730 return &r->o[ r->c * r->d ];
733 inline static int rrdr_line_init(RRDR *r, time_t t)
736 if(unlikely(r->c >= r->n)) {
747 inline static void rrdr_lock_rrdset(RRDR *r) {
749 error("NULL value given!");
753 pthread_rwlock_rdlock(&r->st->rwlock);
757 inline static void rrdr_unlock_rrdset(RRDR *r) {
759 error("NULL value given!");
763 if(likely(r->has_st_lock)) {
764 pthread_rwlock_unlock(&r->st->rwlock);
769 inline static void rrdr_free(RRDR *r)
772 error("NULL value given!");
776 rrdr_unlock_rrdset(r);
777 if(likely(r->t)) free(r->t);
778 if(likely(r->v)) free(r->v);
779 if(likely(r->o)) free(r->o);
780 if(likely(r->od)) free(r->od);
784 static RRDR *rrdr_create(RRDSET *st, int n)
787 error("NULL value given!");
791 RRDR *r = calloc(1, sizeof(RRDR));
792 if(unlikely(!r)) goto cleanup;
799 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
802 r->t = malloc(n * sizeof(time_t));
803 if(unlikely(!r->t)) goto cleanup;
805 r->v = malloc(n * r->d * sizeof(calculated_number));
806 if(unlikely(!r->v)) goto cleanup;
808 r->o = malloc(n * r->d * sizeof(uint8_t));
809 if(unlikely(!r->o)) goto cleanup;
811 r->od = calloc(r->d, sizeof(uint8_t));
812 if(unlikely(!r->od)) goto cleanup;
819 error("Cannot allocate memory");
820 if(likely(r)) rrdr_free(r);
824 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method)
826 int debug = st->debug;
828 time_t first_entry_t = rrdset_first_entry_t(st);
829 time_t last_entry_t = rrdset_last_entry_t(st);
831 if(before == 0 && after == 0) {
832 before = last_entry_t;
833 after = first_entry_t;
836 // allow relative for before and after
837 if(before <= st->update_every * st->entries) before = last_entry_t + before;
838 if(after <= st->update_every * st->entries) after = last_entry_t + after;
840 // make sure they are within our timeframe
841 if(before > last_entry_t) before = last_entry_t;
842 if(before < first_entry_t) before = first_entry_t;
844 if(after > last_entry_t) after = last_entry_t;
845 if(after < first_entry_t) after = first_entry_t;
847 // check if they are upside down
854 // the duration of the chart
855 time_t duration = before - after;
856 if(duration <= 0) return NULL;
858 // check the required points
859 if(points > duration / st->update_every) points = 0;
860 if(points <= 0) points = duration / st->update_every;
862 // calculate proper grouping of source data
863 long group = duration / points;
864 if(group <= 0) group = 1;
865 if(duration / group > points) group++;
867 // error("NEW: points=%d after=%d before=%d group=%d, duration=%d", points, after, before, group, duration);
870 // before = the end time of the calculation
871 // after = the start time of the calculation
872 // duration = the duration of the calculation
873 // group = the number of source points to aggregate / group together
874 // method = the method of grouping source points
875 // points = the number of points to generate
878 // -------------------------------------------------------------------------
879 // initialize our result set
881 RRDR *r = rrdr_create(st, points);
888 // find how many dimensions we have
889 long dimensions = r->d;
892 // -------------------------------------------------------------------------
893 // checks for debugging
895 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"
899 , last_entry_t - first_entry_t
908 // -------------------------------------------------------------------------
909 // temp arrays for keeping values per dimension
911 calculated_number group_values[dimensions]; // keep sums when grouping
912 long group_counts[dimensions]; // keep the number of values added to group_values
913 uint8_t group_options[dimensions];
914 uint8_t found_non_zero[dimensions];
920 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
923 group_options[c] = 0;
924 found_non_zero[c] = 0;
928 // -------------------------------------------------------------------------
931 long start_at_slot = rrdset_time2slot(st, before), // rrdset_last_slot(st),
932 stop_at_slot = rrdset_time2slot(st, after);
934 time_t now = rrdset_slot2time(st, start_at_slot),
935 dt = st->update_every,
938 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"
949 // align to group for proper panning of data
950 start_at_slot -= start_at_slot % group;
951 stop_at_slot -= stop_at_slot % group;
952 now = rrdset_slot2time(st, start_at_slot);
954 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"
966 r->update_every = group * st->update_every;
968 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
969 for(; !stop_now ; now -= dt, slot--, counter++) {
970 if(unlikely(slot < 0)) slot = st->entries - 1;
971 if(unlikely(slot == stop_at_slot)) stop_now = counter;
973 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %lu, %s %s"
980 , (group_count + 1 == group)?"PRINT":" - "
981 , (now >= after && now <= before)?"RANGE":" - "
984 // make sure we return data in the proper time range
985 if(unlikely(now > before)) continue;
986 if(unlikely(now < after)) break;
988 if(unlikely(group_count == 0)) group_start_t = now;
991 if(unlikely(group_count == group)) {
992 if(unlikely(added >= points)) break;
996 // do the calculations
997 for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
998 storage_number n = rd->values[slot];
999 if(unlikely(!does_storage_number_exist(n))) continue;
1003 calculated_number value = unpack_storage_number(n);
1004 if(likely(value != 0.0)) {
1005 group_options[c] |= RRDR_NONZERO;
1006 found_non_zero[c] = 1;
1009 if(unlikely(did_storage_number_reset(n)))
1010 group_options[c] |= RRDR_RESET;
1012 switch(group_method) {
1014 if(unlikely(abs(value) > abs(group_values[c])))
1015 group_values[c] = value;
1021 group_values[c] += value;
1027 if(unlikely(add_this)) {
1028 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1030 calculated_number *cn = rrdr_line_values(r);
1031 uint8_t *co = rrdr_line_options(r);
1033 for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
1035 // update the dimension options
1036 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1037 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] |= RRDR_HIDDEN;
1039 // store the specific point options
1040 co[c] = group_options[c];
1043 if(unlikely(group_counts[c] == 0)) {
1045 co[c] |= RRDR_EMPTY;
1047 else if(unlikely(group_method == GROUP_AVERAGE)) {
1048 cn[c] = group_values[c] / group_counts[c];
1051 cn[c] = group_values[c];
1054 // reset them for the next loop
1055 group_values[c] = 0;
1056 group_counts[c] = 0;
1057 group_options[c] = 0;
1069 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)
1071 RRDR *rrdr = rrd2rrdr(st, points, after, before, group_method);
1073 buffer_strcat(out, "Cannot generate output with these parameters on this chart.");
1078 rrdr_disable_not_selected_dimensions(rrdr, buffer_tostring(dimensions));
1080 if(latest_timestamp && rrdr_rows(rrdr) > 0)
1081 *latest_timestamp = rrdr->t[rrdr_rows(rrdr) - 1];
1084 case DATASOURCE_SSV:
1085 out->contenttype = CT_TEXT_PLAIN;
1086 rrdr2ssv(rrdr, out, options, "", " ", "");
1089 case DATASOURCE_SSV_COMMA:
1090 out->contenttype = CT_TEXT_PLAIN;
1091 rrdr2ssv(rrdr, out, options, "", ",", "");
1094 case DATASOURCE_JS_ARRAY:
1095 out->contenttype = CT_APPLICATION_JSON;
1096 rrdr2ssv(rrdr, out, options, "[", ",", "]");
1099 case DATASOURCE_CSV:
1100 out->contenttype = CT_TEXT_PLAIN;
1101 rrdr2csv(rrdr, out, options, "", ",", "\r\n");
1104 case DATASOURCE_TSV:
1105 out->contenttype = CT_TEXT_PLAIN;
1106 rrdr2csv(rrdr, out, options, "", "\t", "\r\n");
1109 case DATASOURCE_HTML:
1110 out->contenttype = CT_TEXT_HTML;
1111 buffer_strcat(out, "<html>\n<center><table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">");
1112 rrdr2csv(rrdr, out, options, "<tr><td>", "</td><td>", "</td></tr>\n");
1113 buffer_strcat(out, "</table>\n</center>\n</html>\n");
1116 case DATASOURCE_DATATABLE_JSONP:
1117 out->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1118 rrdr2json(rrdr, out, options, 1);
1121 case DATASOURCE_DATATABLE_JSON:
1122 out->contenttype = CT_APPLICATION_JSON;
1123 rrdr2json(rrdr, out, options, 1);
1126 case DATASOURCE_JSONP:
1127 out->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1128 rrdr2json(rrdr, out, options, 0);
1131 case DATASOURCE_JSON:
1133 out->contenttype = CT_APPLICATION_JSON;
1134 rrdr2json(rrdr, out, options, 0);
1142 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)
1145 pthread_rwlock_rdlock(&st->rwlock);
1148 // -------------------------------------------------------------------------
1149 // switch from JSON to google JSON
1154 case DATASOURCE_DATATABLE_JSON:
1155 case DATASOURCE_DATATABLE_JSONP:
1160 case DATASOURCE_JSON:
1166 // -------------------------------------------------------------------------
1167 // validate the parameters
1169 if(points < 1) points = 1;
1170 if(group < 1) group = 1;
1172 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1173 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1177 // our return value (the last timestamp printed)
1178 // this is required to detect re-transmit in google JSONP
1179 time_t last_timestamp = 0;
1182 // -------------------------------------------------------------------------
1183 // find how many dimensions we have
1187 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1189 pthread_rwlock_unlock(&st->rwlock);
1190 buffer_strcat(wb, "No dimensions yet.");
1195 // -------------------------------------------------------------------------
1196 // prepare various strings, to speed up the loop
1198 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);
1199 char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1200 char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1201 char post_date[21]; snprintf(post_date, 20, "%s}", sq);
1202 char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
1203 char post_value[21]; snprintf(post_value, 20, "}");
1206 // -------------------------------------------------------------------------
1207 // checks for debugging
1210 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"
1212 , rrdset_first_entry_t(st)
1213 , rrdset_last_entry_t(st)
1214 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1223 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
1225 if((before - after) > st->entries * st->update_every)
1226 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);
1230 // -------------------------------------------------------------------------
1231 // temp arrays for keeping values per dimension
1233 calculated_number group_values[dimensions]; // keep sums when grouping
1234 int print_hidden[dimensions]; // keep hidden flags
1235 int found_non_zero[dimensions];
1236 int found_non_existing[dimensions];
1239 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1240 group_values[c] = 0;
1241 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1242 found_non_zero[c] = 0;
1243 found_non_existing[c] = 0;
1247 // 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);
1248 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1249 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1251 // -------------------------------------------------------------------------
1252 // remove dimensions that contain only zeros
1255 if(only_non_zero) max_loop = 2;
1257 for(; max_loop ; max_loop--) {
1259 // -------------------------------------------------------------------------
1260 // print the JSON header
1262 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1263 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);
1264 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);
1265 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);
1267 // print the header for each dimension
1268 // and update the print_hidden array for the dimensions that should be hidden
1270 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1271 if(!print_hidden[c]) {
1273 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);
1277 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);
1280 // print the begin of row data
1281 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1284 // -------------------------------------------------------------------------
1287 int annotate_reset = 0;
1288 int annotation_count = 0;
1290 long t = rrdset_time2slot(st, before),
1291 stop_at_t = rrdset_time2slot(st, after),
1296 time_t now = rrdset_slot2time(st, t),
1297 dt = st->update_every;
1299 long count = 0, printed = 0, group_count = 0;
1302 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"
1309 , rrdset_first_entry_t(st)
1310 , rrdset_last_entry_t(st)
1315 for(; !stop_now ; now -= dt, t--) {
1316 if(t < 0) t = st->entries - 1;
1317 if(t == stop_at_t) stop_now = 1;
1321 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
1328 , (group_count + 1 == group)?"PRINT":" - "
1329 , (now >= after && now <= before)?"RANGE":" - "
1333 // make sure we return data in the proper time range
1334 if(now > before) continue;
1335 if(now < after) break;
1337 //if(rrdset_slot2time(st, t) != now)
1338 // 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));
1343 // check if we have to print this now
1344 if(group_count == group) {
1345 if(printed >= points) {
1346 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
1350 // generate the local date time
1351 struct tm *tm = localtime(&now);
1352 if(!tm) { error("localtime() failed."); continue; }
1353 if(now > last_timestamp) last_timestamp = now;
1355 if(printed) buffer_strcat(wb, "]},\n");
1356 buffer_strcat(wb, pre_date);
1357 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1358 buffer_strcat(wb, post_date);
1363 // do the calculations
1364 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1365 storage_number n = rd->values[t];
1366 calculated_number value = unpack_storage_number(n);
1368 if(!does_storage_number_exist(n)) {
1370 found_non_existing[c]++;
1372 if(did_storage_number_reset(n)) annotate_reset = 1;
1374 switch(group_method) {
1376 if(abs(value) > abs(group_values[c])) group_values[c] = value;
1380 group_values[c] += value;
1385 group_values[c] += value;
1386 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
1392 if(annotate_reset) {
1394 buffer_strcat(wb, overflow_annotation);
1398 buffer_strcat(wb, normal_annotation);
1401 for(c = 0 ; c < dimensions ; c++) {
1402 if(found_non_existing[c] == group_count) {
1403 // all entries are non-existing
1405 buffer_strcat(wb, pre_value);
1406 buffer_strcat(wb, "null");
1407 buffer_strcat(wb, post_value);
1409 else if(!print_hidden[c]) {
1411 buffer_strcat(wb, pre_value);
1412 buffer_rrd_value(wb, group_values[c]);
1413 buffer_strcat(wb, post_value);
1415 if(group_values[c]) found_non_zero[c]++;
1418 // reset them for the next loop
1419 group_values[c] = 0;
1420 found_non_existing[c] = 0;
1423 // if all dimensions are hidden, print a null
1425 buffer_strcat(wb, pre_value);
1426 buffer_strcat(wb, "null");
1427 buffer_strcat(wb, post_value);
1435 if(printed) buffer_strcat(wb, "]}");
1436 buffer_strcat(wb, "\n ]\n}\n");
1438 if(only_non_zero && max_loop > 1) {
1440 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1441 group_values[c] = 0;
1442 found_non_existing[c] = 0;
1444 if(!print_hidden[c] && !found_non_zero[c]) {
1446 print_hidden[c] = 1;
1450 if(changed) buffer_flush(wb);
1457 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->len);
1459 pthread_rwlock_unlock(&st->rwlock);
1460 return last_timestamp;