c561a5a29aedae6991bb32bed574f99182938a65
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 char *opt_daemon = NULL;
63 struct option long_options[] = {
64 {"start", required_argument, 0, 's'},
65 {"end", required_argument, 0, 'e'},
66 {"maxrows", required_argument, 0, 'm'},
67 {"step", required_argument, 0, 261},
68 {"enumds", no_argument, 0, 262}, /* these are handled in the frontend ... */
69 {"daemon", required_argument, 0, 'd'},
70 {0, 0, 0, 0}
71 };
73 optind = 0;
74 opterr = 0; /* initialize getopt */
76 rrd_graph_init(&im);
78 rrd_parsetime("end-24h", &start_tv);
79 rrd_parsetime("now", &end_tv);
81 while (1) {
82 int option_index = 0;
83 int opt;
85 opt = getopt_long(argc, argv, "s:e:m:d:", long_options, &option_index);
87 if (opt == EOF)
88 break;
90 switch (opt) {
91 case 261:
92 im.step = atoi(optarg);
93 break;
94 case 262:
95 break;
96 case 's':
97 if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
98 rrd_set_error("start time: %s", parsetime_error);
99 return -1;
100 }
101 break;
102 case 'e':
103 if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
104 rrd_set_error("end time: %s", parsetime_error);
105 return -1;
106 }
107 break;
108 case 'm':
109 im.xsize = atol(optarg);
110 if (im.xsize < 10) {
111 rrd_set_error("maxrows below 10 rows");
112 return -1;
113 }
114 break;
115 case 'd':
116 {
117 if (opt_daemon != NULL)
118 {
119 rrd_set_error ("You cannot specify --daemon "
120 "more than once.");
121 return (-1);
122 }
124 opt_daemon = strdup(optarg);
125 if (opt_daemon == NULL)
126 {
127 rrd_set_error("strdup error");
128 return -1;
129 }
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 { /* try to connect to rrdcached */
172 int status = rrdc_connect(opt_daemon);
173 if (opt_daemon) free(opt_daemon);
174 if (status != 0) return status;
175 }
177 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data) == -1) {
178 im_free(&im);
179 return -1;
180 }
182 im_free(&im);
183 return 0;
184 }
188 int rrd_xport_fn(
189 image_desc_t *im,
190 time_t *start,
191 time_t *end, /* which time frame do you want ?
192 * will be changed to represent reality */
193 unsigned long *step, /* which stepsize do you want?
194 * will be changed to represent reality */
195 unsigned long *col_cnt, /* number of data columns in the result */
196 char ***legend_v, /* legend entries */
197 rrd_value_t **data)
198 { /* two dimensional array containing the data */
200 int i = 0, j = 0;
201 unsigned long *ds_cnt; /* number of data sources in file */
202 unsigned long col, dst_row, row_cnt;
203 rrd_value_t *srcptr, *dstptr;
205 unsigned long nof_xports = 0;
206 unsigned long xport_counter = 0;
207 int *ref_list;
208 rrd_value_t **srcptr_list;
209 char **legend_list;
210 int ii = 0;
212 time_t start_tmp = 0;
213 time_t end_tmp = 0;
214 unsigned long step_tmp = 1;
216 /* pull the data from the rrd files ... */
217 if (data_fetch(im) == -1)
218 return -1;
220 /* evaluate CDEF operations ... */
221 if (data_calc(im) == -1)
222 return -1;
224 /* how many xports? */
225 for (i = 0; i < im->gdes_c; i++) {
226 switch (im->gdes[i].gf) {
227 case GF_XPORT:
228 nof_xports++;
229 break;
230 default:
231 break;
232 }
233 }
234 if (nof_xports == 0) {
235 rrd_set_error("no XPORT found, nothing to do");
236 return -1;
237 }
239 /* a list of referenced gdes */
240 ref_list = malloc(sizeof(int) * nof_xports);
241 if (ref_list == NULL)
242 return -1;
244 /* a list to save pointers into each gdes data */
245 srcptr_list = malloc(sizeof(srcptr) * nof_xports);
246 if (srcptr_list == NULL) {
247 free(ref_list);
248 return -1;
249 }
251 /* a list to save pointers to the column's legend entry */
252 /* this is a return value! */
253 legend_list = malloc(sizeof(char *) * nof_xports);
254 if (legend_list == NULL) {
255 free(srcptr_list);
256 free(ref_list);
257 return -1;
258 }
260 /* find referenced gdes and save their index and */
261 /* a pointer into their data */
262 for (i = 0; i < im->gdes_c; i++) {
263 switch (im->gdes[i].gf) {
264 case GF_XPORT:
265 ii = im->gdes[i].vidx;
266 if (xport_counter > nof_xports) {
267 rrd_set_error("too many xports: should not happen. Hmmm");
268 free(srcptr_list);
269 free(ref_list);
270 free(legend_list);
271 return -1;
272 }
273 srcptr_list[xport_counter] = im->gdes[ii].data;
274 ref_list[xport_counter++] = i;
275 break;
276 default:
277 break;
278 }
279 }
281 start_tmp = im->gdes[0].start;
282 end_tmp = im->gdes[0].end;
283 step_tmp = im->gdes[0].step;
285 /* fill some return values */
286 *col_cnt = nof_xports;
287 *start = start_tmp;
288 *end = end_tmp;
289 *step = step_tmp;
291 row_cnt = ((*end) - (*start)) / (*step);
293 /* room for rearranged data */
294 /* this is a return value! */
295 if (((*data) =
296 malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
297 free(srcptr_list);
298 free(ref_list);
299 free(legend_list);
300 rrd_set_error("malloc xport data area");
301 return (-1);
302 }
303 dstptr = (*data);
305 j = 0;
306 for (i = 0; i < im->gdes_c; i++) {
307 switch (im->gdes[i].gf) {
308 case GF_XPORT:
309 /* reserve room for one legend entry */
310 /* is FMT_LEG_LEN + 5 the correct size? */
311 if ((legend_list[j] =
312 malloc(sizeof(char) * (FMT_LEG_LEN + 5))) == NULL) {
313 free(srcptr_list);
314 free(ref_list);
315 free(*data);
316 *data = NULL;
317 while (--j > -1)
318 free(legend_list[j]);
319 free(legend_list);
320 rrd_set_error("malloc xport legend entry");
321 return (-1);
322 }
324 if (im->gdes[i].legend)
325 /* omit bounds check, should have the same size */
326 strcpy(legend_list[j++], im->gdes[i].legend);
327 else
328 legend_list[j++][0] = '\0';
330 break;
331 default:
332 break;
333 }
334 }
336 /* fill data structure */
337 for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
338 for (i = 0; i < (int) nof_xports; i++) {
339 j = ref_list[i];
340 ii = im->gdes[j].vidx;
341 ds_cnt = &im->gdes[ii].ds_cnt;
343 srcptr = srcptr_list[i];
344 for (col = 0; col < (*ds_cnt); col++) {
345 rrd_value_t newval = DNAN;
347 newval = srcptr[col];
349 if (im->gdes[ii].ds_namv && im->gdes[ii].ds_nam) {
350 if (strcmp(im->gdes[ii].ds_namv[col], im->gdes[ii].ds_nam)
351 == 0)
352 (*dstptr++) = newval;
353 } else {
354 (*dstptr++) = newval;
355 }
357 }
358 srcptr_list[i] += (*ds_cnt);
359 }
360 }
362 *legend_v = legend_list;
363 free(srcptr_list);
364 free(ref_list);
365 return 0;
367 }