66cb5a1b717006ed65fd1c9de4c69ff94074f4af
1 /****************************************************************************
2 * RRDtool 1.3.5 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 rrd_time_value_t start_tv, end_tv;
60 char *parsetime_error = NULL;
61 struct option long_options[] = {
62 {"start", required_argument, 0, 's'},
63 {"end", required_argument, 0, 'e'},
64 {"maxrows", required_argument, 0, 'm'},
65 {"step", required_argument, 0, 261},
66 {"enumds", no_argument, 0, 262}, /* these are handled in the frontend ... */
67 {0, 0, 0, 0}
68 };
70 optind = 0;
71 opterr = 0; /* initialize getopt */
73 rrd_graph_init(&im);
75 rrd_parsetime("end-24h", &start_tv);
76 rrd_parsetime("now", &end_tv);
78 while (1) {
79 int option_index = 0;
80 int opt;
82 opt = getopt_long(argc, argv, "s:e:m:", long_options, &option_index);
84 if (opt == EOF)
85 break;
87 switch (opt) {
88 case 261:
89 im.step = atoi(optarg);
90 break;
91 case 262:
92 break;
93 case 's':
94 if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
95 rrd_set_error("start time: %s", parsetime_error);
96 return -1;
97 }
98 break;
99 case 'e':
100 if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
101 rrd_set_error("end time: %s", parsetime_error);
102 return -1;
103 }
104 break;
105 case 'm':
106 im.xsize = atol(optarg);
107 if (im.xsize < 10) {
108 rrd_set_error("maxrows below 10 rows");
109 return -1;
110 }
111 break;
112 case '?':
113 rrd_set_error("unknown option '%s'", argv[optind - 1]);
114 return -1;
115 }
116 }
118 if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
119 return -1;
120 }
122 if (start_tmp < 3600 * 24 * 365 * 10) {
123 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",
124 start_tmp);
125 return -1;
126 }
128 if (end_tmp < start_tmp) {
129 rrd_set_error("start (%ld) should be less than end (%ld)",
130 start_tmp, end_tmp);
131 return -1;
132 }
134 im.start = start_tmp;
135 im.end = end_tmp;
136 im.step = max((long) im.step, (im.end - im.start) / im.xsize);
138 rrd_graph_script(argc, argv, &im, 0);
139 if (rrd_test_error()) {
140 im_free(&im);
141 return -1;
142 }
144 if (im.gdes_c == 0) {
145 rrd_set_error("can't make an xport without contents");
146 im_free(&im);
147 return (-1);
148 }
150 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data) == -1) {
151 im_free(&im);
152 return -1;
153 }
155 im_free(&im);
156 return 0;
157 }
161 int rrd_xport_fn(
162 image_desc_t *im,
163 time_t *start,
164 time_t *end, /* which time frame do you want ?
165 * will be changed to represent reality */
166 unsigned long *step, /* which stepsize do you want?
167 * will be changed to represent reality */
168 unsigned long *col_cnt, /* number of data columns in the result */
169 char ***legend_v, /* legend entries */
170 rrd_value_t **data)
171 { /* two dimensional array containing the data */
173 int i = 0, j = 0;
174 unsigned long dst_row, row_cnt;
175 rrd_value_t *dstptr;
177 unsigned long xport_counter = 0;
178 int *ref_list;
179 long *step_list;
180 long *step_list_ptr;
181 char **legend_list;
184 /* pull the data from the rrd files ... */
185 if (data_fetch(im) == -1)
186 return -1;
188 /* evaluate CDEF operations ... */
189 if (data_calc(im) == -1)
190 return -1;
192 /* how many xports? */
193 *col_cnt = 0;
194 for (i = 0; i < im->gdes_c; i++) {
195 switch (im->gdes[i].gf) {
196 case GF_XPORT:
197 (*col_cnt)++;
198 break;
199 default:
200 break;
201 }
202 }
203 if ((*col_cnt) == 0) {
204 rrd_set_error("no XPORT found, nothing to do");
205 return -1;
206 }
208 /* a list of referenced gdes */
209 ref_list = (int*)malloc(sizeof(int) * (*col_cnt));
210 if (ref_list == NULL)
211 return -1;
213 /* a list to save pointers to the column's legend entry */
214 /* this is a return value! */
215 legend_list = (char**)malloc(sizeof(char *) * (*col_cnt));
216 if (legend_list == NULL) {
217 free(ref_list);
218 return -1;
219 }
221 /* lets find the step size we have to use for xport */
222 step_list = (long*)malloc(sizeof(long)*((*col_cnt)+1));
223 step_list_ptr = step_list;
224 j = 0;
225 for (i = 0; i < im->gdes_c; i++) {
226 switch (im->gdes[i].gf) {
227 case GF_XPORT:
228 ref_list[xport_counter++] = i;
229 *step_list_ptr = im->gdes[im->gdes[i].vidx].step;
230 printf("%s:%lu\n",im->gdes[i].legend,*step_list_ptr);
231 *step_list_ptr++;
232 /* reserve room for one legend entry */
233 /* is FMT_LEG_LEN + 5 the correct size? */
234 if ((legend_list[j] =
235 (char*)malloc(sizeof(char) * (FMT_LEG_LEN + 5))) == NULL) {
236 free(ref_list);
237 *data = NULL;
238 while (--j > -1)
239 free(legend_list[j]);
240 free(legend_list);
241 rrd_set_error("malloc xport legend entry");
242 return (-1);
243 }
245 if (im->gdes[i].legend)
246 /* omit bounds check, should have the same size */
247 strcpy(legend_list[j++], im->gdes[i].legend);
248 else
249 legend_list[j++][0] = '\0';
250 break;
251 default:
252 break;
253 }
254 }
255 *step_list_ptr=0;
256 /* find a common step */
257 *step = lcd(step_list);
258 printf("step: %lu\n",*step);
259 free(step_list);
261 *start = im->start - im->start % (*step);
262 *end = im->end - im->end % (*step);
265 /* room for rearranged data */
266 /* this is a return value! */
267 row_cnt = ((*end) - (*start)) / (*step);
268 if (((*data) =
269 (rrd_value_t*)malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
270 free(ref_list);
271 free(legend_list);
272 rrd_set_error("malloc xport data area");
273 return (-1);
274 }
275 dstptr = (*data);
277 /* fill data structure */
278 for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
279 for (i = 0; i < (int) (*col_cnt); i++) {
280 long vidx = im->gdes[ref_list[i]].vidx;
281 time_t now = *start + dst_row * *step;
282 (*dstptr++) = im->gdes[vidx].data[(unsigned long)
283 floor((double)
284 (now - im->gdes[vidx].start)
285 /im->gdes[vidx].step)
286 * im->gdes[vidx].ds_cnt +
287 im->gdes[vidx].ds];
289 }
290 }
292 *legend_v = legend_list;
293 free(ref_list);
294 return 0;
296 }