Ensure that response_read() always calls fflush() or fclose().
[rrdtool.git] / src / rrd_xport.c
1 /****************************************************************************
2  * RRDtool 1.4.3  Copyright by Tobi Oetiker, 1997-2010
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
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         {"daemon", required_argument, 0, 'd'},
69         {0, 0, 0, 0}
70     };
71
72     optind = 0;
73     opterr = 0;         /* initialize getopt */
74
75     rrd_graph_init(&im);
76
77     rrd_parsetime("end-24h", &start_tv);
78     rrd_parsetime("now", &end_tv);
79
80     while (1) {
81         int       option_index = 0;
82         int       opt;
83
84         opt = getopt_long(argc, argv, "s:e:m:d:", long_options, &option_index);
85
86         if (opt == EOF)
87             break;
88
89         switch (opt) {
90         case 261:
91             im.step = atoi(optarg);
92             break;
93         case 262:
94             break;
95         case 's':
96             if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
97                 rrd_set_error("start time: %s", parsetime_error);
98                 return -1;
99             }
100             break;
101         case 'e':
102             if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
103                 rrd_set_error("end time: %s", parsetime_error);
104                 return -1;
105             }
106             break;
107         case 'm':
108             im.xsize = atol(optarg);
109             if (im.xsize < 10) {
110                 rrd_set_error("maxrows below 10 rows");
111                 return -1;
112             }
113             break;
114         case 'd':
115         {
116             if (im.daemon_addr != NULL)
117             {
118                 rrd_set_error ("You cannot specify --daemon "
119                         "more than once.");
120                 return (-1);
121             }
122
123             im.daemon_addr = strdup(optarg);
124             if (im.daemon_addr == NULL)
125             {
126                 rrd_set_error("strdup error");
127                 return -1;
128             }
129             break;
130         }
131
132         case '?':
133             rrd_set_error("unknown option '%s'", argv[optind - 1]);
134             return -1;
135         }
136     }
137
138     if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
139         return -1;
140     }
141
142     if (start_tmp < 3600 * 24 * 365 * 10) {
143         rrd_set_error("the first entry to fetch should be after 1980 (%ld)",
144                       start_tmp);
145         return -1;
146     }
147
148     if (end_tmp < start_tmp) {
149         rrd_set_error("start (%ld) should be less than end (%ld)",
150                       start_tmp, end_tmp);
151         return -1;
152     }
153
154     im.start = start_tmp;
155     im.end = end_tmp;
156     im.step = max((long) im.step, (im.end - im.start) / im.xsize);
157
158     rrd_graph_script(argc, argv, &im, 0);
159     if (rrd_test_error()) {
160         im_free(&im);
161         return -1;
162     }
163
164     if (im.gdes_c == 0) {
165         rrd_set_error("can't make an xport without contents");
166         im_free(&im);
167         return (-1);
168     }
169
170     {   /* try to connect to rrdcached */
171         int status = rrdc_connect(im.daemon_addr);
172         if (status != 0) return status;
173     }
174
175     if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data) == -1) {
176         im_free(&im);
177         return -1;
178     }
179
180     im_free(&im);
181     return 0;
182 }
183
184
185
186 int rrd_xport_fn(
187     image_desc_t *im,
188     time_t *start,
189     time_t *end,        /* which time frame do you want ?
190                          * will be changed to represent reality */
191     unsigned long *step,    /* which stepsize do you want? 
192                              * will be changed to represent reality */
193     unsigned long *col_cnt, /* number of data columns in the result */
194     char ***legend_v,   /* legend entries */
195     rrd_value_t **data)
196 {                       /* two dimensional array containing the data */
197
198     int       i = 0, j = 0;
199     unsigned long dst_row, row_cnt;
200     rrd_value_t  *dstptr;
201
202     unsigned long xport_counter = 0;
203     int      *ref_list;
204     long     *step_list;
205     long     *step_list_ptr;    
206     char    **legend_list;
207
208
209     /* pull the data from the rrd files ... */
210     if (data_fetch(im) == -1)
211         return -1;
212
213     /* evaluate CDEF  operations ... */
214     if (data_calc(im) == -1)
215         return -1;
216
217     /* how many xports? */
218     *col_cnt = 0;    
219     for (i = 0; i < im->gdes_c; i++) {
220         switch (im->gdes[i].gf) {
221         case GF_XPORT:
222             (*col_cnt)++;
223             break;
224         default:
225             break;
226         }
227     }
228     if ((*col_cnt) == 0) {
229         rrd_set_error("no XPORT found, nothing to do");
230         return -1;
231     }
232
233     /* a list of referenced gdes */
234     ref_list = (int*)malloc(sizeof(int) * (*col_cnt));
235     if (ref_list == NULL)
236         return -1;
237
238     /* a list to save pointers to the column's legend entry */
239     /* this is a return value! */
240     legend_list = (char**)malloc(sizeof(char *) * (*col_cnt));
241     if (legend_list == NULL) {
242         free(ref_list);
243         return -1;
244     }
245
246     /* lets find the step size we have to use for xport */
247     step_list = (long*)malloc(sizeof(long)*((*col_cnt)+1));
248     step_list_ptr = step_list;
249     j = 0;
250     for (i = 0; i < im->gdes_c; i++) {
251         switch (im->gdes[i].gf) {
252         case GF_XPORT:
253             ref_list[xport_counter++] = i;
254             *step_list_ptr = im->gdes[im->gdes[i].vidx].step;
255             /* printf("%s:%lu\n",im->gdes[i].legend,*step_list_ptr); */
256             step_list_ptr++;
257             /* reserve room for one legend entry */
258             /* is FMT_LEG_LEN + 5 the correct size? */
259             if ((legend_list[j] =
260                 (char*)malloc(sizeof(char) * (FMT_LEG_LEN + 5))) == NULL) {
261                 free(ref_list);
262                 *data = NULL;
263                 while (--j > -1)
264                     free(legend_list[j]);
265                 free(legend_list);
266                 rrd_set_error("malloc xport legend entry");
267                 return (-1);
268             }
269
270             if (im->gdes[i].legend)
271                 /* omit bounds check, should have the same size */
272                 strcpy(legend_list[j++], im->gdes[i].legend);
273             else
274                 legend_list[j++][0] = '\0';
275             break;
276         default:
277             break;            
278         }
279     }
280     *step_list_ptr=0;    
281     /* find a common step */
282     *step = lcd(step_list);
283     /* printf("step: %lu\n",*step); */
284     free(step_list);
285     
286     *start =  im->start - im->start % (*step);
287     *end = im->end - im->end % (*step) + (*step);
288     
289
290     /* room for rearranged data */
291     /* this is a return value! */
292     row_cnt = ((*end) - (*start)) / (*step);
293     if (((*data) =
294         (rrd_value_t*)malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
295         free(ref_list);
296         free(legend_list);
297         rrd_set_error("malloc xport data area");
298         return (-1);
299     }
300     dstptr = (*data);
301
302     /* fill data structure */
303     for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
304         for (i = 0; i < (int) (*col_cnt); i++) {
305             long vidx = im->gdes[ref_list[i]].vidx;
306             time_t now = *start + dst_row * *step;
307             (*dstptr++) = im->gdes[vidx].data[(unsigned long)
308                                               floor((double)
309                                                     (now - im->gdes[vidx].start)
310                                                     /im->gdes[vidx].step)
311                                               * im->gdes[vidx].ds_cnt +
312                                               im->gdes[vidx].ds];
313
314         }
315     }
316
317     *legend_v = legend_list;
318     free(ref_list);
319     return 0;
320
321 }