1 /****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
3 ****************************************************************************
4 * rrd_xport.c export RRD data
5 ****************************************************************************/
11 #include "rrd_graph.h"
12 #include "rrd_xport.h"
14 #include "rrd_client.h"
16 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
50 time_t *end, /* which time frame do you want ?
51 * will be changed to represent reality */
52 unsigned long *step, /* which stepsize do you want?
53 * will be changed to represent reality */
54 unsigned long *col_cnt, /* number of data columns in the result */
55 char ***legend_v, /* legend entries */
57 { /* two dimensional array containing the data */
59 time_t start_tmp = 0, end_tmp = 0;
60 rrd_time_value_t start_tv, end_tv;
61 char *parsetime_error = NULL;
63 struct option long_options[] = {
64 {"start", required_argument, 0, 's'},
65 {"end", required_argument, 0, 'e'},
66 {"maxrows", required_argument, 0, 'm'},
67 {"step", required_argument, 0, 261},
68 {"enumds", no_argument, 0, 262}, /* these are handled in the frontend ... */
69 {"json", no_argument, 0, 263}, /* these are handled in the frontend ... */
70 {"daemon", required_argument, 0, 'd'},
75 opterr = 0; /* initialize getopt */
79 rrd_parsetime("end-24h", &start_tv);
80 rrd_parsetime("now", &end_tv);
86 opt = getopt_long(argc, argv, "s:e:m:d:", long_options, &option_index);
93 im.step = atoi(optarg);
98 if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
99 rrd_set_error("start time: %s", parsetime_error);
104 if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
105 rrd_set_error("end time: %s", parsetime_error);
110 im.xsize = atol(optarg);
112 rrd_set_error("maxrows below 10 rows");
118 if (im.daemon_addr != NULL)
120 rrd_set_error ("You cannot specify --daemon "
125 im.daemon_addr = strdup(optarg);
126 if (im.daemon_addr == NULL)
128 rrd_set_error("strdup error");
135 rrd_set_error("unknown option '%s'", argv[optind - 1]);
140 if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
144 if (start_tmp < 3600 * 24 * 365 * 10) {
145 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",
150 if (end_tmp < start_tmp) {
151 rrd_set_error("start (%ld) should be less than end (%ld)",
156 im.start = start_tmp;
158 im.step = max((long) im.step, (im.end - im.start) / im.xsize);
160 rrd_graph_script(argc, argv, &im, 0);
161 if (rrd_test_error()) {
166 if (im.gdes_c == 0) {
167 rrd_set_error("can't make an xport without contents");
172 { /* try to connect to rrdcached */
173 int status = rrdc_connect(im.daemon_addr);
174 if (status != 0) return status;
177 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data,0) == -1) {
191 time_t *end, /* which time frame do you want ?
192 * will be changed to represent reality */
193 unsigned long *step, /* which stepsize do you want?
194 * will be changed to represent reality */
195 unsigned long *col_cnt, /* number of data columns in the result */
196 char ***legend_v, /* legend entries */
199 { /* two dimensional array containing the data */
202 unsigned long dst_row, row_cnt;
205 unsigned long xport_counter = 0;
212 /* pull the data from the rrd files ... */
213 if (data_fetch(im) == -1)
216 /* evaluate CDEF operations ... */
217 if (data_calc(im) == -1)
220 /* how many xports or lines/AREA/STACK ? */
222 for (i = 0; i < im->gdes_c; i++) {
223 switch (im->gdes[i].gf) {
236 if ((*col_cnt) == 0) {
237 rrd_set_error("no XPORT found, nothing to do");
241 /* a list of referenced gdes */
242 ref_list = (int*)malloc(sizeof(int) * (*col_cnt));
243 if (ref_list == NULL)
246 /* a list to save pointers to the column's legend entry */
247 /* this is a return value! */
248 legend_list = (char**)malloc(sizeof(char *) * (*col_cnt));
249 if (legend_list == NULL) {
254 /* lets find the step size we have to use for xport */
255 step_list = (long*)malloc(sizeof(long)*((*col_cnt)+1));
256 step_list_ptr = step_list;
258 for (i = 0; i < im->gdes_c; i++) {
259 /* decide if we need to handle the output */
261 switch (im->gdes[i].gf) {
274 /* and now do the real work */
276 ref_list[xport_counter++] = i;
277 *step_list_ptr = im->gdes[im->gdes[i].vidx].step;
278 /* printf("%s:%lu\n",im->gdes[i].legend,*step_list_ptr); */
280 /* reserve room for one legend entry */
281 /* is FMT_LEG_LEN + 5 the correct size? */
282 if ((legend_list[j] =
283 (char*)malloc(sizeof(char) * (FMT_LEG_LEN + 5))) == NULL) {
287 free(legend_list[j]);
290 rrd_set_error("malloc xport legend entry");
294 if (im->gdes[i].legend)
295 /* omit bounds check, should have the same size */
296 strcpy(legend_list[j++], im->gdes[i].legend);
298 legend_list[j++][0] = '\0';
302 /* find a common step */
303 *step = lcd(step_list);
304 /* printf("step: %lu\n",*step); */
307 *start = im->start - im->start % (*step);
308 *end = im->end - im->end % (*step) + (*step);
311 /* room for rearranged data */
312 /* this is a return value! */
313 row_cnt = ((*end) - (*start)) / (*step);
315 (rrd_value_t*)malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
318 rrd_set_error("malloc xport data area");
323 /* fill data structure */
324 for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
325 for (i = 0; i < (int) (*col_cnt); i++) {
326 long vidx = im->gdes[ref_list[i]].vidx;
327 time_t now = *start + dst_row * *step;
328 (*dstptr++) = im->gdes[vidx].data[(unsigned long)
330 (now - im->gdes[vidx].start)
331 /im->gdes[vidx].step)
332 * im->gdes[vidx].ds_cnt +
338 *legend_v = legend_list;
344 /* helper function for buffer handling */
345 typedef struct stringbuffer_t {
351 int addToBuffer(stringbuffer_t *,char*,size_t);
353 int rrd_graph_xport(image_desc_t *);
354 int rrd_xport_format_xml(stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*);
355 int rrd_xport_format_json(stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*);
356 int rrd_xport_format_sv(char,stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*);
358 int rrd_graph_xport(image_desc_t *im) {
359 /* prepare the data for processing */
360 unsigned long col_cnt=0;
361 time_t start=im->start;
363 unsigned long step=im->step;
364 char **legend_v=NULL;
365 rrd_value_t *data=NULL;
366 /* initialize buffer */
367 stringbuffer_t buffer={0,0,NULL,NULL};
368 /* if we write a file, then open it */
369 if (strlen(im->graphfile)) {
370 buffer.file=fopen(im->graphfile,"w");
373 /* do the data processing */
374 if (rrd_xport_fn(im,&start,&end,&step,&col_cnt,&legend_v,&data,1)) { return -1;}
376 /* fill in some data */
378 info.u_cnt = im->start;
379 grinfo_push(im, sprintf_alloc("graph_start"), RD_I_CNT, info);
380 info.u_cnt = im->end;
381 grinfo_push(im, sprintf_alloc("graph_end"), RD_I_CNT, info);
382 info.u_cnt = im->step;
383 grinfo_push(im, sprintf_alloc("graph_step"), RD_I_CNT, info);
386 char *old_locale = setlocale(LC_NUMERIC,NULL);
387 setlocale(LC_NUMERIC, "C");
389 /* format it for output */
391 switch(im->imgformat) {
393 r=rrd_xport_format_xml(&buffer,im, start, end, step, col_cnt, legend_v, data);
396 r=rrd_xport_format_json(&buffer,im, start, end, step, col_cnt, legend_v, data);
399 r=rrd_xport_format_sv(',',&buffer,im, start, end, step, col_cnt, legend_v, data);
402 r=rrd_xport_format_sv('\t',&buffer,im, start, end, step, col_cnt, legend_v, data);
405 r=rrd_xport_format_sv(';',&buffer,im, start, end, step, col_cnt, legend_v, data);
411 setlocale(LC_NUMERIC, old_locale);
415 for (unsigned long j = 0; j < col_cnt; j++) {
422 if (buffer.data) {free(buffer.data);}
423 /* and return with error */
427 /* now do the cleanup */
429 fclose(buffer.file); buffer.file=NULL;
430 im->rendered_image_size=0;
431 im->rendered_image=NULL;
433 im->rendered_image_size=buffer.len;
434 im->rendered_image=buffer.data;
437 /* and print stuff */
438 return print_calc(im);
441 int addToBuffer(stringbuffer_t * sb,char* data,size_t len) {
442 /* if len <= 0 we assume a string and calculate the length ourself */
443 if (len<=0) { len=strlen(data); }
444 /* if we have got a file, then take the shortcut */
447 fwrite(data,len,1,sb->file);
450 /* if buffer is 0, then initialize */
452 /* make buffer a multiple of 8192 */
454 sb->allocated-=(sb->allocated%8192);
455 /* and allocate it */
456 sb->data=malloc(sb->allocated);
458 rrd_set_error("malloc issue");
461 /* and initialize the buffer */
465 /* and figure out if we need to extend the buffer */
466 if (sb->len+len+1>=sb->allocated) {
467 /* add so many pages until we have a buffer big enough */
468 while(sb->len+len+1>=sb->allocated) {
471 /* try to resize it */
472 unsigned char* resized=(unsigned char*)realloc(sb->data,sb->allocated);
479 rrd_set_error("realloc issue");
483 /* and finally add to the buffer */
484 memcpy(sb->data+sb->len,data,len);
486 /* and 0 terminate it */
492 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) {
493 /* define the time format */
495 /* unfortunatley we have to do it this way,
496 as when no --x-graph argument is given,
497 then the xlab_user is not in a clean state (e.g. zero-filled) */
498 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
501 unsigned long row_cnt=(end-start)/step;
503 /* estimate buffer size (to avoid multiple allocations) */
505 1024 /* bytes of overhead /header/footer */
506 +(12+19*col_cnt) /* 12 bytes overhead/line plus 19 bytes per column*/
507 *(1+row_cnt) /* number of columns + 1 (for header) */
510 /* now start writing the header*/
511 if (addToBuffer(buffer,"\"time\"",6)) { return 1; }
513 for(unsigned long i=0;i<col_cnt;i++) {
514 /* strip leading spaces */
515 char *t=legend_v[i]; while (isspace(*t)) { t++;}
517 snprintf(buf,255,"%c\"%s\"",sep,t);
518 if (addToBuffer(buffer,buf,0)) { return 1;}
520 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
521 /* and now write the data */
522 rrd_value_t *ptr=data;
523 for(time_t ti=start+step;ti<end;ti+=step) {
527 localtime_r(&ti,&loc);
528 strftime(buf,254,timefmt,&loc);
530 snprintf(buf,254,"%lld",(long long int)ti);
532 if (addToBuffer(buffer,buf,0)) { return 1; }
533 /* write the columns */
534 for(unsigned long i=0;i<col_cnt;i++) {
536 rrd_value_t v=*ptr;ptr++;
539 snprintf(buf,255,"%c\"NaN\"",sep);
541 snprintf(buf,255,"%c\"%0.10e\"",sep,v);
543 if (addToBuffer(buffer,buf,0)) { return 1;}
545 /* and add a newline */
546 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
553 int rrd_xport_format_xml(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) {
554 addToBuffer(buffer,"xml not implemented",0);
558 int rrd_xport_format_json(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) {
559 addToBuffer(buffer,"JSON not implemented",0);