1 /****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
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;
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 {"daemon", required_argument, 0, 'd'},
69 {0, 0, 0, 0}
70 };
72 optind = 0;
73 opterr = 0; /* initialize getopt */
75 rrd_graph_init(&im);
77 rrd_parsetime("end-24h", &start_tv);
78 rrd_parsetime("now", &end_tv);
80 while (1) {
81 int option_index = 0;
82 int opt;
84 opt = getopt_long(argc, argv, "s:e:m:d:", long_options, &option_index);
86 if (opt == EOF)
87 break;
89 switch (opt) {
90 case 261:
91 im.step = atoi(optarg);
92 break;
93 case 262:
94 break;
95 case 's':
96 if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
97 rrd_set_error("start time: %s", parsetime_error);
98 return -1;
99 }
100 break;
101 case 'e':
102 if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
103 rrd_set_error("end time: %s", parsetime_error);
104 return -1;
105 }
106 break;
107 case 'm':
108 im.xsize = atol(optarg);
109 if (im.xsize < 10) {
110 rrd_set_error("maxrows below 10 rows");
111 return -1;
112 }
113 break;
114 case 'd':
115 {
116 if (im.daemon_addr != NULL)
117 {
118 rrd_set_error ("You cannot specify --daemon "
119 "more than once.");
120 return (-1);
121 }
123 im.daemon_addr = strdup(optarg);
124 if (im.daemon_addr == NULL)
125 {
126 rrd_set_error("strdup error");
127 return -1;
128 }
129 break;
130 }
132 case '?':
133 rrd_set_error("unknown option '%s'", argv[optind - 1]);
134 return -1;
135 }
136 }
138 if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
139 return -1;
140 }
142 if (start_tmp < 3600 * 24 * 365 * 10) {
143 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",
144 start_tmp);
145 return -1;
146 }
148 if (end_tmp < start_tmp) {
149 rrd_set_error("start (%ld) should be less than end (%ld)",
150 start_tmp, end_tmp);
151 return -1;
152 }
154 im.start = start_tmp;
155 im.end = end_tmp;
156 im.step = max((long) im.step, (im.end - im.start) / im.xsize);
158 rrd_graph_script(argc, argv, &im, 0);
159 if (rrd_test_error()) {
160 im_free(&im);
161 return -1;
162 }
164 if (im.gdes_c == 0) {
165 rrd_set_error("can't make an xport without contents");
166 im_free(&im);
167 return (-1);
168 }
170 { /* try to connect to rrdcached */
171 int status = rrdc_connect(im.daemon_addr);
172 if (status != 0) return status;
173 }
175 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data) == -1) {
176 im_free(&im);
177 return -1;
178 }
180 im_free(&im);
181 return 0;
182 }
186 int rrd_xport_fn(
187 image_desc_t *im,
188 time_t *start,
189 time_t *end, /* which time frame do you want ?
190 * will be changed to represent reality */
191 unsigned long *step, /* which stepsize do you want?
192 * will be changed to represent reality */
193 unsigned long *col_cnt, /* number of data columns in the result */
194 char ***legend_v, /* legend entries */
195 rrd_value_t **data)
196 { /* two dimensional array containing the data */
198 int i = 0, j = 0;
199 unsigned long dst_row, row_cnt;
200 rrd_value_t *dstptr;
202 unsigned long xport_counter = 0;
203 int *ref_list;
204 long *step_list;
205 long *step_list_ptr;
206 char **legend_list;
209 /* pull the data from the rrd files ... */
210 if (data_fetch(im) == -1)
211 return -1;
213 /* evaluate CDEF operations ... */
214 if (data_calc(im) == -1)
215 return -1;
217 /* how many xports? */
218 *col_cnt = 0;
219 for (i = 0; i < im->gdes_c; i++) {
220 switch (im->gdes[i].gf) {
221 case GF_XPORT:
222 (*col_cnt)++;
223 break;
224 default:
225 break;
226 }
227 }
228 if ((*col_cnt) == 0) {
229 rrd_set_error("no XPORT found, nothing to do");
230 return -1;
231 }
233 /* a list of referenced gdes */
234 ref_list = (int*)malloc(sizeof(int) * (*col_cnt));
235 if (ref_list == NULL)
236 return -1;
238 /* a list to save pointers to the column's legend entry */
239 /* this is a return value! */
240 legend_list = (char**)malloc(sizeof(char *) * (*col_cnt));
241 if (legend_list == NULL) {
242 free(ref_list);
243 return -1;
244 }
246 /* lets find the step size we have to use for xport */
247 step_list = (long*)malloc(sizeof(long)*((*col_cnt)+1));
248 step_list_ptr = step_list;
249 j = 0;
250 for (i = 0; i < im->gdes_c; i++) {
251 switch (im->gdes[i].gf) {
252 case GF_XPORT:
253 ref_list[xport_counter++] = i;
254 *step_list_ptr = im->gdes[im->gdes[i].vidx].step;
255 /* printf("%s:%lu\n",im->gdes[i].legend,*step_list_ptr); */
256 step_list_ptr++;
257 /* reserve room for one legend entry */
258 /* is FMT_LEG_LEN + 5 the correct size? */
259 if ((legend_list[j] =
260 (char*)malloc(sizeof(char) * (FMT_LEG_LEN + 5))) == NULL) {
261 free(ref_list);
262 *data = NULL;
263 while (--j > -1)
264 free(legend_list[j]);
265 free(legend_list);
266 free(step_list);
267 rrd_set_error("malloc xport legend entry");
268 return (-1);
269 }
271 if (im->gdes[i].legend)
272 /* omit bounds check, should have the same size */
273 strcpy(legend_list[j++], im->gdes[i].legend);
274 else
275 legend_list[j++][0] = '\0';
276 break;
277 default:
278 break;
279 }
280 }
281 *step_list_ptr=0;
282 /* find a common step */
283 *step = lcd(step_list);
284 /* printf("step: %lu\n",*step); */
285 free(step_list);
287 *start = im->start - im->start % (*step);
288 *end = im->end - im->end % (*step) + (*step);
291 /* room for rearranged data */
292 /* this is a return value! */
293 row_cnt = ((*end) - (*start)) / (*step);
294 if (((*data) =
295 (rrd_value_t*)malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
296 free(ref_list);
297 free(legend_list);
298 rrd_set_error("malloc xport data area");
299 return (-1);
300 }
301 dstptr = (*data);
303 /* fill data structure */
304 for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
305 for (i = 0; i < (int) (*col_cnt); i++) {
306 long vidx = im->gdes[ref_list[i]].vidx;
307 time_t now = *start + dst_row * *step;
308 (*dstptr++) = im->gdes[vidx].data[(unsigned long)
309 floor((double)
310 (now - im->gdes[vidx].start)
311 /im->gdes[vidx].step)
312 * im->gdes[vidx].ds_cnt +
313 im->gdes[vidx].ds];
315 }
316 }
318 *legend_v = legend_list;
319 free(ref_list);
320 return 0;
322 }