1 /*****************************************************************************
2 * RRDtool 1.2.12 Copyright by Tobi Oetiker, 1997-2005
3 *****************************************************************************
4 * rrd_first Return
5 *****************************************************************************
6 * Initial version by Burton Strauss, ntopSupport.com - 3/2005
7 *****************************************************************************/
9 #include "rrd_tool.h"
12 time_t
13 rrd_first(int argc, char **argv)
14 {
15 int target_rraindex=0;
16 char *endptr;
17 optind = 0; opterr = 0; /* initialize getopt */
19 while (1){
20 static struct option long_options[] =
21 {
22 {"rraindex", required_argument, 0, 129},
23 {0,0,0,0}
24 };
25 int option_index = 0;
26 int opt;
27 opt = getopt_long(argc, argv, "", long_options, &option_index);
29 if(opt == EOF)
30 break;
32 switch(opt) {
33 case 129:
34 target_rraindex=strtol(optarg,&endptr,0);
35 if(target_rraindex < 0) {
36 rrd_set_error("invalid rraindex number");
37 return(-1);
38 }
39 break;
40 default:
41 rrd_set_error("usage rrdtool %s [--rraindex number] file.rrd", argv[0]);
42 return(-1);
43 }
44 }
46 if(optind >= argc){
47 rrd_set_error("not enough arguments");
48 return -1;
49 }
51 return(rrd_first_r(argv[optind], target_rraindex));
52 }
55 time_t
56 rrd_first_r(const char *filename, const int rraindex)
57 {
58 long rra_start,
59 timer;
60 time_t then;
61 FILE *in_file;
62 rrd_t rrd;
64 if(rrd_open(filename,&in_file,&rrd, RRD_READONLY)==-1){
65 rrd_set_error("could not open RRD");
66 return(-1);
67 }
69 if((rraindex < 0) || (rraindex >= (int)rrd.stat_head->rra_cnt)) {
70 rrd_set_error("invalid rraindex number");
71 return(-1);
72 }
74 rra_start = ftell(in_file);
75 fseek(in_file,
76 (rra_start +
77 (rrd.rra_ptr[rraindex].cur_row+1) *
78 rrd.stat_head->ds_cnt *
79 sizeof(rrd_value_t)),
80 SEEK_SET);
81 timer = - (rrd.rra_def[rraindex].row_cnt-1);
82 if (rrd.rra_ptr[rraindex].cur_row + 1 > rrd.rra_def[rraindex].row_cnt) {
83 fseek(in_file,rra_start,SEEK_SET);
84 }
85 then = (rrd.live_head->last_up -
86 rrd.live_head->last_up %
87 (rrd.rra_def[rraindex].pdp_cnt*rrd.stat_head->pdp_step)) +
88 (timer *
89 rrd.rra_def[rraindex].pdp_cnt*rrd.stat_head->pdp_step);
91 rrd_free(&rrd);
92 fclose(in_file);
93 return(then);
94 }