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, 16384);
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 memory += rd->memsize;
74 "\t\t\t\t\t\"id\": \"%s\",\n"
75 "\t\t\t\t\t\"name\": \"%s\",\n"
76 "\t\t\t\t\t\"entries\": %ld,\n"
77 "\t\t\t\t\t\"isHidden\": %d,\n"
78 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
79 "\t\t\t\t\t\"multiplier\": %ld,\n"
80 "\t\t\t\t\t\"divisor\": %ld,\n"
81 "\t\t\t\t\t\"last_entry_t\": %lu,\n"
82 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
83 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
84 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
85 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
86 "\t\t\t\t\t\"memory\": %lu\n"
92 , rrddim_algorithm_name(rd->algorithm)
95 , rd->last_collected_time.tv_sec
97 , rd->calculated_value
98 , rd->last_collected_value
99 , rd->last_calculated_value
105 web_buffer_printf(wb,
107 "\t\t\t\"memory\" : %lu\n"
112 pthread_rwlock_unlock(&st->rwlock);
116 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
117 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
119 void rrd_stats_graph_json(RRDSET *st, char *options, struct web_buffer *wb)
121 web_buffer_increase(wb, 16384);
123 web_buffer_printf(wb, RRD_GRAPH_JSON_HEADER);
124 rrd_stats_one_json(st, options, wb);
125 web_buffer_printf(wb, RRD_GRAPH_JSON_FOOTER);
128 void rrd_stats_all_json(struct web_buffer *wb)
130 web_buffer_increase(wb, 1024);
132 unsigned long memory = 0;
136 web_buffer_printf(wb, RRD_GRAPH_JSON_HEADER);
138 pthread_rwlock_rdlock(&rrdset_root_rwlock);
139 for(st = rrdset_root, c = 0; st ; st = st->next) {
141 if(c) web_buffer_printf(wb, "%s", ",\n");
142 memory += rrd_stats_one_json(st, NULL, wb);
146 pthread_rwlock_unlock(&rrdset_root_rwlock);
148 web_buffer_printf(wb, "\n\t],\n"
149 "\t\"hostname\": \"%s\",\n"
150 "\t\"update_every\": %d,\n"
151 "\t\"history\": %d,\n"
152 "\t\"memory\": %lu\n"
156 , rrd_default_history_entries
161 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)
164 pthread_rwlock_rdlock(&st->rwlock);
167 // -------------------------------------------------------------------------
168 // switch from JSON to google JSON
173 case DATASOURCE_GOOGLE_JSON:
174 case DATASOURCE_GOOGLE_JSONP:
179 case DATASOURCE_JSON:
185 // -------------------------------------------------------------------------
186 // validate the parameters
188 if(entries_to_show < 1) entries_to_show = 1;
189 if(group < 1) group = 1;
191 // make sure current_entry is within limits
192 long current_entry = (long)st->current_entry - (long)1;
193 if(current_entry < 0) current_entry = 0;
194 else if(current_entry >= st->entries) current_entry = st->entries - 1;
196 // find the oldest entry of the round-robin
197 long max_entries_init = (st->counter < (unsigned long)st->entries) ? st->counter : (unsigned long)st->entries;
199 time_t time_init = st->last_updated.tv_sec;
201 if(before == 0 || before > time_init) before = time_init;
202 if(after == 0) after = rrdset_first_entry_t(st);
207 // our return value (the last timestamp printed)
208 // this is required to detect re-transmit in google JSONP
209 time_t last_timestamp = 0;
212 // -------------------------------------------------------------------------
213 // find how many dimensions we have
217 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
219 pthread_rwlock_unlock(&st->rwlock);
220 web_buffer_printf(wb, "No dimensions yet.");
225 // -------------------------------------------------------------------------
226 // prepare various strings, to speed up the loop
228 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);
229 char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
230 char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
231 char post_date[21]; snprintf(post_date, 20, "%s}", sq);
232 char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
233 char post_value[21]; snprintf(post_value, 20, "}");
236 // -------------------------------------------------------------------------
237 // checks for debuging
240 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"
242 , rrdset_first_entry_t(st)
243 , st->last_updated.tv_sec
244 , st->last_updated.tv_sec - rrdset_first_entry_t(st)
254 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
256 if((before - after) > st->entries * st->update_every)
257 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);
261 // -------------------------------------------------------------------------
262 // temp arrays for keeping values per dimension
264 calculated_number group_values[dimensions]; // keep sums when grouping
265 calculated_number print_values[dimensions]; // keep the final value to be printed
266 int print_hidden[dimensions]; // keep hidden flags
267 int found_non_zero[dimensions];
268 int found_non_existing[dimensions];
271 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
272 group_values[c] = print_values[c] = 0;
273 print_hidden[c] = rd->hidden;
274 found_non_zero[c] = 0;
275 found_non_existing[c] = 0;
278 // -------------------------------------------------------------------------
279 // remove dimensions that contain only zeros
282 if(only_non_zero) max_loop = 2;
284 for(; max_loop ; max_loop--) {
286 // -------------------------------------------------------------------------
287 // print the JSON header
289 web_buffer_printf(wb, "{\n %scols%s:\n [\n", kq, kq);
290 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);
291 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);
292 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);
294 // print the header for each dimension
295 // and update the print_hidden array for the dimensions that should be hidden
297 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
298 if(!print_hidden[c]) {
300 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);
304 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);
307 // print the begin of row data
308 web_buffer_printf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
311 // -------------------------------------------------------------------------
314 int annotate_reset = 0;
315 int annotation_count = 0;
317 // to allow grouping on the same values, we need a pad
318 long pad = before % group;
320 // the minimum line length we expect
321 int line_size = 4096 + (dimensions * 200);
323 time_t now = time_init;
324 long max_entries = max_entries_init;
328 long count = 0, printed = 0, group_count = 0;
330 for(t = current_entry; max_entries ; now -= st->update_every, t--, max_entries--) {
331 if(t < 0) t = st->entries - 1;
336 debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
343 , (((count + 1 - pad) % group) == 0)?"PRINT":" - "
344 , (now >= after && now <= before)?"RANGE":" - "
348 // make sure we return data in the proper time range
349 if(now < after || now > before) continue;
354 // check if we have to print this now
355 if(((count - pad) % group) == 0) {
356 if(printed >= entries_to_show) {
357 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
361 if(group_count != group) {
362 // this is an incomplete group, skip it.
363 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
365 found_non_existing[c] = 0;
372 // check if we may exceed the buffer provided
373 web_buffer_increase(wb, line_size);
375 // generate the local date time
376 struct tm *tm = localtime(&now);
377 if(!tm) { error("localtime() failed."); continue; }
378 if(now > last_timestamp) last_timestamp = now;
380 if(printed) web_buffer_strcpy(wb, "]},\n");
381 web_buffer_strcpy(wb, pre_date);
382 web_buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
383 web_buffer_strcpy(wb, post_date);
388 // do the calculations
389 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
390 storage_number n = rd->values[t];
391 calculated_number value = unpack_storage_number(n);
393 if(!does_storage_number_exist(n)) {
395 found_non_existing[c]++;
397 if(did_storage_number_reset(n)) annotate_reset = 1;
399 switch(group_method) {
401 if(abs(value) > abs(group_values[c])) group_values[c] = value;
405 group_values[c] += value;
410 group_values[c] += value;
411 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
419 web_buffer_strcpy(wb, overflow_annotation);
423 web_buffer_strcpy(wb, normal_annotation);
426 for(c = 0 ; c < dimensions ; c++) {
427 if(found_non_existing[c] == group_count) {
428 // all entries are non-existing
430 web_buffer_strcpy(wb, pre_value);
431 web_buffer_strcpy(wb, "null");
432 web_buffer_strcpy(wb, post_value);
434 else if(!print_hidden[c]) {
436 web_buffer_strcpy(wb, pre_value);
437 web_buffer_rrd_value(wb, group_values[c]);
438 web_buffer_strcpy(wb, post_value);
440 if(group_values[c]) found_non_zero[c]++;
443 // reset them for the next loop
445 found_non_existing[c] = 0;
448 // if all dimensions are hidden, print a null
450 web_buffer_strcpy(wb, pre_value);
451 web_buffer_strcpy(wb, "null");
452 web_buffer_strcpy(wb, post_value);
460 if(printed) web_buffer_printf(wb, "]}");
461 web_buffer_printf(wb, "\n ]\n}\n");
463 if(only_non_zero && max_loop > 1) {
465 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
467 found_non_existing[c] = 0;
469 if(!print_hidden[c] && !found_non_zero[c]) {
475 if(changed) web_buffer_reset(wb);
482 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->bytes);
484 pthread_rwlock_unlock(&st->rwlock);
485 return last_timestamp;
490 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) {
492 if(!st->dimensions) {
493 web_buffer_printf(wb, "No dimensions yet.");
498 pthread_rwlock_rdlock(&st->rwlock);
500 // -------------------------------------------------------------------------
501 // validate the parameters
503 if(entries_to_show < 1) entries_to_show = 1;
504 if(group < 1) group = 1;
506 // make sure current_entry is within limits
507 long current_entry = (long)st->current_entry - (long)1;
508 if(current_entry < 0) current_entry = 0;
509 else if(current_entry >= st->entries) current_entry = st->entries - 1;
511 // find the oldest entry of the round-robin
512 long max_entries = (st->counter < (unsigned long)st->entries) ? st->counter : (unsigned long)st->entries;
513 if(entries_to_show > max_entries) entries_to_show = max_entries;
515 if(before == 0 || before > st->last_updated.tv_sec) before = st->last_updated.tv_sec;
516 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
518 if(entries_to_show > (before - after) / st->update_every / group) entries_to_show = (before - after) / st->update_every / group;
520 // -------------------------------------------------------------------------
521 // checks for debuging
524 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"
526 , rrdset_first_entry_t(st)
527 , st->last_updated.tv_sec
528 , st->last_updated.tv_sec - rrdset_first_entry_t(st)
538 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
540 if((before - after) > st->entries * st->update_every)
541 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);
544 // -------------------------------------------------------------------------
546 int dimensions = 0, i, j;
548 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
550 uint32_t *annotations[dimensions];
551 calculated_number *values[dimensions];
552 int enabled[dimensions];
554 for( rd = st->dimensions, i = 0 ; rd && i < dimensions ; rd = rd->next, i++) {
555 annotations[i] = malloc(sizeof(uint32_t) * entries_to_show);
556 if(!annotations[i]) fatal("Cannot allocate %z bytes of memory.", sizeof(uint32_t) * entries_to_show);
558 values[i] = malloc(sizeof(calculated_number) * entries_to_show);
559 if(!values[i]) fatal("Cannot allocate %z bytes of memory.", sizeof(calculated_number) * entries_to_show);
561 calculated_number sum = rrddim2values(st, rd, values, annotations, entries_to_show, group, group_method, after, before);
562 if(only_non_zero && sum == 0) enabled[i] = 0;
567 case DATASOURCE_GOOGLE_JSON:
568 case DATASOURCE_GOOGLE_JSONP:
569 //rrdvalues2googlejson(st, rd, values, annotations, enabled, entries_to_show, );
572 case DATASOURCE_JSON: