~ubuntu-branches/ubuntu/maverick/rrdtool/maverick

« back to all changes in this revision

Viewing changes to src/rrd_fetch.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2010-07-22 08:07:01 UTC
  • mfrom: (1.2.8 upstream) (3.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100722080701-k46mgdfz6euxwqsm
Tags: 1.4.3-1ubuntu1
* Merge from debian unstable, Remaining changes:
  - debian/control: Don't build against ruby1.9 as we don't want
    it in main.
* require libdbi >= 0.8.3 to prevent aborts when using dbi datasources

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
 
 * RRDtool 1.3.8  Copyright by Tobi Oetiker, 1997-2009
 
2
 * RRDtool 1.4.3  Copyright by Tobi Oetiker, 1997-2010
3
3
 *****************************************************************************
4
4
 * rrd_fetch.c  read date from an rrd to use for further processing
5
5
 *****************************************************************************
6
 
 * $Id: rrd_fetch.c 1801 2009-05-19 13:45:05Z oetiker $
 
6
 * $Id: rrd_fetch.c 2042 2010-03-22 16:05:55Z oetiker $
7
7
 * $Log$
8
8
 * Revision 1.8  2004/05/18 18:53:03  oetiker
9
9
 * big spell checking patch -- slif@bellsouth.net
52
52
 *
53
53
 *****************************************************************************/
54
54
 
55
 
#ifdef WIN32
56
 
#include <stdlib.h>
57
 
#endif
58
 
 
59
55
#include "rrd_tool.h"
 
56
#include "rrd_client.h"
60
57
 
61
58
#include "rrd_is_thread_safe.h"
62
 
/*#define DEBUG*/
 
59
/* #define DEBUG */
63
60
 
64
61
int rrd_fetch(
65
62
    int argc,
76
73
    long      step_tmp = 1;
77
74
    time_t    start_tmp = 0, end_tmp = 0;
78
75
    const char *cf;
 
76
    char *opt_daemon = NULL;
 
77
    int status;
79
78
 
80
79
    rrd_time_value_t start_tv, end_tv;
81
80
    char     *parsetime_error = NULL;
83
82
        {"resolution", required_argument, 0, 'r'},
84
83
        {"start", required_argument, 0, 's'},
85
84
        {"end", required_argument, 0, 'e'},
 
85
        {"daemon", required_argument, 0, 'd'},
86
86
        {0, 0, 0, 0}
87
87
    };
88
88
 
97
97
        int       option_index = 0;
98
98
        int       opt;
99
99
 
100
 
        opt = getopt_long(argc, argv, "r:s:e:", long_options, &option_index);
 
100
        opt = getopt_long(argc, argv, "r:s:e:d:", long_options, &option_index);
101
101
 
102
102
        if (opt == EOF)
103
103
            break;
118
118
        case 'r':
119
119
            step_tmp = atol(optarg);
120
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
 
121
133
        case '?':
122
134
            rrd_set_error("unknown option '-%c'", optopt);
123
135
            return (-1);
151
163
    *step = step_tmp;
152
164
 
153
165
    if (optind + 1 >= argc) {
154
 
        rrd_set_error("not enough arguments");
 
166
        rrd_set_error("Usage: rrdtool %s <file> <CF> [options]", argv[0]);
155
167
        return -1;
156
168
    }
157
169
 
 
170
    status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
 
171
    if (opt_daemon) free (opt_daemon);
 
172
    if (status) return (-1);
 
173
 
158
174
    cf = argv[optind + 1];
159
175
 
160
 
    if (rrd_fetch_r(argv[optind], cf, start, end, step, ds_cnt, ds_namv, data)
161
 
        != 0)
 
176
    status = rrd_fetch_r(argv[optind], cf, start, end, step,
 
177
            ds_cnt, ds_namv, data);
 
178
    if (status != 0)
162
179
        return (-1);
163
180
    return (0);
164
181
}
183
200
 
184
201
    return (rrd_fetch_fn
185
202
            (filename, cf_idx, start, end, step, ds_cnt, ds_namv, data));
186
 
}
 
203
} /* int rrd_fetch_r */
187
204
 
188
205
int rrd_fetch_fn(
189
206
    const char *filename,   /* name of the rrd */
204
221
    long      best_full_step_diff = 0, best_part_step_diff =
205
222
        0, tmp_step_diff = 0, tmp_match = 0, best_match = 0;
206
223
    long      full_match, rra_base;
207
 
    long      start_offset, end_offset;
 
224
    off_t     start_offset, end_offset;
208
225
    int       first_full = 1;
209
226
    int       first_part = 1;
210
227
    rrd_t     rrd;
218
235
            *start, *end, *step);
219
236
#endif
220
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);
221
248
    rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
222
249
    if (rrd_file == NULL)
223
250
        goto err_free;
331
358
** database is the one with time stamp (t+s) which means t to t+s.
332
359
*/
333
360
    *ds_cnt = rrd.stat_head->ds_cnt;
334
 
    if (((*data) = (rrd_value_t *)malloc(*ds_cnt * rows * sizeof(rrd_value_t))) == NULL) {
 
361
    if (((*data) = (rrd_value_t*)malloc(*ds_cnt * rows * sizeof(rrd_value_t))) == NULL) {
335
362
        rrd_set_error("malloc fetch data area");
336
363
        goto err_free_all_ds_namv;
337
364
    }
356
383
            "rra_start %lu, rra_end %lu, start_off %li, end_off %li\n",
357
384
            rra_start_time, rra_end_time, start_offset, end_offset);
358
385
#endif
359
 
 
360
 
    /* fill the gap at the start if needs be */
361
 
 
362
 
    if (*start <= rra_end_time && *end >= rra_start_time - *step){
363
 
        
 
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 ){
364
388
        if (start_offset <= 0)
365
389
            rra_pointer = rrd.rra_ptr[chosen_rra].cur_row + 1;
366
390
        else
369
393
        rra_pointer = rra_pointer % (signed) rrd.rra_def[chosen_rra].row_cnt;
370
394
         
371
395
        if (rrd_seek(rrd_file, (rra_base + (rra_pointer * (*ds_cnt)
372
 
                                            * sizeof(rrd_value_t))),
 
396
                                        * sizeof(rrd_value_t))),
373
397
                 SEEK_SET) != 0) {
374
398
            rrd_set_error("seek error in RRA");
375
399
            goto err_free_data;
376
 
        }        
 
400
        }
377
401
#ifdef DEBUG
378
402
        fprintf(stderr, "First Seek: rra_base %lu rra_pointer %lu\n",
379
 
            rra_base, rra_pointer);
 
403
                rra_base, rra_pointer);
380
404
#endif
381
405
    }
382
406
    
444
468
 
445
469
    rrd_close(rrd_file);
446
470
    rrd_free(&rrd);
447
 
 
448
471
    return (0);
449
472
  err_free_data:
450
473
    free(*data);