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;
61 char *opt_daemon = 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 {"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 (opt_daemon != NULL)
119 rrd_set_error ("You cannot specify --daemon "
124 opt_daemon = strdup(optarg);
125 if (opt_daemon == 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(opt_daemon);
173 if (opt_daemon) free(opt_daemon);
174 if (status != 0) return status;
177 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data) == -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 */
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? */
221 for (i = 0; i < im->gdes_c; i++) {
222 switch (im->gdes[i].gf) {
230 if ((*col_cnt) == 0) {
231 rrd_set_error("no XPORT found, nothing to do");
235 /* a list of referenced gdes */
236 ref_list = (int*)malloc(sizeof(int) * (*col_cnt));
237 if (ref_list == NULL)
240 /* a list to save pointers to the column's legend entry */
241 /* this is a return value! */
242 legend_list = (char**)malloc(sizeof(char *) * (*col_cnt));
243 if (legend_list == NULL) {
248 /* lets find the step size we have to use for xport */
249 step_list = (long*)malloc(sizeof(long)*((*col_cnt)+1));
250 step_list_ptr = step_list;
252 for (i = 0; i < im->gdes_c; i++) {
253 switch (im->gdes[i].gf) {
255 ref_list[xport_counter++] = i;
256 *step_list_ptr = im->gdes[im->gdes[i].vidx].step;
257 /* printf("%s:%lu\n",im->gdes[i].legend,*step_list_ptr); */
259 /* reserve room for one legend entry */
260 /* is FMT_LEG_LEN + 5 the correct size? */
261 if ((legend_list[j] =
262 (char*)malloc(sizeof(char) * (FMT_LEG_LEN + 5))) == NULL) {
266 free(legend_list[j]);
268 rrd_set_error("malloc xport legend entry");
272 if (im->gdes[i].legend)
273 /* omit bounds check, should have the same size */
274 strcpy(legend_list[j++], im->gdes[i].legend);
276 legend_list[j++][0] = '\0';
283 /* find a common step */
284 *step = lcd(step_list);
285 /* printf("step: %lu\n",*step); */
288 *start = im->start - im->start % (*step);
289 *end = im->end - im->end % (*step) + (*step);
292 /* room for rearranged data */
293 /* this is a return value! */
294 row_cnt = ((*end) - (*start)) / (*step);
296 (rrd_value_t*)malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
299 rrd_set_error("malloc xport data area");
304 /* fill data structure */
305 for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
306 for (i = 0; i < (int) (*col_cnt); i++) {
307 long vidx = im->gdes[ref_list[i]].vidx;
308 time_t now = *start + dst_row * *step;
309 (*dstptr++) = im->gdes[vidx].data[(unsigned long)
311 (now - im->gdes[vidx].start)
312 /im->gdes[vidx].step)
313 * im->gdes[vidx].ds_cnt +
319 *legend_v = legend_list;