Merge branch 'pull/collectd-4' into collectd-4
[collectd.git] / src / csv.c
index dd33ca5..5d64fb8 100644 (file)
--- a/src/csv.c
+++ b/src/csv.c
 #include "collectd.h"
 #include "plugin.h"
 #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 +76,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))
@@ -94,18 +113,19 @@ static int value_list_to_filename (char *buffer, int buffer_len,
 
        {
                time_t now;
-               struct tm *tm;
+               struct tm stm;
 
-               /* TODO: Find a way to minimize the calls to `localtime', since
-                * they are pretty expensive.. */
+               /* TODO: Find a way to minimize the calls to `localtime_r',
+                * since they are pretty expensive.. */
                now = time (NULL);
-               tm = localtime (&now);
+               if (localtime_r (&now, &stm) == NULL)
+               {
+                       ERROR ("csv plugin: localtime_r failed");
+                       return (1);
+               }
 
                strftime (buffer + offset, buffer_len - offset,
-                               "-%Y-%m-%d", tm);
-
-               /* `localtime(3)' returns a pointer to static data,
-                * therefore the pointer may not be free'd. */
+                               "-%Y-%m-%d", &stm);
        }
 
        return (0);
@@ -122,8 +142,10 @@ static int csv_create_file (const char *filename, const data_set_t *ds)
        csv = fopen (filename, "w");
        if (csv == NULL)
        {
-               syslog (LOG_ERR, "csv plugin: fopen (%s) failed: %s",
-                               filename, strerror(errno));
+               char errbuf[1024];
+               ERROR ("csv plugin: fopen (%s) failed: %s",
+                               filename,
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
                return (-1);
        }
 
@@ -137,6 +159,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 +201,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);
 
+       DEBUG ("csv plugin: csv_write: filename = %s;", filename);
+
        if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
                return (-1);
 
@@ -162,14 +215,16 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl)
                }
                else
                {
-                       syslog (LOG_ERR, "stat(%s) failed: %s",
-                                       filename, strerror (errno));
+                       char errbuf[1024];
+                       ERROR ("stat(%s) failed: %s", filename,
+                                       sstrerror (errno, errbuf,
+                                               sizeof (errbuf)));
                        return (-1);
                }
        }
        else if (!S_ISREG (statbuf.st_mode))
        {
-               syslog (LOG_ERR, "stat(%s): Not a regular file!",
+               ERROR ("stat(%s): Not a regular file!",
                                filename);
                return (-1);
        }
@@ -177,8 +232,9 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl)
        csv = fopen (filename, "a");
        if (csv == NULL)
        {
-               syslog (LOG_ERR, "csv plugin: fopen (%s) failed: %s",
-                               filename, strerror (errno));
+               char errbuf[1024];
+               ERROR ("csv plugin: fopen (%s) failed: %s", filename,
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
                return (-1);
        }
        csv_fd = fileno (csv);
@@ -193,8 +249,9 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl)
        status = fcntl (csv_fd, F_SETLK, &fl);
        if (status != 0)
        {
-               syslog (LOG_ERR, "csv plugin: flock (%s) failed: %s",
-                               filename, strerror (errno));
+               char errbuf[1024];
+               ERROR ("csv plugin: flock (%s) failed: %s", filename,
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
                fclose (csv);
                return (-1);
        }
@@ -208,7 +265,12 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl)
        return (0);
 } /* int csv_write */
 
-void module_register (void)
+void module_register (modreg_e load)
 {
-       plugin_register_write ("csv", csv_write);
-}
+       if (load & MR_WRITE)
+       {
+               plugin_register_config ("csv", csv_config,
+                               config_keys, config_keys_num);
+               plugin_register_write ("csv", csv_write);
+       }
+} /* void module_register */