8 #define HOSTNAME_MAX 1024
9 char *hostname = "unknown";
11 unsigned long rrd_stats_one_json(RRD_STATS *st, char *options, struct web_buffer *wb)
13 time_t now = time(NULL);
14 web_buffer_increase(wb, 16384);
16 pthread_rwlock_rdlock(&st->rwlock);
20 "\t\t\t\"id\": \"%s\",\n"
21 "\t\t\t\"name\": \"%s\",\n"
22 "\t\t\t\"type\": \"%s\",\n"
23 "\t\t\t\"family\": \"%s\",\n"
24 "\t\t\t\"title\": \"%s\",\n"
25 "\t\t\t\"priority\": %ld,\n"
26 "\t\t\t\"enabled\": %d,\n"
27 "\t\t\t\"units\": \"%s\",\n"
28 "\t\t\t\"url\": \"/data/%s/%s\",\n"
29 "\t\t\t\"chart_type\": \"%s\",\n"
30 "\t\t\t\"counter\": %ld,\n"
31 "\t\t\t\"entries\": %ld,\n"
32 "\t\t\t\"first_entry_t\": %lu,\n"
33 "\t\t\t\"last_entry\": %ld,\n"
34 "\t\t\t\"last_entry_t\": %lu,\n"
35 "\t\t\t\"last_entry_secs_ago\": %lu,\n"
36 "\t\t\t\"update_every\": %d,\n"
37 "\t\t\t\"isdetail\": %d,\n"
38 "\t\t\t\"usec_since_last_update\": %llu,\n"
39 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
40 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
41 "\t\t\t\"dimensions\": [\n"
50 , st->name, options?options:""
51 , chart_type_name(st->chart_type)
54 , rrd_stats_first_entry_t(st)
56 , st->last_updated.tv_sec
57 , now - (st->last_updated.tv_sec > now) ? now : st->last_updated.tv_sec
60 , st->usec_since_last_update
62 , st->last_collected_total
65 unsigned long memory = st->memsize;
68 for(rd = st->dimensions; rd ; rd = rd->next) {
69 memory += rd->memsize;
73 "\t\t\t\t\t\"id\": \"%s\",\n"
74 "\t\t\t\t\t\"name\": \"%s\",\n"
75 "\t\t\t\t\t\"entries\": %ld,\n"
76 "\t\t\t\t\t\"isHidden\": %d,\n"
77 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
78 "\t\t\t\t\t\"multiplier\": %ld,\n"
79 "\t\t\t\t\t\"divisor\": %ld,\n"
80 "\t\t\t\t\t\"last_entry_t\": %lu,\n"
81 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
82 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
83 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
84 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
85 "\t\t\t\t\t\"memory\": %lu\n"
91 , algorithm_name(rd->algorithm)
94 , rd->last_collected_time.tv_sec
96 , rd->calculated_value
97 , rd->last_collected_value
98 , rd->last_calculated_value
104 web_buffer_printf(wb,
106 "\t\t\t\"memory\" : %lu\n"
111 pthread_rwlock_unlock(&st->rwlock);
115 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
116 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
118 void rrd_stats_graph_json(RRD_STATS *st, char *options, struct web_buffer *wb)
120 web_buffer_increase(wb, 16384);
122 web_buffer_printf(wb, RRD_GRAPH_JSON_HEADER);
123 rrd_stats_one_json(st, options, wb);
124 web_buffer_printf(wb, RRD_GRAPH_JSON_FOOTER);
127 void rrd_stats_all_json(struct web_buffer *wb)
129 web_buffer_increase(wb, 1024);
131 unsigned long memory = 0;
135 web_buffer_printf(wb, RRD_GRAPH_JSON_HEADER);
137 pthread_rwlock_rdlock(&root_rwlock);
138 for(st = root, c = 0; st ; st = st->next) {
140 if(c) web_buffer_printf(wb, "%s", ",\n");
141 memory += rrd_stats_one_json(st, NULL, wb);
145 pthread_rwlock_unlock(&root_rwlock);
147 web_buffer_printf(wb, "\n\t],\n"
148 "\t\"hostname\": \"%s\",\n"
149 "\t\"update_every\": %d,\n"
150 "\t\"history\": %d,\n"
151 "\t\"memory\": %lu\n"
160 unsigned long rrd_stats_json(int type, RRD_STATS *st, struct web_buffer *wb, int entries_to_show, int group, int group_method, time_t after, time_t before, int only_non_zero)
163 pthread_rwlock_rdlock(&st->rwlock);
166 // -------------------------------------------------------------------------
167 // switch from JSON to google JSON
172 case DATASOURCE_GOOGLE_JSON:
173 case DATASOURCE_GOOGLE_JSONP:
178 case DATASOURCE_JSON:
184 // -------------------------------------------------------------------------
185 // validate the parameters
187 if(entries_to_show < 1) entries_to_show = 1;
188 if(group < 1) group = 1;
190 // make sure current_entry is within limits
191 long current_entry = (long)st->current_entry - (long)1;
192 if(current_entry < 0) current_entry = 0;
193 else if(current_entry >= st->entries) current_entry = st->entries - 1;
195 // find the oldest entry of the round-robin
196 long max_entries_init = (st->counter < (unsigned long)st->entries) ? st->counter : (unsigned long)st->entries;
198 time_t time_init = st->last_updated.tv_sec;
200 if(before == 0 || before > time_init) before = time_init;
201 if(after == 0) after = rrd_stats_first_entry_t(st);
206 // our return value (the last timestamp printed)
207 // this is required to detect re-transmit in google JSONP
208 time_t last_timestamp = 0;
211 // -------------------------------------------------------------------------
212 // find how many dimensions we have
216 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
218 pthread_rwlock_unlock(&st->rwlock);
219 web_buffer_printf(wb, "No dimensions yet.");
224 // -------------------------------------------------------------------------
225 // prepare various strings, to speed up the loop
227 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);
228 char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
229 char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
230 char post_date[21]; snprintf(post_date, 20, "%s}", sq);
231 char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
232 char post_value[21]; snprintf(post_value, 20, "}");
235 // -------------------------------------------------------------------------
236 // checks for debuging
239 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"
241 , rrd_stats_first_entry_t(st)
242 , st->last_updated.tv_sec
243 , st->last_updated.tv_sec - rrd_stats_first_entry_t(st)
253 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
255 if((before - after) > st->entries * st->update_every)
256 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);
260 // -------------------------------------------------------------------------
261 // temp arrays for keeping values per dimension
263 calculated_number group_values[dimensions]; // keep sums when grouping
264 calculated_number print_values[dimensions]; // keep the final value to be printed
265 int print_hidden[dimensions]; // keep hidden flags
266 int found_non_zero[dimensions];
267 int found_non_existing[dimensions];
270 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
271 group_values[c] = print_values[c] = 0;
272 print_hidden[c] = rd->hidden;
273 found_non_zero[c] = 0;
274 found_non_existing[c] = 0;
277 // -------------------------------------------------------------------------
278 // remove dimensions that contain only zeros
281 if(only_non_zero) max_loop = 2;
283 for(; max_loop ; max_loop--) {
285 // -------------------------------------------------------------------------
286 // print the JSON header
288 web_buffer_printf(wb, "{\n %scols%s:\n [\n", kq, kq);
289 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);
290 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);
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:%sannotationText%s}}", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, kq, kq, sq, sq);
293 // print the header for each dimension
294 // and update the print_hidden array for the dimensions that should be hidden
296 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
297 if(!print_hidden[c]) {
299 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);
303 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);
306 // print the begin of row data
307 web_buffer_printf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
310 // -------------------------------------------------------------------------
313 int annotate_reset = 0;
314 int annotation_count = 0;
316 // to allow grouping on the same values, we need a pad
317 long pad = before % group;
319 // the minimum line length we expect
320 int line_size = 4096 + (dimensions * 200);
322 time_t now = time_init;
323 long max_entries = max_entries_init;
327 long count = 0, printed = 0, group_count = 0;
329 for(t = current_entry; max_entries ; now -= st->update_every, t--, max_entries--) {
330 if(t < 0) t = st->entries - 1;
335 debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
342 , (((count + 1 - pad) % group) == 0)?"PRINT":" - "
343 , (now >= after && now <= before)?"RANGE":" - "
347 // make sure we return data in the proper time range
348 if(now < after || now > before) continue;
353 // check if we have to print this now
354 if(((count - pad) % group) == 0) {
355 if(printed >= entries_to_show) {
356 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
360 if(group_count != group) {
361 // this is an incomplete group, skip it.
362 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
364 found_non_existing[c] = 0;
371 // check if we may exceed the buffer provided
372 web_buffer_increase(wb, line_size);
374 // generate the local date time
375 struct tm *tm = localtime(&now);
376 if(!tm) { error("localtime() failed."); continue; }
377 if(now > last_timestamp) last_timestamp = now;
379 if(printed) web_buffer_strcpy(wb, "]},\n");
380 web_buffer_strcpy(wb, pre_date);
381 web_buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
382 web_buffer_strcpy(wb, post_date);
387 // do the calculations
388 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
389 storage_number n = rd->values[t];
390 calculated_number value = unpack_storage_number(n);
392 if(!does_storage_number_exist(n)) {
394 found_non_existing[c]++;
396 if(did_storage_number_reset(n)) annotate_reset = 1;
398 switch(group_method) {
400 if(abs(value) > abs(group_values[c])) group_values[c] = value;
404 group_values[c] += value;
409 group_values[c] += value;
410 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
418 web_buffer_strcpy(wb, overflow_annotation);
422 web_buffer_strcpy(wb, normal_annotation);
425 for(c = 0 ; c < dimensions ; c++) {
426 if(found_non_existing[c] == group_count) {
427 // all entries are non-existing
429 web_buffer_strcpy(wb, pre_value);
430 web_buffer_strcpy(wb, "null");
431 web_buffer_strcpy(wb, post_value);
433 else if(!print_hidden[c]) {
435 web_buffer_strcpy(wb, pre_value);
436 web_buffer_rrd_value(wb, group_values[c]);
437 web_buffer_strcpy(wb, post_value);
439 if(group_values[c]) found_non_zero[c]++;
442 // reset them for the next loop
444 found_non_existing[c] = 0;
447 // if all dimensions are hidden, print a null
449 web_buffer_strcpy(wb, pre_value);
450 web_buffer_strcpy(wb, "null");
451 web_buffer_strcpy(wb, post_value);
459 if(printed) web_buffer_printf(wb, "]}");
460 web_buffer_printf(wb, "\n ]\n}\n");
462 if(only_non_zero && max_loop > 1) {
464 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
466 found_non_existing[c] = 0;
468 if(!print_hidden[c] && !found_non_zero[c]) {
474 if(changed) web_buffer_reset(wb);
481 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->bytes);
483 pthread_rwlock_unlock(&st->rwlock);
484 return last_timestamp;