csv plugin: Add a `DataDir' option.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Thu, 1 Mar 2007 11:14:39 +0000 (12:14 +0100)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Thu, 1 Mar 2007 11:14:39 +0000 (12:14 +0100)
Since the `rrdtool' and `csv' plugins may be loaded at the same time, one may
want to configure another path for this plugin, too.

src/collectd.conf.pod
src/csv.c

index 3c34905..5a04fce 100644 (file)
@@ -130,6 +130,17 @@ TCP-Port to connect to. Defaults to B<3551>.
 
 =back
 
+=head2 Plugin C<csv>
+
+=over 4
+
+=item B<DataDir> I<Directory>
+
+Set the directory to store RRD-files under. Per default RRD-files are generated
+beneath the daemon's working directory, i.E<nbsp>e. the B<BaseDir>.
+
+=back
+
 =head2 Plugin C<df>
 
 =over 4
@@ -346,7 +357,7 @@ can savely ignore these settings.
 
 =item B<DataDir> I<Directory>
 
-Set the directory to store RRD-files under. Per default RRD-files are generated
+Set the directory to store CSV-files under. Per default CSV-files are generated
 beneath the daemon's working directory, i.E<nbsp>e. the B<BaseDir>.
 
 =item B<StepSize> I<Seconds>
index dd33ca5..268bdbf 100644 (file)
--- a/src/csv.c
+++ b/src/csv.c
 #include "common.h"
 #include "utils_debug.h"
 
+/*
+ * Private variables
+ */
+static const char *config_keys[] =
+{
+       "DataDir"
+};
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
+
+static char *datadir   = NULL;
+
 static int value_list_to_string (char *buffer, int buffer_len,
                const data_set_t *ds, const value_list_t *vl)
 {
@@ -66,6 +77,15 @@ static int value_list_to_filename (char *buffer, int buffer_len,
        int offset = 0;
        int status;
 
+       if (datadir != NULL)
+       {
+               status = snprintf (buffer + offset, buffer_len - offset,
+                               "%s/", datadir);
+               if ((status < 1) || (status >= buffer_len - offset))
+                       return (-1);
+               offset += status;
+       }
+
        status = snprintf (buffer + offset, buffer_len - offset,
                        "%s/", vl->host);
        if ((status < 1) || (status >= buffer_len - offset))
@@ -137,6 +157,35 @@ static int csv_create_file (const char *filename, const data_set_t *ds)
        return 0;
 } /* int csv_create_file */
 
+static int csv_config (const char *key, const char *value)
+{
+       if (strcasecmp ("DataDir", key) == 0)
+       {
+               if (datadir != NULL)
+                       free (datadir);
+               datadir = strdup (value);
+               if (datadir != NULL)
+               {
+                       int len = strlen (datadir);
+                       while ((len > 0) && (datadir[len - 1] == '/'))
+                       {
+                               len--;
+                               datadir[len] = '\0';
+                       }
+                       if (len <= 0)
+                       {
+                               free (datadir);
+                               datadir = NULL;
+                       }
+               }
+       }
+       else
+       {
+               return (-1);
+       }
+       return (0);
+} /* int csv_config */
+
 static int csv_write (const data_set_t *ds, const value_list_t *vl)
 {
        struct stat  statbuf;
@@ -150,6 +199,8 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl)
        if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
                return (-1);
 
+       DBG ("filename = %s;", filename);
+
        if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
                return (-1);
 
@@ -210,5 +261,7 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl)
 
 void module_register (void)
 {
+       plugin_register_config ("csv", csv_config,
+                       config_keys, config_keys_num);
        plugin_register_write ("csv", csv_write);
 }