Code

provide have m4 macros go here
[rrdtool.git] / src / rrd_fetch.c
index 74829ffd9f9b395a571f09fb53e2621549b380bc..f045ff6b080caf288697af6e28b5c38e7e489265 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * RRDtool 1.2.23  Copyright by Tobi Oetiker, 1997-2007
+ * RRDtool 1.3.6  Copyright by Tobi Oetiker, 1997-2009
  *****************************************************************************
  * rrd_fetch.c  read date from an rrd to use for further processing
  *****************************************************************************
  *
  *****************************************************************************/
 
+#ifdef WIN32
+#include <stdlib.h>
+#endif
+
 #include "rrd_tool.h"
 
 #include "rrd_is_thread_safe.h"
@@ -73,7 +77,7 @@ int rrd_fetch(
     time_t    start_tmp = 0, end_tmp = 0;
     const char *cf;
 
-    struct rrd_time_value start_tv, end_tv;
+    rrd_time_value_t start_tv, end_tv;
     char     *parsetime_error = NULL;
     struct option long_options[] = {
         {"resolution", required_argument, 0, 'r'},
@@ -86,8 +90,8 @@ int rrd_fetch(
     opterr = 0;         /* initialize getopt */
 
     /* init start and end time */
-    parsetime("end-24h", &start_tv);
-    parsetime("now", &end_tv);
+    rrd_parsetime("end-24h", &start_tv);
+    rrd_parsetime("now", &end_tv);
 
     while (1) {
         int       option_index = 0;
@@ -100,13 +104,13 @@ int rrd_fetch(
 
         switch (opt) {
         case 's':
-            if ((parsetime_error = parsetime(optarg, &start_tv))) {
+            if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
                 rrd_set_error("start time: %s", parsetime_error);
                 return -1;
             }
             break;
         case 'e':
-            if ((parsetime_error = parsetime(optarg, &end_tv))) {
+            if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
                 rrd_set_error("end time: %s", parsetime_error);
                 return -1;
             }
@@ -121,7 +125,7 @@ int rrd_fetch(
     }
 
 
-    if (proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
+    if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
         return -1;
     }
 
@@ -227,7 +231,7 @@ int rrd_fetch_fn(
     }
 
     for (i = 0; (unsigned long) i < rrd.stat_head->ds_cnt; i++) {
-        if ((((*ds_namv)[i]) = malloc(sizeof(char) * DS_NAM_SIZE)) == NULL) {
+        if ((((*ds_namv)[i]) = (char*)malloc(sizeof(char) * DS_NAM_SIZE)) == NULL) {
             rrd_set_error("malloc fetch ds_namv entry");
             goto err_free_ds_namv;
         }
@@ -260,7 +264,7 @@ int rrd_fetch_fn(
             tmp_step_diff = labs(*step - (rrd.stat_head->pdp_step
                                           * rrd.rra_def[i].pdp_cnt));
             /* best full match */
-            if (cal_end >= *end && cal_start <= *start) {
+            if (cal_start <= *start) {
                 if (first_full || (tmp_step_diff < best_full_step_diff)) {
                     first_full = 0;
                     best_full_step_diff = tmp_step_diff;
@@ -277,8 +281,6 @@ int rrd_fetch_fn(
                 tmp_match = full_match;
                 if (cal_start > *start)
                     tmp_match -= (cal_start - *start);
-                if (cal_end < *end)
-                    tmp_match -= (*end - cal_end);
                 if (first_part ||
                     (best_match < tmp_match) ||
                     (best_match == tmp_match &&
@@ -329,7 +331,7 @@ int rrd_fetch_fn(
 ** database is the one with time stamp (t+s) which means t to t+s.
 */
     *ds_cnt = rrd.stat_head->ds_cnt;
-    if (((*data) = malloc(*ds_cnt * rows * sizeof(rrd_value_t))) == NULL) {
+    if (((*data) = (rrd_value_t *)malloc(*ds_cnt * rows * sizeof(rrd_value_t))) == NULL) {
         rrd_set_error("malloc fetch data area");
         goto err_free_all_ds_namv;
     }
@@ -357,21 +359,27 @@ int rrd_fetch_fn(
 
     /* fill the gap at the start if needs be */
 
-    if (start_offset <= 0)
-        rra_pointer = rrd.rra_ptr[chosen_rra].cur_row + 1;
-    else
-        rra_pointer = rrd.rra_ptr[chosen_rra].cur_row + 1 + start_offset;
-
-    if (rrd_seek(rrd_file, (rra_base + (rra_pointer * (*ds_cnt)
-                                        * sizeof(rrd_value_t))),
+    if (*start <= rra_end_time && *end >= rra_start_time - *step){
+        
+        if (start_offset <= 0)
+            rra_pointer = rrd.rra_ptr[chosen_rra].cur_row + 1;
+        else
+            rra_pointer = rrd.rra_ptr[chosen_rra].cur_row + 1 + start_offset;
+
+        rra_pointer = rra_pointer % (signed) rrd.rra_def[chosen_rra].row_cnt;
+         
+        if (rrd_seek(rrd_file, (rra_base + (rra_pointer * (*ds_cnt)
+                                            * sizeof(rrd_value_t))),
                  SEEK_SET) != 0) {
-        rrd_set_error("seek error in RRA");
-        goto err_free_data;
-    }
+            rrd_set_error("seek error in RRA");
+            goto err_free_data;
+        }        
 #ifdef DEBUG
-    fprintf(stderr, "First Seek: rra_base %lu rra_pointer %lu\n",
+        fprintf(stderr, "First Seek: rra_base %lu rra_pointer %lu\n",
             rra_base, rra_pointer);
 #endif
+    }
+    
     /* step trough the array */
 
     for (i = start_offset;
@@ -435,6 +443,8 @@ int rrd_fetch_fn(
     }
 
     rrd_close(rrd_file);
+    rrd_free(&rrd);
+
     return (0);
   err_free_data:
     free(*data);