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>
29 static const char *config_keys[] =
33 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
35 static char *datadir = NULL;
37 static int value_list_to_string (char *buffer, int buffer_len,
38 const data_set_t *ds, const value_list_t *vl)
44 memset (buffer, '\0', buffer_len);
46 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
47 if ((status < 1) || (status >= buffer_len))
51 for (i = 0; i < ds->ds_num; i++)
53 if ((ds->ds[i].type != DS_TYPE_COUNTER)
54 && (ds->ds[i].type != DS_TYPE_GAUGE))
57 if (ds->ds[i].type == DS_TYPE_COUNTER)
58 status = snprintf (buffer + offset, buffer_len - offset,
59 ",%llu", vl->values[i].counter);
61 status = snprintf (buffer + offset, buffer_len - offset,
62 ",%lf", vl->values[i].gauge);
64 if ((status < 1) || (status >= (buffer_len - offset)))
68 } /* for ds->ds_num */
71 } /* int value_list_to_string */
73 static int value_list_to_filename (char *buffer, int buffer_len,
74 const data_set_t *ds, const value_list_t *vl)
81 status = snprintf (buffer + offset, buffer_len - offset,
83 if ((status < 1) || (status >= buffer_len - offset))
88 status = snprintf (buffer + offset, buffer_len - offset,
90 if ((status < 1) || (status >= buffer_len - offset))
94 if (strlen (vl->plugin_instance) > 0)
95 status = snprintf (buffer + offset, buffer_len - offset,
96 "%s-%s/", vl->plugin, vl->plugin_instance);
98 status = snprintf (buffer + offset, buffer_len - offset,
100 if ((status < 1) || (status >= buffer_len - offset))
104 if (strlen (vl->type_instance) > 0)
105 status = snprintf (buffer + offset, buffer_len - offset,
106 "%s-%s", ds->type, vl->type_instance);
108 status = snprintf (buffer + offset, buffer_len - offset,
110 if ((status < 1) || (status >= buffer_len - offset))
118 /* TODO: Find a way to minimize the calls to `localtime_r',
119 * since they are pretty expensive.. */
121 if (localtime_r (&now, &stm) == NULL)
123 ERROR ("csv plugin: localtime_r failed");
127 strftime (buffer + offset, buffer_len - offset,
132 } /* int value_list_to_filename */
134 static int csv_create_file (const char *filename, const data_set_t *ds)
139 if (check_create_dir (filename))
142 csv = fopen (filename, "w");
146 ERROR ("csv plugin: fopen (%s) failed: %s",
148 sstrerror (errno, errbuf, sizeof (errbuf)));
152 fprintf (csv, "epoch");
153 for (i = 0; i < ds->ds_num; i++)
154 fprintf (csv, ",%s", ds->ds[i].name);
160 } /* int csv_create_file */
162 static int csv_config (const char *key, const char *value)
164 if (strcasecmp ("DataDir", key) == 0)
168 datadir = strdup (value);
171 int len = strlen (datadir);
172 while ((len > 0) && (datadir[len - 1] == '/'))
189 } /* int csv_config */
191 static int csv_write (const data_set_t *ds, const value_list_t *vl)
201 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
204 DEBUG ("csv plugin: csv_write: filename = %s;", filename);
206 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
209 if (stat (filename, &statbuf) == -1)
213 if (csv_create_file (filename, ds))
219 ERROR ("stat(%s) failed: %s", filename,
220 sstrerror (errno, errbuf,
225 else if (!S_ISREG (statbuf.st_mode))
227 ERROR ("stat(%s): Not a regular file!",
232 csv = fopen (filename, "a");
236 ERROR ("csv plugin: fopen (%s) failed: %s", filename,
237 sstrerror (errno, errbuf, sizeof (errbuf)));
240 csv_fd = fileno (csv);
242 memset (&fl, '\0', sizeof (fl));
244 fl.l_len = 0; /* till end of file */
245 fl.l_pid = getpid ();
247 fl.l_whence = SEEK_SET;
249 status = fcntl (csv_fd, F_SETLK, &fl);
253 ERROR ("csv plugin: flock (%s) failed: %s", filename,
254 sstrerror (errno, errbuf, sizeof (errbuf)));
259 fprintf (csv, "%s\n", values);
261 /* The lock is implicitely released. I we don't release it explicitely
262 * because the `FILE *' may need to flush a cache first */
266 } /* int csv_write */
268 void module_register (void)
270 plugin_register_config ("csv", csv_config,
271 config_keys, config_keys_num);
272 plugin_register_write ("csv", csv_write);
273 } /* void module_register */