prepare for the release of rrdtool-1.4.2
[rrdtool.git] / src / rrd_fetch.c
1 /*****************************************************************************
2  * RRDtool 1.4.2  Copyright by Tobi Oetiker, 1997-2009
3  *****************************************************************************
4  * rrd_fetch.c  read date from an rrd to use for further processing
5  *****************************************************************************
6  * $Id$
7  * $Log$
8  * Revision 1.8  2004/05/18 18:53:03  oetiker
9  * big spell checking patch -- slif@bellsouth.net
10  *
11  * Revision 1.7  2003/11/11 19:46:21  oetiker
12  * replaced time_value with rrd_time_value as MacOS X introduced a struct of that name in their standard headers
13  *
14  * Revision 1.6  2003/01/16 23:27:54  oetiker
15  * fix border condition in rra selection of rrd_fetch
16  * -- Stanislav Sinyagin <ssinyagin@yahoo.com>
17  *
18  * Revision 1.5  2002/06/23 22:29:40  alex
19  * Added "step=1800" and such to "DEF"
20  * Cleaned some of the signed vs. unsigned problems
21  *
22  * Revision 1.4  2002/02/01 20:34:49  oetiker
23  * fixed version number and date/time
24  *
25  * Revision 1.3  2001/12/24 06:51:49  alex
26  * A patch of size 44Kbytes... in short:
27  *
28  * Found and repaired the off-by-one error in rrd_fetch_fn().
29  * As a result I had to remove the hacks in rrd_fetch_fn(),
30  * rrd_tool.c, vdef_calc(), data_calc(), data_proc() and
31  * reduce_data().  There may be other places which I didn't
32  * find so be careful.
33  *
34  * Enhanced debugging in rrd_fetch_fn(), it shows the RRA selection
35  * process.
36  *
37  * Added the ability to print VDEF timestamps.  At the moment it
38  * is a hack, I needed it now to fix the off-by-one error.
39  * If the format string is "%c" (and nothing else!), the time
40  * will be printed by both ctime() and as a long int.
41  *
42  * Moved some code around (slightly altering it) from rrd_graph()
43  *   initializing     now in rrd_graph_init()
44  *   options parsing  now in rrd_graph_options()
45  *   script parsing   now in rrd_graph_script()
46  *
47  * Revision 1.2  2001/12/17 12:48:43  oetiker
48  * fix overflow error ...
49  *
50  * Revision 1.1.1.1  2001/02/25 22:25:05  oetiker
51  * checkin
52  *
53  *****************************************************************************/
54
55 #include "rrd_tool.h"
56 #include "rrd_client.h"
57
58 #include "rrd_is_thread_safe.h"
59 /* #define DEBUG */
60
61 int rrd_fetch(
62     int argc,
63     char **argv,
64     time_t *start,
65     time_t *end,        /* which time frame do you want ?
66                          * will be changed to represent reality */
67     unsigned long *step,    /* which stepsize do you want? 
68                              * will be changed to represent reality */
69     unsigned long *ds_cnt,  /* number of data sources in file */
70     char ***ds_namv,    /* names of data sources */
71     rrd_value_t **data)
72 {                       /* two dimensional array containing the data */
73     long      step_tmp = 1;
74     time_t    start_tmp = 0, end_tmp = 0;
75     const char *cf;
76     char *opt_daemon = NULL;
77     int status;
78
79     rrd_time_value_t start_tv, end_tv;
80     char     *parsetime_error = NULL;
81     struct option long_options[] = {
82         {"resolution", required_argument, 0, 'r'},
83         {"start", required_argument, 0, 's'},
84         {"end", required_argument, 0, 'e'},
85         {"daemon", required_argument, 0, 'd'},
86         {0, 0, 0, 0}
87     };
88
89     optind = 0;
90     opterr = 0;         /* initialize getopt */
91
92     /* init start and end time */
93     rrd_parsetime("end-24h", &start_tv);
94     rrd_parsetime("now", &end_tv);
95
96     while (1) {
97         int       option_index = 0;
98         int       opt;
99
100         opt = getopt_long(argc, argv, "r:s:e:d:", long_options, &option_index);
101
102         if (opt == EOF)
103             break;
104
105         switch (opt) {
106         case 's':
107             if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
108                 rrd_set_error("start time: %s", parsetime_error);
109                 return -1;
110             }
111             break;
112         case 'e':
113             if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
114                 rrd_set_error("end time: %s", parsetime_error);
115                 return -1;
116             }
117             break;
118         case 'r':
119             step_tmp = atol(optarg);
120             break;
121
122         case 'd':
123             if (opt_daemon != NULL)
124                     free (opt_daemon);
125             opt_daemon = strdup (optarg);
126             if (opt_daemon == NULL)
127             {
128                 rrd_set_error ("strdup failed.");
129                 return (-1);
130             }
131             break;
132
133         case '?':
134             rrd_set_error("unknown option '-%c'", optopt);
135             return (-1);
136         }
137     }
138
139
140     if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
141         return -1;
142     }
143
144
145     if (start_tmp < 3600 * 24 * 365 * 10) {
146         rrd_set_error("the first entry to fetch should be after 1980");
147         return (-1);
148     }
149
150     if (end_tmp < start_tmp) {
151         rrd_set_error("start (%ld) should be less than end (%ld)", start_tmp,
152                       end_tmp);
153         return (-1);
154     }
155
156     *start = start_tmp;
157     *end = end_tmp;
158
159     if (step_tmp < 1) {
160         rrd_set_error("step must be >= 1 second");
161         return -1;
162     }
163     *step = step_tmp;
164
165     if (optind + 1 >= argc) {
166         rrd_set_error("Usage: rrdtool %s <file> <CF> [options]", argv[0]);
167         return -1;
168     }
169
170     status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
171     if (opt_daemon) free (opt_daemon);
172     if (status) return (-1);
173
174     cf = argv[optind + 1];
175
176     status = rrd_fetch_r(argv[optind], cf, start, end, step,
177             ds_cnt, ds_namv, data);
178     if (status != 0)
179         return (-1);
180     return (0);
181 }
182
183 int rrd_fetch_r(
184     const char *filename,   /* name of the rrd */
185     const char *cf,     /* which consolidation function ? */
186     time_t *start,
187     time_t *end,        /* which time frame do you want ?
188                          * will be changed to represent reality */
189     unsigned long *step,    /* which stepsize do you want? 
190                              * will be changed to represent reality */
191     unsigned long *ds_cnt,  /* number of data sources in file */
192     char ***ds_namv,    /* names of data_sources */
193     rrd_value_t **data)
194 {                       /* two dimensional array containing the data */
195     enum cf_en cf_idx;
196
197     if ((int) (cf_idx = cf_conv(cf)) == -1) {
198         return -1;
199     }
200
201     return (rrd_fetch_fn
202             (filename, cf_idx, start, end, step, ds_cnt, ds_namv, data));
203 } /* int rrd_fetch_r */
204
205 int rrd_fetch_fn(
206     const char *filename,   /* name of the rrd */
207     enum cf_en cf_idx,  /* which consolidation function ? */
208     time_t *start,
209     time_t *end,        /* which time frame do you want ?
210                          * will be changed to represent reality */
211     unsigned long *step,    /* which stepsize do you want? 
212                              * will be changed to represent reality */
213     unsigned long *ds_cnt,  /* number of data sources in file */
214     char ***ds_namv,    /* names of data_sources */
215     rrd_value_t **data)
216 {                       /* two dimensional array containing the data */
217     long      i, ii;
218     time_t    cal_start, cal_end, rra_start_time, rra_end_time;
219     long      best_full_rra = 0, best_part_rra = 0, chosen_rra =
220         0, rra_pointer = 0;
221     long      best_full_step_diff = 0, best_part_step_diff =
222         0, tmp_step_diff = 0, tmp_match = 0, best_match = 0;
223     long      full_match, rra_base;
224     off_t     start_offset, end_offset;
225     int       first_full = 1;
226     int       first_part = 1;
227     rrd_t     rrd;
228     rrd_file_t *rrd_file;
229     rrd_value_t *data_ptr;
230     unsigned long rows;
231
232 #ifdef DEBUG
233     fprintf(stderr, "Entered rrd_fetch_fn() searching for the best match\n");
234     fprintf(stderr, "Looking for: start %10lu end %10lu step %5lu\n",
235             *start, *end, *step);
236 #endif
237
238 #ifdef HAVE_LIBDBI
239     /* handle libdbi datasources */
240     if (strncmp("sql",filename,3)==0) {
241       if (filename[3]==filename[4]) {
242         return rrd_fetch_fn_libdbi(filename,cf_idx,start,end,step,ds_cnt,ds_namv,data);
243       }
244     }
245 #endif
246
247     rrd_init(&rrd);
248     rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
249     if (rrd_file == NULL)
250         goto err_free;
251
252     /* when was the really last update of this file ? */
253
254     if (((*ds_namv) =
255          (char **) malloc(rrd.stat_head->ds_cnt * sizeof(char *))) == NULL) {
256         rrd_set_error("malloc fetch ds_namv array");
257         goto err_close;
258     }
259
260     for (i = 0; (unsigned long) i < rrd.stat_head->ds_cnt; i++) {
261         if ((((*ds_namv)[i]) = (char*)malloc(sizeof(char) * DS_NAM_SIZE)) == NULL) {
262             rrd_set_error("malloc fetch ds_namv entry");
263             goto err_free_ds_namv;
264         }
265         strncpy((*ds_namv)[i], rrd.ds_def[i].ds_nam, DS_NAM_SIZE - 1);
266         (*ds_namv)[i][DS_NAM_SIZE - 1] = '\0';
267
268     }
269
270     /* find the rra which best matches the requirements */
271     for (i = 0; (unsigned) i < rrd.stat_head->rra_cnt; i++) {
272         if (cf_conv(rrd.rra_def[i].cf_nam) == cf_idx) {
273
274             cal_end = (rrd.live_head->last_up - (rrd.live_head->last_up
275                                                  % (rrd.rra_def[i].pdp_cnt
276                                                     *
277                                                     rrd.stat_head->
278                                                     pdp_step)));
279             cal_start =
280                 (cal_end -
281                  (rrd.rra_def[i].pdp_cnt * rrd.rra_def[i].row_cnt *
282                   rrd.stat_head->pdp_step));
283
284             full_match = *end - *start;
285 #ifdef DEBUG
286             fprintf(stderr, "Considering: start %10lu end %10lu step %5lu ",
287                     cal_start, cal_end,
288                     rrd.stat_head->pdp_step * rrd.rra_def[i].pdp_cnt);
289 #endif
290             /* we need step difference in either full or partial case */
291             tmp_step_diff = labs(*step - (rrd.stat_head->pdp_step
292                                           * rrd.rra_def[i].pdp_cnt));
293             /* best full match */
294             if (cal_start <= *start) {
295                 if (first_full || (tmp_step_diff < best_full_step_diff)) {
296                     first_full = 0;
297                     best_full_step_diff = tmp_step_diff;
298                     best_full_rra = i;
299 #ifdef DEBUG
300                     fprintf(stderr, "best full match so far\n");
301                 } else {
302                     fprintf(stderr, "full match, not best\n");
303 #endif
304                 }
305
306             } else {
307                 /* best partial match */
308                 tmp_match = full_match;
309                 if (cal_start > *start)
310                     tmp_match -= (cal_start - *start);
311                 if (first_part ||
312                     (best_match < tmp_match) ||
313                     (best_match == tmp_match &&
314                      tmp_step_diff < best_part_step_diff)) {
315 #ifdef DEBUG
316                     fprintf(stderr, "best partial so far\n");
317 #endif
318                     first_part = 0;
319                     best_match = tmp_match;
320                     best_part_step_diff = tmp_step_diff;
321                     best_part_rra = i;
322                 } else {
323 #ifdef DEBUG
324                     fprintf(stderr, "partial match, not best\n");
325 #endif
326                 }
327             }
328         }
329     }
330
331     /* lets see how the matching went. */
332     if (first_full == 0)
333         chosen_rra = best_full_rra;
334     else if (first_part == 0)
335         chosen_rra = best_part_rra;
336     else {
337         rrd_set_error
338             ("the RRD does not contain an RRA matching the chosen CF");
339         goto err_free_all_ds_namv;
340     }
341
342     /* set the wish parameters to their real values */
343     *step = rrd.stat_head->pdp_step * rrd.rra_def[chosen_rra].pdp_cnt;
344     *start -= (*start % *step);
345     *end += (*step - *end % *step);
346     rows = (*end - *start) / *step + 1;
347
348 #ifdef DEBUG
349     fprintf(stderr,
350             "We found:    start %10lu end %10lu step %5lu rows  %lu\n",
351             *start, *end, *step, rows);
352 #endif
353
354 /* Start and end are now multiples of the step size.  The amount of
355 ** steps we want is (end-start)/step and *not* an extra one.
356 ** Reasoning:  if step is s and we want to graph from t to t+s,
357 ** we need exactly ((t+s)-t)/s rows.  The row to collect from the
358 ** database is the one with time stamp (t+s) which means t to t+s.
359 */
360     *ds_cnt = rrd.stat_head->ds_cnt;
361     if (((*data) = (rrd_value_t*)malloc(*ds_cnt * rows * sizeof(rrd_value_t))) == NULL) {
362         rrd_set_error("malloc fetch data area");
363         goto err_free_all_ds_namv;
364     }
365
366     data_ptr = (*data);
367
368     /* find base address of rra */
369     rra_base = rrd_file->header_len;
370     for (i = 0; i < chosen_rra; i++)
371         rra_base += (*ds_cnt * rrd.rra_def[i].row_cnt * sizeof(rrd_value_t));
372
373     /* find start and end offset */
374     rra_end_time = (rrd.live_head->last_up
375                     - (rrd.live_head->last_up % *step));
376     rra_start_time = (rra_end_time
377                       - (*step * (rrd.rra_def[chosen_rra].row_cnt - 1)));
378     /* here's an error by one if we don't be careful */
379     start_offset = (long) (*start + *step - rra_start_time) / (long) *step;
380     end_offset = (long) (rra_end_time - *end) / (long) *step;
381 #ifdef DEBUG
382     fprintf(stderr,
383             "rra_start %lu, rra_end %lu, start_off %li, end_off %li\n",
384             rra_start_time, rra_end_time, start_offset, end_offset);
385 #endif
386     /* only seek if the start time is before the end time */
387     if (*start <= rra_end_time && *end >= rra_start_time - (off_t)*step ){
388         if (start_offset <= 0)
389             rra_pointer = rrd.rra_ptr[chosen_rra].cur_row + 1;
390         else
391             rra_pointer = rrd.rra_ptr[chosen_rra].cur_row + 1 + start_offset;
392
393         rra_pointer = rra_pointer % (signed) rrd.rra_def[chosen_rra].row_cnt;
394          
395         if (rrd_seek(rrd_file, (rra_base + (rra_pointer * (*ds_cnt)
396                                         * sizeof(rrd_value_t))),
397                  SEEK_SET) != 0) {
398             rrd_set_error("seek error in RRA");
399             goto err_free_data;
400         }
401 #ifdef DEBUG
402         fprintf(stderr, "First Seek: rra_base %lu rra_pointer %lu\n",
403                 rra_base, rra_pointer);
404 #endif
405     }
406     
407     /* step trough the array */
408
409     for (i = start_offset;
410          i < (signed) rrd.rra_def[chosen_rra].row_cnt - end_offset; i++) {
411         /* no valid data yet */
412         if (i < 0) {
413 #ifdef DEBUG
414             fprintf(stderr, "pre fetch %li -- ", i);
415 #endif
416             for (ii = 0; (unsigned) ii < *ds_cnt; ii++) {
417                 *(data_ptr++) = DNAN;
418 #ifdef DEBUG
419                 fprintf(stderr, "%10.2f ", *(data_ptr - 1));
420 #endif
421             }
422         }
423         /* past the valid data area */
424         else if (i >= (signed) rrd.rra_def[chosen_rra].row_cnt) {
425 #ifdef DEBUG
426             fprintf(stderr, "past fetch %li -- ", i);
427 #endif
428             for (ii = 0; (unsigned) ii < *ds_cnt; ii++) {
429                 *(data_ptr++) = DNAN;
430 #ifdef DEBUG
431                 fprintf(stderr, "%10.2f ", *(data_ptr - 1));
432 #endif
433             }
434         } else {
435             /* OK we are inside the valid area but the pointer has to 
436              * be wrapped*/
437             if (rra_pointer >= (signed) rrd.rra_def[chosen_rra].row_cnt) {
438                 rra_pointer -= rrd.rra_def[chosen_rra].row_cnt;
439                 if (rrd_seek(rrd_file, (rra_base + rra_pointer * (*ds_cnt)
440                                         * sizeof(rrd_value_t)),
441                              SEEK_SET) != 0) {
442                     rrd_set_error("wrap seek in RRA did fail");
443                     goto err_free_data;
444                 }
445 #ifdef DEBUG
446                 fprintf(stderr, "wrap seek ...\n");
447 #endif
448             }
449
450             if (rrd_read(rrd_file, data_ptr, sizeof(rrd_value_t) * (*ds_cnt))
451                 != (ssize_t) (sizeof(rrd_value_t) * (*ds_cnt))) {
452                 rrd_set_error("fetching cdp from rra");
453                 goto err_free_data;
454             }
455 #ifdef DEBUG
456             fprintf(stderr, "post fetch %li -- ", i);
457             for (ii = 0; ii < *ds_cnt; ii++)
458                 fprintf(stderr, "%10.2f ", *(data_ptr + ii));
459 #endif
460             data_ptr += *ds_cnt;
461             rra_pointer++;
462         }
463 #ifdef DEBUG
464         fprintf(stderr, "\n");
465 #endif
466
467     }
468
469     rrd_close(rrd_file);
470     rrd_free(&rrd);
471     return (0);
472   err_free_data:
473     free(*data);
474     *data = NULL;
475   err_free_all_ds_namv:
476     for (i = 0; (unsigned long) i < rrd.stat_head->ds_cnt; ++i)
477         free((*ds_namv)[i]);
478   err_free_ds_namv:
479     free(*ds_namv);
480   err_close:
481     rrd_close(rrd_file);
482   err_free:
483     rrd_free(&rrd);
484     return (-1);
485 }