prepare for the release of rrdtool-1.4.2
[rrdtool.git] / src / rrd_xport.c
1 /****************************************************************************
2  * RRDtool 1.4.2  Copyright by Tobi Oetiker, 1997-2009
3  ****************************************************************************
4  * rrd_xport.c  export RRD data 
5  ****************************************************************************/
6
7 #include <sys/stat.h>
8
9 #include "rrd_tool.h"
10 #include "rrd_graph.h"
11 #include "rrd_xport.h"
12 #include "unused.h"
13 #include "rrd_client.h"
14
15 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
16 #include <io.h>
17 #include <fcntl.h>
18 #endif
19
20
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 **);
31
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
41
42
43
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;
61     char     *opt_daemon = NULL;
62
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'},
70         {0, 0, 0, 0}
71     };
72
73     optind = 0;
74     opterr = 0;         /* initialize getopt */
75
76     rrd_graph_init(&im);
77
78     rrd_parsetime("end-24h", &start_tv);
79     rrd_parsetime("now", &end_tv);
80
81     while (1) {
82         int       option_index = 0;
83         int       opt;
84
85         opt = getopt_long(argc, argv, "s:e:m:d:", long_options, &option_index);
86
87         if (opt == EOF)
88             break;
89
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 (opt_daemon != NULL)
118             {
119                 rrd_set_error ("You cannot specify --daemon "
120                         "more than once.");
121                 return (-1);
122             }
123
124             opt_daemon = strdup(optarg);
125             if (opt_daemon == NULL)
126             {
127                 rrd_set_error("strdup error");
128                 return -1;
129             }
130             break;
131         }
132
133         case '?':
134             rrd_set_error("unknown option '%s'", argv[optind - 1]);
135             return -1;
136         }
137     }
138
139     if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
140         return -1;
141     }
142
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     }
148
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     }
154
155     im.start = start_tmp;
156     im.end = end_tmp;
157     im.step = max((long) im.step, (im.end - im.start) / im.xsize);
158
159     rrd_graph_script(argc, argv, &im, 0);
160     if (rrd_test_error()) {
161         im_free(&im);
162         return -1;
163     }
164
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     }
170
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;
175     }
176
177     if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data) == -1) {
178         im_free(&im);
179         return -1;
180     }
181
182     im_free(&im);
183     return 0;
184 }
185
186
187
188 int rrd_xport_fn(
189     image_desc_t *im,
190     time_t *start,
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 */
197     rrd_value_t **data)
198 {                       /* two dimensional array containing the data */
199
200     int       i = 0, j = 0;
201     unsigned long dst_row, row_cnt;
202     rrd_value_t  *dstptr;
203
204     unsigned long xport_counter = 0;
205     int      *ref_list;
206     long     *step_list;
207     long     *step_list_ptr;    
208     char    **legend_list;
209
210
211     /* pull the data from the rrd files ... */
212     if (data_fetch(im) == -1)
213         return -1;
214
215     /* evaluate CDEF  operations ... */
216     if (data_calc(im) == -1)
217         return -1;
218
219     /* how many xports? */
220     *col_cnt = 0;    
221     for (i = 0; i < im->gdes_c; i++) {
222         switch (im->gdes[i].gf) {
223         case GF_XPORT:
224             (*col_cnt)++;
225             break;
226         default:
227             break;
228         }
229     }
230     if ((*col_cnt) == 0) {
231         rrd_set_error("no XPORT found, nothing to do");
232         return -1;
233     }
234
235     /* a list of referenced gdes */
236     ref_list = (int*)malloc(sizeof(int) * (*col_cnt));
237     if (ref_list == NULL)
238         return -1;
239
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) {
244         free(ref_list);
245         return -1;
246     }
247
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;
251     j = 0;
252     for (i = 0; i < im->gdes_c; i++) {
253         switch (im->gdes[i].gf) {
254         case GF_XPORT:
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); */
258             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) {
263                 free(ref_list);
264                 *data = NULL;
265                 while (--j > -1)
266                     free(legend_list[j]);
267                 free(legend_list);
268                 rrd_set_error("malloc xport legend entry");
269                 return (-1);
270             }
271
272             if (im->gdes[i].legend)
273                 /* omit bounds check, should have the same size */
274                 strcpy(legend_list[j++], im->gdes[i].legend);
275             else
276                 legend_list[j++][0] = '\0';
277             break;
278         default:
279             break;            
280         }
281     }
282     *step_list_ptr=0;    
283     /* find a common step */
284     *step = lcd(step_list);
285     /* printf("step: %lu\n",*step); */
286     free(step_list);
287     
288     *start =  im->start - im->start % (*step);
289     *end = im->end - im->end % (*step);
290     
291
292     /* room for rearranged data */
293     /* this is a return value! */
294     row_cnt = ((*end) - (*start)) / (*step);
295     if (((*data) =
296         (rrd_value_t*)malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
297         free(ref_list);
298         free(legend_list);
299         rrd_set_error("malloc xport data area");
300         return (-1);
301     }
302     dstptr = (*data);
303
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)
310                                               floor((double)
311                                                     (now - im->gdes[vidx].start)
312                                                     /im->gdes[vidx].step)
313                                               * im->gdes[vidx].ds_cnt +
314                                               im->gdes[vidx].ds];
315
316         }
317     }
318
319     *legend_v = legend_list;
320     free(ref_list);
321     return 0;
322
323 }