1 /****************************************************************************
2 * RRDtool 1.3.1 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 a graph 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 *ds_cnt; /* number of data sources in file */
175 unsigned long col, dst_row, row_cnt;
176 rrd_value_t *srcptr, *dstptr;
178 unsigned long nof_xports = 0;
179 unsigned long xport_counter = 0;
180 int *ref_list;
181 rrd_value_t **srcptr_list;
182 char **legend_list;
183 int ii = 0;
185 time_t start_tmp = 0;
186 time_t end_tmp = 0;
187 unsigned long step_tmp = 1;
189 /* pull the data from the rrd files ... */
190 if (data_fetch(im) == -1)
191 return -1;
193 /* evaluate CDEF operations ... */
194 if (data_calc(im) == -1)
195 return -1;
197 /* how many xports? */
198 for (i = 0; i < im->gdes_c; i++) {
199 switch (im->gdes[i].gf) {
200 case GF_XPORT:
201 nof_xports++;
202 break;
203 default:
204 break;
205 }
206 }
207 if (nof_xports == 0) {
208 rrd_set_error("no XPORT found, nothing to do");
209 return -1;
210 }
212 /* a list of referenced gdes */
213 ref_list = malloc(sizeof(int) * nof_xports);
214 if (ref_list == NULL)
215 return -1;
217 /* a list to save pointers into each gdes data */
218 srcptr_list = malloc(sizeof(srcptr) * nof_xports);
219 if (srcptr_list == NULL) {
220 free(ref_list);
221 return -1;
222 }
224 /* a list to save pointers to the column's legend entry */
225 /* this is a return value! */
226 legend_list = malloc(sizeof(char *) * nof_xports);
227 if (legend_list == NULL) {
228 free(srcptr_list);
229 free(ref_list);
230 return -1;
231 }
233 /* find referenced gdes and save their index and */
234 /* a pointer into their data */
235 for (i = 0; i < im->gdes_c; i++) {
236 switch (im->gdes[i].gf) {
237 case GF_XPORT:
238 ii = im->gdes[i].vidx;
239 if (xport_counter > nof_xports) {
240 rrd_set_error("too many xports: should not happen. Hmmm");
241 free(srcptr_list);
242 free(ref_list);
243 free(legend_list);
244 return -1;
245 }
246 srcptr_list[xport_counter] = im->gdes[ii].data;
247 ref_list[xport_counter++] = i;
248 break;
249 default:
250 break;
251 }
252 }
254 start_tmp = im->gdes[0].start;
255 end_tmp = im->gdes[0].end;
256 step_tmp = im->gdes[0].step;
258 /* fill some return values */
259 *col_cnt = nof_xports;
260 *start = start_tmp;
261 *end = end_tmp;
262 *step = step_tmp;
264 row_cnt = ((*end) - (*start)) / (*step);
266 /* room for rearranged data */
267 /* this is a return value! */
268 if (((*data) =
269 malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
270 free(srcptr_list);
271 free(ref_list);
272 free(legend_list);
273 rrd_set_error("malloc xport data area");
274 return (-1);
275 }
276 dstptr = (*data);
278 j = 0;
279 for (i = 0; i < im->gdes_c; i++) {
280 switch (im->gdes[i].gf) {
281 case GF_XPORT:
282 /* reserve room for one legend entry */
283 /* is FMT_LEG_LEN + 5 the correct size? */
284 if ((legend_list[j] =
285 malloc(sizeof(char) * (FMT_LEG_LEN + 5))) == NULL) {
286 free(srcptr_list);
287 free(ref_list);
288 free(*data);
289 *data = NULL;
290 while (--j > -1)
291 free(legend_list[j]);
292 free(legend_list);
293 rrd_set_error("malloc xport legend entry");
294 return (-1);
295 }
297 if (im->gdes[i].legend)
298 /* omit bounds check, should have the same size */
299 strcpy(legend_list[j++], im->gdes[i].legend);
300 else
301 legend_list[j++][0] = '\0';
303 break;
304 default:
305 break;
306 }
307 }
309 /* fill data structure */
310 for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
311 for (i = 0; i < (int) nof_xports; i++) {
312 j = ref_list[i];
313 ii = im->gdes[j].vidx;
314 ds_cnt = &im->gdes[ii].ds_cnt;
316 srcptr = srcptr_list[i];
317 for (col = 0; col < (*ds_cnt); col++) {
318 rrd_value_t newval = DNAN;
320 newval = srcptr[col];
322 if (im->gdes[ii].ds_namv && im->gdes[ii].ds_nam) {
323 if (strcmp(im->gdes[ii].ds_namv[col], im->gdes[ii].ds_nam)
324 == 0)
325 (*dstptr++) = newval;
326 } else {
327 (*dstptr++) = newval;
328 }
330 }
331 srcptr_list[i] += (*ds_cnt);
332 }
333 }
335 *legend_v = legend_list;
336 free(srcptr_list);
337 free(ref_list);
338 return 0;
340 }