9 #define HOSTNAME_MAX 1024
10 char *hostname = "unknown";
12 unsigned long rrd_stats_one_json(RRDSET *st, char *options, struct web_buffer *wb)
14 time_t now = time(NULL);
15 web_buffer_increase(wb, 65536);
17 pthread_rwlock_rdlock(&st->rwlock);
21 "\t\t\t\"id\": \"%s\",\n"
22 "\t\t\t\"name\": \"%s\",\n"
23 "\t\t\t\"type\": \"%s\",\n"
24 "\t\t\t\"family\": \"%s\",\n"
25 "\t\t\t\"title\": \"%s\",\n"
26 "\t\t\t\"priority\": %ld,\n"
27 "\t\t\t\"enabled\": %d,\n"
28 "\t\t\t\"units\": \"%s\",\n"
29 "\t\t\t\"url\": \"/data/%s/%s\",\n"
30 "\t\t\t\"chart_type\": \"%s\",\n"
31 "\t\t\t\"counter\": %ld,\n"
32 "\t\t\t\"entries\": %ld,\n"
33 "\t\t\t\"first_entry_t\": %lu,\n"
34 "\t\t\t\"last_entry\": %ld,\n"
35 "\t\t\t\"last_entry_t\": %lu,\n"
36 "\t\t\t\"last_entry_secs_ago\": %lu,\n"
37 "\t\t\t\"update_every\": %d,\n"
38 "\t\t\t\"isdetail\": %d,\n"
39 "\t\t\t\"usec_since_last_update\": %llu,\n"
40 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
41 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
42 "\t\t\t\"dimensions\": [\n"
51 , st->name, options?options:""
52 , rrdset_type_name(st->chart_type)
55 , rrdset_first_entry_t(st)
57 , st->last_updated.tv_sec
58 , now - (st->last_updated.tv_sec > now) ? now : st->last_updated.tv_sec
61 , st->usec_since_last_update
63 , st->last_collected_total
66 unsigned long memory = st->memsize;
69 for(rd = st->dimensions; rd ; rd = rd->next) {
70 web_buffer_increase(wb, 4096);
72 memory += rd->memsize;
76 "\t\t\t\t\t\"id\": \"%s\",\n"
77 "\t\t\t\t\t\"name\": \"%s\",\n"
78 "\t\t\t\t\t\"entries\": %ld,\n"
79 "\t\t\t\t\t\"isHidden\": %d,\n"
80 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
81 "\t\t\t\t\t\"multiplier\": %ld,\n"
82 "\t\t\t\t\t\"divisor\": %ld,\n"
83 "\t\t\t\t\t\"last_entry_t\": %lu,\n"
84 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
85 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
86 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
87 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
88 "\t\t\t\t\t\"memory\": %lu\n"
94 , rrddim_algorithm_name(rd->algorithm)
97 , rd->last_collected_time.tv_sec
99 , rd->calculated_value
100 , rd->last_collected_value
101 , rd->last_calculated_value
107 web_buffer_printf(wb,
109 "\t\t\t\"memory\" : %lu\n"
114 pthread_rwlock_unlock(&st->rwlock);
118 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
119 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
121 void rrd_stats_graph_json(RRDSET *st, char *options, struct web_buffer *wb)
123 web_buffer_increase(wb, 2048);
125 web_buffer_printf(wb, RRD_GRAPH_JSON_HEADER);
126 rrd_stats_one_json(st, options, wb);
127 web_buffer_printf(wb, RRD_GRAPH_JSON_FOOTER);
130 void rrd_stats_all_json(struct web_buffer *wb)
132 web_buffer_increase(wb, 2048);
134 unsigned long memory = 0;
138 web_buffer_printf(wb, RRD_GRAPH_JSON_HEADER);
140 pthread_rwlock_rdlock(&rrdset_root_rwlock);
141 for(st = rrdset_root, c = 0; st ; st = st->next) {
143 if(c) web_buffer_printf(wb, "%s", ",\n");
144 memory += rrd_stats_one_json(st, NULL, wb);
148 pthread_rwlock_unlock(&rrdset_root_rwlock);
150 web_buffer_printf(wb, "\n\t],\n"
151 "\t\"hostname\": \"%s\",\n"
152 "\t\"update_every\": %d,\n"
153 "\t\"history\": %d,\n"
154 "\t\"memory\": %lu\n"
158 , rrd_default_history_entries
163 unsigned long rrd_stats_json(int type, RRDSET *st, struct web_buffer *wb, int entries_to_show, int group, int group_method, time_t after, time_t before, int only_non_zero)
166 pthread_rwlock_rdlock(&st->rwlock);
169 // -------------------------------------------------------------------------
170 // switch from JSON to google JSON
175 case DATASOURCE_GOOGLE_JSON:
176 case DATASOURCE_GOOGLE_JSONP:
181 case DATASOURCE_JSON:
187 // -------------------------------------------------------------------------
188 // validate the parameters
190 if(entries_to_show < 1) entries_to_show = 1;
191 if(group < 1) group = 1;
193 // make sure current_entry is within limits
194 long current_entry = (long)st->current_entry - (long)1;
195 if(current_entry < 0) current_entry = 0;
196 else if(current_entry >= st->entries) current_entry = st->entries - 1;
198 // find the oldest entry of the round-robin
199 long max_entries_init = (st->counter < (unsigned long)st->entries) ? st->counter : (unsigned long)st->entries;
201 time_t time_init = st->last_updated.tv_sec;
203 if(before == 0 || before > time_init) before = time_init;
204 if(after == 0) after = rrdset_first_entry_t(st);
209 // our return value (the last timestamp printed)
210 // this is required to detect re-transmit in google JSONP
211 time_t last_timestamp = 0;
214 // -------------------------------------------------------------------------
215 // find how many dimensions we have
219 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
221 pthread_rwlock_unlock(&st->rwlock);
222 web_buffer_printf(wb, "No dimensions yet.");
227 // -------------------------------------------------------------------------
228 // prepare various strings, to speed up the loop
230 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);
231 char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
232 char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
233 char post_date[21]; snprintf(post_date, 20, "%s}", sq);
234 char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
235 char post_value[21]; snprintf(post_value, 20, "}");
238 // -------------------------------------------------------------------------
239 // checks for debuging
242 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, max_entries = %ld"
244 , rrdset_first_entry_t(st)
245 , st->last_updated.tv_sec
246 , st->last_updated.tv_sec - rrdset_first_entry_t(st)
256 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
258 if((before - after) > st->entries * st->update_every)
259 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);
263 // -------------------------------------------------------------------------
264 // temp arrays for keeping values per dimension
266 calculated_number group_values[dimensions]; // keep sums when grouping
267 calculated_number print_values[dimensions]; // keep the final value to be printed
268 int print_hidden[dimensions]; // keep hidden flags
269 int found_non_zero[dimensions];
270 int found_non_existing[dimensions];
273 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
274 group_values[c] = print_values[c] = 0;
275 print_hidden[c] = rd->hidden;
276 found_non_zero[c] = 0;
277 found_non_existing[c] = 0;
280 // -------------------------------------------------------------------------
281 // remove dimensions that contain only zeros
284 if(only_non_zero) max_loop = 2;
286 for(; max_loop ; max_loop--) {
288 // -------------------------------------------------------------------------
289 // print the JSON header
291 web_buffer_printf(wb, "{\n %scols%s:\n [\n", kq, kq);
292 web_buffer_printf(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);
293 web_buffer_printf(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);
294 web_buffer_printf(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);
296 // print the header for each dimension
297 // and update the print_hidden array for the dimensions that should be hidden
299 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
300 if(!print_hidden[c]) {
302 web_buffer_printf(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);
306 web_buffer_printf(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);
309 // print the begin of row data
310 web_buffer_printf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
313 // -------------------------------------------------------------------------
316 int annotate_reset = 0;
317 int annotation_count = 0;
319 // to allow grouping on the same values, we need a pad
320 long pad = before % group;
322 // the minimum line length we expect
323 int line_size = 4096 + (dimensions * 200);
325 time_t now = time_init;
326 long max_entries = max_entries_init;
330 long count = 0, printed = 0, group_count = 0;
332 for(t = current_entry; max_entries ; now -= st->update_every, t--, max_entries--) {
333 if(t < 0) t = st->entries - 1;
338 debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
345 , (((count + 1 - pad) % group) == 0)?"PRINT":" - "
346 , (now >= after && now <= before)?"RANGE":" - "
350 // make sure we return data in the proper time range
351 if(now < after || now > before) continue;
356 // check if we have to print this now
357 if(((count - pad) % group) == 0) {
358 if(printed >= entries_to_show) {
359 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
363 if(group_count != group) {
364 // this is an incomplete group, skip it.
365 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
367 found_non_existing[c] = 0;
374 // check if we may exceed the buffer provided
375 web_buffer_increase(wb, line_size);
377 // generate the local date time
378 struct tm *tm = localtime(&now);
379 if(!tm) { error("localtime() failed."); continue; }
380 if(now > last_timestamp) last_timestamp = now;
382 if(printed) web_buffer_strcpy(wb, "]},\n");
383 web_buffer_strcpy(wb, pre_date);
384 web_buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
385 web_buffer_strcpy(wb, post_date);
390 // do the calculations
391 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
392 storage_number n = rd->values[t];
393 calculated_number value = unpack_storage_number(n);
395 if(!does_storage_number_exist(n)) {
397 found_non_existing[c]++;
399 if(did_storage_number_reset(n)) annotate_reset = 1;
401 switch(group_method) {
403 if(abs(value) > abs(group_values[c])) group_values[c] = value;
407 group_values[c] += value;
412 group_values[c] += value;
413 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
421 web_buffer_strcpy(wb, overflow_annotation);
425 web_buffer_strcpy(wb, normal_annotation);
428 for(c = 0 ; c < dimensions ; c++) {
429 if(found_non_existing[c] == group_count) {
430 // all entries are non-existing
432 web_buffer_strcpy(wb, pre_value);
433 web_buffer_strcpy(wb, "null");
434 web_buffer_strcpy(wb, post_value);
436 else if(!print_hidden[c]) {
438 web_buffer_strcpy(wb, pre_value);
439 web_buffer_rrd_value(wb, group_values[c]);
440 web_buffer_strcpy(wb, post_value);
442 if(group_values[c]) found_non_zero[c]++;
445 // reset them for the next loop
447 found_non_existing[c] = 0;
450 // if all dimensions are hidden, print a null
452 web_buffer_strcpy(wb, pre_value);
453 web_buffer_strcpy(wb, "null");
454 web_buffer_strcpy(wb, post_value);
462 if(printed) web_buffer_printf(wb, "]}");
463 web_buffer_printf(wb, "\n ]\n}\n");
465 if(only_non_zero && max_loop > 1) {
467 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
469 found_non_existing[c] = 0;
471 if(!print_hidden[c] && !found_non_zero[c]) {
477 if(changed) web_buffer_reset(wb);
484 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->bytes);
486 pthread_rwlock_unlock(&st->rwlock);
487 return last_timestamp;
492 unsigned long rrdset2json(int type, RRDSET *st, struct web_buffer *wb, int entries_to_show, int group, int group_method, time_t after, time_t before, int only_non_zero) {
494 if(!st->dimensions) {
495 web_buffer_printf(wb, "No dimensions yet.");
500 pthread_rwlock_rdlock(&st->rwlock);
502 // -------------------------------------------------------------------------
503 // validate the parameters
505 if(entries_to_show < 1) entries_to_show = 1;
506 if(group < 1) group = 1;
508 // make sure current_entry is within limits
509 long current_entry = (long)st->current_entry - (long)1;
510 if(current_entry < 0) current_entry = 0;
511 else if(current_entry >= st->entries) current_entry = st->entries - 1;
513 // find the oldest entry of the round-robin
514 long max_entries = (st->counter < (unsigned long)st->entries) ? st->counter : (unsigned long)st->entries;
515 if(entries_to_show > max_entries) entries_to_show = max_entries;
517 if(before == 0 || before > st->last_updated.tv_sec) before = st->last_updated.tv_sec;
518 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
520 if(entries_to_show > (before - after) / st->update_every / group) entries_to_show = (before - after) / st->update_every / group;
522 // -------------------------------------------------------------------------
523 // checks for debuging
526 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, max_entries = %ld"
528 , rrdset_first_entry_t(st)
529 , st->last_updated.tv_sec
530 , st->last_updated.tv_sec - rrdset_first_entry_t(st)
540 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
542 if((before - after) > st->entries * st->update_every)
543 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);
546 // -------------------------------------------------------------------------
548 int dimensions = 0, i, j;
550 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
552 uint32_t *annotations[dimensions];
553 calculated_number *values[dimensions];
554 int enabled[dimensions];
556 for( rd = st->dimensions, i = 0 ; rd && i < dimensions ; rd = rd->next, i++) {
557 annotations[i] = malloc(sizeof(uint32_t) * entries_to_show);
558 if(!annotations[i]) fatal("Cannot allocate %z bytes of memory.", sizeof(uint32_t) * entries_to_show);
560 values[i] = malloc(sizeof(calculated_number) * entries_to_show);
561 if(!values[i]) fatal("Cannot allocate %z bytes of memory.", sizeof(calculated_number) * entries_to_show);
563 calculated_number sum = rrddim2values(st, rd, values, annotations, entries_to_show, group, group_method, after, before);
564 if(only_non_zero && sum == 0) enabled[i] = 0;
569 case DATASOURCE_GOOGLE_JSON:
570 case DATASOURCE_GOOGLE_JSONP:
571 //rrdvalues2googlejson(st, rd, values, annotations, enabled, entries_to_show, );
574 case DATASOURCE_JSON: