src/rrd_dump.c: Implement the `--daemon' option for `rrdtool dump'.
authorFlorian Forster <octo@leeloo.home.verplant.org>
Sun, 13 Jul 2008 12:49:58 +0000 (14:49 +0200)
committerFlorian Forster <octo@leeloo.home.verplant.org>
Sun, 13 Jul 2008 12:49:58 +0000 (14:49 +0200)
src/rrd_dump.c

index accf2f7..093e7d2 100644 (file)
@@ -43,6 +43,7 @@
  *****************************************************************************/
 #include "rrd_tool.h"
 #include "rrd_rpncalc.h"
+#include "rrd_client.h"
 
 #if !(defined(NETWARE) || defined(WIN32))
 extern char *tzname[2];
@@ -441,6 +442,7 @@ int rrd_dump(
 {
     int       rc;
     int       opt_noheader = 0;
+    char     *rrdcached = NULL;
 
     /* init rrd clean */
 
@@ -451,16 +453,28 @@ int rrd_dump(
         int       opt;
         int       option_index = 0;
         static struct option long_options[] = {
+            {"daemon", required_argument, 0, 'd'},
             {"no-header", no_argument, 0, 'n'},
             {0, 0, 0, 0}
         };
 
-        opt = getopt_long(argc, argv, "n", long_options, &option_index);
+        opt = getopt_long(argc, argv, "d:n", long_options, &option_index);
 
         if (opt == EOF)
             break;
 
         switch (opt) {
+        case 'd':
+            if (rrdcached != NULL)
+                    free (rrdcached);
+            rrdcached = strdup (optarg);
+            if (rrdcached == NULL)
+            {
+                rrd_set_error ("strdup failed.");
+                return (-1);
+            }
+            break;
+
         case 'n':
             opt_noheader = 1;
             break;
@@ -479,6 +493,44 @@ int rrd_dump(
         return (-1);
     }
 
+    if (rrdcached == NULL)
+    {
+        char *temp;
+
+        temp = getenv (ENV_RRDCACHED_ADDRESS);
+        if (temp != NULL)
+        {
+            rrdcached = strdup (temp);
+            if (rrdcached == NULL)
+            {
+                rrd_set_error("strdup failed.");
+                return (-1);
+            }
+        }
+    }
+
+    if (rrdcached != NULL)
+    {
+        int status;
+
+        status = rrdc_connect (rrdcached);
+        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 (rrdcached) */
+
     if ((argc - optind) == 2) {
         rc = rrd_dump_opt_r(argv[optind], argv[optind + 1], opt_noheader);
     } else {