]> arthur.barton.de Git - netdata.git/blob - src/rrd2json.c
moved web_client functions to web_client.c; added dictionary.c to support indexed...
[netdata.git] / src / rrd2json.c
1 #include <pthread.h>
2 #include <sys/time.h>
3 #include <stdlib.h>
4
5 #include "log.h"
6 #include "common.h"
7 #include "rrd2json.h"
8
9 #define HOSTNAME_MAX 1024
10 char *hostname = "unknown";
11
12 unsigned long rrd_stats_one_json(RRDSET *st, char *options, struct web_buffer *wb)
13 {
14         time_t now = time(NULL);
15         web_buffer_increase(wb, 16384);
16
17         pthread_rwlock_rdlock(&st->rwlock);
18
19         web_buffer_printf(wb,
20                 "\t\t{\n"
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"
43                 , st->id
44                 , st->name
45                 , st->type
46                 , st->family
47                 , st->title
48                 , st->priority
49                 , st->enabled
50                 , st->units
51                 , st->name, options?options:""
52                 , rrdset_type_name(st->chart_type)
53                 , st->counter
54                 , st->entries
55                 , rrdset_first_entry_t(st)
56                 , st->current_entry
57                 , st->last_updated.tv_sec
58                 , now - (st->last_updated.tv_sec > now) ? now : st->last_updated.tv_sec
59                 , st->update_every
60                 , st->isdetail
61                 , st->usec_since_last_update
62                 , st->collected_total
63                 , st->last_collected_total
64                 );
65
66         unsigned long memory = st->memsize;
67
68         RRDDIM *rd;
69         for(rd = st->dimensions; rd ; rd = rd->next) {
70                 memory += rd->memsize;
71
72                 web_buffer_printf(wb,
73                         "\t\t\t\t{\n"
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"
87                         "\t\t\t\t}%s\n"
88                         , rd->id
89                         , rd->name
90                         , rd->entries
91                         , rd->hidden
92                         , rrddim_algorithm_name(rd->algorithm)
93                         , rd->multiplier
94                         , rd->divisor
95                         , rd->last_collected_time.tv_sec
96                         , rd->collected_value
97                         , rd->calculated_value
98                         , rd->last_collected_value
99                         , rd->last_calculated_value
100                         , rd->memsize
101                         , rd->next?",":""
102                         );
103         }
104
105         web_buffer_printf(wb,
106                 "\t\t\t],\n"
107                 "\t\t\t\"memory\" : %lu\n"
108                 "\t\t}"
109                 , memory
110                 );
111
112         pthread_rwlock_unlock(&st->rwlock);
113         return memory;
114 }
115
116 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
117 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
118
119 void rrd_stats_graph_json(RRDSET *st, char *options, struct web_buffer *wb)
120 {
121         web_buffer_increase(wb, 16384);
122
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);
126 }
127
128 void rrd_stats_all_json(struct web_buffer *wb)
129 {
130         web_buffer_increase(wb, 1024);
131
132         unsigned long memory = 0;
133         long c;
134         RRDSET *st;
135
136         web_buffer_printf(wb, RRD_GRAPH_JSON_HEADER);
137
138         pthread_rwlock_rdlock(&rrdset_root_rwlock);
139         for(st = rrdset_root, c = 0; st ; st = st->next) {
140                 if(st->enabled) {
141                         if(c) web_buffer_printf(wb, "%s", ",\n");
142                         memory += rrd_stats_one_json(st, NULL, wb);
143                         c++;
144                 }
145         }
146         pthread_rwlock_unlock(&rrdset_root_rwlock);
147         
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"
153                 "}\n"
154                 , hostname
155                 , rrd_update_every
156                 , rrd_default_history_entries
157                 , memory
158                 );
159 }
160
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)
162 {
163         int c;
164         pthread_rwlock_rdlock(&st->rwlock);
165
166
167         // -------------------------------------------------------------------------
168         // switch from JSON to google JSON
169         
170         char kq[2] = "\"";
171         char sq[2] = "\"";
172         switch(type) {
173                 case DATASOURCE_GOOGLE_JSON:
174                 case DATASOURCE_GOOGLE_JSONP:
175                         kq[0] = '\0';
176                         sq[0] = '\'';
177                         break;
178
179                 case DATASOURCE_JSON:
180                 default:
181                         break;
182         }
183
184
185         // -------------------------------------------------------------------------
186         // validate the parameters
187         
188         if(entries_to_show < 1) entries_to_show = 1;
189         if(group < 1) group = 1;
190         
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;
195         
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;
198         
199         time_t time_init = st->last_updated.tv_sec;
200         
201         if(before == 0 || before > time_init) before = time_init;
202         if(after  == 0) after = rrdset_first_entry_t(st);
203
204
205         // ---
206
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;                      
210
211
212         // -------------------------------------------------------------------------
213         // find how many dimensions we have
214         
215         int dimensions = 0;
216         RRDDIM *rd;
217         for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
218         if(!dimensions) {
219                 pthread_rwlock_unlock(&st->rwlock);
220                 web_buffer_printf(wb, "No dimensions yet.");
221                 return 0;
222         }
223
224         
225         // -------------------------------------------------------------------------
226         // prepare various strings, to speed up the loop
227         
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, "}");
234
235
236         // -------------------------------------------------------------------------
237         // checks for debuging
238         
239         if(st->debug) {
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"
241                         , st->id
242                         , rrdset_first_entry_t(st)
243                         , st->last_updated.tv_sec
244                         , st->last_updated.tv_sec - rrdset_first_entry_t(st)
245                         , after
246                         , before
247                         , before - after
248                         , entries_to_show
249                         , group
250                         , max_entries_init
251                         );
252
253                 if(before < after)
254                         debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
255
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);
258         }
259
260
261         // -------------------------------------------------------------------------
262         // temp arrays for keeping values per dimension
263         
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];
269
270         // initialize them
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;
276         }
277
278         // -------------------------------------------------------------------------
279         // remove dimensions that contain only zeros
280
281         int max_loop = 1;
282         if(only_non_zero) max_loop = 2;
283
284         for(; max_loop ; max_loop--) {
285
286                 // -------------------------------------------------------------------------
287                 // print the JSON header
288                 
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);
293
294                 // print the header for each dimension
295                 // and update the print_hidden array for the dimensions that should be hidden
296                 int pc = 0;
297                 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
298                         if(!print_hidden[c]) {
299                                 pc++;
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);
301                         }
302                 }
303                 if(!pc) {
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);
305                 }
306
307                 // print the begin of row data
308                 web_buffer_printf(wb, "\n       ],\n    %srows%s:\n     [\n", kq, kq);
309
310
311                 // -------------------------------------------------------------------------
312                 // the main loop
313
314                 int annotate_reset = 0;
315                 int annotation_count = 0;
316                 
317                 // to allow grouping on the same values, we need a pad
318                 long pad = before % group;
319
320                 // the minimum line length we expect
321                 int line_size = 4096 + (dimensions * 200);
322
323                 time_t now = time_init;
324                 long max_entries = max_entries_init;
325
326                 long t;
327
328                 long count = 0, printed = 0, group_count = 0;
329                 last_timestamp = 0;
330                 for(t = current_entry; max_entries ; now -= st->update_every, t--, max_entries--) {
331                         if(t < 0) t = st->entries - 1;
332
333                         int print_this = 0;
334
335                         if(st->debug) {
336                                 debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
337                                         , st->id
338                                         , t
339                                         , count + 1
340                                         , group_count + 1
341                                         , printed
342                                         , now
343                                         , (((count + 1 - pad) % group) == 0)?"PRINT":"  -  "
344                                         , (now >= after && now <= before)?"RANGE":"  -  "
345                                         );
346                         }
347
348                         // make sure we return data in the proper time range
349                         if(now < after || now > before) continue;
350
351                         count++;
352                         group_count++;
353
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.");
358                                         break;
359                                 }
360                                 
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++) {
364                                                 group_values[c] = 0;
365                                                 found_non_existing[c] = 0;
366                                         }
367                                                 
368                                         group_count = 0;
369                                         continue;
370                                 }
371
372                                 // check if we may exceed the buffer provided
373                                 web_buffer_increase(wb, line_size);
374
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;
379
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);
384
385                                 print_this = 1;
386                         }
387
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);
392                                 
393                                 if(!does_storage_number_exist(n)) {
394                                         value = 0.0;
395                                         found_non_existing[c]++;
396                                 }
397                                 if(did_storage_number_reset(n)) annotate_reset = 1;
398
399                                 switch(group_method) {
400                                         case GROUP_MAX:
401                                                 if(abs(value) > abs(group_values[c])) group_values[c] = value;
402                                                 break;
403
404                                         case GROUP_SUM:
405                                                 group_values[c] += value;
406                                                 break;
407
408                                         default:
409                                         case GROUP_AVERAGE:
410                                                 group_values[c] += value;
411                                                 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
412                                                 break;
413                                 }
414                         }
415
416                         if(print_this) {
417                                 if(annotate_reset) {
418                                         annotation_count++;
419                                         web_buffer_strcpy(wb, overflow_annotation);
420                                         annotate_reset = 0;
421                                 }
422                                 else
423                                         web_buffer_strcpy(wb, normal_annotation);
424
425                                 pc = 0;
426                                 for(c = 0 ; c < dimensions ; c++) {
427                                         if(found_non_existing[c] == group_count) {
428                                                 // all entries are non-existing
429                                                 pc++;
430                                                 web_buffer_strcpy(wb, pre_value);
431                                                 web_buffer_strcpy(wb, "null");
432                                                 web_buffer_strcpy(wb, post_value);
433                                         }
434                                         else if(!print_hidden[c]) {
435                                                 pc++;
436                                                 web_buffer_strcpy(wb, pre_value);
437                                                 web_buffer_rrd_value(wb, group_values[c]);
438                                                 web_buffer_strcpy(wb, post_value);
439
440                                                 if(group_values[c]) found_non_zero[c]++;
441                                         }
442
443                                         // reset them for the next loop
444                                         group_values[c] = 0;
445                                         found_non_existing[c] = 0;
446                                 }
447
448                                 // if all dimensions are hidden, print a null
449                                 if(!pc) {
450                                         web_buffer_strcpy(wb, pre_value);
451                                         web_buffer_strcpy(wb, "null");
452                                         web_buffer_strcpy(wb, post_value);
453                                 }
454
455                                 printed++;
456                                 group_count = 0;
457                         }
458                 }
459
460                 if(printed) web_buffer_printf(wb, "]}");
461                 web_buffer_printf(wb, "\n       ]\n}\n");
462
463                 if(only_non_zero && max_loop > 1) {
464                         int changed = 0;
465                         for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
466                                 group_values[c] = 0;
467                                 found_non_existing[c] = 0;
468
469                                 if(!print_hidden[c] && !found_non_zero[c]) {
470                                         changed = 1;
471                                         print_hidden[c] = 1;
472                                 }
473                         }
474
475                         if(changed) web_buffer_reset(wb);
476                         else break;
477                 }
478                 else break;
479                 
480         } // max_loop
481
482         debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->bytes);
483
484         pthread_rwlock_unlock(&st->rwlock);
485         return last_timestamp;
486 }
487
488
489 /*
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) {
491
492         if(!st->dimensions) {
493                 web_buffer_printf(wb, "No dimensions yet.");
494                 return 0;
495         }
496
497         int c;
498         pthread_rwlock_rdlock(&st->rwlock);
499
500         // -------------------------------------------------------------------------
501         // validate the parameters
502
503         if(entries_to_show < 1) entries_to_show = 1;
504         if(group < 1) group = 1;
505
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;
510
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;
514
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);
517
518         if(entries_to_show > (before - after) / st->update_every / group) entries_to_show = (before - after) / st->update_every / group;
519
520         // -------------------------------------------------------------------------
521         // checks for debuging
522
523         if(st->debug) {
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"
525                         , st->id
526                         , rrdset_first_entry_t(st)
527                         , st->last_updated.tv_sec
528                         , st->last_updated.tv_sec - rrdset_first_entry_t(st)
529                         , after
530                         , before
531                         , before - after
532                         , entries_to_show
533                         , group
534                         , max_entries
535                         );
536
537                 if(before < after)
538                         debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
539
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);
542         }
543
544         // -------------------------------------------------------------------------
545
546         int dimensions = 0, i, j;
547         RRDDIM *rd;
548         for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
549
550         uint32_t *annotations[dimensions];
551         calculated_number *values[dimensions];
552         int enabled[dimensions];
553
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);
557
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);
560
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;
563                 else enabled[i] = 1;
564         }
565
566         switch(type) {
567                 case DATASOURCE_GOOGLE_JSON:
568                 case DATASOURCE_GOOGLE_JSONP:
569                         //rrdvalues2googlejson(st, rd, values, annotations, enabled, entries_to_show, );
570                         break;
571
572                 case DATASOURCE_JSON:
573                 default:
574                         //rrdvalues2json();
575                         break;
576         }
577
578         //return ???;
579 }
580 */