1 /****************************************************************************
2 * RRDtool 1.2.1 Copyright by Tobi Oetiker, 1997-2005
3 ****************************************************************************
4 * rrd_xport.c export RRD data
5 ****************************************************************************/
10 #include "rrd_graph.h"
11 #include "rrd_xport.h"
13 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
19 int rrd_xport(int, char **, int *,
21 unsigned long *, unsigned long *,
22 char ***, rrd_value_t **);
24 int rrd_xport_fn(image_desc_t *,
26 unsigned long *, unsigned long *,
27 char ***, rrd_value_t **);
33 rrd_xport(int argc, char **argv, int *xsize,
35 time_t *end, /* which time frame do you want ?
36 * will be changed to represent reality */
37 unsigned long *step, /* which stepsize do you want?
38 * will be changed to represent reality */
39 unsigned long *col_cnt, /* number of data columns in the result */
40 char ***legend_v, /* legend entries */
41 rrd_value_t **data) /* two dimensional array containing the data */
46 time_t start_tmp=0,end_tmp=0;
47 struct rrd_time_value start_tv, end_tv;
48 char *parsetime_error = NULL;
49 optind = 0; opterr = 0; /* initialize getopt */
53 parsetime("end-24h", &start_tv);
54 parsetime("now", &end_tv);
57 static struct option long_options[] =
59 {"start", required_argument, 0, 's'},
60 {"end", required_argument, 0, 'e'},
61 {"maxrows", required_argument, 0, 'm'},
62 {"step", required_argument, 0, 261},
68 opt = getopt_long(argc, argv, "s:e:m:",
69 long_options, &option_index);
76 im.step = atoi(optarg);
79 if ((parsetime_error = parsetime(optarg, &start_tv))) {
80 rrd_set_error( "start time: %s", parsetime_error );
85 if ((parsetime_error = parsetime(optarg, &end_tv))) {
86 rrd_set_error( "end time: %s", parsetime_error );
91 im.xsize = atol(optarg);
93 rrd_set_error("maxrows below 10 rows");
98 rrd_set_error("unknown option '%c'", optopt);
103 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
107 if (start_tmp < 3600*24*365*10){
108 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",start_tmp);
112 if (end_tmp < start_tmp) {
113 rrd_set_error("start (%ld) should be less than end (%ld)",
118 im.start = start_tmp;
120 im.step = max((long)im.step, (im.end-im.start)/im.xsize);
122 rrd_graph_script(argc,argv,&im,0);
123 if (rrd_test_error()) {
129 rrd_set_error("can't make a graph without contents");
134 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data) == -1){
146 rrd_xport_fn(image_desc_t *im,
148 time_t *end, /* which time frame do you want ?
149 * will be changed to represent reality */
150 unsigned long *step, /* which stepsize do you want?
151 * will be changed to represent reality */
152 unsigned long *col_cnt, /* number of data columns in the result */
153 char ***legend_v, /* legend entries */
154 rrd_value_t **data) /* two dimensional array containing the data */
158 unsigned long *ds_cnt; /* number of data sources in file */
159 unsigned long col, dst_row, row_cnt;
160 rrd_value_t *srcptr, *dstptr;
162 unsigned long nof_xports = 0;
163 unsigned long xport_counter = 0;
164 unsigned long *ref_list;
165 rrd_value_t **srcptr_list;
169 time_t start_tmp = 0;
171 unsigned long step_tmp = 1;
173 /* pull the data from the rrd files ... */
174 if(data_fetch(im)==-1)
177 /* evaluate CDEF operations ... */
178 if(data_calc(im)==-1)
181 /* how many xports? */
182 for(i = 0; i < im->gdes_c; i++) {
183 switch(im->gdes[i].gf) {
192 if(nof_xports == 0) {
193 rrd_set_error("no XPORT found, nothing to do");
197 /* a list of referenced gdes */
198 ref_list = malloc(sizeof(int) * nof_xports);
202 /* a list to save pointers into each gdes data */
203 srcptr_list = malloc(sizeof(srcptr) * nof_xports);
204 if(srcptr_list == NULL) {
209 /* a list to save pointers to the column's legend entry */
210 /* this is a return value! */
211 legend_list = malloc(sizeof(char *) * nof_xports);
212 if(legend_list == NULL) {
218 /* find referenced gdes and save their index and */
219 /* a pointer into their data */
220 for(i = 0; i < im->gdes_c; i++) {
221 switch(im->gdes[i].gf) {
223 ii = im->gdes[i].vidx;
224 if(xport_counter > nof_xports) {
225 rrd_set_error( "too many xports: should not happen. Hmmm");
231 srcptr_list[xport_counter] = im->gdes[ii].data;
232 ref_list[xport_counter++] = i;
239 start_tmp = im->gdes[0].start;
240 end_tmp = im->gdes[0].end;
241 step_tmp = im->gdes[0].step;
243 /* fill some return values */
244 *col_cnt = nof_xports;
249 row_cnt = ((*end)-(*start))/(*step);
251 /* room for rearranged data */
252 /* this is a return value! */
253 if (((*data) = malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t)))==NULL){
257 rrd_set_error("malloc xport data area");
263 for(i = 0; i < im->gdes_c; i++) {
264 switch(im->gdes[i].gf) {
266 /* reserve room for one legend entry */
267 /* is FMT_LEG_LEN + 5 the correct size? */
268 if ((legend_list[j] = malloc(sizeof(char) * (FMT_LEG_LEN+5)))==NULL) {
271 free(*data); *data = NULL;
272 while (--j > -1) free(legend_list[j]);
274 rrd_set_error("malloc xport legend entry");
278 if (im->gdes[i].legend)
279 /* omit bounds check, should have the same size */
280 strcpy (legend_list[j++], im->gdes[i].legend);
282 legend_list[j++][0] = '\0';
290 /* fill data structure */
291 for(dst_row = 0; (int)dst_row < (int)row_cnt; dst_row++) {
292 for(i = 0; i < (int)nof_xports; i++) {
294 ii = im->gdes[j].vidx;
295 ds_cnt = &im->gdes[ii].ds_cnt;
297 srcptr = srcptr_list[i];
298 for(col = 0; col < (*ds_cnt); col++) {
299 rrd_value_t newval = DNAN;
300 newval = srcptr[col];
302 if (im->gdes[ii].ds_namv && im->gdes[ii].ds_nam) {
303 if(strcmp(im->gdes[ii].ds_namv[col],im->gdes[ii].ds_nam) == 0)
304 (*dstptr++) = newval;
306 (*dstptr++) = newval;
310 srcptr_list[i] += (*ds_cnt);
314 *legend_v = legend_list;