1 /****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
3 ****************************************************************************
4 * rrd_xport.c export RRD data
5 ****************************************************************************/
7 #include <sys/stat.h>
8 #include <locale.h>
10 #include "rrd_tool.h"
11 #include "rrd_graph.h"
12 #include "rrd_xport.h"
13 #include "unused.h"
14 #include "rrd_client.h"
16 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
17 #include <io.h>
18 #include <fcntl.h>
19 #endif
21 int rrd_xport(
22 int,
23 char **,
24 int *,
25 time_t *,
26 time_t *,
27 unsigned long *,
28 unsigned long *,
29 char ***,
30 rrd_value_t **);
32 int rrd_xport_fn(
33 image_desc_t *,
34 time_t *,
35 time_t *,
36 unsigned long *,
37 unsigned long *,
38 char ***,
39 rrd_value_t **,
40 int);
42 /* helper function for buffer handling */
43 typedef struct stringbuffer_t {
44 size_t allocated;
45 size_t len;
46 unsigned char* data;
47 FILE *file;
48 } stringbuffer_t;
49 int addToBuffer(stringbuffer_t *,char*,size_t);
50 void escapeJSON(char*,size_t);
52 int rrd_graph_xport(image_desc_t *);
53 int rrd_xport_format_xmljson(int,stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*);
54 int rrd_xport_format_sv(char,stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*);
55 int rrd_xport_format_addprints(int,stringbuffer_t *,image_desc_t *);
57 int rrd_xport(
58 int argc,
59 char **argv,
60 int UNUSED(*xsize),
61 time_t *start,
62 time_t *end, /* which time frame do you want ?
63 * will be changed to represent reality */
64 unsigned long *step, /* which stepsize do you want?
65 * will be changed to represent reality */
66 unsigned long *col_cnt, /* number of data columns in the result */
67 char ***legend_v, /* legend entries */
68 rrd_value_t **data)
69 { /* two dimensional array containing the data */
70 image_desc_t im;
71 time_t start_tmp = 0, end_tmp = 0;
72 rrd_time_value_t start_tv, end_tv;
73 char *parsetime_error = NULL;
75 struct option long_options[] = {
76 {"start", required_argument, 0, 's'},
77 {"end", required_argument, 0, 'e'},
78 {"maxrows", required_argument, 0, 'm'},
79 {"step", required_argument, 0, 261},
80 {"enumds", no_argument, 0, 262}, /* these are handled in the frontend ... */
81 {"json", no_argument, 0, 263}, /* these are handled in the frontend ... */
82 {"daemon", required_argument, 0, 'd'},
83 {0, 0, 0, 0}
84 };
86 optind = 0;
87 opterr = 0; /* initialize getopt */
89 rrd_graph_init(&im);
91 rrd_parsetime("end-24h", &start_tv);
92 rrd_parsetime("now", &end_tv);
94 int enumds=0;
95 int json=0;
97 while (1) {
98 int option_index = 0;
99 int opt;
101 opt = getopt_long(argc, argv, "s:e:m:d:", long_options, &option_index);
103 if (opt == EOF)
104 break;
106 switch (opt) {
107 case 261:
108 im.step = atoi(optarg);
109 break;
110 case 262:
111 enumds=1;
112 break;
113 case 263:
114 json=1;
115 break;
116 case 's':
117 if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
118 rrd_set_error("start time: %s", parsetime_error);
119 return -1;
120 }
121 break;
122 case 'e':
123 if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
124 rrd_set_error("end time: %s", parsetime_error);
125 return -1;
126 }
127 break;
128 case 'm':
129 im.xsize = atol(optarg);
130 if (im.xsize < 10) {
131 rrd_set_error("maxrows below 10 rows");
132 return -1;
133 }
134 break;
135 case 'd':
136 {
137 if (im.daemon_addr != NULL)
138 {
139 rrd_set_error ("You cannot specify --daemon "
140 "more than once.");
141 return (-1);
142 }
144 im.daemon_addr = strdup(optarg);
145 if (im.daemon_addr == NULL)
146 {
147 rrd_set_error("strdup error");
148 return -1;
149 }
150 break;
151 }
153 case '?':
154 rrd_set_error("unknown option '%s'", argv[optind - 1]);
155 return -1;
156 }
157 }
159 if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
160 return -1;
161 }
163 if (start_tmp < 3600 * 24 * 365 * 10) {
164 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",
165 start_tmp);
166 return -1;
167 }
169 if (end_tmp < start_tmp) {
170 rrd_set_error("start (%ld) should be less than end (%ld)",
171 start_tmp, end_tmp);
172 return -1;
173 }
175 im.start = start_tmp;
176 im.end = end_tmp;
177 im.step = max((long) im.step, (im.end - im.start) / im.xsize);
179 rrd_graph_script(argc, argv, &im, 0);
180 if (rrd_test_error()) {
181 im_free(&im);
182 return -1;
183 }
185 if (im.gdes_c == 0) {
186 rrd_set_error("can't make an xport without contents");
187 im_free(&im);
188 return (-1);
189 }
191 { /* try to connect to rrdcached */
192 int status = rrdc_connect(im.daemon_addr);
193 if (status != 0) return status;
194 }
196 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data,0) == -1) {
197 im_free(&im);
198 return -1;
199 }
201 /* and create the export */
202 if (!xsize) {
203 int flags=0;
204 if (json) { flags|=1; }
205 if (enumds) { flags|=4; }
206 stringbuffer_t buffer={0,0,NULL,stdout};
207 rrd_xport_format_xmljson(flags,&buffer,&im,
208 *start, *end, *step,
209 *col_cnt, *legend_v,
210 *data);
211 }
213 im_free(&im);
214 return 0;
215 }
219 int rrd_xport_fn(
220 image_desc_t *im,
221 time_t *start,
222 time_t *end, /* which time frame do you want ?
223 * will be changed to represent reality */
224 unsigned long *step, /* which stepsize do you want?
225 * will be changed to represent reality */
226 unsigned long *col_cnt, /* number of data columns in the result */
227 char ***legend_v, /* legend entries */
228 rrd_value_t **data,
229 int dolines)
230 { /* two dimensional array containing the data */
232 int i = 0, j = 0;
233 unsigned long dst_row, row_cnt;
234 rrd_value_t *dstptr;
236 unsigned long xport_counter = 0;
237 int *ref_list;
238 long *step_list;
239 long *step_list_ptr;
240 char **legend_list;
243 /* pull the data from the rrd files ... */
244 if (data_fetch(im) == -1)
245 return -1;
247 /* evaluate CDEF operations ... */
248 if (data_calc(im) == -1)
249 return -1;
251 /* how many xports or lines/AREA/STACK ? */
252 *col_cnt = 0;
253 for (i = 0; i < im->gdes_c; i++) {
254 switch (im->gdes[i].gf) {
255 case GF_LINE:
256 case GF_AREA:
257 case GF_STACK:
258 (*col_cnt)+=dolines;
259 break;
260 case GF_XPORT:
261 (*col_cnt)++;
262 break;
263 default:
264 break;
265 }
266 }
267 if ((*col_cnt) == 0) {
268 rrd_set_error("no XPORT found, nothing to do");
269 return -1;
270 }
272 /* a list of referenced gdes */
273 ref_list = (int*)malloc(sizeof(int) * (*col_cnt));
274 if (ref_list == NULL)
275 return -1;
277 /* a list to save pointers to the column's legend entry */
278 /* this is a return value! */
279 legend_list = (char**)malloc(sizeof(char *) * (*col_cnt));
280 if (legend_list == NULL) {
281 free(ref_list);
282 return -1;
283 }
285 /* lets find the step size we have to use for xport */
286 step_list = (long*)malloc(sizeof(long)*((*col_cnt)+1));
287 step_list_ptr = step_list;
288 j = 0;
289 for (i = 0; i < im->gdes_c; i++) {
290 /* decide if we need to handle the output */
291 int handle=0;
292 switch (im->gdes[i].gf) {
293 case GF_LINE:
294 case GF_AREA:
295 case GF_STACK:
296 handle=dolines;
297 break;
298 case GF_XPORT:
299 handle=1;
300 break;
301 default:
302 handle=0;
303 break;
304 }
305 /* and now do the real work */
306 if (handle) {
307 ref_list[xport_counter++] = i;
308 *step_list_ptr = im->gdes[im->gdes[i].vidx].step;
309 /* printf("%s:%lu\n",im->gdes[i].legend,*step_list_ptr); */
310 step_list_ptr++;
311 /* reserve room for one legend entry */
312 /* is FMT_LEG_LEN + 5 the correct size? */
313 if ((legend_list[j] =
314 (char*)malloc(sizeof(char) * (FMT_LEG_LEN + 5))) == NULL) {
315 free(ref_list);
316 *data = NULL;
317 while (--j > -1)
318 free(legend_list[j]);
319 free(legend_list);
320 free(step_list);
321 rrd_set_error("malloc xport legend entry");
322 return (-1);
323 }
325 if (im->gdes[i].legend)
326 /* omit bounds check, should have the same size */
327 strcpy(legend_list[j++], im->gdes[i].legend);
328 else
329 legend_list[j++][0] = '\0';
330 }
331 }
332 *step_list_ptr=0;
333 /* find a common step */
334 *step = lcd(step_list);
335 /* printf("step: %lu\n",*step); */
336 free(step_list);
338 *start = im->start - im->start % (*step);
339 *end = im->end - im->end % (*step) + (*step);
342 /* room for rearranged data */
343 /* this is a return value! */
344 row_cnt = ((*end) - (*start)) / (*step);
345 if (((*data) =
346 (rrd_value_t*)malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
347 free(ref_list);
348 free(legend_list);
349 rrd_set_error("malloc xport data area");
350 return (-1);
351 }
352 dstptr = (*data);
354 /* fill data structure */
355 for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
356 for (i = 0; i < (int) (*col_cnt); i++) {
357 long vidx = im->gdes[ref_list[i]].vidx;
358 time_t now = *start + dst_row * *step;
359 (*dstptr++) = im->gdes[vidx].data[(unsigned long)
360 floor((double)
361 (now - im->gdes[vidx].start)
362 /im->gdes[vidx].step)
363 * im->gdes[vidx].ds_cnt +
364 im->gdes[vidx].ds];
366 }
367 }
369 *legend_v = legend_list;
370 free(ref_list);
371 return 0;
373 }
375 int rrd_graph_xport(image_desc_t *im) {
376 /* prepare the data for processing */
377 unsigned long col_cnt=0;
378 time_t start=im->start;
379 time_t end=im->end;
380 unsigned long step=im->step;
381 char **legend_v=NULL;
382 rrd_value_t *data=NULL;
383 /* initialize buffer */
384 stringbuffer_t buffer={0,0,NULL,NULL};
386 /* check if we have a supported ggraph format */
387 switch (im->graph_type) {
388 /* allow the following to pass */
389 case GTYPE_TIME:
390 case GTYPE_XY:
391 break;
392 default:
393 rrd_set_error("Not supported graph type");
394 return -1;
395 }
397 /* if we write a file, then open it */
398 if (strlen(im->graphfile)) {
399 buffer.file=fopen(im->graphfile,"w");
400 }
402 /* do the data processing */
403 if (rrd_xport_fn(im,&start,&end,&step,&col_cnt,&legend_v,&data,1)) { return -1;}
405 /* fill in some data */
406 rrd_infoval_t info;
407 info.u_cnt = start;
408 grinfo_push(im, sprintf_alloc("graph_start"), RD_I_CNT, info);
409 info.u_cnt = end;
410 grinfo_push(im, sprintf_alloc("graph_end"), RD_I_CNT, info);
411 info.u_cnt = step;
412 grinfo_push(im, sprintf_alloc("graph_step"), RD_I_CNT, info);
414 /* set locale */
415 char *old_locale = setlocale(LC_NUMERIC,NULL);
416 setlocale(LC_NUMERIC, "C");
418 /* format it for output */
419 int r=0;
420 switch(im->imgformat) {
421 case IF_XML:
422 r=rrd_xport_format_xmljson(2,&buffer,im, start, end, step, col_cnt, legend_v, data);
423 break;
424 case IF_XMLENUM:
425 r=rrd_xport_format_xmljson(6,&buffer,im, start, end, step, col_cnt, legend_v, data);
426 break;
427 case IF_JSON:
428 r=rrd_xport_format_xmljson(1,&buffer,im, start, end, step, col_cnt, legend_v, data);
429 break;
430 case IF_JSONTIME:
431 r=rrd_xport_format_xmljson(3,&buffer,im, start, end, step, col_cnt, legend_v, data);
432 break;
433 case IF_CSV:
434 r=rrd_xport_format_sv(',',&buffer,im, start, end, step, col_cnt, legend_v, data);
435 break;
436 case IF_TSV:
437 r=rrd_xport_format_sv('\t',&buffer,im, start, end, step, col_cnt, legend_v, data);
438 break;
439 case IF_SSV:
440 r=rrd_xport_format_sv(';',&buffer,im, start, end, step, col_cnt, legend_v, data);
441 break;
442 default:
443 break;
444 }
445 /* restore locale */
446 setlocale(LC_NUMERIC, old_locale);
447 /* handle errors */
448 if (r) {
449 /* free legend */
450 for (unsigned long j = 0; j < col_cnt; j++) {
451 free(legend_v[j]);
452 }
453 free(legend_v);
454 /* free data */
455 free(data);
456 /* free the bufer */
457 if (buffer.data) {free(buffer.data);}
458 /* close the file */
459 if (buffer.file) {fclose(buffer.file);}
460 /* and return with error */
461 return r;
462 }
464 /* now do the cleanup */
465 if (buffer.file) {
466 fclose(buffer.file); buffer.file=NULL;
467 im->rendered_image_size=0;
468 im->rendered_image=NULL;
469 } else {
470 im->rendered_image_size=buffer.len;
471 im->rendered_image=buffer.data;
472 }
474 /* and print stuff */
475 return print_calc(im);
476 }
478 int addToBuffer(stringbuffer_t * sb,char* data,size_t len) {
479 /* if len <= 0 we assume a string and calculate the length ourself */
480 if (len<=0) { len=strlen(data); }
481 /* if we have got a file, then take the shortcut */
482 if (sb->file) {
483 sb->len+=len;
484 fwrite(data,len,1,sb->file);
485 return 0;
486 }
487 /* if buffer is 0, then initialize */
488 if (! sb->data) {
489 /* make buffer a multiple of 8192 */
490 sb->allocated+=8192;
491 sb->allocated-=(sb->allocated%8192);
492 /* and allocate it */
493 sb->data=malloc(sb->allocated);
494 if (! sb->data) {
495 rrd_set_error("malloc issue");
496 return 1;
497 }
498 /* and initialize the buffer */
499 sb->len=0;
500 sb->data[0]=0;
501 }
502 /* and figure out if we need to extend the buffer */
503 if (sb->len+len+1>=sb->allocated) {
504 /* add so many pages until we have a buffer big enough */
505 while(sb->len+len+1>=sb->allocated) {
506 sb->allocated+=8192;
507 }
508 /* try to resize it */
509 unsigned char* resized=(unsigned char*)realloc(sb->data,sb->allocated);
510 if (resized) {
511 sb->data=resized;
512 } else {
513 free(sb->data);
514 sb->data=NULL;
515 sb->allocated=0;
516 rrd_set_error("realloc issue");
517 return -1;
518 }
519 }
520 /* and finally add to the buffer */
521 memcpy(sb->data+sb->len,data,len);
522 sb->len+=len;
523 /* and 0 terminate it */
524 sb->data[sb->len]=0;
525 /* and return */
526 return 0;
527 }
529 int rrd_xport_format_sv(char sep, stringbuffer_t *buffer,image_desc_t *im,time_t start, time_t end, unsigned long step, unsigned long col_cnt, char **legend_v, rrd_value_t* data) {
530 /* define the time format */
531 char* timefmt=NULL;
532 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
534 /* row count */
535 unsigned long row_cnt=(end-start)/step;
537 /* estimate buffer size (to avoid multiple allocations) */
538 buffer->allocated=
539 1024 /* bytes of overhead /header/footer */
540 +(12+19*col_cnt) /* 12 bytes overhead/line plus 19 bytes per column*/
541 *(1+row_cnt) /* number of columns + 1 (for header) */
542 ;
544 char buf[256];
546 /* now start writing the header*/
547 if (addToBuffer(buffer,"\"time\"",6)) { return 1; }
548 for(unsigned long i=0;i<col_cnt;i++) {
549 /* strip leading spaces */
550 char *t=legend_v[i]; while (isspace(*t)) { t++;}
551 /* and print it */
552 snprintf(buf,255,"%c\"%s\"",sep,t);
553 if (addToBuffer(buffer,buf,0)) { return 1;}
554 }
555 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
556 /* and now write the data */
557 rrd_value_t *ptr=data;
558 for(time_t ti=start+step;ti<end;ti+=step) {
559 /* write time */
560 if (timefmt) {
561 struct tm loc;
562 localtime_r(&ti,&loc);
563 strftime(buf,254,timefmt,&loc);
564 } else {
565 snprintf(buf,254,"%lld",(long long int)ti);
566 }
567 if (addToBuffer(buffer,buf,0)) { return 1; }
568 /* write the columns */
569 for(unsigned long i=0;i<col_cnt;i++) {
570 /* get the value */
571 rrd_value_t v=*ptr;ptr++;
572 /* and print it */
573 if (isnan(v)) {
574 snprintf(buf,255,"%c\"NaN\"",sep);
575 } else {
576 snprintf(buf,255,"%c\"%0.10e\"",sep,v);
577 }
578 if (addToBuffer(buffer,buf,0)) { return 1;}
579 }
580 /* and add a newline */
581 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
582 }
584 /* and return OK */
585 return 0;
586 }
588 int rrd_xport_format_xmljson(int flags,stringbuffer_t *buffer,image_desc_t *im,time_t start, time_t end, unsigned long step, unsigned long col_cnt, char **legend_v, rrd_value_t* data) {
590 /* define some other stuff based on flags */
591 int json=0;
592 if (flags &1) { json=1; }
593 int showtime=0;
594 if (flags &2) { showtime=1;}
595 int enumds=0;
596 if (flags &4) { enumds=1;}
598 /* define the time format */
599 char* timefmt=NULL;
600 /* unfortunatley we have to do it this way,
601 as when no --x-graph argument is given,
602 then the xlab_user is not in a clean state (e.g. zero-filled) */
603 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
605 /* row count */
606 unsigned long row_cnt=(end-start)/step;
608 /* estimate buffer size (to avoid multiple allocations) */
609 /* better estimates are needed here */
610 buffer->allocated=
611 1024 /* bytes of overhead /header/footer */
612 +(12+19*col_cnt) /* 12 bytes overhead/line plus 19 bytes per column*/
613 *(1+row_cnt) /* number of columns + 1 (for header) */
614 ;
615 char buf[256];
616 char dbuf[1024];
618 rrd_value_t *ptr = data;
619 if (json == 0){
620 snprintf(buf,sizeof(buf),
621 "<?xml version=\"1.0\" encoding=\"%s\"?>\n\n<%s>\n <%s>\n",
622 XML_ENCODING,ROOT_TAG,META_TAG);
623 addToBuffer(buffer,buf,0);
624 }
625 else {
626 addToBuffer(buffer,"{ \"about\": \"RRDtool graph JSON output\",\n \"meta\": {\n",0);
627 }
629 /* calculate start time */
630 if (timefmt) {
631 struct tm loc;
632 time_t ti=start+step;
633 localtime_r(&ti,&loc);
634 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
635 if (json) {
636 snprintf(buf,sizeof(buf)," \"%s\": \"%s\",\n",META_START_TAG,dbuf);
637 } else {
638 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",META_START_TAG,dbuf,META_START_TAG);
639 }
640 } else {
641 if (json) {
642 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_START_TAG,(long long int)start+step);
643 } else {
644 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_START_TAG,(long long int)start+step,META_START_TAG);
645 }
646 }
647 addToBuffer(buffer,buf,0);
649 /* calculate end time */
650 if (timefmt) {
651 struct tm loc;
652 time_t ti=end;
653 localtime_r(&ti,&loc);
654 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
655 if (json) {
656 snprintf(buf,sizeof(buf)," \"%s\": \"%s\",\n",META_END_TAG,dbuf);
657 } else {
658 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",META_END_TAG,dbuf,META_END_TAG);
659 }
660 } else {
661 if (json) {
662 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_END_TAG,(long long int)end);
663 } else {
664 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_END_TAG,(long long int)end,META_END_TAG);
665 }
666 }
667 addToBuffer(buffer,buf,0);
668 /* print other info */
669 if (json) {
670 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_STEP_TAG,(long long int)step);
671 addToBuffer(buffer,buf,0);
672 } else {
673 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_STEP_TAG,(long long int)step,META_STEP_TAG);
674 addToBuffer(buffer,buf,0);
675 snprintf(buf,sizeof(buf)," <%s>%lu</%s>\n",META_ROWS_TAG,row_cnt,META_ROWS_TAG);
676 addToBuffer(buffer,buf,0);
677 snprintf(buf,sizeof(buf)," <%s>%lu</%s>\n",META_COLS_TAG,col_cnt,META_COLS_TAG);
678 addToBuffer(buffer,buf,0);
679 }
681 /* start legend */
682 if (json){
683 snprintf(buf,sizeof(buf)," \"%s\": [\n", LEGEND_TAG);
684 }
685 else {
686 snprintf(buf,sizeof(buf)," <%s>\n", LEGEND_TAG);
687 }
688 addToBuffer(buffer,buf,0);
689 /* add legend entries */
690 for (unsigned long j = 0; j < col_cnt; j++) {
691 char *entry = legend_v[j];
692 /* I do not know why the legend is "spaced", but let us skip it */
693 while(isspace(*entry)){entry++;}
694 /* now output it */
695 if (json){
696 snprintf(buf,sizeof(buf)," \"%s\"", entry);
697 addToBuffer(buffer,buf,0);
698 if (j < col_cnt -1){
699 addToBuffer(buffer,",",1);
700 }
701 addToBuffer(buffer,"\n",1);
702 }
703 else {
704 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n", LEGEND_ENTRY_TAG, entry,LEGEND_ENTRY_TAG);
705 addToBuffer(buffer,buf,0);
706 }
707 }
708 /* end legend */
709 if (json){
710 snprintf(buf,sizeof(buf)," ],\n");
711 }
712 else {
713 snprintf(buf,sizeof(buf)," </%s>\n", LEGEND_TAG);
714 }
715 addToBuffer(buffer,buf,0);
717 /* add graphs and prints */
718 if (rrd_xport_format_addprints(json,buffer,im)) {return -1;}
720 /* if we have got a trailing , then kill it */
721 if (buffer->data) {
722 if (buffer->data[buffer->len-2]==',') {
723 buffer->data[buffer->len-2]=buffer->data[buffer->len-1];
724 buffer->len--;
725 }
726 }
728 /* end meta */
729 if (json){
730 snprintf(buf,sizeof(buf)," },\n");
731 } else {
732 snprintf(buf,sizeof(buf)," </%s>\n", META_TAG);
733 }
734 addToBuffer(buffer,buf,0);
737 /* start data */
738 if (json){
739 snprintf(buf,sizeof(buf)," \"%s\": [\n",DATA_TAG);
740 } else {
741 snprintf(buf,sizeof(buf)," <%s>\n", DATA_TAG);
742 }
743 addToBuffer(buffer,buf,0);
744 /* iterate over data */
745 for (time_t ti = start + step; ti <= end; ti += step) {
746 if (timefmt) {
747 struct tm loc;
748 localtime_r(&ti,&loc);
749 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
750 } else {
751 snprintf(dbuf,sizeof(dbuf),"%lld",(long long int)ti);
752 }
753 if (json){
754 addToBuffer(buffer," [ ",0);
755 if(showtime){
756 addToBuffer(buffer,"\"",1);
757 addToBuffer(buffer,dbuf,0);
758 addToBuffer(buffer,"\",",2);
759 }
760 }
761 else {
762 if (showtime) {
763 snprintf(buf,sizeof(buf),
764 " <%s><%s>%s</%s>", DATA_ROW_TAG,COL_TIME_TAG, dbuf, COL_TIME_TAG);
765 } else {
766 snprintf(buf,sizeof(buf),
767 " <%s>", DATA_ROW_TAG);
768 }
769 addToBuffer(buffer,buf,0);
770 }
771 for (unsigned long j = 0; j < col_cnt; j++) {
772 rrd_value_t newval = DNAN;
773 newval = *ptr;
774 if (json){
775 if (isnan(newval)){
776 addToBuffer(buffer,"null",0);
777 } else {
778 snprintf(buf,sizeof(buf),"%0.10e",newval);
779 addToBuffer(buffer,buf,0);
780 }
781 if (j < col_cnt -1){
782 addToBuffer(buffer,", ",0);
783 }
784 }
785 else {
786 if (isnan(newval)) {
787 if (enumds) {
788 snprintf(buf,sizeof(buf),"<%s%lu>NaN</%s%lu>", COL_DATA_TAG,j,COL_DATA_TAG,j);
789 } else {
790 snprintf(buf,sizeof(buf),"<%s>NaN</%s>", COL_DATA_TAG,COL_DATA_TAG);
791 }
792 } else {
793 if (enumds) {
794 snprintf(buf,sizeof(buf),"<%s%lu>%0.10e</%s%lu>", COL_DATA_TAG,j,newval,COL_DATA_TAG,j);
795 } else {
796 snprintf(buf,sizeof(buf),"<%s>%0.10e</%s>", COL_DATA_TAG,newval,COL_DATA_TAG);
797 }
798 }
799 addToBuffer(buffer,buf,0);
800 }
801 ptr++;
802 }
803 if (json){
804 addToBuffer(buffer,(ti < end ? " ],\n" : " ]\n"),0);
805 }
806 else {
807 snprintf(buf,sizeof(buf),"</%s>\n", DATA_ROW_TAG);
808 addToBuffer(buffer,buf,0);
809 }
810 }
811 /* end data */
812 if (json){
813 addToBuffer(buffer," ]\n",0);
814 }
815 else {
816 snprintf(buf,sizeof(buf)," </%s>\n",DATA_TAG);
817 addToBuffer(buffer,buf,0);
818 }
820 /* end all */
821 if (json){
822 addToBuffer(buffer,"}\n",0);
823 } else {
824 snprintf(buf,sizeof(buf),"</%s>\n", ROOT_TAG);
825 addToBuffer(buffer,buf,0);
826 }
827 return 0;
828 }
830 void escapeJSON(char* txt,size_t len) {
831 char *tmp=(char*)malloc(len+2);
832 size_t l=strlen(txt);
833 size_t pos=0;
834 /* now iterate over the chars */
835 for(size_t i=0;(i<l)&&(pos<len);i++,pos++) {
836 switch (txt[i]) {
837 case '"':
838 case '\\':
839 tmp[pos]='\\';pos++;
840 tmp[pos]=txt[i];
841 break;
842 default:
843 tmp[pos]=txt[i];
844 break;
845 }
846 }
847 /* 0 terminate it */
848 tmp[pos]=0;
849 /* and copy back over txt */
850 strncpy(txt,tmp,len);
851 /* and release tmp */
852 free(tmp);
853 }
855 int rrd_xport_format_addprints(int flags,stringbuffer_t *buffer,image_desc_t *im) {
856 /* initialize buffer */
857 stringbuffer_t prints={1024,0,NULL,NULL};
858 stringbuffer_t rules={1024,0,NULL,NULL};
859 stringbuffer_t gprints={4096,0,NULL,NULL};
860 char buf[256];
861 char dbuf[1024];
862 char* val;
863 char* timefmt=NULL;
864 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
866 /* define some other stuff based on flags */
867 int json=0;
868 if (flags &1) { json=1; }
869 int showtime=0;
870 if (flags &2) { showtime=1;}
871 int enumds=0;
872 if (flags &4) { enumds=1;}
874 /* define some values */
875 time_t now = time(NULL);
876 struct tm tmvdef;
877 localtime_r(&now, &tmvdef);
878 double printval=DNAN;
880 /* iterate over all fields and start the work writing to the correct buffers */
881 for (long i = 0; i < im->gdes_c; i++) {
882 long vidx = im->gdes[i].vidx;
883 char* entry;
884 switch (im->gdes[i].gf) {
885 case GF_PRINT:
886 case GF_GPRINT:
887 /* PRINT and GPRINT can now print VDEF generated values.
888 * There's no need to do any calculations on them as these
889 * calculations were already made.
890 * we do not support the depreciated version here
891 */
892 printval=DNAN;
893 /* decide to which buffer we print to*/
894 stringbuffer_t *usebuffer=&gprints;
895 char* usetag="gprint";
896 if (im->gdes[i].gf==GF_PRINT) {
897 usebuffer=&prints;
898 usetag="print";
899 }
900 /* get the value */
901 if (im->gdes[vidx].gf == GF_VDEF) { /* simply use vals */
902 printval = im->gdes[vidx].vf.val;
903 localtime_r(&im->gdes[vidx].vf.when, &tmvdef);
904 } else {
905 int max_ii = ((im->gdes[vidx].end - im->gdes[vidx].start)
906 / im->gdes[vidx].step * im->gdes[vidx].ds_cnt);
907 printval = DNAN;
908 long validsteps = 0;
909 for (long ii = im->gdes[vidx].ds;
910 ii < max_ii; ii += im->gdes[vidx].ds_cnt) {
911 if (!finite(im->gdes[vidx].data[ii]))
912 continue;
913 if (isnan(printval)) {
914 printval = im->gdes[vidx].data[ii];
915 validsteps++;
916 continue;
917 }
919 switch (im->gdes[i].cf) {
920 case CF_HWPREDICT:
921 case CF_MHWPREDICT:
922 case CF_DEVPREDICT:
923 case CF_DEVSEASONAL:
924 case CF_SEASONAL:
925 case CF_AVERAGE:
926 validsteps++;
927 printval += im->gdes[vidx].data[ii];
928 break;
929 case CF_MINIMUM:
930 printval = min(printval, im->gdes[vidx].data[ii]);
931 break;
932 case CF_FAILURES:
933 case CF_MAXIMUM:
934 printval = max(printval, im->gdes[vidx].data[ii]);
935 break;
936 case CF_LAST:
937 printval = im->gdes[vidx].data[ii];
938 }
939 }
940 if (im->gdes[i].cf == CF_AVERAGE || im->gdes[i].cf > CF_LAST) {
941 if (validsteps > 1) {
942 printval = (printval / validsteps);
943 }
944 }
945 }
946 /* we handle PRINT and GPRINT the same - format now*/
947 if (im->gdes[i].strftm) {
948 if (im->gdes[vidx].vf.never == 1) {
949 time_clean(buf, im->gdes[i].format);
950 } else {
951 strftime(dbuf,sizeof(dbuf), im->gdes[i].format, &tmvdef);
952 }
953 } else if (bad_format(im->gdes[i].format)) {
954 rrd_set_error
955 ("bad format for PRINT in \"%s'", im->gdes[i].format);
956 return -1;
957 } else {
958 snprintf(dbuf,sizeof(dbuf), im->gdes[i].format, printval,"");
959 }
960 /* print */
961 if (json) {
962 escapeJSON(dbuf,sizeof(dbuf));
963 snprintf(buf,sizeof(buf),",\n { \"%s\": \"%s\" }",usetag,dbuf);
964 } else {
965 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",usetag,dbuf,usetag);
966 }
967 addToBuffer(usebuffer,buf,0);
968 break;
969 case GF_COMMENT:
970 if (json) {
971 strncpy(dbuf,im->gdes[i].legend,sizeof(dbuf));
972 escapeJSON(dbuf,sizeof(dbuf));
973 snprintf(buf,sizeof(buf),",\n { \"comment\": \"%s\" }",dbuf);
974 } else {
975 snprintf(buf,sizeof(buf)," <comment>%s</comment>\n",im->gdes[i].legend);
976 }
977 addToBuffer(&gprints,buf,0);
978 break;
979 case GF_LINE:
980 entry = im->gdes[i].legend;
981 /* I do not know why the legend is "spaced", but let us skip it */
982 while(isspace(*entry)){entry++;}
983 if (json) {
984 snprintf(buf,sizeof(buf),",\n { \"line\": \"%s\" }",entry);
985 } else {
986 snprintf(buf,sizeof(buf)," <line>%s</line>\n",entry);
987 }
988 addToBuffer(&gprints,buf,0);
989 break;
990 case GF_AREA:
991 if (json) {
992 snprintf(buf,sizeof(buf),",\n { \"area\": \"%s\" }",im->gdes[i].legend);
993 } else {
994 snprintf(buf,sizeof(buf)," <area>%s</area>\n",im->gdes[i].legend);
995 }
996 addToBuffer(&gprints,buf,0);
997 break;
998 case GF_STACK:
999 if (json) {
1000 snprintf(buf,sizeof(buf),",\n { \"stack\": \"%s\" }",im->gdes[i].legend);
1001 } else {
1002 snprintf(buf,sizeof(buf)," <stack>%s</stack>\n",im->gdes[i].legend);
1003 }
1004 addToBuffer(&gprints,buf,0);
1005 break;
1006 case GF_TEXTALIGN:
1007 val="";
1008 switch (im->gdes[i].txtalign) {
1009 case TXA_LEFT: val="left"; break;
1010 case TXA_RIGHT: val="right"; break;
1011 case TXA_CENTER: val="center"; break;
1012 case TXA_JUSTIFIED: val="justified"; break;
1013 }
1014 if (json) {
1015 snprintf(buf,sizeof(buf),",\n { \"align\": \"%s\" }",val);
1016 } else {
1017 snprintf(buf,sizeof(buf)," <align>%s</align>\n",val);
1018 }
1019 addToBuffer(&gprints,buf,0);
1020 break;
1021 case GF_HRULE:
1022 /* This does not work as expected - Tobi please help!!! */
1023 snprintf(dbuf,sizeof(dbuf),"%0.10e",im->gdes[i].vf.val);
1024 /* and output it */
1025 if (json) {
1026 snprintf(buf,sizeof(buf),",\n { \"hrule\": \"%s\" }",dbuf);
1027 } else {
1028 snprintf(buf,sizeof(buf)," <hrule>%s</hrule>\n",dbuf);
1029 }
1030 addToBuffer(&rules,buf,0);
1031 break;
1032 case GF_VRULE:
1033 if (timefmt) {
1034 struct tm loc;
1035 localtime_r(&im->gdes[i].xrule,&loc);
1036 strftime(dbuf,254,timefmt,&loc);
1037 } else {
1038 snprintf(dbuf,254,"%lld",(long long int)im->gdes[i].vf.when);
1039 }
1040 /* and output it */
1041 if (json) {
1042 snprintf(buf,sizeof(buf),",\n { \"vrule\": \"%s\" }",dbuf);
1043 } else {
1044 snprintf(buf,sizeof(buf)," <vrule>%s</vrule>\n",dbuf);
1045 }
1046 addToBuffer(&rules,buf,0);
1047 break;
1048 default:
1049 break;
1050 }
1051 }
1052 /* now add prints */
1053 if (prints.len) {
1054 if (json){
1055 snprintf(buf,sizeof(buf)," \"%s\": [\n","prints");
1056 addToBuffer(buffer,buf,0);
1057 addToBuffer(buffer,(char*)prints.data+2,prints.len-2);
1058 addToBuffer(buffer,"\n ],\n",0);
1059 } else {
1060 snprintf(buf,sizeof(buf)," <%s>\n", "prints");
1061 addToBuffer(buffer,buf,0);
1062 addToBuffer(buffer,(char*)prints.data,prints.len);
1063 snprintf(buf,sizeof(buf)," </%s>\n", "prints");
1064 addToBuffer(buffer,buf,0);
1065 }
1066 free(prints.data);
1067 }
1068 /* now add gprints */
1069 if (gprints.len) {
1070 if (json){
1071 snprintf(buf,sizeof(buf)," \"%s\": [\n","gprints");
1072 addToBuffer(buffer,buf,0);
1073 addToBuffer(buffer,(char*)gprints.data+2,gprints.len-2);
1074 addToBuffer(buffer,"\n ],\n",0);
1075 } else {
1076 snprintf(buf,sizeof(buf)," <%s>\n", "gprints");
1077 addToBuffer(buffer,buf,0);
1078 addToBuffer(buffer,(char*)gprints.data,gprints.len);
1079 snprintf(buf,sizeof(buf)," </%s>\n", "gprints");
1080 addToBuffer(buffer,buf,0);
1081 }
1082 free(gprints.data);
1083 }
1084 /* now add rules */
1085 if (rules.len) {
1086 if (json){
1087 snprintf(buf,sizeof(buf)," \"%s\": [\n","rules");
1088 addToBuffer(buffer,buf,0);
1089 addToBuffer(buffer,(char*)rules.data+2,rules.len-2);
1090 addToBuffer(buffer,"\n ],\n",0);
1091 } else {
1092 snprintf(buf,sizeof(buf)," <%s>\n", "rules");
1093 addToBuffer(buffer,buf,0);
1094 addToBuffer(buffer,(char*)rules.data,rules.len);
1095 snprintf(buf,sizeof(buf)," </%s>\n", "rules");
1096 addToBuffer(buffer,buf,0);
1097 }
1098 free(rules.data);
1099 }
1100 /* and return */
1101 return 0;
1102 }