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->t = malloc(n * sizeof(time_t));
806 if(unlikely(!r->t)) goto cleanup;
808 r->v = malloc(n * r->d * sizeof(calculated_number));
809 if(unlikely(!r->v)) goto cleanup;
811 r->o = malloc(n * r->d * sizeof(uint8_t));
812 if(unlikely(!r->o)) goto cleanup;
814 r->od = calloc(r->d, sizeof(uint8_t));
815 if(unlikely(!r->od)) goto cleanup;
822 error("Cannot allocate memory");
823 if(likely(r)) rrdr_free(r);
827 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method)
829 int debug = st->debug;
831 time_t first_entry_t = rrdset_first_entry_t(st);
832 time_t last_entry_t = rrdset_last_entry_t(st);
834 if(before == 0 && after == 0) {
835 before = last_entry_t;
836 after = first_entry_t;
839 // allow relative for before and after
840 if(before <= st->update_every * st->entries) before = last_entry_t + before;
841 if(after <= st->update_every * st->entries) after = last_entry_t + after;
843 // make sure they are within our timeframe
844 if(before > last_entry_t) before = last_entry_t;
845 if(before < first_entry_t) before = first_entry_t;
847 if(after > last_entry_t) after = last_entry_t;
848 if(after < first_entry_t) after = first_entry_t;
850 // check if they are upside down
857 // the duration of the chart
858 time_t duration = before - after;
859 if(duration <= 0) return NULL;
861 // check the required points
862 if(points > duration / st->update_every) points = 0;
863 if(points <= 0) points = duration / st->update_every;
865 // calculate proper grouping of source data
866 long group = duration / points;
867 if(group <= 0) group = 1;
868 if(duration / group > points) group++;
870 // error("NEW: points=%d after=%d before=%d group=%d, duration=%d", points, after, before, group, duration);
873 // before = the end time of the calculation
874 // after = the start time of the calculation
875 // duration = the duration of the calculation
876 // group = the number of source points to aggregate / group together
877 // method = the method of grouping source points
878 // points = the number of points to generate
881 // -------------------------------------------------------------------------
882 // initialize our result set
884 RRDR *r = rrdr_create(st, points);
891 // find how many dimensions we have
892 long dimensions = r->d;
895 // -------------------------------------------------------------------------
896 // checks for debugging
898 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"
902 , last_entry_t - first_entry_t
911 // -------------------------------------------------------------------------
912 // temp arrays for keeping values per dimension
914 calculated_number group_values[dimensions]; // keep sums when grouping
915 long group_counts[dimensions]; // keep the number of values added to group_values
916 uint8_t group_options[dimensions];
917 uint8_t found_non_zero[dimensions];
923 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
926 group_options[c] = 0;
927 found_non_zero[c] = 0;
931 // -------------------------------------------------------------------------
934 long start_at_slot = rrdset_time2slot(st, before), // rrdset_last_slot(st),
935 stop_at_slot = rrdset_time2slot(st, after);
937 time_t now = rrdset_slot2time(st, start_at_slot),
938 dt = st->update_every,
941 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"
952 // align to group for proper panning of data
953 start_at_slot -= start_at_slot % group;
954 stop_at_slot -= stop_at_slot % group;
955 now = rrdset_slot2time(st, start_at_slot);
957 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"
969 r->update_every = group * st->update_every;
971 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
972 for(; !stop_now ; now -= dt, slot--, counter++) {
973 if(unlikely(slot < 0)) slot = st->entries - 1;
974 if(unlikely(slot == stop_at_slot)) stop_now = counter;
976 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %lu, %s %s"
983 , (group_count + 1 == group)?"PRINT":" - "
984 , (now >= after && now <= before)?"RANGE":" - "
987 // make sure we return data in the proper time range
988 if(unlikely(now > before)) continue;
989 if(unlikely(now < after)) break;
991 if(unlikely(group_count == 0)) group_start_t = now;
994 if(unlikely(group_count == group)) {
995 if(unlikely(added >= points)) break;
999 // do the calculations
1000 for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
1001 storage_number n = rd->values[slot];
1002 if(unlikely(!does_storage_number_exist(n))) continue;
1006 calculated_number value = unpack_storage_number(n);
1007 if(likely(value != 0.0)) {
1008 group_options[c] |= RRDR_NONZERO;
1009 found_non_zero[c] = 1;
1012 if(unlikely(did_storage_number_reset(n)))
1013 group_options[c] |= RRDR_RESET;
1015 switch(group_method) {
1017 if(unlikely(abs(value) > abs(group_values[c])))
1018 group_values[c] = value;
1024 group_values[c] += value;
1030 if(unlikely(add_this)) {
1031 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1033 calculated_number *cn = rrdr_line_values(r);
1034 uint8_t *co = rrdr_line_options(r);
1036 for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
1038 // update the dimension options
1039 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1040 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] |= RRDR_HIDDEN;
1042 // store the specific point options
1043 co[c] = group_options[c];
1046 if(unlikely(group_counts[c] == 0)) {
1048 co[c] |= RRDR_EMPTY;
1050 else if(unlikely(group_method == GROUP_AVERAGE)) {
1051 cn[c] = group_values[c] / group_counts[c];
1054 cn[c] = group_values[c];
1057 // reset them for the next loop
1058 group_values[c] = 0;
1059 group_counts[c] = 0;
1060 group_options[c] = 0;
1072 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)
1074 RRDR *rrdr = rrd2rrdr(st, points, after, before, group_method);
1076 buffer_strcat(out, "Cannot generate output with these parameters on this chart.");
1081 rrdr_disable_not_selected_dimensions(rrdr, buffer_tostring(dimensions));
1083 if(latest_timestamp && rrdr_rows(rrdr) > 0)
1084 *latest_timestamp = rrdr->t[rrdr_rows(rrdr) - 1];
1087 case DATASOURCE_SSV:
1088 out->contenttype = CT_TEXT_PLAIN;
1089 rrdr2ssv(rrdr, out, options, "", " ", "");
1092 case DATASOURCE_SSV_COMMA:
1093 out->contenttype = CT_TEXT_PLAIN;
1094 rrdr2ssv(rrdr, out, options, "", ",", "");
1097 case DATASOURCE_JS_ARRAY:
1098 out->contenttype = CT_APPLICATION_JSON;
1099 rrdr2ssv(rrdr, out, options, "[", ",", "]");
1102 case DATASOURCE_CSV:
1103 out->contenttype = CT_TEXT_PLAIN;
1104 rrdr2csv(rrdr, out, options, "", ",", "\r\n");
1107 case DATASOURCE_TSV:
1108 out->contenttype = CT_TEXT_PLAIN;
1109 rrdr2csv(rrdr, out, options, "", "\t", "\r\n");
1112 case DATASOURCE_HTML:
1113 out->contenttype = CT_TEXT_HTML;
1114 buffer_strcat(out, "<html>\n<center><table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">");
1115 rrdr2csv(rrdr, out, options, "<tr><td>", "</td><td>", "</td></tr>\n");
1116 buffer_strcat(out, "</table>\n</center>\n</html>\n");
1119 case DATASOURCE_DATATABLE_JSONP:
1120 out->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1121 rrdr2json(rrdr, out, options, 1);
1124 case DATASOURCE_DATATABLE_JSON:
1125 out->contenttype = CT_APPLICATION_JSON;
1126 rrdr2json(rrdr, out, options, 1);
1129 case DATASOURCE_JSONP:
1130 out->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1131 rrdr2json(rrdr, out, options, 0);
1134 case DATASOURCE_JSON:
1136 out->contenttype = CT_APPLICATION_JSON;
1137 rrdr2json(rrdr, out, options, 0);
1145 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)
1148 pthread_rwlock_rdlock(&st->rwlock);
1151 // -------------------------------------------------------------------------
1152 // switch from JSON to google JSON
1157 case DATASOURCE_DATATABLE_JSON:
1158 case DATASOURCE_DATATABLE_JSONP:
1163 case DATASOURCE_JSON:
1169 // -------------------------------------------------------------------------
1170 // validate the parameters
1172 if(points < 1) points = 1;
1173 if(group < 1) group = 1;
1175 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1176 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1180 // our return value (the last timestamp printed)
1181 // this is required to detect re-transmit in google JSONP
1182 time_t last_timestamp = 0;
1185 // -------------------------------------------------------------------------
1186 // find how many dimensions we have
1190 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1192 pthread_rwlock_unlock(&st->rwlock);
1193 buffer_strcat(wb, "No dimensions yet.");
1198 // -------------------------------------------------------------------------
1199 // prepare various strings, to speed up the loop
1201 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);
1202 char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1203 char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1204 char post_date[21]; snprintf(post_date, 20, "%s}", sq);
1205 char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
1206 char post_value[21]; snprintf(post_value, 20, "}");
1209 // -------------------------------------------------------------------------
1210 // checks for debugging
1213 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"
1215 , rrdset_first_entry_t(st)
1216 , rrdset_last_entry_t(st)
1217 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1226 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
1228 if((before - after) > st->entries * st->update_every)
1229 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);
1233 // -------------------------------------------------------------------------
1234 // temp arrays for keeping values per dimension
1236 calculated_number group_values[dimensions]; // keep sums when grouping
1237 int print_hidden[dimensions]; // keep hidden flags
1238 int found_non_zero[dimensions];
1239 int found_non_existing[dimensions];
1242 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1243 group_values[c] = 0;
1244 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1245 found_non_zero[c] = 0;
1246 found_non_existing[c] = 0;
1250 // 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);
1251 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1252 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1254 // -------------------------------------------------------------------------
1255 // remove dimensions that contain only zeros
1258 if(only_non_zero) max_loop = 2;
1260 for(; max_loop ; max_loop--) {
1262 // -------------------------------------------------------------------------
1263 // print the JSON header
1265 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1266 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);
1267 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);
1268 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);
1270 // print the header for each dimension
1271 // and update the print_hidden array for the dimensions that should be hidden
1273 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1274 if(!print_hidden[c]) {
1276 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);
1280 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);
1283 // print the begin of row data
1284 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1287 // -------------------------------------------------------------------------
1290 int annotate_reset = 0;
1291 int annotation_count = 0;
1293 long t = rrdset_time2slot(st, before),
1294 stop_at_t = rrdset_time2slot(st, after),
1299 time_t now = rrdset_slot2time(st, t),
1300 dt = st->update_every;
1302 long count = 0, printed = 0, group_count = 0;
1305 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"
1312 , rrdset_first_entry_t(st)
1313 , rrdset_last_entry_t(st)
1318 for(; !stop_now ; now -= dt, t--) {
1319 if(t < 0) t = st->entries - 1;
1320 if(t == stop_at_t) stop_now = 1;
1324 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
1331 , (group_count + 1 == group)?"PRINT":" - "
1332 , (now >= after && now <= before)?"RANGE":" - "
1336 // make sure we return data in the proper time range
1337 if(now > before) continue;
1338 if(now < after) break;
1340 //if(rrdset_slot2time(st, t) != now)
1341 // 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));
1346 // check if we have to print this now
1347 if(group_count == group) {
1348 if(printed >= points) {
1349 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
1353 // generate the local date time
1354 struct tm *tm = localtime(&now);
1355 if(!tm) { error("localtime() failed."); continue; }
1356 if(now > last_timestamp) last_timestamp = now;
1358 if(printed) buffer_strcat(wb, "]},\n");
1359 buffer_strcat(wb, pre_date);
1360 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1361 buffer_strcat(wb, post_date);
1366 // do the calculations
1367 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1368 storage_number n = rd->values[t];
1369 calculated_number value = unpack_storage_number(n);
1371 if(!does_storage_number_exist(n)) {
1373 found_non_existing[c]++;
1375 if(did_storage_number_reset(n)) annotate_reset = 1;
1377 switch(group_method) {
1379 if(abs(value) > abs(group_values[c])) group_values[c] = value;
1383 group_values[c] += value;
1388 group_values[c] += value;
1389 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
1395 if(annotate_reset) {
1397 buffer_strcat(wb, overflow_annotation);
1401 buffer_strcat(wb, normal_annotation);
1404 for(c = 0 ; c < dimensions ; c++) {
1405 if(found_non_existing[c] == group_count) {
1406 // all entries are non-existing
1408 buffer_strcat(wb, pre_value);
1409 buffer_strcat(wb, "null");
1410 buffer_strcat(wb, post_value);
1412 else if(!print_hidden[c]) {
1414 buffer_strcat(wb, pre_value);
1415 buffer_rrd_value(wb, group_values[c]);
1416 buffer_strcat(wb, post_value);
1418 if(group_values[c]) found_non_zero[c]++;
1421 // reset them for the next loop
1422 group_values[c] = 0;
1423 found_non_existing[c] = 0;
1426 // if all dimensions are hidden, print a null
1428 buffer_strcat(wb, pre_value);
1429 buffer_strcat(wb, "null");
1430 buffer_strcat(wb, post_value);
1438 if(printed) buffer_strcat(wb, "]}");
1439 buffer_strcat(wb, "\n ]\n}\n");
1441 if(only_non_zero && max_loop > 1) {
1443 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1444 group_values[c] = 0;
1445 found_non_existing[c] = 0;
1447 if(!print_hidden[c] && !found_non_zero[c]) {
1449 print_hidden[c] = 1;
1453 if(changed) buffer_flush(wb);
1460 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->len);
1462 pthread_rwlock_unlock(&st->rwlock);
1463 return last_timestamp;