#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)
int status;
int i;
- memset (buffer, '\0', sizeof (buffer_len));
+ memset (buffer, '\0', buffer_len);
status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
if ((status < 1) || (status >= 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))
{
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);
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);
}
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;
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);
}
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);
}
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);
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);
}
void module_register (void)
{
+ plugin_register_config ("csv", csv_config,
+ config_keys, config_keys_num);
plugin_register_write ("csv", csv_write);
-}
+} /* void module_register */