]> arthur.barton.de Git - netdata.git/blob - src/rrd2json.c
splitted netdata to multiple source files
[netdata.git] / src / rrd2json.c
1 #include <pthread.h>
2 #include <sys/time.h>
3
4 #include "log.h"
5 #include "common.h"
6 #include "rrd2json.h"
7
8 #define HOSTNAME_MAX 1024
9 char *hostname = "unknown";
10
11 unsigned long rrd_stats_one_json(RRD_STATS *st, char *options, struct web_buffer *wb)
12 {
13         time_t now = time(NULL);
14         web_buffer_increase(wb, 16384);
15
16         pthread_rwlock_rdlock(&st->rwlock);
17
18         web_buffer_printf(wb,
19                 "\t\t{\n"
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"
42                 , st->id
43                 , st->name
44                 , st->type
45                 , st->family
46                 , st->title
47                 , st->priority
48                 , st->enabled
49                 , st->units
50                 , st->name, options?options:""
51                 , chart_type_name(st->chart_type)
52                 , st->counter
53                 , st->entries
54                 , rrd_stats_first_entry_t(st)
55                 , st->current_entry
56                 , st->last_updated.tv_sec
57                 , now - (st->last_updated.tv_sec > now) ? now : st->last_updated.tv_sec
58                 , st->update_every
59                 , st->isdetail
60                 , st->usec_since_last_update
61                 , st->collected_total
62                 , st->last_collected_total
63                 );
64
65         unsigned long memory = st->memsize;
66
67         RRD_DIMENSION *rd;
68         for(rd = st->dimensions; rd ; rd = rd->next) {
69                 memory += rd->memsize;
70
71                 web_buffer_printf(wb,
72                         "\t\t\t\t{\n"
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"
86                         "\t\t\t\t}%s\n"
87                         , rd->id
88                         , rd->name
89                         , rd->entries
90                         , rd->hidden
91                         , algorithm_name(rd->algorithm)
92                         , rd->multiplier
93                         , rd->divisor
94                         , rd->last_collected_time.tv_sec
95                         , rd->collected_value
96                         , rd->calculated_value
97                         , rd->last_collected_value
98                         , rd->last_calculated_value
99                         , rd->memsize
100                         , rd->next?",":""
101                         );
102         }
103
104         web_buffer_printf(wb,
105                 "\t\t\t],\n"
106                 "\t\t\t\"memory\" : %lu\n"
107                 "\t\t}"
108                 , memory
109                 );
110
111         pthread_rwlock_unlock(&st->rwlock);
112         return memory;
113 }
114
115 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
116 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
117
118 void rrd_stats_graph_json(RRD_STATS *st, char *options, struct web_buffer *wb)
119 {
120         web_buffer_increase(wb, 16384);
121
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);
125 }
126
127 void rrd_stats_all_json(struct web_buffer *wb)
128 {
129         web_buffer_increase(wb, 1024);
130
131         unsigned long memory = 0;
132         long c;
133         RRD_STATS *st;
134
135         web_buffer_printf(wb, RRD_GRAPH_JSON_HEADER);
136
137         pthread_rwlock_rdlock(&root_rwlock);
138         for(st = root, c = 0; st ; st = st->next) {
139                 if(st->enabled) {
140                         if(c) web_buffer_printf(wb, "%s", ",\n");
141                         memory += rrd_stats_one_json(st, NULL, wb);
142                         c++;
143                 }
144         }
145         pthread_rwlock_unlock(&root_rwlock);
146         
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"
152                 "}\n"
153                 , hostname
154                 , update_every
155                 , save_history
156                 , memory
157                 );
158 }
159
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)
161 {
162         int c;
163         pthread_rwlock_rdlock(&st->rwlock);
164
165
166         // -------------------------------------------------------------------------
167         // switch from JSON to google JSON
168         
169         char kq[2] = "\"";
170         char sq[2] = "\"";
171         switch(type) {
172                 case DATASOURCE_GOOGLE_JSON:
173                 case DATASOURCE_GOOGLE_JSONP:
174                         kq[0] = '\0';
175                         sq[0] = '\'';
176                         break;
177
178                 case DATASOURCE_JSON:
179                 default:
180                         break;
181         }
182
183
184         // -------------------------------------------------------------------------
185         // validate the parameters
186         
187         if(entries_to_show < 1) entries_to_show = 1;
188         if(group < 1) group = 1;
189         
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;
194         
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;
197         
198         time_t time_init = st->last_updated.tv_sec;
199         
200         if(before == 0 || before > time_init) before = time_init;
201         if(after  == 0) after = rrd_stats_first_entry_t(st);
202
203
204         // ---
205
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;                      
209
210
211         // -------------------------------------------------------------------------
212         // find how many dimensions we have
213         
214         int dimensions = 0;
215         RRD_DIMENSION *rd;
216         for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
217         if(!dimensions) {
218                 pthread_rwlock_unlock(&st->rwlock);
219                 web_buffer_printf(wb, "No dimensions yet.");
220                 return 0;
221         }
222
223         
224         // -------------------------------------------------------------------------
225         // prepare various strings, to speed up the loop
226         
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, "}");
233
234
235         // -------------------------------------------------------------------------
236         // checks for debuging
237         
238         if(st->debug) {
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"
240                         , st->id
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)
244                         , after
245                         , before
246                         , before - after
247                         , entries_to_show
248                         , group
249                         , max_entries_init
250                         );
251
252                 if(before < after)
253                         debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
254
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);
257         }
258
259
260         // -------------------------------------------------------------------------
261         // temp arrays for keeping values per dimension
262         
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
268         // initialize them
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;
273         }
274
275         // -------------------------------------------------------------------------
276         // remove dimensions that contain only zeros
277
278         int max_loop = 1;
279         if(only_non_zero) max_loop = 2;
280
281         for(; max_loop ; max_loop--) {
282
283                 // -------------------------------------------------------------------------
284                 // print the JSON header
285                 
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);
290
291                 // print the header for each dimension
292                 // and update the print_hidden array for the dimensions that should be hidden
293                 int pc = 0;
294                 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
295                         if(!print_hidden[c]) {
296                                 pc++;
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);
298                         }
299                 }
300                 if(!pc) {
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);
302                 }
303
304                 // print the begin of row data
305                 web_buffer_printf(wb, "\n       ],\n    %srows%s:\n     [\n", kq, kq);
306
307
308                 // -------------------------------------------------------------------------
309                 // the main loop
310
311                 int annotate_reset = 0;
312                 int annotation_count = 0;
313                 
314                 // to allow grouping on the same values, we need a pad
315                 long pad = before % group;
316
317                 // the minimum line length we expect
318                 int line_size = 4096 + (dimensions * 200);
319
320                 time_t now = time_init;
321                 long max_entries = max_entries_init;
322
323                 long t;
324
325                 long count = 0, printed = 0, group_count = 0;
326                 last_timestamp = 0;
327                 for(t = current_entry; max_entries ; now -= st->update_every, t--, max_entries--) {
328                         if(t < 0) t = st->entries - 1;
329
330                         int print_this = 0;
331
332                         if(st->debug) {
333                                 debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
334                                         , st->id
335                                         , t
336                                         , count + 1
337                                         , group_count + 1
338                                         , printed
339                                         , now
340                                         , (((count + 1 - pad) % group) == 0)?"PRINT":"  -  "
341                                         , (now >= after && now <= before)?"RANGE":"  -  "
342                                         );
343                         }
344
345                         // make sure we return data in the proper time range
346                         if(now < after || now > before) continue;
347
348                         count++;
349                         group_count++;
350
351                         if(((count - pad) % group) == 0) {
352                                 if(printed >= entries_to_show) {
353                                         // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
354                                         break;
355                                 }
356                                 
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++)
360                                                 group_values[c] = 0;
361                                                 
362                                         group_count = 0;
363                                         continue;
364                                 }
365
366                                 // check if we may exceed the buffer provided
367                                 web_buffer_increase(wb, line_size);
368
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;
373
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);
378
379                                 print_this = 1;
380                         }
381
382                         for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
383                                 calculated_number value = unpack_storage_number(rd->values[t]);
384                                 
385                                 switch(group_method) {
386                                         case GROUP_MAX:
387                                                 if(abs(value) > abs(group_values[c])) group_values[c] = value;
388                                                 break;
389
390                                         case GROUP_SUM:
391                                                 group_values[c] += value;
392                                                 break;
393
394                                         default:
395                                         case GROUP_AVERAGE:
396                                                 group_values[c] += value;
397                                                 if(print_this) group_values[c] /= group_count;
398                                                 break;
399                                 }
400
401                                 if(print_this) {
402                                         print_values[c] = group_values[c];
403                                         group_values[c] = 0;
404                                 }
405                         }
406
407                         if(print_this) {
408                                 group_count = 0;
409                                 
410                                 if(annotate_reset) {
411                                         annotation_count++;
412                                         web_buffer_strcpy(wb, overflow_annotation);
413                                         annotate_reset = 0;
414                                 }
415                                 else
416                                         web_buffer_strcpy(wb, normal_annotation);
417
418                                 pc = 0;
419                                 for(c = 0 ; c < dimensions ; c++) {
420                                         if(!print_hidden[c]) {
421                                                 pc++;
422                                                 web_buffer_strcpy(wb, pre_value);
423                                                 web_buffer_rrd_value(wb, print_values[c]);
424                                                 web_buffer_strcpy(wb, post_value);
425
426                                                 if(print_values[c]) found_non_zero[c]++;
427                                         }
428                                 }
429                                 if(!pc) {
430                                         web_buffer_strcpy(wb, pre_value);
431                                         web_buffer_rrd_value(wb, (calculated_number)0);
432                                         web_buffer_strcpy(wb, post_value);
433                                 }
434
435                                 printed++;
436                         }
437                 }
438
439                 if(printed) web_buffer_printf(wb, "]}");
440                 web_buffer_printf(wb, "\n       ]\n}\n");
441
442                 if(only_non_zero && max_loop > 1) {
443                         int changed = 0;
444                         for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
445                                 group_values[c] = 0;
446
447                                 if(!print_hidden[c] && !found_non_zero[c]) {
448                                         changed = 1;
449                                         print_hidden[c] = 1;
450                                 }
451                         }
452
453                         if(changed) web_buffer_reset(wb);
454                         else break;
455                 }
456                 else break;
457                 
458         } // max_loop
459
460         debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->bytes);
461
462         pthread_rwlock_unlock(&st->rwlock);
463         return last_timestamp;
464 }