1 /****************************************************************************
2 * RRDtool 1.3.2 Copyright by Tobi Oetiker, 1997-2008
3 ****************************************************************************
4 * rrd_xport.c export RRD data
5 ****************************************************************************/
10 #include "rrd_graph.h"
11 #include "rrd_xport.h"
13 #include "rrd_client.h"
15 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
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 */
56 { /* two dimensional array containing the data */
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'},
74 opterr = 0; /* initialize getopt */
78 rrd_parsetime("end-24h", &start_tv);
79 rrd_parsetime("now", &end_tv);
85 opt = getopt_long(argc, argv, "s:e:m:d:", long_options, &option_index);
92 im.step = atoi(optarg);
97 if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
98 rrd_set_error("start time: %s", parsetime_error);
103 if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
104 rrd_set_error("end time: %s", parsetime_error);
109 im.xsize = atol(optarg);
111 rrd_set_error("maxrows below 10 rows");
117 if (opt_daemon != NULL)
119 rrd_set_error ("You cannot specify --daemon "
124 opt_daemon = strdup(optarg);
125 if (opt_daemon == NULL)
127 rrd_set_error("strdup error");
134 rrd_set_error("unknown option '%s'", argv[optind - 1]);
139 if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
143 if (start_tmp < 3600 * 24 * 365 * 10) {
144 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",
149 if (end_tmp < start_tmp) {
150 rrd_set_error("start (%ld) should be less than end (%ld)",
155 im.start = start_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()) {
165 if (im.gdes_c == 0) {
166 rrd_set_error("can't make a graph without contents");
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;
177 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data) == -1) {
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 */
198 { /* two dimensional array containing the data */
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;
208 rrd_value_t **srcptr_list;
212 time_t start_tmp = 0;
214 unsigned long step_tmp = 1;
216 /* pull the data from the rrd files ... */
217 if (data_fetch(im) == -1)
220 /* evaluate CDEF operations ... */
221 if (data_calc(im) == -1)
224 /* how many xports? */
225 for (i = 0; i < im->gdes_c; i++) {
226 switch (im->gdes[i].gf) {
234 if (nof_xports == 0) {
235 rrd_set_error("no XPORT found, nothing to do");
239 /* a list of referenced gdes */
240 ref_list = malloc(sizeof(int) * nof_xports);
241 if (ref_list == NULL)
244 /* a list to save pointers into each gdes data */
245 srcptr_list = malloc(sizeof(srcptr) * nof_xports);
246 if (srcptr_list == NULL) {
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) {
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) {
265 ii = im->gdes[i].vidx;
266 if (xport_counter > nof_xports) {
267 rrd_set_error("too many xports: should not happen. Hmmm");
273 srcptr_list[xport_counter] = im->gdes[ii].data;
274 ref_list[xport_counter++] = i;
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;
291 row_cnt = ((*end) - (*start)) / (*step);
293 /* room for rearranged data */
294 /* this is a return value! */
296 malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
300 rrd_set_error("malloc xport data area");
306 for (i = 0; i < im->gdes_c; i++) {
307 switch (im->gdes[i].gf) {
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) {
318 free(legend_list[j]);
320 rrd_set_error("malloc xport legend entry");
324 if (im->gdes[i].legend)
325 /* omit bounds check, should have the same size */
326 strcpy(legend_list[j++], im->gdes[i].legend);
328 legend_list[j++][0] = '\0';
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++) {
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)
352 (*dstptr++) = newval;
354 (*dstptr++) = newval;
358 srcptr_list[i] += (*ds_cnt);
362 *legend_v = legend_list;