2d0233d824c476aca80756c8cb5e4c286c7ac620
1 /****************************************************************************
2 * RRDtool 1.3.2 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"
13 #include "rrd_client.h"
15 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
16 #include <io.h>
17 #include <fcntl.h>
18 #endif
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 **);
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 **);
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 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 {"daemon", required_argument, 0, 'd'},
68 {0, 0, 0, 0}
69 };
71 optind = 0;
72 opterr = 0; /* initialize getopt */
74 rrd_graph_init(&im);
76 rrd_parsetime("end-24h", &start_tv);
77 rrd_parsetime("now", &end_tv);
79 while (1) {
80 int option_index = 0;
81 int opt;
83 opt = getopt_long(argc, argv, "s:e:m:d:", 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 = rrd_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 = rrd_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 'd':
114 {
115 int status;
116 if (im.use_rrdcached)
117 {
118 rrd_set_error ("You cannot specify --daemon "
119 "more than once.");
120 return (-1);
121 }
122 status = rrdc_connect (optarg);
123 if (status != 0)
124 {
125 rrd_set_error ("rrdc_connect(%s) failed with status %i.",
126 optarg, status);
127 return (-1);
128 }
129 im.use_rrdcached = 1;
130 break;
131 }
133 case '?':
134 rrd_set_error("unknown option '%s'", argv[optind - 1]);
135 return -1;
136 }
137 }
139 if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
140 return -1;
141 }
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 }
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 }
155 im.start = start_tmp;
156 im.end = end_tmp;
157 im.step = max((long) im.step, (im.end - im.start) / im.xsize);
159 rrd_graph_script(argc, argv, &im, 0);
160 if (rrd_test_error()) {
161 im_free(&im);
162 return -1;
163 }
165 if (im.gdes_c == 0) {
166 rrd_set_error("can't make a graph without contents");
167 im_free(&im);
168 return (-1);
169 }
171 if (im.use_rrdcached == 0)
172 {
173 char *temp;
175 temp = getenv (ENV_RRDCACHED_ADDRESS);
176 if (temp != NULL)
177 {
178 int status;
180 status = rrdc_connect (temp);
181 if (status != 0)
182 {
183 rrd_set_error ("rrdc_connect(%s) failed with status %i.",
184 temp, status);
185 return (-1);
186 }
187 im.use_rrdcached = 1;
188 }
189 }
191 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data) == -1) {
192 im_free(&im);
193 return -1;
194 }
196 im_free(&im);
197 return 0;
198 }
202 int rrd_xport_fn(
203 image_desc_t *im,
204 time_t *start,
205 time_t *end, /* which time frame do you want ?
206 * will be changed to represent reality */
207 unsigned long *step, /* which stepsize do you want?
208 * will be changed to represent reality */
209 unsigned long *col_cnt, /* number of data columns in the result */
210 char ***legend_v, /* legend entries */
211 rrd_value_t **data)
212 { /* two dimensional array containing the data */
214 int i = 0, j = 0;
215 unsigned long *ds_cnt; /* number of data sources in file */
216 unsigned long col, dst_row, row_cnt;
217 rrd_value_t *srcptr, *dstptr;
219 unsigned long nof_xports = 0;
220 unsigned long xport_counter = 0;
221 int *ref_list;
222 rrd_value_t **srcptr_list;
223 char **legend_list;
224 int ii = 0;
226 time_t start_tmp = 0;
227 time_t end_tmp = 0;
228 unsigned long step_tmp = 1;
230 /* pull the data from the rrd files ... */
231 if (data_fetch(im) == -1)
232 return -1;
234 /* evaluate CDEF operations ... */
235 if (data_calc(im) == -1)
236 return -1;
238 /* how many xports? */
239 for (i = 0; i < im->gdes_c; i++) {
240 switch (im->gdes[i].gf) {
241 case GF_XPORT:
242 nof_xports++;
243 break;
244 default:
245 break;
246 }
247 }
248 if (nof_xports == 0) {
249 rrd_set_error("no XPORT found, nothing to do");
250 return -1;
251 }
253 /* a list of referenced gdes */
254 ref_list = malloc(sizeof(int) * nof_xports);
255 if (ref_list == NULL)
256 return -1;
258 /* a list to save pointers into each gdes data */
259 srcptr_list = malloc(sizeof(srcptr) * nof_xports);
260 if (srcptr_list == NULL) {
261 free(ref_list);
262 return -1;
263 }
265 /* a list to save pointers to the column's legend entry */
266 /* this is a return value! */
267 legend_list = malloc(sizeof(char *) * nof_xports);
268 if (legend_list == NULL) {
269 free(srcptr_list);
270 free(ref_list);
271 return -1;
272 }
274 /* find referenced gdes and save their index and */
275 /* a pointer into their data */
276 for (i = 0; i < im->gdes_c; i++) {
277 switch (im->gdes[i].gf) {
278 case GF_XPORT:
279 ii = im->gdes[i].vidx;
280 if (xport_counter > nof_xports) {
281 rrd_set_error("too many xports: should not happen. Hmmm");
282 free(srcptr_list);
283 free(ref_list);
284 free(legend_list);
285 return -1;
286 }
287 srcptr_list[xport_counter] = im->gdes[ii].data;
288 ref_list[xport_counter++] = i;
289 break;
290 default:
291 break;
292 }
293 }
295 start_tmp = im->gdes[0].start;
296 end_tmp = im->gdes[0].end;
297 step_tmp = im->gdes[0].step;
299 /* fill some return values */
300 *col_cnt = nof_xports;
301 *start = start_tmp;
302 *end = end_tmp;
303 *step = step_tmp;
305 row_cnt = ((*end) - (*start)) / (*step);
307 /* room for rearranged data */
308 /* this is a return value! */
309 if (((*data) =
310 malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
311 free(srcptr_list);
312 free(ref_list);
313 free(legend_list);
314 rrd_set_error("malloc xport data area");
315 return (-1);
316 }
317 dstptr = (*data);
319 j = 0;
320 for (i = 0; i < im->gdes_c; i++) {
321 switch (im->gdes[i].gf) {
322 case GF_XPORT:
323 /* reserve room for one legend entry */
324 /* is FMT_LEG_LEN + 5 the correct size? */
325 if ((legend_list[j] =
326 malloc(sizeof(char) * (FMT_LEG_LEN + 5))) == NULL) {
327 free(srcptr_list);
328 free(ref_list);
329 free(*data);
330 *data = NULL;
331 while (--j > -1)
332 free(legend_list[j]);
333 free(legend_list);
334 rrd_set_error("malloc xport legend entry");
335 return (-1);
336 }
338 if (im->gdes[i].legend)
339 /* omit bounds check, should have the same size */
340 strcpy(legend_list[j++], im->gdes[i].legend);
341 else
342 legend_list[j++][0] = '\0';
344 break;
345 default:
346 break;
347 }
348 }
350 /* fill data structure */
351 for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
352 for (i = 0; i < (int) nof_xports; i++) {
353 j = ref_list[i];
354 ii = im->gdes[j].vidx;
355 ds_cnt = &im->gdes[ii].ds_cnt;
357 srcptr = srcptr_list[i];
358 for (col = 0; col < (*ds_cnt); col++) {
359 rrd_value_t newval = DNAN;
361 newval = srcptr[col];
363 if (im->gdes[ii].ds_namv && im->gdes[ii].ds_nam) {
364 if (strcmp(im->gdes[ii].ds_namv[col], im->gdes[ii].ds_nam)
365 == 0)
366 (*dstptr++) = newval;
367 } else {
368 (*dstptr++) = newval;
369 }
371 }
372 srcptr_list[i] += (*ds_cnt);
373 }
374 }
376 *legend_v = legend_list;
377 free(srcptr_list);
378 free(ref_list);
379 return 0;
381 }