1 /**
2 * collectd - src/csv.c
3 * Copyright (C) 2007-2009 Florian octo Forster
4 * Copyright (C) 2009 Doug MacEachern
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at collectd.org>
21 * Doug MacEachern <dougm@hyperic.com>
22 **/
24 #include "collectd.h"
25 #include "plugin.h"
26 #include "common.h"
27 #include "utils_cache.h"
29 /*
30 * Private variables
31 */
32 static const char *config_keys[] =
33 {
34 "DataDir",
35 "StoreRates"
36 };
37 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
39 static char *datadir = NULL;
40 static int store_rates = 0;
41 static int use_stdio = 0;
43 static int value_list_to_string (char *buffer, int buffer_len,
44 const data_set_t *ds, const value_list_t *vl)
45 {
46 int offset;
47 int status;
48 size_t i;
49 gauge_t *rates = NULL;
51 assert (0 == strcmp (ds->type, vl->type));
53 memset (buffer, '\0', buffer_len);
55 status = ssnprintf (buffer, buffer_len, "%.3f",
56 CDTIME_T_TO_DOUBLE (vl->time));
57 if ((status < 1) || (status >= buffer_len))
58 return (-1);
59 offset = status;
61 for (i = 0; i < ds->ds_num; i++)
62 {
63 if ((ds->ds[i].type != DS_TYPE_COUNTER)
64 && (ds->ds[i].type != DS_TYPE_GAUGE)
65 && (ds->ds[i].type != DS_TYPE_DERIVE)
66 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
67 {
68 sfree (rates);
69 return (-1);
70 }
72 if (ds->ds[i].type == DS_TYPE_GAUGE)
73 {
74 status = ssnprintf (buffer + offset, buffer_len - offset,
75 ",%lf", vl->values[i].gauge);
76 }
77 else if (store_rates != 0)
78 {
79 if (rates == NULL)
80 rates = uc_get_rate (ds, vl);
81 if (rates == NULL)
82 {
83 WARNING ("csv plugin: "
84 "uc_get_rate failed.");
85 return (-1);
86 }
87 status = ssnprintf (buffer + offset,
88 buffer_len - offset,
89 ",%lf", rates[i]);
90 }
91 else if (ds->ds[i].type == DS_TYPE_COUNTER)
92 {
93 status = ssnprintf (buffer + offset,
94 buffer_len - offset,
95 ",%llu",
96 vl->values[i].counter);
97 }
98 else if (ds->ds[i].type == DS_TYPE_DERIVE)
99 {
100 status = ssnprintf (buffer + offset,
101 buffer_len - offset,
102 ",%"PRIi64,
103 vl->values[i].derive);
104 }
105 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
106 {
107 status = ssnprintf (buffer + offset,
108 buffer_len - offset,
109 ",%"PRIu64,
110 vl->values[i].absolute);
111 }
113 if ((status < 1) || (status >= (buffer_len - offset)))
114 {
115 sfree (rates);
116 return (-1);
117 }
119 offset += status;
120 } /* for ds->ds_num */
122 sfree (rates);
123 return (0);
124 } /* int value_list_to_string */
126 static int value_list_to_filename (char *buffer, size_t buffer_size,
127 value_list_t const *vl)
128 {
129 int status;
131 char *ptr = buffer;
132 size_t ptr_size = buffer_size;
133 time_t now;
134 struct tm struct_tm;
136 if (datadir != NULL)
137 {
138 size_t len = strlen (datadir) + 1;
140 if (len >= ptr_size)
141 return (ENOBUFS);
143 memcpy (ptr, datadir, len);
144 ptr[len-1] = '/';
145 ptr_size -= len;
146 ptr += len;
147 }
149 status = FORMAT_VL (ptr, ptr_size, vl);
150 if (status != 0)
151 return (status);
153 /* Skip all the time formatting stuff when printing to STDOUT or
154 * STDERR. */
155 if (use_stdio)
156 return (0);
158 ptr_size -= strlen (ptr);
159 ptr += strlen (ptr);
161 /* "-2013-07-12" => 11 bytes */
162 if (ptr_size < 12)
163 {
164 ERROR ("csv plugin: Buffer too small.");
165 return (ENOMEM);
166 }
168 /* TODO: Find a way to minimize the calls to `localtime_r',
169 * since they are pretty expensive.. */
170 now = time (NULL);
171 if (localtime_r (&now, &struct_tm) == NULL)
172 {
173 ERROR ("csv plugin: localtime_r failed");
174 return (-1);
175 }
177 status = strftime (ptr, ptr_size, "-%Y-%m-%d", &struct_tm);
178 if (status == 0) /* yep, it returns zero on error. */
179 {
180 ERROR ("csv plugin: strftime failed");
181 return (-1);
182 }
184 return (0);
185 } /* int value_list_to_filename */
187 static int csv_create_file (const char *filename, const data_set_t *ds)
188 {
189 FILE *csv;
190 size_t i;
192 if (check_create_dir (filename))
193 return (-1);
195 csv = fopen (filename, "w");
196 if (csv == NULL)
197 {
198 char errbuf[1024];
199 ERROR ("csv plugin: fopen (%s) failed: %s",
200 filename,
201 sstrerror (errno, errbuf, sizeof (errbuf)));
202 return (-1);
203 }
205 fprintf (csv, "epoch");
206 for (i = 0; i < ds->ds_num; i++)
207 fprintf (csv, ",%s", ds->ds[i].name);
209 fprintf (csv, "\n");
210 fclose (csv);
212 return 0;
213 } /* int csv_create_file */
215 static int csv_config (const char *key, const char *value)
216 {
217 if (strcasecmp ("DataDir", key) == 0)
218 {
219 if (datadir != NULL)
220 {
221 free (datadir);
222 datadir = NULL;
223 }
224 if (strcasecmp ("stdout", value) == 0)
225 {
226 use_stdio = 1;
227 return (0);
228 }
229 else if (strcasecmp ("stderr", value) == 0)
230 {
231 use_stdio = 2;
232 return (0);
233 }
234 datadir = strdup (value);
235 if (datadir != NULL)
236 {
237 int len = strlen (datadir);
238 while ((len > 0) && (datadir[len - 1] == '/'))
239 {
240 len--;
241 datadir[len] = '\0';
242 }
243 if (len <= 0)
244 {
245 free (datadir);
246 datadir = NULL;
247 }
248 }
249 }
250 else if (strcasecmp ("StoreRates", key) == 0)
251 {
252 if (IS_TRUE (value))
253 store_rates = 1;
254 else
255 store_rates = 0;
256 }
257 else
258 {
259 return (-1);
260 }
261 return (0);
262 } /* int csv_config */
264 static int csv_write (const data_set_t *ds, const value_list_t *vl,
265 user_data_t __attribute__((unused)) *user_data)
266 {
267 struct stat statbuf;
268 char filename[512];
269 char values[4096];
270 FILE *csv;
271 int csv_fd;
272 struct flock fl;
273 int status;
275 if (0 != strcmp (ds->type, vl->type)) {
276 ERROR ("csv plugin: DS type does not match value list type");
277 return -1;
278 }
280 status = value_list_to_filename (filename, sizeof (filename), vl);
281 if (status != 0)
282 return (-1);
284 DEBUG ("csv plugin: csv_write: filename = %s;", filename);
286 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
287 return (-1);
289 if (use_stdio)
290 {
291 size_t i;
293 escape_string (filename, sizeof (filename));
295 /* Replace commas by colons for PUTVAL compatible output. */
296 for (i = 0; i < sizeof (values); i++)
297 {
298 if (values[i] == 0)
299 break;
300 else if (values[i] == ',')
301 values[i] = ':';
302 }
304 fprintf (use_stdio == 1 ? stdout : stderr,
305 "PUTVAL %s interval=%.3f %s\n",
306 filename,
307 CDTIME_T_TO_DOUBLE (vl->interval),
308 values);
309 return (0);
310 }
312 if (stat (filename, &statbuf) == -1)
313 {
314 if (errno == ENOENT)
315 {
316 if (csv_create_file (filename, ds))
317 return (-1);
318 }
319 else
320 {
321 char errbuf[1024];
322 ERROR ("stat(%s) failed: %s", filename,
323 sstrerror (errno, errbuf,
324 sizeof (errbuf)));
325 return (-1);
326 }
327 }
328 else if (!S_ISREG (statbuf.st_mode))
329 {
330 ERROR ("stat(%s): Not a regular file!",
331 filename);
332 return (-1);
333 }
335 csv = fopen (filename, "a");
336 if (csv == NULL)
337 {
338 char errbuf[1024];
339 ERROR ("csv plugin: fopen (%s) failed: %s", filename,
340 sstrerror (errno, errbuf, sizeof (errbuf)));
341 return (-1);
342 }
343 csv_fd = fileno (csv);
345 memset (&fl, '\0', sizeof (fl));
346 fl.l_start = 0;
347 fl.l_len = 0; /* till end of file */
348 fl.l_pid = getpid ();
349 fl.l_type = F_WRLCK;
350 fl.l_whence = SEEK_SET;
352 status = fcntl (csv_fd, F_SETLK, &fl);
353 if (status != 0)
354 {
355 char errbuf[1024];
356 ERROR ("csv plugin: flock (%s) failed: %s", filename,
357 sstrerror (errno, errbuf, sizeof (errbuf)));
358 fclose (csv);
359 return (-1);
360 }
362 fprintf (csv, "%s\n", values);
364 /* The lock is implicitely released. I we don't release it explicitely
365 * because the `FILE *' may need to flush a cache first */
366 fclose (csv);
368 return (0);
369 } /* int csv_write */
371 void module_register (void)
372 {
373 plugin_register_config ("csv", csv_config,
374 config_keys, config_keys_num);
375 plugin_register_write ("csv", csv_write, /* user_data = */ NULL);
376 } /* void module_register */