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 verplant.org>
21 * Doug MacEachern <dougm@hyperic.com>
27 #include "utils_cache.h"
28 #include "utils_parse_option.h"
33 static const char *config_keys[] =
38 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
40 static char *datadir = NULL;
41 static int store_rates = 0;
42 static int use_stdio = 0;
44 static int value_list_to_string (char *buffer, int buffer_len,
45 const data_set_t *ds, const value_list_t *vl)
50 gauge_t *rates = NULL;
52 assert (0 == strcmp (ds->type, vl->type));
54 memset (buffer, '\0', buffer_len);
56 status = ssnprintf (buffer, buffer_len, "%.3f",
57 CDTIME_T_TO_DOUBLE (vl->time));
58 if ((status < 1) || (status >= buffer_len))
62 for (i = 0; i < ds->ds_num; i++)
64 if ((ds->ds[i].type != DS_TYPE_COUNTER)
65 && (ds->ds[i].type != DS_TYPE_GAUGE)
66 && (ds->ds[i].type != DS_TYPE_DERIVE)
67 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
70 if (ds->ds[i].type == DS_TYPE_GAUGE)
72 status = ssnprintf (buffer + offset, buffer_len - offset,
73 ",%lf", vl->values[i].gauge);
75 else if (store_rates != 0)
78 rates = uc_get_rate (ds, vl);
81 WARNING ("csv plugin: "
82 "uc_get_rate failed.");
85 status = ssnprintf (buffer + offset,
89 else if (ds->ds[i].type == DS_TYPE_COUNTER)
91 status = ssnprintf (buffer + offset,
94 vl->values[i].counter);
96 else if (ds->ds[i].type == DS_TYPE_DERIVE)
98 status = ssnprintf (buffer + offset,
101 vl->values[i].derive);
103 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
105 status = ssnprintf (buffer + offset,
108 vl->values[i].absolute);
111 if ((status < 1) || (status >= (buffer_len - offset)))
118 } /* for ds->ds_num */
122 } /* int value_list_to_string */
124 static int value_list_to_filename (char *buffer, size_t buffer_size,
125 value_list_t const *vl)
130 size_t ptr_size = buffer_size;
136 size_t len = strlen (datadir) + 1;
141 memcpy (ptr, datadir, len);
147 status = FORMAT_VL (ptr, ptr_size, vl);
151 /* Skip all the time formatting stuff when printing to STDOUT or
156 ptr_size -= strlen (ptr);
159 /* "-2013-07-12" => 11 bytes */
162 ERROR ("csv plugin: Buffer too small.");
166 /* TODO: Find a way to minimize the calls to `localtime_r',
167 * since they are pretty expensive.. */
169 if (localtime_r (&now, &struct_tm) == NULL)
171 ERROR ("csv plugin: localtime_r failed");
175 status = strftime (ptr, ptr_size, "-%Y-%m-%d", &struct_tm);
176 if (status == 0) /* yep, it returns zero on error. */
178 ERROR ("csv plugin: strftime failed");
183 } /* int value_list_to_filename */
185 static int csv_create_file (const char *filename, const data_set_t *ds)
190 if (check_create_dir (filename))
193 csv = fopen (filename, "w");
197 ERROR ("csv plugin: fopen (%s) failed: %s",
199 sstrerror (errno, errbuf, sizeof (errbuf)));
203 fprintf (csv, "epoch");
204 for (i = 0; i < ds->ds_num; i++)
205 fprintf (csv, ",%s", ds->ds[i].name);
211 } /* int csv_create_file */
213 static int csv_config (const char *key, const char *value)
215 if (strcasecmp ("DataDir", key) == 0)
222 if (strcasecmp ("stdout", value) == 0)
227 else if (strcasecmp ("stderr", value) == 0)
232 datadir = strdup (value);
235 int len = strlen (datadir);
236 while ((len > 0) && (datadir[len - 1] == '/'))
248 else if (strcasecmp ("StoreRates", key) == 0)
260 } /* int csv_config */
262 static int csv_write (const data_set_t *ds, const value_list_t *vl,
263 user_data_t __attribute__((unused)) *user_data)
273 if (0 != strcmp (ds->type, vl->type)) {
274 ERROR ("csv plugin: DS type does not match value list type");
278 status = value_list_to_filename (filename, sizeof (filename), vl);
282 DEBUG ("csv plugin: csv_write: filename = %s;", filename);
284 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
291 escape_string (filename, sizeof (filename));
293 /* Replace commas by colons for PUTVAL compatible output. */
294 for (i = 0; i < sizeof (values); i++)
298 else if (values[i] == ',')
302 fprintf (use_stdio == 1 ? stdout : stderr,
303 "PUTVAL %s interval=%.3f %s\n",
305 CDTIME_T_TO_DOUBLE (vl->interval),
310 if (stat (filename, &statbuf) == -1)
314 if (csv_create_file (filename, ds))
320 ERROR ("stat(%s) failed: %s", filename,
321 sstrerror (errno, errbuf,
326 else if (!S_ISREG (statbuf.st_mode))
328 ERROR ("stat(%s): Not a regular file!",
333 csv = fopen (filename, "a");
337 ERROR ("csv plugin: fopen (%s) failed: %s", filename,
338 sstrerror (errno, errbuf, sizeof (errbuf)));
341 csv_fd = fileno (csv);
343 memset (&fl, '\0', sizeof (fl));
345 fl.l_len = 0; /* till end of file */
346 fl.l_pid = getpid ();
348 fl.l_whence = SEEK_SET;
350 status = fcntl (csv_fd, F_SETLK, &fl);
354 ERROR ("csv plugin: flock (%s) failed: %s", filename,
355 sstrerror (errno, errbuf, sizeof (errbuf)));
360 fprintf (csv, "%s\n", values);
362 /* The lock is implicitely released. I we don't release it explicitely
363 * because the `FILE *' may need to flush a cache first */
367 } /* int csv_write */
369 void module_register (void)
371 plugin_register_config ("csv", csv_config,
372 config_keys, config_keys_num);
373 plugin_register_write ("csv", csv_write, /* user_data = */ NULL);
374 } /* void module_register */