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];
269 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
270 group_values[c] = print_values[c] = 0;
271 print_hidden[c] = rd->hidden;
272 found_non_zero[c] = 0;
275 // -------------------------------------------------------------------------
276 // remove dimensions that contain only zeros
279 if(only_non_zero) max_loop = 2;
281 for(; max_loop ; max_loop--) {
283 // -------------------------------------------------------------------------
284 // print the JSON header
286 web_buffer_printf(wb, "{\n %scols%s:\n [\n", kq, kq);
287 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);
288 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);
289 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);
291 // print the header for each dimension
292 // and update the print_hidden array for the dimensions that should be hidden
294 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
295 if(!print_hidden[c]) {
297 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);
301 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);
304 // print the begin of row data
305 web_buffer_printf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
308 // -------------------------------------------------------------------------
311 int annotate_reset = 0;
312 int annotation_count = 0;
314 // to allow grouping on the same values, we need a pad
315 long pad = before % group;
317 // the minimum line length we expect
318 int line_size = 4096 + (dimensions * 200);
320 time_t now = time_init;
321 long max_entries = max_entries_init;
325 long count = 0, printed = 0, group_count = 0;
327 for(t = current_entry; max_entries ; now -= st->update_every, t--, max_entries--) {
328 if(t < 0) t = st->entries - 1;
333 debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
340 , (((count + 1 - pad) % group) == 0)?"PRINT":" - "
341 , (now >= after && now <= before)?"RANGE":" - "
345 // make sure we return data in the proper time range
346 if(now < after || now > before) continue;
351 if(((count - pad) % group) == 0) {
352 if(printed >= entries_to_show) {
353 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
357 if(group_count != group) {
358 // this is an incomplete group, skip it.
359 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++)
366 // check if we may exceed the buffer provided
367 web_buffer_increase(wb, line_size);
369 // generate the local date time
370 struct tm *tm = localtime(&now);
371 if(!tm) { error("localtime() failed."); continue; }
372 if(now > last_timestamp) last_timestamp = now;
374 if(printed) web_buffer_strcpy(wb, "]},\n");
375 web_buffer_strcpy(wb, pre_date);
376 web_buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
377 web_buffer_strcpy(wb, post_date);
382 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
383 calculated_number value = unpack_storage_number(rd->values[t]);
385 switch(group_method) {
387 if(abs(value) > abs(group_values[c])) group_values[c] = value;
391 group_values[c] += value;
396 group_values[c] += value;
397 if(print_this) group_values[c] /= group_count;
402 print_values[c] = group_values[c];
412 web_buffer_strcpy(wb, overflow_annotation);
416 web_buffer_strcpy(wb, normal_annotation);
419 for(c = 0 ; c < dimensions ; c++) {
420 if(!print_hidden[c]) {
422 web_buffer_strcpy(wb, pre_value);
423 web_buffer_rrd_value(wb, print_values[c]);
424 web_buffer_strcpy(wb, post_value);
426 if(print_values[c]) found_non_zero[c]++;
430 web_buffer_strcpy(wb, pre_value);
431 web_buffer_rrd_value(wb, (calculated_number)0);
432 web_buffer_strcpy(wb, post_value);
439 if(printed) web_buffer_printf(wb, "]}");
440 web_buffer_printf(wb, "\n ]\n}\n");
442 if(only_non_zero && max_loop > 1) {
444 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
447 if(!print_hidden[c] && !found_non_zero[c]) {
453 if(changed) web_buffer_reset(wb);
460 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->bytes);
462 pthread_rwlock_unlock(&st->rwlock);
463 return last_timestamp;