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, "%u", (unsigned int) 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))
67 if (ds->ds[i].type == DS_TYPE_COUNTER)
71 status = ssnprintf (buffer + offset,
74 vl->values[i].counter);
76 else /* if (store_rates == 1) */
79 rates = uc_get_rate (ds, vl);
82 WARNING ("csv plugin: "
83 "uc_get_rate failed.");
86 status = ssnprintf (buffer + offset,
91 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
93 status = ssnprintf (buffer + offset, buffer_len - offset,
94 ",%lf", vl->values[i].gauge);
97 if ((status < 1) || (status >= (buffer_len - offset)))
104 } /* for ds->ds_num */
108 } /* int value_list_to_string */
110 static int value_list_to_filename (char *buffer, int buffer_len,
111 const data_set_t *ds, const value_list_t *vl)
116 assert (0 == strcmp (ds->type, vl->type));
120 status = ssnprintf (buffer + offset, buffer_len - offset,
122 if ((status < 1) || (status >= buffer_len - offset))
127 status = ssnprintf (buffer + offset, buffer_len - offset,
129 if ((status < 1) || (status >= buffer_len - offset))
133 if (strlen (vl->plugin_instance) > 0)
134 status = ssnprintf (buffer + offset, buffer_len - offset,
135 "%s-%s/", vl->plugin, vl->plugin_instance);
137 status = ssnprintf (buffer + offset, buffer_len - offset,
139 if ((status < 1) || (status >= buffer_len - offset))
143 if (strlen (vl->type_instance) > 0)
144 status = ssnprintf (buffer + offset, buffer_len - offset,
145 "%s-%s", vl->type, vl->type_instance);
147 status = ssnprintf (buffer + offset, buffer_len - offset,
149 if ((status < 1) || (status >= buffer_len - offset))
158 /* TODO: Find a way to minimize the calls to `localtime_r',
159 * since they are pretty expensive.. */
161 if (localtime_r (&now, &stm) == NULL)
163 ERROR ("csv plugin: localtime_r failed");
167 strftime (buffer + offset, buffer_len - offset,
172 } /* int value_list_to_filename */
174 static int csv_create_file (const char *filename, const data_set_t *ds)
179 if (check_create_dir (filename))
182 csv = fopen (filename, "w");
186 ERROR ("csv plugin: fopen (%s) failed: %s",
188 sstrerror (errno, errbuf, sizeof (errbuf)));
192 fprintf (csv, "epoch");
193 for (i = 0; i < ds->ds_num; i++)
194 fprintf (csv, ",%s", ds->ds[i].name);
200 } /* int csv_create_file */
202 static int csv_config (const char *key, const char *value)
204 if (strcasecmp ("DataDir", key) == 0)
208 if (strcasecmp ("stdout", value) == 0)
213 else if (strcasecmp ("stderr", value) == 0)
218 datadir = strdup (value);
221 int len = strlen (datadir);
222 while ((len > 0) && (datadir[len - 1] == '/'))
234 else if (strcasecmp ("StoreRates", key) == 0)
236 if ((strcasecmp ("True", value) == 0)
237 || (strcasecmp ("Yes", value) == 0)
238 || (strcasecmp ("On", value) == 0))
252 } /* int csv_config */
254 static int csv_write (const data_set_t *ds, const value_list_t *vl,
255 user_data_t __attribute__((unused)) *user_data)
265 if (0 != strcmp (ds->type, vl->type)) {
266 ERROR ("csv plugin: DS type does not match value list type");
270 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
273 DEBUG ("csv plugin: csv_write: filename = %s;", filename);
275 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
282 escape_string (filename, sizeof (filename));
284 /* Replace commas by colons for PUTVAL compatible output. */
285 for (i = 0; i < sizeof (values); i++)
289 else if (values[i] == ',')
293 fprintf (use_stdio == 1 ? stdout : stderr,
294 "PUTVAL %s interval=%i %s\n",
295 filename, interval_g, values);
299 if (stat (filename, &statbuf) == -1)
303 if (csv_create_file (filename, ds))
309 ERROR ("stat(%s) failed: %s", filename,
310 sstrerror (errno, errbuf,
315 else if (!S_ISREG (statbuf.st_mode))
317 ERROR ("stat(%s): Not a regular file!",
322 csv = fopen (filename, "a");
326 ERROR ("csv plugin: fopen (%s) failed: %s", filename,
327 sstrerror (errno, errbuf, sizeof (errbuf)));
330 csv_fd = fileno (csv);
332 memset (&fl, '\0', sizeof (fl));
334 fl.l_len = 0; /* till end of file */
335 fl.l_pid = getpid ();
337 fl.l_whence = SEEK_SET;
339 status = fcntl (csv_fd, F_SETLK, &fl);
343 ERROR ("csv plugin: flock (%s) failed: %s", filename,
344 sstrerror (errno, errbuf, sizeof (errbuf)));
349 fprintf (csv, "%s\n", values);
351 /* The lock is implicitely released. I we don't release it explicitely
352 * because the `FILE *' may need to flush a cache first */
356 } /* int csv_write */
358 void module_register (void)
360 plugin_register_config ("csv", csv_config,
361 config_keys, config_keys_num);
362 plugin_register_write ("csv", csv_write, /* user_data = */ NULL);
363 } /* void module_register */