src/rrd_update.c: Add support for a caching daemon.
authorFlorian Forster <octo@leeloo.home.verplant.org>
Sun, 22 Jun 2008 15:09:04 +0000 (17:09 +0200)
committerFlorian Forster <octo@leeloo.home.verplant.org>
Sun, 22 Jun 2008 15:09:04 +0000 (17:09 +0200)
The `rrdtool update' command has been enhanced to be aware of a caching
daemon. The options
 --cache[=yes|=no]
 --nocache
 --daemon=<address>
have been added to control usage of the daemon.

See email <20080622082045.GL2999@verplant.org>, sent to the
rrd-developers mailing list on Sun, 22 Jun 2008 10:20:45 +0200.

src/rrd_update.c

index e135f16..6158a9e 100644 (file)
@@ -1,6 +1,7 @@
 
 /*****************************************************************************
  * RRDtool 1.3.0  Copyright by Tobi Oetiker, 1997-2008
+ *                Copyright by Florian Forster, 2008
  *****************************************************************************
  * rrd_update.c  RRD Update Function
  *****************************************************************************
@@ -23,6 +24,8 @@
 #include "rrd_is_thread_safe.h"
 #include "unused.h"
 
+#include "rrd_client.h"
+
 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
 /*
  * WIN32 does not have gettimeofday    and struct timeval. This is a quick and dirty
@@ -315,6 +318,42 @@ static inline void initialize_time(
     }
 }
 
+static int send_values_to_daemon (const char *addr, const char *file,
+        int values_num, const char * const *values, int silent)
+{
+    int status;
+
+    status = rrdd_connect ((addr != NULL) ? addr : RRDD_SOCK_PATH);
+    if (status != 0)
+    {
+        if (!silent)
+        {
+            rrd_set_error("Unable to connect to daemon: %s",
+                    (status < 0)
+                    ? "Internal error"
+                    : rrd_strerror (status));
+        }
+        return (status);
+    }
+
+    status = rrdd_update (file, values_num, values);
+    if (status != 0)
+    {
+        if (!silent)
+        {
+            rrd_set_error("Failed sending the values to the daemon: %s",
+                    (status < 0)
+                    ? "Internal error"
+                    : rrd_strerror (status));
+        }
+        rrdd_disconnect ();
+        return (status);
+    }
+
+    rrdd_disconnect ();
+    return (0);
+} /* int send_values_to_daemon */
+
 #define IFDNAN(X,Y) (isnan(X) ? (Y) : (X));
 
 rrd_info_t *rrd_update_v(
@@ -374,12 +413,18 @@ int rrd_update(
 {
     struct option long_options[] = {
         {"template", required_argument, 0, 't'},
+        {"cache",    optional_argument, 0, 'c'},
+        {"nocache",  no_argument      , 0, 'n'},
+        {"daemon",   required_argument, 0, 's'},
         {0, 0, 0, 0}
     };
     int       option_index = 0;
     int       opt;
     char     *tmplt = NULL;
     int       rc = -1;
+    /* force_cache: 0 = default; -1 = force no cache; 1 = force cache */
+    int       force_cache = 0;
+    char     *daemon_address = NULL;
 
     optind = 0;
     opterr = 0;         /* initialize getopt */
@@ -395,6 +440,39 @@ int rrd_update(
             tmplt = strdup(optarg);
             break;
 
+        case 'c':
+            if (optarg != NULL)
+            {
+                if (strcmp ("no", optarg) == 0)
+                    force_cache = -1;
+                else if (strcmp ("yes", optarg) == 0)
+                    force_cache = 1;
+                else
+                {
+                    rrd_set_error ("option 'cache' must be "
+                            "either 'yes' or 'no'.");
+                    goto out;
+                }
+            }
+            else
+                force_cache = 1;
+            break;
+
+        case 'n':
+            force_cache = -1;
+            break;
+
+        case 's':
+            if (daemon_address != NULL)
+                free (daemon_address);
+            daemon_address = strdup (optarg);
+            if (daemon_address == NULL)
+            {
+                rrd_set_error("strdup failed.");
+                goto out;
+            }
+            break;
+
         case '?':
             rrd_set_error("unknown option '%s'", argv[optind - 1]);
             goto out;
@@ -407,10 +485,39 @@ int rrd_update(
         goto out;
     }
 
+    if ((tmplt != NULL) && (force_cache > 0))
+    {
+        rrd_set_error("The caching daemon cannot be used together with "
+                "templates yet.");
+        goto out;
+    }
+
+    if ((tmplt == NULL) && (force_cache >= 0))
+    {
+        int status;
+
+        status = send_values_to_daemon (daemon_address,
+                /* file = */ argv[optind],
+                /* values_num = */ argc - optind - 1,
+                /* values = */ (void *) (argv + optind + 1),
+                /* silent = */ (force_cache > 0) ? 0 : 1);
+        if ((status == 0) || (force_cache > 0))
+            goto out;
+    } /* if ((tmplt != NULL) && (force_cache >= 0)) */
+
     rc = rrd_update_r(argv[optind], tmplt,
                       argc - optind - 1, (const char **) (argv + optind + 1));
   out:
-    free(tmplt);
+    if (tmplt != NULL)
+    {
+        free(tmplt);
+        tmplt = NULL;
+    }
+    if (daemon_address != NULL)
+    {
+        free (daemon_address);
+        daemon_address = NULL;
+    }
     return rc;
 }