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>
9 #include "rrd_tool.h"
10 #include "rrd_graph.h"
11 #include "rrd_xport.h"
12 #include "unused.h"
13 #include "rrd_client.h"
15 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
16 #include <io.h>
17 #include <fcntl.h>
18 #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);
44 int rrd_xport(
45 int argc,
46 char **argv,
47 int UNUSED(*xsize),
48 time_t *start,
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 */
55 rrd_value_t **data)
56 { /* two dimensional array containing the data */
57 image_desc_t im;
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'},
70 {0, 0, 0, 0}
71 };
73 optind = 0;
74 opterr = 0; /* initialize getopt */
76 rrd_graph_init(&im);
78 rrd_parsetime("end-24h", &start_tv);
79 rrd_parsetime("now", &end_tv);
81 while (1) {
82 int option_index = 0;
83 int opt;
85 opt = getopt_long(argc, argv, "s:e:m:d:", long_options, &option_index);
87 if (opt == EOF)
88 break;
90 switch (opt) {
91 case 261:
92 im.step = atoi(optarg);
93 break;
94 case 262:
95 break;
96 case 's':
97 if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
98 rrd_set_error("start time: %s", parsetime_error);
99 return -1;
100 }
101 break;
102 case 'e':
103 if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
104 rrd_set_error("end time: %s", parsetime_error);
105 return -1;
106 }
107 break;
108 case 'm':
109 im.xsize = atol(optarg);
110 if (im.xsize < 10) {
111 rrd_set_error("maxrows below 10 rows");
112 return -1;
113 }
114 break;
115 case 'd':
116 {
117 if (im.daemon_addr != NULL)
118 {
119 rrd_set_error ("You cannot specify --daemon "
120 "more than once.");
121 return (-1);
122 }
124 im.daemon_addr = strdup(optarg);
125 if (im.daemon_addr == NULL)
126 {
127 rrd_set_error("strdup error");
128 return -1;
129 }
130 break;
131 }
133 case '?':
134 rrd_set_error("unknown option '%s'", argv[optind - 1]);
135 return -1;
136 }
137 }
139 if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
140 return -1;
141 }
143 if (start_tmp < 3600 * 24 * 365 * 10) {
144 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",
145 start_tmp);
146 return -1;
147 }
149 if (end_tmp < start_tmp) {
150 rrd_set_error("start (%ld) should be less than end (%ld)",
151 start_tmp, end_tmp);
152 return -1;
153 }
155 im.start = start_tmp;
156 im.end = end_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()) {
161 im_free(&im);
162 return -1;
163 }
165 if (im.gdes_c == 0) {
166 rrd_set_error("can't make an xport without contents");
167 im_free(&im);
168 return (-1);
169 }
171 { /* try to connect to rrdcached */
172 int status = rrdc_connect(im.daemon_addr);
173 if (status != 0) return status;
174 }
176 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data,0) == -1) {
177 im_free(&im);
178 return -1;
179 }
181 im_free(&im);
182 return 0;
183 }
187 int rrd_xport_fn(
188 image_desc_t *im,
189 time_t *start,
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 */
196 rrd_value_t **data,
197 int dolines)
198 { /* two dimensional array containing the data */
200 int i = 0, j = 0;
201 unsigned long dst_row, row_cnt;
202 rrd_value_t *dstptr;
204 unsigned long xport_counter = 0;
205 int *ref_list;
206 long *step_list;
207 long *step_list_ptr;
208 char **legend_list;
211 /* pull the data from the rrd files ... */
212 if (data_fetch(im) == -1)
213 return -1;
215 /* evaluate CDEF operations ... */
216 if (data_calc(im) == -1)
217 return -1;
219 /* how many xports or lines/AREA/STACK ? */
220 *col_cnt = 0;
221 for (i = 0; i < im->gdes_c; i++) {
222 switch (im->gdes[i].gf) {
223 case GF_LINE:
224 case GF_AREA:
225 case GF_STACK:
226 (*col_cnt)+=dolines;
227 break;
228 case GF_XPORT:
229 (*col_cnt)++;
230 break;
231 default:
232 break;
233 }
234 }
235 if ((*col_cnt) == 0) {
236 rrd_set_error("no XPORT found, nothing to do");
237 return -1;
238 }
240 /* a list of referenced gdes */
241 ref_list = (int*)malloc(sizeof(int) * (*col_cnt));
242 if (ref_list == NULL)
243 return -1;
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) {
249 free(ref_list);
250 return -1;
251 }
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;
256 j = 0;
257 for (i = 0; i < im->gdes_c; i++) {
258 /* decide if we need to handle the output */
259 int handle=0;
260 switch (im->gdes[i].gf) {
261 case GF_LINE:
262 case GF_AREA:
263 case GF_STACK:
264 handle=dolines;
265 break;
266 case GF_XPORT:
267 handle=1;
268 break;
269 default:
270 handle=0;
271 break;
272 }
273 /* and now do the real work */
274 if (handle) {
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); */
278 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) {
283 free(ref_list);
284 *data = NULL;
285 while (--j > -1)
286 free(legend_list[j]);
287 free(legend_list);
288 free(step_list);
289 rrd_set_error("malloc xport legend entry");
290 return (-1);
291 }
293 if (im->gdes[i].legend)
294 /* omit bounds check, should have the same size */
295 strcpy(legend_list[j++], im->gdes[i].legend);
296 else
297 legend_list[j++][0] = '\0';
298 }
299 }
300 *step_list_ptr=0;
301 /* find a common step */
302 *step = lcd(step_list);
303 /* printf("step: %lu\n",*step); */
304 free(step_list);
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);
313 if (((*data) =
314 (rrd_value_t*)malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
315 free(ref_list);
316 free(legend_list);
317 rrd_set_error("malloc xport data area");
318 return (-1);
319 }
320 dstptr = (*data);
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)
328 floor((double)
329 (now - im->gdes[vidx].start)
330 /im->gdes[vidx].step)
331 * im->gdes[vidx].ds_cnt +
332 im->gdes[vidx].ds];
334 }
335 }
337 *legend_v = legend_list;
338 free(ref_list);
339 return 0;
341 }
343 /* helper function for buffer handling */
344 typedef struct stringbuffer_t {
345 size_t allocated;
346 size_t len;
347 unsigned char* data;
348 FILE *file;
349 } 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;
361 time_t end=im->end;
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");
370 }
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 */
376 rrd_infoval_t info;
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 */
386 int r=0;
387 switch(im->imgformat) {
388 case IF_XML:
389 r=rrd_xport_format_xml(&buffer,im, start, end, step, col_cnt, legend_v, data);
390 break;
391 case IF_JSON:
392 r=rrd_xport_format_json(&buffer,im, start, end, step, col_cnt, legend_v, data);
393 break;
394 case IF_CSV:
395 r=rrd_xport_format_sv(',',&buffer,im, start, end, step, col_cnt, legend_v, data);
396 break;
397 case IF_TSV:
398 r=rrd_xport_format_sv('\t',&buffer,im, start, end, step, col_cnt, legend_v, data);
399 break;
400 case IF_SSV:
401 r=rrd_xport_format_sv(';',&buffer,im, start, end, step, col_cnt, legend_v, data);
402 break;
403 default:
404 break;
405 }
406 /* handle errors */
407 if (r) {
408 if (buffer.data) {free(buffer.data);}
409 return r;
410 }
412 /* now do the cleanup */
413 if (buffer.file) {
414 fclose(buffer.file); buffer.file=NULL;
415 im->rendered_image_size=0;
416 im->rendered_image=NULL;
417 } else {
418 im->rendered_image_size=buffer.len;
419 im->rendered_image=buffer.data;
420 }
422 /* and print stuff */
423 return print_calc(im);
424 }
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 */
430 if (sb->file) {
431 sb->len+=len;
432 fwrite(data,len,1,sb->file);
433 return 0;
434 }
435 /* if buffer is 0, then initialize */
436 if (! sb->data) {
437 /* make buffer a multiple of 8192 */
438 sb->allocated+=8192;
439 sb->allocated-=(sb->allocated%8192);
440 /* and allocate it */
441 sb->data=malloc(sb->allocated);
442 if (! sb->data) {
443 rrd_set_error("malloc issue");
444 return 1;
445 }
446 /* and initialize the buffer */
447 sb->len=0;
448 sb->data[0]=0;
449 }
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) {
454 sb->allocated+=8192;
455 }
456 /* try to resize it */
457 unsigned char* resized=(unsigned char*)realloc(sb->data,sb->allocated);
458 if (resized) {
459 sb->data=resized;
460 } else {
461 free(sb->data);
462 sb->data=NULL;
463 sb->allocated=0;
464 rrd_set_error("realloc issue");
465 return -1;
466 }
467 }
468 /* and finally add to the buffer */
469 memcpy(sb->data+sb->len,data,len);
470 sb->len+=len;
471 /* and 0 terminate it */
472 sb->data[sb->len]=0;
473 /* and return */
474 return 0;
475 }
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 */
479 char* timefmt=NULL;
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; }
485 /* row count */
486 unsigned long row_cnt=(end-start)/step;
488 /* estimate buffer size (to avoid multiple allocations) */
489 buffer->allocated=
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) */
493 ;
495 /* now start writing the header*/
496 if (addToBuffer(buffer,"\"time\"",6)) { return 1; }
497 char buf[256];
498 for(unsigned long i=0;i<col_cnt;i++) {
499 /* strip leading spaces */
500 char *t=legend_v[i]; while (isspace(*t)) { t++;}
501 /* and print it */
502 snprintf(buf,255,"%c\"%s\"",sep,t);
503 if (addToBuffer(buffer,buf,0)) { return 1;}
504 }
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) {
509 /* write time */
510 if (timefmt) {
511 struct tm loc;
512 localtime_r(&ti,&loc);
513 strftime(buf,254,timefmt,&loc);
514 } else {
515 snprintf(buf,254,"%ul",ti);
516 }
517 if (addToBuffer(buffer,buf,0)) { return 1; }
518 /* write the columns */
519 for(unsigned long i=0;i<col_cnt;i++) {
520 /* get the value */
521 rrd_value_t v=*ptr;ptr++;
522 /* and print it */
523 if (isnan(v)) {
524 snprintf(buf,255,"%c\"NaN\"",sep);
525 } else {
526 snprintf(buf,255,"%c\"%0.10e\"",sep,v);
527 }
528 if (addToBuffer(buffer,buf,0)) { return 1;}
529 }
530 /* and add a newline */
531 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
532 }
534 /* and return OK */
535 return 0;
536 }
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);
540 return 0;
541 }
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);
545 return 0;
546 }