1 /****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
3 ****************************************************************************
4 * rrd_xport.c export RRD data
5 ****************************************************************************/
10 #include "rrd_graph.h"
11 #include "rrd_xport.h"
13 #include "rrd_client.h"
15 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
49 time_t *end, /* which time frame do you want ?
50 * will be changed to represent reality */
51 unsigned long *step, /* which stepsize do you want?
52 * will be changed to represent reality */
53 unsigned long *col_cnt, /* number of data columns in the result */
54 char ***legend_v, /* legend entries */
56 { /* two dimensional array containing the data */
58 time_t start_tmp = 0, end_tmp = 0;
59 rrd_time_value_t start_tv, end_tv;
60 char *parsetime_error = NULL;
62 struct option long_options[] = {
63 {"start", required_argument, 0, 's'},
64 {"end", required_argument, 0, 'e'},
65 {"maxrows", required_argument, 0, 'm'},
66 {"step", required_argument, 0, 261},
67 {"enumds", no_argument, 0, 262}, /* these are handled in the frontend ... */
68 {"json", no_argument, 0, 263}, /* these are handled in the frontend ... */
69 {"daemon", required_argument, 0, 'd'},
74 opterr = 0; /* initialize getopt */
78 rrd_parsetime("end-24h", &start_tv);
79 rrd_parsetime("now", &end_tv);
85 opt = getopt_long(argc, argv, "s:e:m:d:", long_options, &option_index);
92 im.step = atoi(optarg);
97 if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
98 rrd_set_error("start time: %s", parsetime_error);
103 if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
104 rrd_set_error("end time: %s", parsetime_error);
109 im.xsize = atol(optarg);
111 rrd_set_error("maxrows below 10 rows");
117 if (im.daemon_addr != NULL)
119 rrd_set_error ("You cannot specify --daemon "
124 im.daemon_addr = strdup(optarg);
125 if (im.daemon_addr == NULL)
127 rrd_set_error("strdup error");
134 rrd_set_error("unknown option '%s'", argv[optind - 1]);
139 if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
143 if (start_tmp < 3600 * 24 * 365 * 10) {
144 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",
149 if (end_tmp < start_tmp) {
150 rrd_set_error("start (%ld) should be less than end (%ld)",
155 im.start = start_tmp;
157 im.step = max((long) im.step, (im.end - im.start) / im.xsize);
159 rrd_graph_script(argc, argv, &im, 0);
160 if (rrd_test_error()) {
165 if (im.gdes_c == 0) {
166 rrd_set_error("can't make an xport without contents");
171 { /* try to connect to rrdcached */
172 int status = rrdc_connect(im.daemon_addr);
173 if (status != 0) return status;
176 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data,0) == -1) {
190 time_t *end, /* which time frame do you want ?
191 * will be changed to represent reality */
192 unsigned long *step, /* which stepsize do you want?
193 * will be changed to represent reality */
194 unsigned long *col_cnt, /* number of data columns in the result */
195 char ***legend_v, /* legend entries */
198 { /* two dimensional array containing the data */
201 unsigned long dst_row, row_cnt;
204 unsigned long xport_counter = 0;
211 /* pull the data from the rrd files ... */
212 if (data_fetch(im) == -1)
215 /* evaluate CDEF operations ... */
216 if (data_calc(im) == -1)
219 /* how many xports or lines/AREA/STACK ? */
221 for (i = 0; i < im->gdes_c; i++) {
222 switch (im->gdes[i].gf) {
235 if ((*col_cnt) == 0) {
236 rrd_set_error("no XPORT found, nothing to do");
240 /* a list of referenced gdes */
241 ref_list = (int*)malloc(sizeof(int) * (*col_cnt));
242 if (ref_list == NULL)
245 /* a list to save pointers to the column's legend entry */
246 /* this is a return value! */
247 legend_list = (char**)malloc(sizeof(char *) * (*col_cnt));
248 if (legend_list == NULL) {
253 /* lets find the step size we have to use for xport */
254 step_list = (long*)malloc(sizeof(long)*((*col_cnt)+1));
255 step_list_ptr = step_list;
257 for (i = 0; i < im->gdes_c; i++) {
258 /* decide if we need to handle the output */
260 switch (im->gdes[i].gf) {
273 /* and now do the real work */
275 ref_list[xport_counter++] = i;
276 *step_list_ptr = im->gdes[im->gdes[i].vidx].step;
277 /* printf("%s:%lu\n",im->gdes[i].legend,*step_list_ptr); */
279 /* reserve room for one legend entry */
280 /* is FMT_LEG_LEN + 5 the correct size? */
281 if ((legend_list[j] =
282 (char*)malloc(sizeof(char) * (FMT_LEG_LEN + 5))) == NULL) {
286 free(legend_list[j]);
289 rrd_set_error("malloc xport legend entry");
293 if (im->gdes[i].legend)
294 /* omit bounds check, should have the same size */
295 strcpy(legend_list[j++], im->gdes[i].legend);
297 legend_list[j++][0] = '\0';
301 /* find a common step */
302 *step = lcd(step_list);
303 /* printf("step: %lu\n",*step); */
306 *start = im->start - im->start % (*step);
307 *end = im->end - im->end % (*step) + (*step);
310 /* room for rearranged data */
311 /* this is a return value! */
312 row_cnt = ((*end) - (*start)) / (*step);
314 (rrd_value_t*)malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
317 rrd_set_error("malloc xport data area");
322 /* fill data structure */
323 for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
324 for (i = 0; i < (int) (*col_cnt); i++) {
325 long vidx = im->gdes[ref_list[i]].vidx;
326 time_t now = *start + dst_row * *step;
327 (*dstptr++) = im->gdes[vidx].data[(unsigned long)
329 (now - im->gdes[vidx].start)
330 /im->gdes[vidx].step)
331 * im->gdes[vidx].ds_cnt +
337 *legend_v = legend_list;
343 /* helper function for buffer handling */
344 typedef struct stringbuffer_t {
350 int addToBuffer(stringbuffer_t *,char*,size_t);
352 int rrd_graph_xport(image_desc_t *);
353 int rrd_xport_format_xml(stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*);
354 int rrd_xport_format_json(stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*);
355 int rrd_xport_format_sv(char,stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*);
357 int rrd_graph_xport(image_desc_t *im) {
358 /* prepare the data for processing */
359 unsigned long col_cnt=0;
360 time_t start=im->start;
362 unsigned long step=im->step;
363 char **legend_v=NULL;
364 rrd_value_t *data=NULL;
365 /* initialize buffer */
366 stringbuffer_t buffer={0,0,NULL,NULL};
367 /* if we write a file, then open it */
368 if (strlen(im->graphfile)) {
369 buffer.file=fopen(im->graphfile,"w");
372 /* do the data processing */
373 if (rrd_xport_fn(im,&start,&end,&step,&col_cnt,&legend_v,&data,1)) { return -1;}
375 /* fill in some data */
377 info.u_cnt = im->start;
378 grinfo_push(im, sprintf_alloc("graph_start"), RD_I_CNT, info);
379 info.u_cnt = im->end;
380 grinfo_push(im, sprintf_alloc("graph_end"), RD_I_CNT, info);
381 info.u_cnt = im->step;
382 grinfo_push(im, sprintf_alloc("graph_step"), RD_I_CNT, info);
385 /* format it for output */
387 switch(im->imgformat) {
389 r=rrd_xport_format_xml(&buffer,im, start, end, step, col_cnt, legend_v, data);
392 r=rrd_xport_format_json(&buffer,im, start, end, step, col_cnt, legend_v, data);
395 r=rrd_xport_format_sv(',',&buffer,im, start, end, step, col_cnt, legend_v, data);
398 r=rrd_xport_format_sv('\t',&buffer,im, start, end, step, col_cnt, legend_v, data);
401 r=rrd_xport_format_sv(';',&buffer,im, start, end, step, col_cnt, legend_v, data);
408 if (buffer.data) {free(buffer.data);}
412 /* now do the cleanup */
414 fclose(buffer.file); buffer.file=NULL;
415 im->rendered_image_size=0;
416 im->rendered_image=NULL;
418 im->rendered_image_size=buffer.len;
419 im->rendered_image=buffer.data;
422 /* and print stuff */
423 return print_calc(im);
426 int addToBuffer(stringbuffer_t * sb,char* data,size_t len) {
427 /* if len <= 0 we assume a string and calculate the length ourself */
428 if (len<=0) { len=strlen(data); }
429 /* if we have got a file, then take the shortcut */
432 fwrite(data,len,1,sb->file);
435 /* if buffer is 0, then initialize */
437 /* make buffer a multiple of 8192 */
439 sb->allocated-=(sb->allocated%8192);
440 /* and allocate it */
441 sb->data=malloc(sb->allocated);
443 rrd_set_error("malloc issue");
446 /* and initialize the buffer */
450 /* and figure out if we need to extend the buffer */
451 if (sb->len+len+1>=sb->allocated) {
452 /* add so many pages until we have a buffer big enough */
453 while(sb->len+len+1>=sb->allocated) {
456 /* try to resize it */
457 unsigned char* resized=(unsigned char*)realloc(sb->data,sb->allocated);
464 rrd_set_error("realloc issue");
468 /* and finally add to the buffer */
469 memcpy(sb->data+sb->len,data,len);
471 /* and 0 terminate it */
477 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) {
478 /* define the time format */
480 /* unfortunatley we have to do it this way,
481 as when no --x-graph argument is given,
482 then the xlab_user is not in a clean state (e.g. zero-filled) */
483 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
486 unsigned long row_cnt=(end-start)/step;
488 /* estimate buffer size (to avoid multiple allocations) */
490 1024 /* bytes of overhead /header/footer */
491 +(12+19*col_cnt) /* 12 bytes overhead/line plus 19 bytes per column*/
492 *(1+row_cnt) /* number of columns + 1 (for header) */
495 /* now start writing the header*/
496 if (addToBuffer(buffer,"\"time\"",6)) { return 1; }
498 for(unsigned long i=0;i<col_cnt;i++) {
499 /* strip leading spaces */
500 char *t=legend_v[i]; while (isspace(*t)) { t++;}
502 snprintf(buf,255,"%c\"%s\"",sep,t);
503 if (addToBuffer(buffer,buf,0)) { return 1;}
505 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
506 /* and now write the data */
507 rrd_value_t *ptr=data;
508 for(time_t ti=start+step;ti<end;ti+=step) {
512 localtime_r(&ti,&loc);
513 strftime(buf,254,timefmt,&loc);
515 snprintf(buf,254,"%ul",ti);
517 if (addToBuffer(buffer,buf,0)) { return 1; }
518 /* write the columns */
519 for(unsigned long i=0;i<col_cnt;i++) {
521 rrd_value_t v=*ptr;ptr++;
524 snprintf(buf,255,"%c\"NaN\"",sep);
526 snprintf(buf,255,"%c\"%0.10e\"",sep,v);
528 if (addToBuffer(buffer,buf,0)) { return 1;}
530 /* and add a newline */
531 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
538 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) {
539 addToBuffer(buffer,"xml not implemented",0);
543 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) {
544 addToBuffer(buffer,"JSON not implemented",0);