39638a807c99108d9787317864bf39465960d140
1 /*****************************************************************************
2 * RRDtool 1.2.22 Copyright by Tobi Oetiker, 1997-2007
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 rrd_free(&rrd);
72 fclose(in_file);
73 return(-1);
74 }
76 rra_start = ftell(in_file);
77 fseek(in_file,
78 (rra_start +
79 (rrd.rra_ptr[rraindex].cur_row+1) *
80 rrd.stat_head->ds_cnt *
81 sizeof(rrd_value_t)),
82 SEEK_SET);
83 timer = - (rrd.rra_def[rraindex].row_cnt-1);
84 if (rrd.rra_ptr[rraindex].cur_row + 1 > rrd.rra_def[rraindex].row_cnt) {
85 fseek(in_file,rra_start,SEEK_SET);
86 }
87 then = (rrd.live_head->last_up -
88 rrd.live_head->last_up %
89 (rrd.rra_def[rraindex].pdp_cnt*rrd.stat_head->pdp_step)) +
90 (timer *
91 rrd.rra_def[rraindex].pdp_cnt*rrd.stat_head->pdp_step);
93 rrd_free(&rrd);
94 fclose(in_file);
95 return(then);
96 }