3 * Copyright (C) 2007 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_cache.h"
30 static const char *config_keys[] =
35 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
37 static char *datadir = NULL;
38 static int store_rates = 0;
39 static int use_stdio = 0;
41 static int value_list_to_string (char *buffer, int buffer_len,
42 const data_set_t *ds, const value_list_t *vl)
47 gauge_t *rates = NULL;
49 assert (0 == strcmp (ds->type, vl->type));
51 memset (buffer, '\0', buffer_len);
53 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
54 if ((status < 1) || (status >= buffer_len))
58 for (i = 0; i < ds->ds_num; i++)
60 if ((ds->ds[i].type != DS_TYPE_COUNTER)
61 && (ds->ds[i].type != DS_TYPE_GAUGE))
64 if (ds->ds[i].type == DS_TYPE_COUNTER)
68 status = ssnprintf (buffer + offset,
71 vl->values[i].counter);
73 else /* if (store_rates == 1) */
76 rates = uc_get_rate (ds, vl);
79 WARNING ("csv plugin: "
80 "uc_get_rate failed.");
83 status = ssnprintf (buffer + offset,
88 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
90 status = ssnprintf (buffer + offset, buffer_len - offset,
91 ",%lf", vl->values[i].gauge);
94 if ((status < 1) || (status >= (buffer_len - offset)))
101 } /* for ds->ds_num */
105 } /* int value_list_to_string */
107 static int value_list_to_filename (char *buffer, int buffer_len,
108 const data_set_t *ds, const value_list_t *vl)
113 assert (0 == strcmp (ds->type, vl->type));
117 status = ssnprintf (buffer + offset, buffer_len - offset,
119 if ((status < 1) || (status >= buffer_len - offset))
124 status = ssnprintf (buffer + offset, buffer_len - offset,
126 if ((status < 1) || (status >= buffer_len - offset))
130 if (strlen (vl->plugin_instance) > 0)
131 status = ssnprintf (buffer + offset, buffer_len - offset,
132 "%s-%s/", vl->plugin, vl->plugin_instance);
134 status = ssnprintf (buffer + offset, buffer_len - offset,
136 if ((status < 1) || (status >= buffer_len - offset))
140 if (strlen (vl->type_instance) > 0)
141 status = ssnprintf (buffer + offset, buffer_len - offset,
142 "%s-%s", vl->type, vl->type_instance);
144 status = ssnprintf (buffer + offset, buffer_len - offset,
146 if ((status < 1) || (status >= buffer_len - offset))
155 /* TODO: Find a way to minimize the calls to `localtime_r',
156 * since they are pretty expensive.. */
158 if (localtime_r (&now, &stm) == NULL)
160 ERROR ("csv plugin: localtime_r failed");
164 strftime (buffer + offset, buffer_len - offset,
169 } /* int value_list_to_filename */
171 static int csv_create_file (const char *filename, const data_set_t *ds)
176 if (check_create_dir (filename))
179 csv = fopen (filename, "w");
183 ERROR ("csv plugin: fopen (%s) failed: %s",
185 sstrerror (errno, errbuf, sizeof (errbuf)));
189 fprintf (csv, "epoch");
190 for (i = 0; i < ds->ds_num; i++)
191 fprintf (csv, ",%s", ds->ds[i].name);
197 } /* int csv_create_file */
199 static int csv_config (const char *key, const char *value)
201 if (strcasecmp ("DataDir", key) == 0)
205 if (strcasecmp ("stdout", value) == 0)
210 else if (strcasecmp ("stderr", value) == 0)
215 datadir = strdup (value);
218 int len = strlen (datadir);
219 while ((len > 0) && (datadir[len - 1] == '/'))
231 else if (strcasecmp ("StoreRates", key) == 0)
233 if ((strcasecmp ("True", value) == 0)
234 || (strcasecmp ("Yes", value) == 0)
235 || (strcasecmp ("On", value) == 0))
249 } /* int csv_config */
251 static int csv_write (const data_set_t *ds, const value_list_t *vl)
261 if (0 != strcmp (ds->type, vl->type)) {
262 ERROR ("csv plugin: DS type does not match value list type");
266 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
269 DEBUG ("csv plugin: csv_write: filename = %s;", filename);
271 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
276 fprintf (use_stdio == 1 ? stdout : stderr,
277 "%s=%s\n", filename, values);
281 if (stat (filename, &statbuf) == -1)
285 if (csv_create_file (filename, ds))
291 ERROR ("stat(%s) failed: %s", filename,
292 sstrerror (errno, errbuf,
297 else if (!S_ISREG (statbuf.st_mode))
299 ERROR ("stat(%s): Not a regular file!",
304 csv = fopen (filename, "a");
308 ERROR ("csv plugin: fopen (%s) failed: %s", filename,
309 sstrerror (errno, errbuf, sizeof (errbuf)));
312 csv_fd = fileno (csv);
314 memset (&fl, '\0', sizeof (fl));
316 fl.l_len = 0; /* till end of file */
317 fl.l_pid = getpid ();
319 fl.l_whence = SEEK_SET;
321 status = fcntl (csv_fd, F_SETLK, &fl);
325 ERROR ("csv plugin: flock (%s) failed: %s", filename,
326 sstrerror (errno, errbuf, sizeof (errbuf)));
331 fprintf (csv, "%s\n", values);
333 /* The lock is implicitely released. I we don't release it explicitely
334 * because the `FILE *' may need to flush a cache first */
338 } /* int csv_write */
340 void module_register (void)
342 plugin_register_config ("csv", csv_config,
343 config_keys, config_keys_num);
344 plugin_register_write ("csv", csv_write);
345 } /* void module_register */