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 ... */
65 {"unknownaszero", no_argument, 0, 263}, /* just ignore them here */
71 opt = getopt_long(argc, argv, "s:e:m:",
72 long_options, &option_index);
79 im.step = atoi(optarg);
85 if ((parsetime_error = parsetime(optarg, &start_tv))) {
86 rrd_set_error( "start time: %s", parsetime_error );
91 if ((parsetime_error = parsetime(optarg, &end_tv))) {
92 rrd_set_error( "end time: %s", parsetime_error );
97 im.xsize = atol(optarg);
99 rrd_set_error("maxrows below 10 rows");
104 rrd_set_error("unknown option '%s'",argv[optind-1]);
109 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
113 if (start_tmp < 3600*24*365*10){
114 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",start_tmp);
118 if (end_tmp < start_tmp) {
119 rrd_set_error("start (%ld) should be less than end (%ld)",
124 im.start = start_tmp;
126 im.step = max((long)im.step, (im.end-im.start)/im.xsize);
128 rrd_graph_script(argc,argv,&im,0);
129 if (rrd_test_error()) {
135 rrd_set_error("can't make a graph without contents");
140 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data) == -1){
152 rrd_xport_fn(image_desc_t *im,
154 time_t *end, /* which time frame do you want ?
155 * will be changed to represent reality */
156 unsigned long *step, /* which stepsize do you want?
157 * will be changed to represent reality */
158 unsigned long *col_cnt, /* number of data columns in the result */
159 char ***legend_v, /* legend entries */
160 rrd_value_t **data) /* two dimensional array containing the data */
164 unsigned long *ds_cnt; /* number of data sources in file */
165 unsigned long col, dst_row, row_cnt;
166 rrd_value_t *srcptr, *dstptr;
168 unsigned long nof_xports = 0;
169 unsigned long xport_counter = 0;
170 unsigned long *ref_list;
171 rrd_value_t **srcptr_list;
175 time_t start_tmp = 0;
177 unsigned long step_tmp = 1;
179 /* pull the data from the rrd files ... */
180 if(data_fetch(im)==-1)
183 /* evaluate CDEF operations ... */
184 if(data_calc(im)==-1)
187 /* how many xports? */
188 for(i = 0; i < im->gdes_c; i++) {
189 switch(im->gdes[i].gf) {
198 if(nof_xports == 0) {
199 rrd_set_error("no XPORT found, nothing to do");
203 /* a list of referenced gdes */
204 ref_list = malloc(sizeof(int) * nof_xports);
208 /* a list to save pointers into each gdes data */
209 srcptr_list = malloc(sizeof(srcptr) * nof_xports);
210 if(srcptr_list == NULL) {
215 /* a list to save pointers to the column's legend entry */
216 /* this is a return value! */
217 legend_list = malloc(sizeof(char *) * nof_xports);
218 if(legend_list == NULL) {
224 /* find referenced gdes and save their index and */
225 /* a pointer into their data */
226 for(i = 0; i < im->gdes_c; i++) {
227 switch(im->gdes[i].gf) {
229 ii = im->gdes[i].vidx;
230 if(xport_counter > nof_xports) {
231 rrd_set_error( "too many xports: should not happen. Hmmm");
237 srcptr_list[xport_counter] = im->gdes[ii].data;
238 ref_list[xport_counter++] = i;
245 start_tmp = im->gdes[0].start;
246 end_tmp = im->gdes[0].end;
247 step_tmp = im->gdes[0].step;
249 /* fill some return values */
250 *col_cnt = nof_xports;
255 row_cnt = ((*end)-(*start))/(*step);
257 /* room for rearranged data */
258 /* this is a return value! */
259 if (((*data) = malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t)))==NULL){
263 rrd_set_error("malloc xport data area");
269 for(i = 0; i < im->gdes_c; i++) {
270 switch(im->gdes[i].gf) {
272 /* reserve room for one legend entry */
273 /* is FMT_LEG_LEN + 5 the correct size? */
274 if ((legend_list[j] = malloc(sizeof(char) * (FMT_LEG_LEN+5)))==NULL) {
277 free(*data); *data = NULL;
278 while (--j > -1) free(legend_list[j]);
280 rrd_set_error("malloc xport legend entry");
284 if (im->gdes[i].legend)
285 /* omit bounds check, should have the same size */
286 strcpy (legend_list[j++], im->gdes[i].legend);
288 legend_list[j++][0] = '\0';
296 /* fill data structure */
297 for(dst_row = 0; (int)dst_row < (int)row_cnt; dst_row++) {
298 for(i = 0; i < (int)nof_xports; i++) {
300 ii = im->gdes[j].vidx;
301 ds_cnt = &im->gdes[ii].ds_cnt;
303 srcptr = srcptr_list[i];
304 for(col = 0; col < (*ds_cnt); col++) {
305 rrd_value_t newval = DNAN;
306 newval = srcptr[col];
308 if (im->gdes[ii].ds_namv && im->gdes[ii].ds_nam) {
309 if(strcmp(im->gdes[ii].ds_namv[col],im->gdes[ii].ds_nam) == 0)
310 (*dstptr++) = newval;
312 (*dstptr++) = newval;
316 srcptr_list[i] += (*ds_cnt);
320 *legend_v = legend_list;