3 * Copyright (C) 2007-2009 Florian octo Forster
4 * Copyright (C) 2009 Doug MacEachern
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at collectd.org>
21 * Doug MacEachern <dougm@hyperic.com>
27 #include "utils_cache.h"
32 static const char *config_keys[] =
37 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
39 static char *datadir = NULL;
40 static int store_rates = 0;
41 static int use_stdio = 0;
43 static int value_list_to_string (char *buffer, int buffer_len,
44 const data_set_t *ds, const value_list_t *vl)
49 gauge_t *rates = NULL;
51 assert (0 == strcmp (ds->type, vl->type));
53 memset (buffer, '\0', buffer_len);
55 status = ssnprintf (buffer, buffer_len, "%.3f",
56 CDTIME_T_TO_DOUBLE (vl->time));
57 if ((status < 1) || (status >= buffer_len))
61 for (i = 0; i < ds->ds_num; i++)
63 if ((ds->ds[i].type != DS_TYPE_COUNTER)
64 && (ds->ds[i].type != DS_TYPE_GAUGE)
65 && (ds->ds[i].type != DS_TYPE_DERIVE)
66 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
69 if (ds->ds[i].type == DS_TYPE_GAUGE)
71 status = ssnprintf (buffer + offset, buffer_len - offset,
72 ",%lf", vl->values[i].gauge);
74 else if (store_rates != 0)
77 rates = uc_get_rate (ds, vl);
80 WARNING ("csv plugin: "
81 "uc_get_rate failed.");
84 status = ssnprintf (buffer + offset,
88 else if (ds->ds[i].type == DS_TYPE_COUNTER)
90 status = ssnprintf (buffer + offset,
93 vl->values[i].counter);
95 else if (ds->ds[i].type == DS_TYPE_DERIVE)
97 status = ssnprintf (buffer + offset,
100 vl->values[i].derive);
102 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
104 status = ssnprintf (buffer + offset,
107 vl->values[i].absolute);
110 if ((status < 1) || (status >= (buffer_len - offset)))
117 } /* for ds->ds_num */
121 } /* int value_list_to_string */
123 static int value_list_to_filename (char *buffer, size_t buffer_size,
124 value_list_t const *vl)
129 size_t ptr_size = buffer_size;
135 size_t len = strlen (datadir) + 1;
140 memcpy (ptr, datadir, len);
146 status = FORMAT_VL (ptr, ptr_size, vl);
150 /* Skip all the time formatting stuff when printing to STDOUT or
155 ptr_size -= strlen (ptr);
158 /* "-2013-07-12" => 11 bytes */
161 ERROR ("csv plugin: Buffer too small.");
165 /* TODO: Find a way to minimize the calls to `localtime_r',
166 * since they are pretty expensive.. */
168 if (localtime_r (&now, &struct_tm) == NULL)
170 ERROR ("csv plugin: localtime_r failed");
174 status = strftime (ptr, ptr_size, "-%Y-%m-%d", &struct_tm);
175 if (status == 0) /* yep, it returns zero on error. */
177 ERROR ("csv plugin: strftime failed");
182 } /* int value_list_to_filename */
184 static int csv_create_file (const char *filename, const data_set_t *ds)
189 if (check_create_dir (filename))
192 csv = fopen (filename, "w");
196 ERROR ("csv plugin: fopen (%s) failed: %s",
198 sstrerror (errno, errbuf, sizeof (errbuf)));
202 fprintf (csv, "epoch");
203 for (i = 0; i < ds->ds_num; i++)
204 fprintf (csv, ",%s", ds->ds[i].name);
210 } /* int csv_create_file */
212 static int csv_config (const char *key, const char *value)
214 if (strcasecmp ("DataDir", key) == 0)
221 if (strcasecmp ("stdout", value) == 0)
226 else if (strcasecmp ("stderr", value) == 0)
231 datadir = strdup (value);
234 int len = strlen (datadir);
235 while ((len > 0) && (datadir[len - 1] == '/'))
247 else if (strcasecmp ("StoreRates", key) == 0)
259 } /* int csv_config */
261 static int csv_write (const data_set_t *ds, const value_list_t *vl,
262 user_data_t __attribute__((unused)) *user_data)
272 if (0 != strcmp (ds->type, vl->type)) {
273 ERROR ("csv plugin: DS type does not match value list type");
277 status = value_list_to_filename (filename, sizeof (filename), vl);
281 DEBUG ("csv plugin: csv_write: filename = %s;", filename);
283 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
290 escape_string (filename, sizeof (filename));
292 /* Replace commas by colons for PUTVAL compatible output. */
293 for (i = 0; i < sizeof (values); i++)
297 else if (values[i] == ',')
301 fprintf (use_stdio == 1 ? stdout : stderr,
302 "PUTVAL %s interval=%.3f %s\n",
304 CDTIME_T_TO_DOUBLE (vl->interval),
309 if (stat (filename, &statbuf) == -1)
313 if (csv_create_file (filename, ds))
319 ERROR ("stat(%s) failed: %s", filename,
320 sstrerror (errno, errbuf,
325 else if (!S_ISREG (statbuf.st_mode))
327 ERROR ("stat(%s): Not a regular file!",
332 csv = fopen (filename, "a");
336 ERROR ("csv plugin: fopen (%s) failed: %s", filename,
337 sstrerror (errno, errbuf, sizeof (errbuf)));
340 csv_fd = fileno (csv);
342 memset (&fl, '\0', sizeof (fl));
344 fl.l_len = 0; /* till end of file */
345 fl.l_pid = getpid ();
347 fl.l_whence = SEEK_SET;
349 status = fcntl (csv_fd, F_SETLK, &fl);
353 ERROR ("csv plugin: flock (%s) failed: %s", filename,
354 sstrerror (errno, errbuf, sizeof (errbuf)));
359 fprintf (csv, "%s\n", values);
361 /* The lock is implicitely released. I we don't release it explicitely
362 * because the `FILE *' may need to flush a cache first */
366 } /* int csv_write */
368 void module_register (void)
370 plugin_register_config ("csv", csv_config,
371 config_keys, config_keys_num);
372 plugin_register_write ("csv", csv_write, /* user_data = */ NULL);
373 } /* void module_register */