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, "%.3f",
57 CDTIME_T_TO_DOUBLE (vl->time));
58 if ((status < 1) || (status >= buffer_len))
59 return (-1);
60 offset = status;
62 for (i = 0; i < ds->ds_num; i++)
63 {
64 if ((ds->ds[i].type != DS_TYPE_COUNTER)
65 && (ds->ds[i].type != DS_TYPE_GAUGE)
66 && (ds->ds[i].type != DS_TYPE_DERIVE)
67 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
68 return (-1);
70 if (ds->ds[i].type == DS_TYPE_GAUGE)
71 {
72 status = ssnprintf (buffer + offset, buffer_len - offset,
73 ",%lf", vl->values[i].gauge);
74 }
75 else if (store_rates != 0)
76 {
77 if (rates == NULL)
78 rates = uc_get_rate (ds, vl);
79 if (rates == NULL)
80 {
81 WARNING ("csv plugin: "
82 "uc_get_rate failed.");
83 return (-1);
84 }
85 status = ssnprintf (buffer + offset,
86 buffer_len - offset,
87 ",%lf", rates[i]);
88 }
89 else if (ds->ds[i].type == DS_TYPE_COUNTER)
90 {
91 status = ssnprintf (buffer + offset,
92 buffer_len - offset,
93 ",%llu",
94 vl->values[i].counter);
95 }
96 else if (ds->ds[i].type == DS_TYPE_DERIVE)
97 {
98 status = ssnprintf (buffer + offset,
99 buffer_len - offset,
100 ",%"PRIi64,
101 vl->values[i].derive);
102 }
103 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
104 {
105 status = ssnprintf (buffer + offset,
106 buffer_len - offset,
107 ",%"PRIu64,
108 vl->values[i].absolute);
109 }
111 if ((status < 1) || (status >= (buffer_len - offset)))
112 {
113 sfree (rates);
114 return (-1);
115 }
117 offset += status;
118 } /* for ds->ds_num */
120 sfree (rates);
121 return (0);
122 } /* int value_list_to_string */
124 static int value_list_to_filename (char *buffer, int buffer_len,
125 const data_set_t *ds, const value_list_t *vl)
126 {
127 int offset = 0;
128 int status;
130 assert (0 == strcmp (ds->type, vl->type));
132 if (datadir != NULL)
133 {
134 status = ssnprintf (buffer + offset, buffer_len - offset,
135 "%s/", datadir);
136 if ((status < 1) || (status >= buffer_len - offset))
137 return (-1);
138 offset += status;
139 }
141 status = ssnprintf (buffer + offset, buffer_len - offset,
142 "%s/", vl->host);
143 if ((status < 1) || (status >= buffer_len - offset))
144 return (-1);
145 offset += status;
147 if (strlen (vl->plugin_instance) > 0)
148 status = ssnprintf (buffer + offset, buffer_len - offset,
149 "%s-%s/", vl->plugin, vl->plugin_instance);
150 else
151 status = ssnprintf (buffer + offset, buffer_len - offset,
152 "%s/", vl->plugin);
153 if ((status < 1) || (status >= buffer_len - offset))
154 return (-1);
155 offset += status;
157 if (strlen (vl->type_instance) > 0)
158 status = ssnprintf (buffer + offset, buffer_len - offset,
159 "%s-%s", vl->type, vl->type_instance);
160 else
161 status = ssnprintf (buffer + offset, buffer_len - offset,
162 "%s", vl->type);
163 if ((status < 1) || (status >= buffer_len - offset))
164 return (-1);
165 offset += status;
167 if (!use_stdio)
168 {
169 time_t now;
170 struct tm stm;
172 /* TODO: Find a way to minimize the calls to `localtime_r',
173 * since they are pretty expensive.. */
174 now = time (NULL);
175 if (localtime_r (&now, &stm) == NULL)
176 {
177 ERROR ("csv plugin: localtime_r failed");
178 return (1);
179 }
181 strftime (buffer + offset, buffer_len - offset,
182 "-%Y-%m-%d", &stm);
183 }
185 return (0);
186 } /* int value_list_to_filename */
188 static int csv_create_file (const char *filename, const data_set_t *ds)
189 {
190 FILE *csv;
191 int i;
193 if (check_create_dir (filename))
194 return (-1);
196 csv = fopen (filename, "w");
197 if (csv == NULL)
198 {
199 char errbuf[1024];
200 ERROR ("csv plugin: fopen (%s) failed: %s",
201 filename,
202 sstrerror (errno, errbuf, sizeof (errbuf)));
203 return (-1);
204 }
206 fprintf (csv, "epoch");
207 for (i = 0; i < ds->ds_num; i++)
208 fprintf (csv, ",%s", ds->ds[i].name);
210 fprintf (csv, "\n");
211 fclose (csv);
213 return 0;
214 } /* int csv_create_file */
216 static int csv_config (const char *key, const char *value)
217 {
218 if (strcasecmp ("DataDir", key) == 0)
219 {
220 if (datadir != NULL)
221 free (datadir);
222 if (strcasecmp ("stdout", value) == 0)
223 {
224 use_stdio = 1;
225 return (0);
226 }
227 else if (strcasecmp ("stderr", value) == 0)
228 {
229 use_stdio = 2;
230 return (0);
231 }
232 datadir = strdup (value);
233 if (datadir != NULL)
234 {
235 int len = strlen (datadir);
236 while ((len > 0) && (datadir[len - 1] == '/'))
237 {
238 len--;
239 datadir[len] = '\0';
240 }
241 if (len <= 0)
242 {
243 free (datadir);
244 datadir = NULL;
245 }
246 }
247 }
248 else if (strcasecmp ("StoreRates", key) == 0)
249 {
250 if (IS_TRUE (value))
251 store_rates = 1;
252 else
253 store_rates = 0;
254 }
255 else
256 {
257 return (-1);
258 }
259 return (0);
260 } /* int csv_config */
262 static int csv_write (const data_set_t *ds, const value_list_t *vl,
263 user_data_t __attribute__((unused)) *user_data)
264 {
265 struct stat statbuf;
266 char filename[512];
267 char values[4096];
268 FILE *csv;
269 int csv_fd;
270 struct flock fl;
271 int status;
273 if (0 != strcmp (ds->type, vl->type)) {
274 ERROR ("csv plugin: DS type does not match value list type");
275 return -1;
276 }
278 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
279 return (-1);
281 DEBUG ("csv plugin: csv_write: filename = %s;", filename);
283 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
284 return (-1);
286 if (use_stdio)
287 {
288 size_t i;
290 escape_string (filename, sizeof (filename));
292 /* Replace commas by colons for PUTVAL compatible output. */
293 for (i = 0; i < sizeof (values); i++)
294 {
295 if (values[i] == 0)
296 break;
297 else if (values[i] == ',')
298 values[i] = ':';
299 }
301 fprintf (use_stdio == 1 ? stdout : stderr,
302 "PUTVAL %s interval=%.3f %s\n",
303 filename,
304 CDTIME_T_TO_DOUBLE (vl->interval),
305 values);
306 return (0);
307 }
309 if (stat (filename, &statbuf) == -1)
310 {
311 if (errno == ENOENT)
312 {
313 if (csv_create_file (filename, ds))
314 return (-1);
315 }
316 else
317 {
318 char errbuf[1024];
319 ERROR ("stat(%s) failed: %s", filename,
320 sstrerror (errno, errbuf,
321 sizeof (errbuf)));
322 return (-1);
323 }
324 }
325 else if (!S_ISREG (statbuf.st_mode))
326 {
327 ERROR ("stat(%s): Not a regular file!",
328 filename);
329 return (-1);
330 }
332 csv = fopen (filename, "a");
333 if (csv == NULL)
334 {
335 char errbuf[1024];
336 ERROR ("csv plugin: fopen (%s) failed: %s", filename,
337 sstrerror (errno, errbuf, sizeof (errbuf)));
338 return (-1);
339 }
340 csv_fd = fileno (csv);
342 memset (&fl, '\0', sizeof (fl));
343 fl.l_start = 0;
344 fl.l_len = 0; /* till end of file */
345 fl.l_pid = getpid ();
346 fl.l_type = F_WRLCK;
347 fl.l_whence = SEEK_SET;
349 status = fcntl (csv_fd, F_SETLK, &fl);
350 if (status != 0)
351 {
352 char errbuf[1024];
353 ERROR ("csv plugin: flock (%s) failed: %s", filename,
354 sstrerror (errno, errbuf, sizeof (errbuf)));
355 fclose (csv);
356 return (-1);
357 }
359 fprintf (csv, "%s\n", values);
361 /* The lock is implicitely released. I we don't release it explicitely
362 * because the `FILE *' may need to flush a cache first */
363 fclose (csv);
365 return (0);
366 } /* int csv_write */
368 void module_register (void)
369 {
370 plugin_register_config ("csv", csv_config,
371 config_keys, config_keys_num);
372 plugin_register_write ("csv", csv_write, /* user_data = */ NULL);
373 } /* void module_register */