1 /****************************************************************************
2 * RRDtool 1.2.15 Copyright by Tobi Oetiker, 1997-2006
3 ****************************************************************************
4 * rrd_xport.c export RRD data
5 ****************************************************************************/
10 #include "rrd_graph.h"
11 #include "rrd_xport.h"
14 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
20 int rrd_xport(int, char **, int *,
22 unsigned long *, unsigned long *,
23 char ***, rrd_value_t **);
25 int rrd_xport_fn(image_desc_t *,
27 unsigned long *, unsigned long *,
28 char ***, rrd_value_t **);
34 rrd_xport(int argc, char **argv, int UNUSED(*xsize),
36 time_t *end, /* which time frame do you want ?
37 * will be changed to represent reality */
38 unsigned long *step, /* which stepsize do you want?
39 * will be changed to represent reality */
40 unsigned long *col_cnt, /* number of data columns in the result */
41 char ***legend_v, /* legend entries */
42 rrd_value_t **data) /* two dimensional array containing the data */
47 time_t start_tmp=0,end_tmp=0;
48 struct rrd_time_value start_tv, end_tv;
49 char *parsetime_error = NULL;
50 optind = 0; opterr = 0; /* initialize getopt */
54 parsetime("end-24h", &start_tv);
55 parsetime("now", &end_tv);
58 static struct option long_options[] =
60 {"start", required_argument, 0, 's'},
61 {"end", required_argument, 0, 'e'},
62 {"maxrows", required_argument, 0, 'm'},
63 {"step", required_argument, 0, 261},
64 {"enumds", no_argument, 0, 262}, /* these are handled in the frontend ... */
70 opt = getopt_long(argc, argv, "s:e:m:",
71 long_options, &option_index);
78 im.step = atoi(optarg);
83 if ((parsetime_error = parsetime(optarg, &start_tv))) {
84 rrd_set_error( "start time: %s", parsetime_error );
89 if ((parsetime_error = parsetime(optarg, &end_tv))) {
90 rrd_set_error( "end time: %s", parsetime_error );
95 im.xsize = atol(optarg);
97 rrd_set_error("maxrows below 10 rows");
102 rrd_set_error("unknown option '%s'",argv[optind-1]);
107 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
111 if (start_tmp < 3600*24*365*10){
112 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",start_tmp);
116 if (end_tmp < start_tmp) {
117 rrd_set_error("start (%ld) should be less than end (%ld)",
122 im.start = start_tmp;
124 im.step = max((long)im.step, (im.end-im.start)/im.xsize);
126 rrd_graph_script(argc,argv,&im,0);
127 if (rrd_test_error()) {
133 rrd_set_error("can't make a graph without contents");
138 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data) == -1){
150 rrd_xport_fn(image_desc_t *im,
152 time_t *end, /* which time frame do you want ?
153 * will be changed to represent reality */
154 unsigned long *step, /* which stepsize do you want?
155 * will be changed to represent reality */
156 unsigned long *col_cnt, /* number of data columns in the result */
157 char ***legend_v, /* legend entries */
158 rrd_value_t **data) /* two dimensional array containing the data */
162 unsigned long *ds_cnt; /* number of data sources in file */
163 unsigned long col, dst_row, row_cnt;
164 rrd_value_t *srcptr, *dstptr;
166 unsigned long nof_xports = 0;
167 unsigned long xport_counter = 0;
168 unsigned long *ref_list;
169 rrd_value_t **srcptr_list;
173 time_t start_tmp = 0;
175 unsigned long step_tmp = 1;
177 /* pull the data from the rrd files ... */
178 if(data_fetch(im)==-1)
181 /* evaluate CDEF operations ... */
182 if(data_calc(im)==-1)
185 /* how many xports? */
186 for(i = 0; i < im->gdes_c; i++) {
187 switch(im->gdes[i].gf) {
196 if(nof_xports == 0) {
197 rrd_set_error("no XPORT found, nothing to do");
201 /* a list of referenced gdes */
202 ref_list = malloc(sizeof(int) * nof_xports);
206 /* a list to save pointers into each gdes data */
207 srcptr_list = malloc(sizeof(srcptr) * nof_xports);
208 if(srcptr_list == NULL) {
213 /* a list to save pointers to the column's legend entry */
214 /* this is a return value! */
215 legend_list = malloc(sizeof(char *) * nof_xports);
216 if(legend_list == NULL) {
222 /* find referenced gdes and save their index and */
223 /* a pointer into their data */
224 for(i = 0; i < im->gdes_c; i++) {
225 switch(im->gdes[i].gf) {
227 ii = im->gdes[i].vidx;
228 if(xport_counter > nof_xports) {
229 rrd_set_error( "too many xports: should not happen. Hmmm");
235 srcptr_list[xport_counter] = im->gdes[ii].data;
236 ref_list[xport_counter++] = i;
243 start_tmp = im->gdes[0].start;
244 end_tmp = im->gdes[0].end;
245 step_tmp = im->gdes[0].step;
247 /* fill some return values */
248 *col_cnt = nof_xports;
253 row_cnt = ((*end)-(*start))/(*step);
255 /* room for rearranged data */
256 /* this is a return value! */
257 if (((*data) = malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t)))==NULL){
261 rrd_set_error("malloc xport data area");
267 for(i = 0; i < im->gdes_c; i++) {
268 switch(im->gdes[i].gf) {
270 /* reserve room for one legend entry */
271 /* is FMT_LEG_LEN + 5 the correct size? */
272 if ((legend_list[j] = malloc(sizeof(char) * (FMT_LEG_LEN+5)))==NULL) {
275 free(*data); *data = NULL;
276 while (--j > -1) free(legend_list[j]);
278 rrd_set_error("malloc xport legend entry");
282 if (im->gdes[i].legend)
283 /* omit bounds check, should have the same size */
284 strcpy (legend_list[j++], im->gdes[i].legend);
286 legend_list[j++][0] = '\0';
294 /* fill data structure */
295 for(dst_row = 0; (int)dst_row < (int)row_cnt; dst_row++) {
296 for(i = 0; i < (int)nof_xports; i++) {
298 ii = im->gdes[j].vidx;
299 ds_cnt = &im->gdes[ii].ds_cnt;
301 srcptr = srcptr_list[i];
302 for(col = 0; col < (*ds_cnt); col++) {
303 rrd_value_t newval = DNAN;
304 newval = srcptr[col];
306 if (im->gdes[ii].ds_namv && im->gdes[ii].ds_nam) {
307 if(strcmp(im->gdes[ii].ds_namv[col],im->gdes[ii].ds_nam) == 0)
308 (*dstptr++) = newval;
310 (*dstptr++) = newval;
314 srcptr_list[i] += (*ds_cnt);
318 *legend_v = legend_list;