src/rrd_flush.c: Add the `flush' command to the `rrdtool' application.
authorFlorian Forster <octo@leeloo.home.verplant.org>
Tue, 15 Jul 2008 17:20:42 +0000 (19:20 +0200)
committerFlorian Forster <octo@leeloo.home.verplant.org>
Tue, 15 Jul 2008 17:20:42 +0000 (19:20 +0200)
src/Makefile.am
src/librrd.sym
src/rrd.h
src/rrd_flush.c [new file with mode: 0644]
src/rrd_tool.c

index ea4cb20..b11031c 100644 (file)
@@ -44,6 +44,7 @@ RRD_C_FILES =         \
        rrd_xport.c     \
        rrd_gfx.c \
        rrd_dump.c      \
+       rrd_flush.c     \
        rrd_fetch.c     \
        rrd_resize.c \
        rrd_tune.c
index 47cc2b8..de94fa1 100644 (file)
@@ -1,5 +1,6 @@
 rrd_clear_error
 rrd_close
+rrd_cmd_flush
 rrd_create
 rrd_create_r
 rrd_dontneed
index f621e35..e351525 100644 (file)
--- a/src/rrd.h
+++ b/src/rrd.h
@@ -192,6 +192,7 @@ extern    "C" {
     unsigned long *,
     char ***,
     rrd_value_t **);
+    int       rrd_cmd_flush (int argc, char **argv);
 
     void      rrd_freemem(
     void *mem);
diff --git a/src/rrd_flush.c b/src/rrd_flush.c
new file mode 100644 (file)
index 0000000..ba5f4f2
--- /dev/null
@@ -0,0 +1,118 @@
+/**
+ * RRDTool - src/rrd_flush.c
+ * Copyright (C) 2008 Florian octo Forster
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; only version 2 of the License is applicable.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ *
+ * Authors:
+ *   Florian octo Forster <octo at verplant.org>
+ **/
+
+#include "rrd_tool.h"
+#include "rrd_client.h"
+
+int rrd_cmd_flush (int argc, char **argv)
+{
+    char *opt_daemon = NULL;
+    int status;
+
+    /* initialize getopt */
+    optind = 0;
+    opterr = 0;
+
+    while (42)
+    {
+        int opt;
+        static struct option long_options[] =
+        {
+            {"daemon", required_argument, 0, 'd'},
+            {0, 0, 0, 0}
+        };
+
+        opt = getopt_long(argc, argv, "d:", long_options, NULL);
+
+        if (opt == -1)
+            break;
+
+        switch (opt)
+        {
+            case 'd':
+                if (opt_daemon != NULL)
+                    free (opt_daemon);
+                opt_daemon = strdup (optarg);
+                if (opt_daemon == NULL)
+                {
+                    rrd_set_error ("strdup failed.");
+                    return (-1);
+                }
+                break;
+
+            default:
+                rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
+                        argv[0]);
+                return (-1);
+        }
+    } /* while (42) */
+
+    if ((argc - optind) != 1)
+    {
+        rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>", argv[0]);
+        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)
+    {
+        rrd_set_error ("Daemon address unknown. Please use the \"--daemon\" "
+                "option to set an address on the command line or set the "
+                "\"%s\" environment variable.",
+                ENV_RRDCACHED_ADDRESS);
+        return (-1);
+    }
+
+    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);
+
+    rrdc_disconnect ();
+
+    return ((status == 0) ? 0 : -1);
+} /* int rrd_flush */
+
+/*
+ * vim: set sw=4 sts=4 et fdm=marker :
+ */
index 983cfb6..2d0e178 100644 (file)
@@ -55,7 +55,7 @@ void PrintUsage(
         N_
         ("Valid commands: create, update, updatev, graph, graphv,  dump, restore,\n"
          "\t\tlast, lastupdate, first, info, fetch, tune,\n"
-         "\t\tresize, xport\n\n");
+         "\t\tresize, xport, flush\n\n");
 
     const char *help_listremote =
         N_("Valid remote commands: quit, ls, cd, mkdir, pwd\n\n");
@@ -117,6 +117,11 @@ void PrintUsage(
            "\t\t[-s|--start start] [-e|--end end]\n"
           "\t\t[--daemon <address>]\n\n");
 
+    const char *help_flush =
+        N_("* flush - flush cached data out to an RRD file\n\n"
+           "\trrdtool flush filename.rrd\n"
+          "\t\t[--daemon <address>]\n\n");
+
 /* break up very large strings (help_graph, help_tune) for ISO C89 compliance*/
 
     const char *help_graph0 =
@@ -219,7 +224,7 @@ void PrintUsage(
         C_LASTUPDATE, C_FIRST, C_UPDATE, C_FETCH, C_GRAPH, C_GRAPHV,
         C_TUNE,
         C_RESIZE, C_XPORT, C_QUIT, C_LS, C_CD, C_MKDIR, C_PWD,
-        C_UPDATEV
+        C_UPDATEV, C_FLUSH
     };
     int       help_cmd = C_NONE;
 
@@ -244,6 +249,8 @@ void PrintUsage(
             help_cmd = C_UPDATEV;
         else if (!strcmp(cmd, "fetch"))
             help_cmd = C_FETCH;
+        else if (!strcmp(cmd, "flush"))
+            help_cmd = C_FLUSH;
         else if (!strcmp(cmd, "graph"))
             help_cmd = C_GRAPH;
         else if (!strcmp(cmd, "graphv"))
@@ -304,6 +311,9 @@ void PrintUsage(
     case C_FETCH:
         fputs(_(help_fetch), stdout);
         break;
+    case C_FLUSH:
+        fputs(_(help_flush), stdout);
+        break;
     case C_GRAPH:
         fputs(_(help_graph0), stdout);
         fputs(_(help_graph1), stdout);
@@ -797,6 +807,8 @@ int HandleInputLine(
 
     } else if (strcmp("tune", argv[1]) == 0)
         rrd_tune(argc - 1, &argv[1]);
+    else if (strcmp("flush", argv[1]) == 0)
+        rrd_cmd_flush(argc - 1, &argv[1]);
     else {
         rrd_set_error("unknown function '%s'", argv[1]);
     }