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 verplant.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"
28 #include "utils_parse_option.h"
30 /*
31 * Private variables
32 */
33 static const char *config_keys[] =
34 {
35 "DataDir",
36 "StoreRates"
37 };
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)
46 {
47 int offset;
48 int status;
49 int i;
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))
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 return (-1);
69 if (ds->ds[i].type == DS_TYPE_GAUGE)
70 {
71 status = ssnprintf (buffer + offset, buffer_len - offset,
72 ",%lf", vl->values[i].gauge);
73 }
74 else if (store_rates != 0)
75 {
76 if (rates == NULL)
77 rates = uc_get_rate (ds, vl);
78 if (rates == NULL)
79 {
80 WARNING ("csv plugin: "
81 "uc_get_rate failed.");
82 return (-1);
83 }
84 status = ssnprintf (buffer + offset,
85 buffer_len - offset,
86 ",%lf", rates[i]);
87 }
88 else if (ds->ds[i].type == DS_TYPE_COUNTER)
89 {
90 status = ssnprintf (buffer + offset,
91 buffer_len - offset,
92 ",%llu",
93 vl->values[i].counter);
94 }
95 else if (ds->ds[i].type == DS_TYPE_DERIVE)
96 {
97 status = ssnprintf (buffer + offset,
98 buffer_len - offset,
99 ",%"PRIi64,
100 vl->values[i].derive);
101 }
102 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
103 {
104 status = ssnprintf (buffer + offset,
105 buffer_len - offset,
106 ",%"PRIu64,
107 vl->values[i].absolute);
108 }
110 if ((status < 1) || (status >= (buffer_len - offset)))
111 {
112 sfree (rates);
113 return (-1);
114 }
116 offset += status;
117 } /* for ds->ds_num */
119 sfree (rates);
120 return (0);
121 } /* int value_list_to_string */
123 static int value_list_to_filename (char *buffer, int buffer_len,
124 const data_set_t *ds, const value_list_t *vl)
125 {
126 int offset = 0;
127 int status;
129 assert (0 == strcmp (ds->type, vl->type));
131 if (datadir != NULL)
132 {
133 status = ssnprintf (buffer + offset, buffer_len - offset,
134 "%s/", datadir);
135 if ((status < 1) || (status >= buffer_len - offset))
136 return (-1);
137 offset += status;
138 }
140 status = ssnprintf (buffer + offset, buffer_len - offset,
141 "%s/", vl->host);
142 if ((status < 1) || (status >= buffer_len - offset))
143 return (-1);
144 offset += status;
146 if (strlen (vl->plugin_instance) > 0)
147 status = ssnprintf (buffer + offset, buffer_len - offset,
148 "%s-%s/", vl->plugin, vl->plugin_instance);
149 else
150 status = ssnprintf (buffer + offset, buffer_len - offset,
151 "%s/", vl->plugin);
152 if ((status < 1) || (status >= buffer_len - offset))
153 return (-1);
154 offset += status;
156 if (strlen (vl->type_instance) > 0)
157 status = ssnprintf (buffer + offset, buffer_len - offset,
158 "%s-%s", vl->type, vl->type_instance);
159 else
160 status = ssnprintf (buffer + offset, buffer_len - offset,
161 "%s", vl->type);
162 if ((status < 1) || (status >= buffer_len - offset))
163 return (-1);
164 offset += status;
166 if (!use_stdio)
167 {
168 time_t now;
169 struct tm stm;
171 /* TODO: Find a way to minimize the calls to `localtime_r',
172 * since they are pretty expensive.. */
173 now = time (NULL);
174 if (localtime_r (&now, &stm) == NULL)
175 {
176 ERROR ("csv plugin: localtime_r failed");
177 return (1);
178 }
180 strftime (buffer + offset, buffer_len - offset,
181 "-%Y-%m-%d", &stm);
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 int 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 free (datadir);
221 if (strcasecmp ("stdout", value) == 0)
222 {
223 use_stdio = 1;
224 return (0);
225 }
226 else if (strcasecmp ("stderr", value) == 0)
227 {
228 use_stdio = 2;
229 return (0);
230 }
231 datadir = strdup (value);
232 if (datadir != NULL)
233 {
234 int len = strlen (datadir);
235 while ((len > 0) && (datadir[len - 1] == '/'))
236 {
237 len--;
238 datadir[len] = '\0';
239 }
240 if (len <= 0)
241 {
242 free (datadir);
243 datadir = NULL;
244 }
245 }
246 }
247 else if (strcasecmp ("StoreRates", key) == 0)
248 {
249 if ((strcasecmp ("True", value) == 0)
250 || (strcasecmp ("Yes", value) == 0)
251 || (strcasecmp ("On", value) == 0))
252 {
253 store_rates = 1;
254 }
255 else
256 {
257 store_rates = 0;
258 }
259 }
260 else
261 {
262 return (-1);
263 }
264 return (0);
265 } /* int csv_config */
267 static int csv_write (const data_set_t *ds, const value_list_t *vl,
268 user_data_t __attribute__((unused)) *user_data)
269 {
270 struct stat statbuf;
271 char filename[512];
272 char values[512];
273 FILE *csv;
274 int csv_fd;
275 struct flock fl;
276 int status;
278 if (0 != strcmp (ds->type, vl->type)) {
279 ERROR ("csv plugin: DS type does not match value list type");
280 return -1;
281 }
283 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
284 return (-1);
286 DEBUG ("csv plugin: csv_write: filename = %s;", filename);
288 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
289 return (-1);
291 if (use_stdio)
292 {
293 size_t i;
295 escape_string (filename, sizeof (filename));
297 /* Replace commas by colons for PUTVAL compatible output. */
298 for (i = 0; i < sizeof (values); i++)
299 {
300 if (values[i] == 0)
301 break;
302 else if (values[i] == ',')
303 values[i] = ':';
304 }
306 fprintf (use_stdio == 1 ? stdout : stderr,
307 "PUTVAL %s interval=%i %s\n",
308 filename, interval_g, 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 */