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 {"json", no_argument, 0, 263}, /* 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 (im.daemon_addr != NULL)
118 {
119 rrd_set_error ("You cannot specify --daemon "
120 "more than once.");
121 return (-1);
122 }
124 im.daemon_addr = strdup(optarg);
125 if (im.daemon_addr == 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 an xport without contents");
167 im_free(&im);
168 return (-1);
169 }
171 { /* try to connect to rrdcached */
172 int status = rrdc_connect(im.daemon_addr);
173 if (status != 0) return status;
174 }
176 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data) == -1) {
177 im_free(&im);
178 return -1;
179 }
181 im_free(&im);
182 return 0;
183 }
187 int rrd_xport_fn(
188 image_desc_t *im,
189 time_t *start,
190 time_t *end, /* which time frame do you want ?
191 * will be changed to represent reality */
192 unsigned long *step, /* which stepsize do you want?
193 * will be changed to represent reality */
194 unsigned long *col_cnt, /* number of data columns in the result */
195 char ***legend_v, /* legend entries */
196 rrd_value_t **data)
197 { /* two dimensional array containing the data */
199 int i = 0, j = 0;
200 unsigned long dst_row, row_cnt;
201 rrd_value_t *dstptr;
203 unsigned long xport_counter = 0;
204 int *ref_list;
205 long *step_list;
206 long *step_list_ptr;
207 char **legend_list;
210 /* pull the data from the rrd files ... */
211 if (data_fetch(im) == -1)
212 return -1;
214 /* evaluate CDEF operations ... */
215 if (data_calc(im) == -1)
216 return -1;
218 /* how many xports? */
219 *col_cnt = 0;
220 for (i = 0; i < im->gdes_c; i++) {
221 switch (im->gdes[i].gf) {
222 case GF_XPORT:
223 (*col_cnt)++;
224 break;
225 default:
226 break;
227 }
228 }
229 if ((*col_cnt) == 0) {
230 rrd_set_error("no XPORT found, nothing to do");
231 return -1;
232 }
234 /* a list of referenced gdes */
235 ref_list = (int*)malloc(sizeof(int) * (*col_cnt));
236 if (ref_list == NULL)
237 return -1;
239 /* a list to save pointers to the column's legend entry */
240 /* this is a return value! */
241 legend_list = (char**)malloc(sizeof(char *) * (*col_cnt));
242 if (legend_list == NULL) {
243 free(ref_list);
244 return -1;
245 }
247 /* lets find the step size we have to use for xport */
248 step_list = (long*)malloc(sizeof(long)*((*col_cnt)+1));
249 step_list_ptr = step_list;
250 j = 0;
251 for (i = 0; i < im->gdes_c; i++) {
252 switch (im->gdes[i].gf) {
253 case GF_XPORT:
254 ref_list[xport_counter++] = i;
255 *step_list_ptr = im->gdes[im->gdes[i].vidx].step;
256 /* printf("%s:%lu\n",im->gdes[i].legend,*step_list_ptr); */
257 step_list_ptr++;
258 /* reserve room for one legend entry */
259 /* is FMT_LEG_LEN + 5 the correct size? */
260 if ((legend_list[j] =
261 (char*)malloc(sizeof(char) * (FMT_LEG_LEN + 5))) == NULL) {
262 free(ref_list);
263 *data = NULL;
264 while (--j > -1)
265 free(legend_list[j]);
266 free(legend_list);
267 free(step_list);
268 rrd_set_error("malloc xport legend entry");
269 return (-1);
270 }
272 if (im->gdes[i].legend)
273 /* omit bounds check, should have the same size */
274 strcpy(legend_list[j++], im->gdes[i].legend);
275 else
276 legend_list[j++][0] = '\0';
277 break;
278 default:
279 break;
280 }
281 }
282 *step_list_ptr=0;
283 /* find a common step */
284 *step = lcd(step_list);
285 /* printf("step: %lu\n",*step); */
286 free(step_list);
288 *start = im->start - im->start % (*step);
289 *end = im->end - im->end % (*step) + (*step);
292 /* room for rearranged data */
293 /* this is a return value! */
294 row_cnt = ((*end) - (*start)) / (*step);
295 if (((*data) =
296 (rrd_value_t*)malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
297 free(ref_list);
298 free(legend_list);
299 rrd_set_error("malloc xport data area");
300 return (-1);
301 }
302 dstptr = (*data);
304 /* fill data structure */
305 for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
306 for (i = 0; i < (int) (*col_cnt); i++) {
307 long vidx = im->gdes[ref_list[i]].vidx;
308 time_t now = *start + dst_row * *step;
309 (*dstptr++) = im->gdes[vidx].data[(unsigned long)
310 floor((double)
311 (now - im->gdes[vidx].start)
312 /im->gdes[vidx].step)
313 * im->gdes[vidx].ds_cnt +
314 im->gdes[vidx].ds];
316 }
317 }
319 *legend_v = legend_list;
320 free(ref_list);
321 return 0;
323 }