Merge branch 'master' into ff/rrdd
[rrdtool.git] / src / rrd_fetch.c
index c745f06..563e76b 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * RRDtool 1.3.0  Copyright by Tobi Oetiker, 1997-2008
+ * RRDtool 1.3.2  Copyright by Tobi Oetiker, 1997-2008
  *****************************************************************************
  * rrd_fetch.c  read date from an rrd to use for further processing
  *****************************************************************************
@@ -73,7 +73,8 @@ int rrd_fetch(
     long      step_tmp = 1;
     time_t    start_tmp = 0, end_tmp = 0;
     const char *cf;
-    char *daemon = NULL;
+    char *opt_daemon = NULL;
+    int status;
 
     rrd_time_value_t start_tv, end_tv;
     char     *parsetime_error = NULL;
@@ -119,10 +120,10 @@ int rrd_fetch(
             break;
 
         case 'd':
-           if (daemon != NULL)
-                   free (daemon);
-            daemon = strdup (optarg);
-            if (daemon == NULL)
+            if (opt_daemon != NULL)
+                    free (opt_daemon);
+            opt_daemon = strdup (optarg);
+            if (opt_daemon == NULL)
             {
                 rrd_set_error ("strdup failed.");
                 return (-1);
@@ -166,10 +167,47 @@ int rrd_fetch(
         return -1;
     }
 
+    if (opt_daemon == NULL)
+    {
+        char *temp;
+
+        temp = getenv (ENV_RRDCACHED_ADDRESS);
+        if (temp != NULL)
+        {
+            opt_daemon = strdup (temp);
+            if (opt_daemon == NULL)
+            {
+                rrd_set_error("strdup failed.");
+                return (-1);
+            }
+        }
+    }
+
+    if (opt_daemon != NULL)
+    {
+        status = rrdc_connect (opt_daemon);
+        if (status != 0)
+        {
+            rrd_set_error ("rrdc_connect failed with status %i.", status);
+            return (-1);
+        }
+
+        status = rrdc_flush (argv[optind]);
+        if (status != 0)
+        {
+            rrd_set_error ("rrdc_flush (%s) failed with status %i.",
+                    argv[optind], status);
+            return (-1);
+        }
+
+        rrdc_disconnect ();
+    } /* if (opt_daemon) */
+
     cf = argv[optind + 1];
 
-    if (rrd_fetch_r(argv[optind], cf, start, end, step, daemon, ds_cnt,
-                           ds_namv, data) != 0)
+    status = rrd_fetch_r(argv[optind], cf, start, end, step,
+            ds_cnt, ds_namv, data);
+    if (status != 0)
         return (-1);
     return (0);
 }
@@ -182,35 +220,18 @@ int rrd_fetch_r(
                          * will be changed to represent reality */
     unsigned long *step,    /* which stepsize do you want? 
                              * will be changed to represent reality */
-    const char *daemon,
     unsigned long *ds_cnt,  /* number of data sources in file */
     char ***ds_namv,    /* names of data_sources */
     rrd_value_t **data)
 {                       /* two dimensional array containing the data */
     enum cf_en cf_idx;
-    int status;
 
     if ((int) (cf_idx = cf_conv(cf)) == -1) {
         return -1;
     }
 
-    if (daemon != NULL)
-    {
-        status = rrdc_connect (daemon);
-        if (status != 0)
-        {
-            rrd_set_error ("rrdc_connect failed with status %i.", status);
-            return (-1);
-        }
-    }
-
-    status = rrd_fetch_fn (filename, cf_idx, start, end, step,
-            (daemon == NULL) ? 0 : 1,
-            ds_cnt, ds_namv, data);
-
-    rrdc_disconnect ();
-
-    return (status);
+    return (rrd_fetch_fn
+            (filename, cf_idx, start, end, step, ds_cnt, ds_namv, data));
 } /* int rrd_fetch_r */
 
 int rrd_fetch_fn(
@@ -221,7 +242,6 @@ int rrd_fetch_fn(
                          * will be changed to represent reality */
     unsigned long *step,    /* which stepsize do you want? 
                              * will be changed to represent reality */
-    int use_rrdcached,
     unsigned long *ds_cnt,  /* number of data sources in file */
     char ***ds_namv,    /* names of data_sources */
     rrd_value_t **data)
@@ -241,18 +261,6 @@ int rrd_fetch_fn(
     rrd_value_t *data_ptr;
     unsigned long rows;
 
-    if (use_rrdcached)
-    {
-        int status;
-
-        status = rrdc_flush (filename);
-        if (status != 0)
-        {
-            rrd_set_error ("rrdc_flush failed with status %i.", status);
-            return (-1);
-        }
-    }
-
 #ifdef DEBUG
     fprintf(stderr, "Entered rrd_fetch_fn() searching for the best match\n");
     fprintf(stderr, "Looking for: start %10lu end %10lu step %5lu\n",