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;
40 static int value_list_to_string (char *buffer, int buffer_len,
41 const data_set_t *ds, const value_list_t *vl)
46 gauge_t *rates = NULL;
48 assert (0 == strcmp (ds->type, vl->type));
50 memset (buffer, '\0', buffer_len);
52 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
53 if ((status < 1) || (status >= buffer_len))
57 for (i = 0; i < ds->ds_num; i++)
59 if ((ds->ds[i].type != DS_TYPE_COUNTER)
60 && (ds->ds[i].type != DS_TYPE_GAUGE))
63 if (ds->ds[i].type == DS_TYPE_COUNTER)
67 status = ssnprintf (buffer + offset,
70 vl->values[i].counter);
72 else /* if (store_rates == 1) */
75 rates = uc_get_rate (ds, vl);
78 WARNING ("csv plugin: "
79 "uc_get_rate failed.");
82 status = ssnprintf (buffer + offset,
87 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
89 status = ssnprintf (buffer + offset, buffer_len - offset,
90 ",%lf", vl->values[i].gauge);
93 if ((status < 1) || (status >= (buffer_len - offset)))
100 } /* for ds->ds_num */
104 } /* int value_list_to_string */
106 static int value_list_to_filename (char *buffer, int buffer_len,
107 const data_set_t *ds, const value_list_t *vl)
112 assert (0 == strcmp (ds->type, vl->type));
116 status = ssnprintf (buffer + offset, buffer_len - offset,
118 if ((status < 1) || (status >= buffer_len - offset))
123 status = ssnprintf (buffer + offset, buffer_len - offset,
125 if ((status < 1) || (status >= buffer_len - offset))
129 if (strlen (vl->plugin_instance) > 0)
130 status = ssnprintf (buffer + offset, buffer_len - offset,
131 "%s-%s/", vl->plugin, vl->plugin_instance);
133 status = ssnprintf (buffer + offset, buffer_len - offset,
135 if ((status < 1) || (status >= buffer_len - offset))
139 if (strlen (vl->type_instance) > 0)
140 status = ssnprintf (buffer + offset, buffer_len - offset,
141 "%s-%s", vl->type, vl->type_instance);
143 status = ssnprintf (buffer + offset, buffer_len - offset,
145 if ((status < 1) || (status >= buffer_len - offset))
153 /* TODO: Find a way to minimize the calls to `localtime_r',
154 * since they are pretty expensive.. */
156 if (localtime_r (&now, &stm) == NULL)
158 ERROR ("csv plugin: localtime_r failed");
162 strftime (buffer + offset, buffer_len - offset,
167 } /* int value_list_to_filename */
169 static int csv_create_file (const char *filename, const data_set_t *ds)
174 if (check_create_dir (filename))
177 csv = fopen (filename, "w");
181 ERROR ("csv plugin: fopen (%s) failed: %s",
183 sstrerror (errno, errbuf, sizeof (errbuf)));
187 fprintf (csv, "epoch");
188 for (i = 0; i < ds->ds_num; i++)
189 fprintf (csv, ",%s", ds->ds[i].name);
195 } /* int csv_create_file */
197 static int csv_config (const char *key, const char *value)
199 if (strcasecmp ("DataDir", key) == 0)
203 datadir = strdup (value);
206 int len = strlen (datadir);
207 while ((len > 0) && (datadir[len - 1] == '/'))
219 else if (strcasecmp ("StoreRates", key) == 0)
221 if ((strcasecmp ("True", value) == 0)
222 || (strcasecmp ("Yes", value) == 0)
223 || (strcasecmp ("On", value) == 0))
237 } /* int csv_config */
239 static int csv_write (const data_set_t *ds, const value_list_t *vl)
249 if (0 != strcmp (ds->type, vl->type)) {
250 ERROR ("csv plugin: DS type does not match value list type");
254 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
257 DEBUG ("csv plugin: csv_write: filename = %s;", filename);
259 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
262 if (stat (filename, &statbuf) == -1)
266 if (csv_create_file (filename, ds))
272 ERROR ("stat(%s) failed: %s", filename,
273 sstrerror (errno, errbuf,
278 else if (!S_ISREG (statbuf.st_mode))
280 ERROR ("stat(%s): Not a regular file!",
285 csv = fopen (filename, "a");
289 ERROR ("csv plugin: fopen (%s) failed: %s", filename,
290 sstrerror (errno, errbuf, sizeof (errbuf)));
293 csv_fd = fileno (csv);
295 memset (&fl, '\0', sizeof (fl));
297 fl.l_len = 0; /* till end of file */
298 fl.l_pid = getpid ();
300 fl.l_whence = SEEK_SET;
302 status = fcntl (csv_fd, F_SETLK, &fl);
306 ERROR ("csv plugin: flock (%s) failed: %s", filename,
307 sstrerror (errno, errbuf, sizeof (errbuf)));
312 fprintf (csv, "%s\n", values);
314 /* The lock is implicitely released. I we don't release it explicitely
315 * because the `FILE *' may need to flush a cache first */
319 } /* int csv_write */
321 void module_register (void)
323 plugin_register_config ("csv", csv_config,
324 config_keys, config_keys_num);
325 plugin_register_write ("csv", csv_write);
326 } /* void module_register */