forked from netdata/netdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rrd2json.c
2077 lines (1691 loc) · 55.2 KB
/
rrd2json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include "log.h"
#include "common.h"
#include "rrd2json.h"
#define HOSTNAME_MAX 1024
char *hostname = "unknown";
void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb)
{
pthread_rwlock_rdlock(&st->rwlock);
buffer_sprintf(wb,
"\t\t{\n"
"\t\t\t\"id\": \"%s\",\n"
"\t\t\t\"name\": \"%s\",\n"
"\t\t\t\"type\": \"%s\",\n"
"\t\t\t\"family\": \"%s\",\n"
"\t\t\t\"context\": \"%s\",\n"
"\t\t\t\"title\": \"%s\",\n"
"\t\t\t\"priority\": %ld,\n"
"\t\t\t\"enabled\": %s,\n"
"\t\t\t\"units\": \"%s\",\n"
"\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
"\t\t\t\"chart_type\": \"%s\",\n"
"\t\t\t\"duration\": %ld,\n"
"\t\t\t\"first_entry\": %ld,\n"
"\t\t\t\"last_entry\": %ld,\n"
"\t\t\t\"update_every\": %d,\n"
"\t\t\t\"dimensions\": {\n"
, st->id
, st->name
, st->type
, st->family
, st->context
, st->title
, st->priority
, st->enabled?"true":"false"
, st->units
, st->name
, rrdset_type_name(st->chart_type)
, st->entries * st->update_every
, rrdset_first_entry_t(st)
, rrdset_last_entry_t(st)
, st->update_every
);
unsigned long memory = st->memsize;
int c = 0;
RRDDIM *rd;
for(rd = st->dimensions; rd ; rd = rd->next) {
if(rd->flags & RRDDIM_FLAG_HIDDEN) continue;
memory += rd->memsize;
buffer_sprintf(wb,
"%s"
"\t\t\t\t\"%s\": { \"name\": \"%s\" }"
, c?",\n":""
, rd->id
, rd->name
);
c++;
}
buffer_sprintf(wb,
"\n\t\t\t}\n"
"\t\t}"
);
pthread_rwlock_unlock(&st->rwlock);
}
void rrd_stats_api_v1_charts(BUFFER *wb)
{
long c;
RRDSET *st;
buffer_sprintf(wb, "{\n"
"\t\"hostname\": \"%s\""
",\n\t\"update_every\": %d"
",\n\t\"history\": %d"
",\n\t\"charts\": {"
, hostname
, rrd_update_every
, rrd_default_history_entries
);
pthread_rwlock_rdlock(&rrdset_root_rwlock);
for(st = rrdset_root, c = 0; st ; st = st->next) {
if(st->enabled && st->dimensions) {
if(c) buffer_strcat(wb, ",");
buffer_strcat(wb, "\n\t\t\"");
buffer_strcat(wb, st->id);
buffer_strcat(wb, "\": ");
rrd_stats_api_v1_chart(st, wb);
c++;
}
}
pthread_rwlock_unlock(&rrdset_root_rwlock);
buffer_strcat(wb, "\n\t}\n}\n");
}
unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
{
time_t now = time(NULL);
pthread_rwlock_rdlock(&st->rwlock);
buffer_sprintf(wb,
"\t\t{\n"
"\t\t\t\"id\": \"%s\",\n"
"\t\t\t\"name\": \"%s\",\n"
"\t\t\t\"type\": \"%s\",\n"
"\t\t\t\"family\": \"%s\",\n"
"\t\t\t\"context\": \"%s\",\n"
"\t\t\t\"title\": \"%s\",\n"
"\t\t\t\"priority\": %ld,\n"
"\t\t\t\"enabled\": %d,\n"
"\t\t\t\"units\": \"%s\",\n"
"\t\t\t\"url\": \"/data/%s/%s\",\n"
"\t\t\t\"chart_type\": \"%s\",\n"
"\t\t\t\"counter\": %lu,\n"
"\t\t\t\"entries\": %ld,\n"
"\t\t\t\"first_entry_t\": %ld,\n"
"\t\t\t\"last_entry\": %lu,\n"
"\t\t\t\"last_entry_t\": %ld,\n"
"\t\t\t\"last_entry_secs_ago\": %ld,\n"
"\t\t\t\"update_every\": %d,\n"
"\t\t\t\"isdetail\": %d,\n"
"\t\t\t\"usec_since_last_update\": %llu,\n"
"\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
"\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
"\t\t\t\"dimensions\": [\n"
, st->id
, st->name
, st->type
, st->family
, st->context
, st->title
, st->priority
, st->enabled
, st->units
, st->name, options?options:""
, rrdset_type_name(st->chart_type)
, st->counter
, st->entries
, rrdset_first_entry_t(st)
, rrdset_last_slot(st)
, rrdset_last_entry_t(st)
, (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
, st->update_every
, st->isdetail
, st->usec_since_last_update
, st->collected_total
, st->last_collected_total
);
unsigned long memory = st->memsize;
RRDDIM *rd;
for(rd = st->dimensions; rd ; rd = rd->next) {
memory += rd->memsize;
buffer_sprintf(wb,
"\t\t\t\t{\n"
"\t\t\t\t\t\"id\": \"%s\",\n"
"\t\t\t\t\t\"name\": \"%s\",\n"
"\t\t\t\t\t\"entries\": %ld,\n"
"\t\t\t\t\t\"isHidden\": %d,\n"
"\t\t\t\t\t\"algorithm\": \"%s\",\n"
"\t\t\t\t\t\"multiplier\": %ld,\n"
"\t\t\t\t\t\"divisor\": %ld,\n"
"\t\t\t\t\t\"last_entry_t\": %ld,\n"
"\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
"\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
"\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
"\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
"\t\t\t\t\t\"memory\": %lu\n"
"\t\t\t\t}%s\n"
, rd->id
, rd->name
, rd->entries
, (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
, rrddim_algorithm_name(rd->algorithm)
, rd->multiplier
, rd->divisor
, rd->last_collected_time.tv_sec
, rd->collected_value
, rd->calculated_value
, rd->last_collected_value
, rd->last_calculated_value
, rd->memsize
, rd->next?",":""
);
}
buffer_sprintf(wb,
"\t\t\t],\n"
"\t\t\t\"memory\" : %lu\n"
"\t\t}"
, memory
);
pthread_rwlock_unlock(&st->rwlock);
return memory;
}
#define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
#define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
{
buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
rrd_stats_one_json(st, options, wb);
buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
}
void rrd_stats_all_json(BUFFER *wb)
{
unsigned long memory = 0;
long c;
RRDSET *st;
buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
pthread_rwlock_rdlock(&rrdset_root_rwlock);
for(st = rrdset_root, c = 0; st ; st = st->next) {
if(st->enabled && st->dimensions) {
if(c) buffer_strcat(wb, ",\n");
memory += rrd_stats_one_json(st, NULL, wb);
c++;
}
}
pthread_rwlock_unlock(&rrdset_root_rwlock);
buffer_sprintf(wb, "\n\t],\n"
"\t\"hostname\": \"%s\",\n"
"\t\"update_every\": %d,\n"
"\t\"history\": %d,\n"
"\t\"memory\": %lu\n"
"}\n"
, hostname
, rrd_update_every
, rrd_default_history_entries
, memory
);
}
// ----------------------------------------------------------------------------
// RRDR dimension options
#define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
#define RRDR_RESET 0x02 // the dimension contains / the value is reset
#define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
#define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
// RRDR result options
#define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
#define RRDR_RESULT_OPTION_RELATIVE 0x00000002
typedef struct rrdresult {
RRDSET *st; // the chart this result refers to
uint32_t result_options; // RRDR_RESULT_OPTION_*
int d; // the number of dimensions
long n; // the number of values in the arrays
long rows; // the number of rows used
uint8_t *od; // the options for the dimensions
time_t *t; // array of n timestamps
calculated_number *v; // array n x d values
uint8_t *o; // array n x d options
long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
long group; // how many collected values were grouped for each row
int update_every; // what is the suggested update frequency in seconds
calculated_number min;
calculated_number max;
time_t before;
time_t after;
int has_st_lock; // if st is read locked by us
} RRDR;
#define rrdr_rows(r) ((r)->rows)
/*
static void rrdr_dump(RRDR *r)
{
long c, i;
RRDDIM *d;
fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
, d->id
, d->name
, (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
, (r->od[c] & RRDR_RESET)?"RESET ":""
, (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
, (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
);
}
if(r->rows <= 0) {
fprintf(stderr, "RRDR does not have any values in it.\n");
return;
}
fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
// for each line in the array
for(i = 0; i < r->rows ;i++) {
calculated_number *cn = &r->v[ i * r->d ];
uint8_t *co = &r->o[ i * r->d ];
// print the id and the timestamp of the line
fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
// for each dimension
for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
if(co[c] & RRDR_EMPTY)
fprintf(stderr, "null ");
else
fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
, cn[c]
, (co[c] & RRDR_EMPTY)?"E":" "
, (co[c] & RRDR_RESET)?"R":" "
, (co[c] & RRDR_HIDDEN)?"H":" "
, (co[c] & RRDR_NONZERO)?"N":" "
);
}
fprintf(stderr, "\n");
}
}
*/
void rrdr_disable_not_selected_dimensions(RRDR *r, const char *dims)
{
char b[strlen(dims) + 1];
char *o = b, *tok;
strcpy(o, dims);
long c;
RRDDIM *d;
// disable all of them
for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
r->od[c] |= RRDR_HIDDEN;
while(o && *o && (tok = mystrsep(&o, ",|"))) {
if(!*tok) continue;
uint32_t hash = simple_hash(tok);
// find it and enable it
for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
if(unlikely((hash == d->hash && !strcmp(d->id, tok)) || !strcmp(d->name, tok))) {
r->od[c] &= ~RRDR_HIDDEN;
// since the user needs this dimension
// make it appear as NONZERO, to return it
// even if the dimension has only zeros
r->od[c] |= RRDR_NONZERO;
}
}
}
}
void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
{
switch(format) {
case DATASOURCE_JSON:
buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
break;
case DATASOURCE_DATATABLE_JSON:
buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
break;
case DATASOURCE_DATATABLE_JSONP:
buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
break;
case DATASOURCE_JSONP:
buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
break;
case DATASOURCE_SSV:
buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
break;
case DATASOURCE_CSV:
buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
break;
case DATASOURCE_TSV:
buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
break;
case DATASOURCE_HTML:
buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
break;
case DATASOURCE_JS_ARRAY:
buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
break;
case DATASOURCE_SSV_COMMA:
buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
break;
default:
buffer_strcat(wb, "unknown");
break;
}
}
uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
{
if(options & RRDR_OPTION_NONZERO) {
long i;
if(dims && *dims) {
// the caller wants specific dimensions
// disable NONZERO option
// to make sure we don't accidentally prevent
// the specific dimensions from being returned
i = 0;
}
else {
// find how many dimensions are not zero
long c;
RRDDIM *rd;
for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
i++;
}
}
// if with nonzero we get i = 0 (no dimensions will be returned)
// disable nonzero to show all dimensions
if(!i) options &= ~RRDR_OPTION_NONZERO;
}
return options;
}
void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
{
long rows = rrdr_rows(r);
long c, i;
RRDDIM *rd;
//info("JSONWRAPPER(): %s: BEGIN", r->st->id);
char kq[2] = "", // key quote
sq[2] = ""; // string quote
if( options & RRDR_OPTION_GOOGLE_JSON ) {
kq[0] = '\0';
sq[0] = '\'';
}
else {
kq[0] = '"';
sq[0] = '"';
}
buffer_sprintf(wb, "{\n"
" %sapi%s: 1,\n"
" %sid%s: %s%s%s,\n"
" %sname%s: %s%s%s,\n"
" %sview_update_every%s: %d,\n"
" %supdate_every%s: %d,\n"
" %sfirst_entry%s: %u,\n"
" %slast_entry%s: %u,\n"
" %sbefore%s: %u,\n"
" %safter%s: %u,\n"
" %sdimension_names%s: ["
, kq, kq
, kq, kq, sq, r->st->id, sq
, kq, kq, sq, r->st->name, sq
, kq, kq, r->update_every
, kq, kq, r->st->update_every
, kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
, kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
, kq, kq, (uint32_t)r->before
, kq, kq, (uint32_t)r->after
, kq, kq);
for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
if(i) buffer_strcat(wb, ", ");
buffer_strcat(wb, sq);
buffer_strcat(wb, rd->name);
buffer_strcat(wb, sq);
i++;
}
if(!i) {
#ifdef NETDATA_INTERNAL_CHECKS
error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
#endif
rows = 0;
buffer_strcat(wb, sq);
buffer_strcat(wb, "no data");
buffer_strcat(wb, sq);
}
buffer_sprintf(wb, "],\n"
" %sdimension_ids%s: ["
, kq, kq);
for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
if(i) buffer_strcat(wb, ", ");
buffer_strcat(wb, sq);
buffer_strcat(wb, rd->id);
buffer_strcat(wb, sq);
i++;
}
if(!i) {
rows = 0;
buffer_strcat(wb, sq);
buffer_strcat(wb, "no data");
buffer_strcat(wb, sq);
}
buffer_sprintf(wb, "],\n"
" %slatest_values%s: ["
, kq, kq);
for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
if(i) buffer_strcat(wb, ", ");
i++;
storage_number n = rd->values[rrdset_last_slot(r->st)];
if(!does_storage_number_exist(n))
buffer_strcat(wb, "null");
else
buffer_rrd_value(wb, unpack_storage_number(n));
}
if(!i) {
rows = 0;
buffer_strcat(wb, "null");
}
buffer_sprintf(wb, "],\n"
" %sview_latest_values%s: ["
, kq, kq);
i = 0;
if(rows) {
for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
if(i) buffer_strcat(wb, ", ");
i++;
calculated_number *cn = &r->v[ (0) * r->d ];
uint8_t *co = &r->o[ (0) * r->d ];
if(co[c] & RRDR_EMPTY)
buffer_strcat(wb, "null");
else
buffer_rrd_value(wb, cn[c]);
}
}
if(!i) {
rows = 0;
buffer_strcat(wb, "null");
}
buffer_sprintf(wb, "],\n"
" %sdimensions%s: %ld,\n"
" %spoints%s: %ld,\n"
" %sformat%s: %s"
, kq, kq, i
, kq, kq, rows
, kq, kq, sq
);
rrdr_buffer_print_format(wb, format);
buffer_sprintf(wb, "%s,\n"
" %sresult%s: "
, sq
, kq, kq
);
if(string_value) buffer_strcat(wb, sq);
//info("JSONWRAPPER(): %s: END", r->st->id);
}
void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
{
(void)format;
char kq[2] = "", // key quote
sq[2] = ""; // string quote
if( options & RRDR_OPTION_GOOGLE_JSON ) {
kq[0] = '\0';
sq[0] = '\'';
}
else {
kq[0] = '"';
sq[0] = '"';
}
if(string_value) buffer_strcat(wb, sq);
buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
buffer_rrd_value(wb, r->min);
buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
buffer_rrd_value(wb, r->max);
buffer_strcat(wb, "\n}\n");
}
#define JSON_DATES_JS 1
#define JSON_DATES_TIMESTAMP 2
static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
{
//info("RRD2JSON(): %s: BEGIN", r->st->id);
int row_annotations = 0, dates, dates_with_new = 0;
char kq[2] = "", // key quote
sq[2] = "", // string quote
pre_label[101] = "", // before each label
post_label[101] = "", // after each label
pre_date[101] = "", // the beginning of line, to the date
post_date[101] = "", // closing the date
pre_value[101] = "", // before each value
post_value[101] = "", // after each value
post_line[101] = "", // at the end of each row
normal_annotation[201] = "", // default row annotation
overflow_annotation[201] = "", // overflow row annotation
data_begin[101] = "", // between labels and values
finish[101] = ""; // at the end of everything
if(datatable) {
dates = JSON_DATES_JS;
if( options & RRDR_OPTION_GOOGLE_JSON ) {
kq[0] = '\0';
sq[0] = '\'';
}
else {
kq[0] = '"';
sq[0] = '"';
}
row_annotations = 1;
snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
snprintfz(post_date, 100, "%s}", sq);
snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
strcpy(post_value, "}");
strcpy(post_line, "]}");
snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
strcpy(finish, "\n ]\n}");
snprintfz(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);
snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
buffer_sprintf(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);
buffer_sprintf(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);
buffer_sprintf(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);
// remove the valueobjects flag
// google wants its own keys
if(options & RRDR_OPTION_OBJECTSROWS)
options &= ~RRDR_OPTION_OBJECTSROWS;
}
else {
kq[0] = '"';
sq[0] = '"';
if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
dates = JSON_DATES_TIMESTAMP;
dates_with_new = 0;
}
else {
dates = JSON_DATES_JS;
dates_with_new = 1;
}
if( options & RRDR_OPTION_OBJECTSROWS )
strcpy(pre_date, " { ");
else
strcpy(pre_date, " [ ");
strcpy(pre_label, ", \"");
strcpy(post_label, "\"");
strcpy(pre_value, ", ");
if( options & RRDR_OPTION_OBJECTSROWS )
strcpy(post_line, "}");
else
strcpy(post_line, "]");
snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
strcpy(finish, "\n ]\n}");
buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
buffer_sprintf(wb, "%stime%s", sq, sq);
}
// -------------------------------------------------------------------------
// print the JSON header
long c, i;
RRDDIM *rd;
// print the header lines
for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
buffer_strcat(wb, pre_label);
buffer_strcat(wb, rd->name);
buffer_strcat(wb, post_label);
i++;
}
if(!i) {
buffer_strcat(wb, pre_label);
buffer_strcat(wb, "no data");
buffer_strcat(wb, post_label);
}
// print the begin of row data
buffer_strcat(wb, data_begin);
// if all dimensions are hidden, print a null
if(!i) {
buffer_strcat(wb, finish);
return;
}
long start = 0, end = rrdr_rows(r), step = 1;
if((options & RRDR_OPTION_REVERSED)) {
start = rrdr_rows(r) - 1;
end = -1;
step = -1;
}
// for each line in the array
calculated_number total = 1;
for(i = start; i != end ;i += step) {
calculated_number *cn = &r->v[ i * r->d ];
uint8_t *co = &r->o[ i * r->d ];
time_t now = r->t[i];
if(dates == JSON_DATES_JS) {
// generate the local date time
struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
if(!tm) { error("localtime_r() failed."); continue; }
if(likely(i != start)) buffer_strcat(wb, ",\n");
buffer_strcat(wb, pre_date);
if( options & RRDR_OPTION_OBJECTSROWS )
buffer_sprintf(wb, "%stime%s: ", kq, kq);
if(dates_with_new)
buffer_strcat(wb, "new ");
buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
buffer_strcat(wb, post_date);
if(row_annotations) {
// google supports one annotation per row
int annotation_found = 0;
for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
if(co[c] & RRDR_RESET) {
buffer_strcat(wb, overflow_annotation);
annotation_found = 1;
break;
}
}
if(!annotation_found)
buffer_strcat(wb, normal_annotation);
}
}
else {
// print the timestamp of the line
if(likely(i != start)) buffer_strcat(wb, ",\n");
buffer_strcat(wb, pre_date);
if( options & RRDR_OPTION_OBJECTSROWS )
buffer_sprintf(wb, "%stime%s: ", kq, kq);
buffer_rrd_value(wb, (calculated_number)r->t[i]);
// in ms
if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
buffer_strcat(wb, post_date);
}
if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
total = 0;
for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
calculated_number n = cn[c];
if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
n = -n;
total += n;
}
// prevent a division by zero
if(total == 0) total = 1;
}
// for each dimension
for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
calculated_number n = cn[c];
buffer_strcat(wb, pre_value);
if( options & RRDR_OPTION_OBJECTSROWS )
buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
if(co[c] & RRDR_EMPTY) {
if(options & RRDR_OPTION_NULL2ZERO)
buffer_strcat(wb, "0");
else
buffer_strcat(wb, "null");
}
else {
if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
n = -n;
if(unlikely(options & RRDR_OPTION_PERCENTAGE))
n = n * 100 / total;
buffer_rrd_value(wb, n);
}
buffer_strcat(wb, post_value);
}
buffer_strcat(wb, post_line);
}
buffer_strcat(wb, finish);
//info("RRD2JSON(): %s: END", r->st->id);
}
static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
{
//info("RRD2CSV(): %s: BEGIN", r->st->id);
long c, i;
RRDDIM *d;
// print the csv header
for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
if(!i) {
buffer_strcat(wb, startline);
if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
buffer_strcat(wb, "time");
if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
}
buffer_strcat(wb, separator);
if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
buffer_strcat(wb, d->name);
if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
i++;
}
buffer_strcat(wb, endline);
if(!i) {
// no dimensions present
return;
}
long start = 0, end = rrdr_rows(r), step = 1;
if((options & RRDR_OPTION_REVERSED)) {
start = rrdr_rows(r) - 1;
end = -1;
step = -1;
}
// for each line in the array
calculated_number total = 1;
for(i = start; i != end ;i += step) {
calculated_number *cn = &r->v[ i * r->d ];
uint8_t *co = &r->o[ i * r->d ];
buffer_strcat(wb, betweenlines);
buffer_strcat(wb, startline);
time_t now = r->t[i];
if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
// print the timestamp of the line
buffer_rrd_value(wb, (calculated_number)now);
// in ms
if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
}
else {
// generate the local date time
struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
if(!tm) { error("localtime() failed."); continue; }
buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
}
if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
total = 0;
for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
calculated_number n = cn[c];
if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
n = -n;
total += n;
}
// prevent a division by zero
if(total == 0) total = 1;
}
// for each dimension
for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
buffer_strcat(wb, separator);
calculated_number n = cn[c];
if(co[c] & RRDR_EMPTY) {
if(options & RRDR_OPTION_NULL2ZERO)
buffer_strcat(wb, "0");
else
buffer_strcat(wb, "null");
}
else {
if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
n = -n;
if(unlikely(options & RRDR_OPTION_PERCENTAGE))
n = n * 100 / total;
buffer_rrd_value(wb, n);
}
}
buffer_strcat(wb, endline);
}
//info("RRD2CSV(): %s: END", r->st->id);
}
inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
long c;
RRDDIM *d;
calculated_number *cn = &r->v[ i * r->d ];
uint8_t *co = &r->o[ i * r->d ];
calculated_number sum = 0, min = 0, max = 0, v;
int all_null = 1, init = 1;
calculated_number total = 1;
if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {