1 /****************************************************************************
2 * RRDtool 1.2.16 Copyright by Tobi Oetiker, 1997-2006
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"
14 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
15 #include <io.h>
16 #include <fcntl.h>
17 #endif
20 int rrd_xport(int, char **, int *,
21 time_t *, time_t *,
22 unsigned long *, unsigned long *,
23 char ***, rrd_value_t **);
25 int rrd_xport_fn(image_desc_t *,
26 time_t *, time_t *,
27 unsigned long *, unsigned long *,
28 char ***, rrd_value_t **);
33 int
34 rrd_xport(int argc, char **argv, int UNUSED(*xsize),
35 time_t *start,
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 */
44 {
46 image_desc_t im;
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 */
52 rrd_graph_init(&im);
54 parsetime("end-24h", &start_tv);
55 parsetime("now", &end_tv);
57 while (1){
58 static struct option long_options[] =
59 {
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 {0,0,0,0}
66 };
67 int option_index = 0;
68 int opt;
70 opt = getopt_long(argc, argv, "s:e:m:",
71 long_options, &option_index);
73 if (opt == EOF)
74 break;
76 switch(opt) {
77 case 261:
78 im.step = atoi(optarg);
79 break;
80 case 262:
81 break;
82 case 's':
83 if ((parsetime_error = parsetime(optarg, &start_tv))) {
84 rrd_set_error( "start time: %s", parsetime_error );
85 return -1;
86 }
87 break;
88 case 'e':
89 if ((parsetime_error = parsetime(optarg, &end_tv))) {
90 rrd_set_error( "end time: %s", parsetime_error );
91 return -1;
92 }
93 break;
94 case 'm':
95 im.xsize = atol(optarg);
96 if (im.xsize < 10) {
97 rrd_set_error("maxrows below 10 rows");
98 return -1;
99 }
100 break;
101 case '?':
102 rrd_set_error("unknown option '%s'",argv[optind-1]);
103 return -1;
104 }
105 }
107 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
108 return -1;
109 }
111 if (start_tmp < 3600*24*365*10){
112 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",start_tmp);
113 return -1;
114 }
116 if (end_tmp < start_tmp) {
117 rrd_set_error("start (%ld) should be less than end (%ld)",
118 start_tmp, end_tmp);
119 return -1;
120 }
122 im.start = start_tmp;
123 im.end = end_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()) {
128 im_free(&im);
129 return -1;
130 }
132 if (im.gdes_c == 0){
133 rrd_set_error("can't make a graph without contents");
134 im_free(&im);
135 return(-1);
136 }
138 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data) == -1){
139 im_free(&im);
140 return -1;
141 }
143 im_free(&im);
144 return 0;
145 }
149 int
150 rrd_xport_fn(image_desc_t *im,
151 time_t *start,
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 */
159 {
161 int i = 0, j = 0;
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;
170 char **legend_list;
171 int ii = 0;
173 time_t start_tmp = 0;
174 time_t end_tmp = 0;
175 unsigned long step_tmp = 1;
177 /* pull the data from the rrd files ... */
178 if(data_fetch(im)==-1)
179 return -1;
181 /* evaluate CDEF operations ... */
182 if(data_calc(im)==-1)
183 return -1;
185 /* how many xports? */
186 for(i = 0; i < im->gdes_c; i++) {
187 switch(im->gdes[i].gf) {
188 case GF_XPORT:
189 nof_xports++;
190 break;
191 default:
192 break;
193 }
194 }
196 if(nof_xports == 0) {
197 rrd_set_error("no XPORT found, nothing to do");
198 return -1;
199 }
201 /* a list of referenced gdes */
202 ref_list = malloc(sizeof(int) * nof_xports);
203 if(ref_list == NULL)
204 return -1;
206 /* a list to save pointers into each gdes data */
207 srcptr_list = malloc(sizeof(srcptr) * nof_xports);
208 if(srcptr_list == NULL) {
209 free(ref_list);
210 return -1;
211 }
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) {
217 free(srcptr_list);
218 free(ref_list);
219 return -1;
220 }
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) {
226 case GF_XPORT:
227 ii = im->gdes[i].vidx;
228 if(xport_counter > nof_xports) {
229 rrd_set_error( "too many xports: should not happen. Hmmm");
230 free(srcptr_list);
231 free(ref_list);
232 free(legend_list);
233 return -1;
234 }
235 srcptr_list[xport_counter] = im->gdes[ii].data;
236 ref_list[xport_counter++] = i;
237 break;
238 default:
239 break;
240 }
241 }
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;
249 *start = start_tmp;
250 *end = end_tmp;
251 *step = step_tmp;
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){
258 free(srcptr_list);
259 free(ref_list);
260 free(legend_list);
261 rrd_set_error("malloc xport data area");
262 return(-1);
263 }
264 dstptr = (*data);
266 j = 0;
267 for(i = 0; i < im->gdes_c; i++) {
268 switch(im->gdes[i].gf) {
269 case GF_XPORT:
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) {
273 free(srcptr_list);
274 free(ref_list);
275 free(*data); *data = NULL;
276 while (--j > -1) free(legend_list[j]);
277 free(legend_list);
278 rrd_set_error("malloc xport legend entry");
279 return(-1);
280 }
282 if (im->gdes[i].legend)
283 /* omit bounds check, should have the same size */
284 strcpy (legend_list[j++], im->gdes[i].legend);
285 else
286 legend_list[j++][0] = '\0';
288 break;
289 default:
290 break;
291 }
292 }
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++) {
297 j = ref_list[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;
309 } else {
310 (*dstptr++) = newval;
311 }
313 }
314 srcptr_list[i] += (*ds_cnt);
315 }
316 }
318 *legend_v = legend_list;
319 free(srcptr_list);
320 free(ref_list);
321 return 0;
323 }