Code

faf73702e48417beb34054b3842311b331724cfd
[rrdtool.git] / src / rrd_xport.c
1 /****************************************************************************
2  * RRDtool 1.2.29  Copyright by Tobi Oetiker, 1997-2008
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"
14 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
15 #include <io.h>
16 #include <fcntl.h>
17 #endif
20 int       rrd_xport(
21     int,
22     char **,
23     int *,
24     time_t *,
25     time_t *,
26     unsigned long *,
27     unsigned long *,
28     char ***,
29     rrd_value_t **);
31 int       rrd_xport_fn(
32     image_desc_t *,
33     time_t *,
34     time_t *,
35     unsigned long *,
36     unsigned long *,
37     char ***,
38     rrd_value_t **);
43 int rrd_xport(
44     int argc,
45     char **argv,
46     int UNUSED(*xsize),
47     time_t *start,
48     time_t *end,        /* which time frame do you want ?
49                          * will be changed to represent reality */
50     unsigned long *step,    /* which stepsize do you want? 
51                              * will be changed to represent reality */
52     unsigned long *col_cnt, /* number of data columns in the result */
53     char ***legend_v,   /* legend entries */
54     rrd_value_t **data)
55 {                       /* two dimensional array containing the data */
57     image_desc_t im;
58     time_t    start_tmp = 0, end_tmp = 0;
59     struct rrd_time_value start_tv;
60     struct rrd_time_value end_tv;
61     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         {0, 0, 0, 0}
69     };
71     optind = 0;
72     opterr = 0;         /* initialize getopt */
74     rrd_graph_init(&im);
76     parsetime("end-24h", &start_tv);
77     parsetime("now", &end_tv);
79     while (1) {
80         int       option_index = 0;
81         int       opt;
83         opt = getopt_long(argc, argv, "s:e:m:", long_options, &option_index);
85         if (opt == EOF)
86             break;
88         switch (opt) {
89         case 261:
90             im.step = atoi(optarg);
91             break;
92         case 262:
93             break;
94         case 's':
95             if ((parsetime_error = parsetime(optarg, &start_tv))) {
96                 rrd_set_error("start time: %s", parsetime_error);
97                 return -1;
98             }
99             break;
100         case 'e':
101             if ((parsetime_error = parsetime(optarg, &end_tv))) {
102                 rrd_set_error("end time: %s", parsetime_error);
103                 return -1;
104             }
105             break;
106         case 'm':
107             im.xsize = atol(optarg);
108             if (im.xsize < 10) {
109                 rrd_set_error("maxrows below 10 rows");
110                 return -1;
111             }
112             break;
113         case '?':
114             rrd_set_error("unknown option '%s'", argv[optind - 1]);
115             return -1;
116         }
117     }
119     if (proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
120         return -1;
121     }
123     if (start_tmp < 3600 * 24 * 365 * 10) {
124         rrd_set_error("the first entry to fetch should be after 1980 (%ld)",
125                       start_tmp);
126         return -1;
127     }
129     if (end_tmp < start_tmp) {
130         rrd_set_error("start (%ld) should be less than end (%ld)",
131                       start_tmp, end_tmp);
132         return -1;
133     }
135     im.start = start_tmp;
136     im.end = end_tmp;
137     im.step = max((long) im.step, (im.end - im.start) / im.xsize);
139     rrd_graph_script(argc, argv, &im, 0);
140     if (rrd_test_error()) {
141         im_free(&im);
142         return -1;
143     }
145     if (im.gdes_c == 0) {
146         rrd_set_error("can't make an xport without contents");
147         im_free(&im);
148         return (-1);
149     }
151     if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data) == -1) {
152         im_free(&im);
153         return -1;
154     }
156     im_free(&im);
157     return 0;
162 int rrd_xport_fn(
163     image_desc_t *im,
164     time_t *start,
165     time_t *end,        /* which time frame do you want ?
166                          * will be changed to represent reality */
167     unsigned long *step,    /* which stepsize do you want? 
168                              * will be changed to represent reality */
169     unsigned long *col_cnt, /* number of data columns in the result */
170     char ***legend_v,   /* legend entries */
171     rrd_value_t **data)
172 {                       /* two dimensional array containing the data */
174     int       i = 0, j = 0;
175     unsigned long dst_row, row_cnt;
176     rrd_value_t  *dstptr;
178     unsigned long xport_counter = 0;
179     int      *ref_list;
180     long     *step_list;
181     long     *step_list_ptr;    
182     char    **legend_list;
185     /* pull the data from the rrd files ... */
186     if (data_fetch(im) == -1)
187         return -1;
189     /* evaluate CDEF  operations ... */
190     if (data_calc(im) == -1)
191         return -1;
193     /* how many xports? */
194     *col_cnt = 0;    
195     for (i = 0; i < im->gdes_c; i++) {
196         switch (im->gdes[i].gf) {
197         case GF_XPORT:
198             (*col_cnt)++;
199             break;
200         default:
201             break;
202         }
203     }
204     if ((*col_cnt) == 0) {
205         rrd_set_error("no XPORT found, nothing to do");
206         return -1;
207     }
209     /* a list of referenced gdes */
210     ref_list = malloc(sizeof(int) * (*col_cnt));
211     if (ref_list == NULL)
212         return -1;
214     /* a list to save pointers to the column's legend entry */
215     /* this is a return value! */
216     legend_list = malloc(sizeof(char *) * (*col_cnt));
217     if (legend_list == NULL) {
218         free(ref_list);
219         return -1;
220     }
222     /* lets find the step size we have to use for xport */
223     step_list = malloc(sizeof(long)*((*col_cnt)+1));
224     step_list_ptr = step_list;
225     j = 0;
226     for (i = 0; i < im->gdes_c; i++) {
227         switch (im->gdes[i].gf) {
228         case GF_XPORT:
229             ref_list[xport_counter++] = i;
230             *step_list_ptr = im->gdes[im->gdes[i].vidx].step;
231             printf("%s:%lu\n",im->gdes[i].legend,*step_list_ptr);
232             *step_list_ptr++;
233             /* reserve room for one legend entry */
234             /* is FMT_LEG_LEN + 5 the correct size? */
235             if ((legend_list[j] =
236                 malloc(sizeof(char) * (FMT_LEG_LEN + 5))) == NULL) {
237                 free(ref_list);
238                 *data = NULL;
239                 while (--j > -1)
240                     free(legend_list[j]);
241                 free(legend_list);
242                 rrd_set_error("malloc xport legend entry");
243                 return (-1);
244             }
246             if (im->gdes[i].legend)
247                 /* omit bounds check, should have the same size */
248                 strcpy(legend_list[j++], im->gdes[i].legend);
249             else
250                 legend_list[j++][0] = '\0';
251             break;
252         default:
253             break;            
254         }
255     }
256     *step_list_ptr=0;    
257     /* find a common step */
258     *step = lcd(step_list);
259     printf("step: %lu\n",*step);
260     free(step_list);
261     
262     *start =  im->start - im->start % (*step);
263     *end = im->end - im->end % (*step);
264     
266     /* room for rearranged data */
267     /* this is a return value! */
268     row_cnt = ((*end) - (*start)) / (*step);
269     if (((*data) =
270          malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
271         free(ref_list);
272         free(legend_list);
273         rrd_set_error("malloc xport data area");
274         return (-1);
275     }
276     dstptr = (*data);
278     /* fill data structure */
279     for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
280         for (i = 0; i < (int) (*col_cnt); i++) {
281             long vidx = im->gdes[ref_list[i]].vidx;
282             time_t now = *start + dst_row * *step;
283             (*dstptr++) = im->gdes[vidx].data[(unsigned long)
284                                               floor((double)
285                                                     (now - im->gdes[vidx].start)
286                                                     /im->gdes[vidx].step)
287                                               * im->gdes[vidx].ds_cnt +
288                                               im->gdes[vidx].ds];
290         }
291     }
293     *legend_v = legend_list;
294     free(ref_list);
295     return 0;