1 /**
2 * collectd - src/csv.c
3 * Copyright (C) 2007 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "utils_cache.h"
27 /*
28 * Private variables
29 */
30 static const char *config_keys[] =
31 {
32 "DataDir",
33 "StoreRates"
34 };
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)
42 {
43 int offset;
44 int status;
45 int i;
46 gauge_t *rates = NULL;
48 memset (buffer, '\0', buffer_len);
50 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
51 if ((status < 1) || (status >= buffer_len))
52 return (-1);
53 offset = status;
55 for (i = 0; i < ds->ds_num; i++)
56 {
57 if ((ds->ds[i].type != DS_TYPE_COUNTER)
58 && (ds->ds[i].type != DS_TYPE_GAUGE))
59 return (-1);
61 if (ds->ds[i].type == DS_TYPE_COUNTER)
62 {
63 if (store_rates == 0)
64 {
65 status = snprintf (buffer + offset,
66 buffer_len - offset,
67 ",%llu",
68 vl->values[i].counter);
69 }
70 else /* if (store_rates == 1) */
71 {
72 if (rates == NULL)
73 rates = uc_get_rate (ds, vl);
74 if (rates == NULL)
75 {
76 WARNING ("csv plugin: "
77 "uc_get_rate failed.");
78 return (-1);
79 }
80 status = snprintf (buffer + offset,
81 buffer_len - offset,
82 ",%lf", rates[i]);
83 }
84 }
85 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
86 {
87 status = snprintf (buffer + offset, buffer_len - offset,
88 ",%lf", vl->values[i].gauge);
89 }
91 if ((status < 1) || (status >= (buffer_len - offset)))
92 {
93 sfree (rates);
94 return (-1);
95 }
97 offset += status;
98 } /* for ds->ds_num */
100 sfree (rates);
101 return (0);
102 } /* int value_list_to_string */
104 static int value_list_to_filename (char *buffer, int buffer_len,
105 const data_set_t *ds, const value_list_t *vl)
106 {
107 int offset = 0;
108 int status;
110 if (datadir != NULL)
111 {
112 status = snprintf (buffer + offset, buffer_len - offset,
113 "%s/", datadir);
114 if ((status < 1) || (status >= buffer_len - offset))
115 return (-1);
116 offset += status;
117 }
119 status = snprintf (buffer + offset, buffer_len - offset,
120 "%s/", vl->host);
121 if ((status < 1) || (status >= buffer_len - offset))
122 return (-1);
123 offset += status;
125 if (strlen (vl->plugin_instance) > 0)
126 status = snprintf (buffer + offset, buffer_len - offset,
127 "%s-%s/", vl->plugin, vl->plugin_instance);
128 else
129 status = snprintf (buffer + offset, buffer_len - offset,
130 "%s/", vl->plugin);
131 if ((status < 1) || (status >= buffer_len - offset))
132 return (-1);
133 offset += status;
135 if (strlen (vl->type_instance) > 0)
136 status = snprintf (buffer + offset, buffer_len - offset,
137 "%s-%s", ds->type, vl->type_instance);
138 else
139 status = snprintf (buffer + offset, buffer_len - offset,
140 "%s", ds->type);
141 if ((status < 1) || (status >= buffer_len - offset))
142 return (-1);
143 offset += status;
145 {
146 time_t now;
147 struct tm stm;
149 /* TODO: Find a way to minimize the calls to `localtime_r',
150 * since they are pretty expensive.. */
151 now = time (NULL);
152 if (localtime_r (&now, &stm) == NULL)
153 {
154 ERROR ("csv plugin: localtime_r failed");
155 return (1);
156 }
158 strftime (buffer + offset, buffer_len - offset,
159 "-%Y-%m-%d", &stm);
160 }
162 return (0);
163 } /* int value_list_to_filename */
165 static int csv_create_file (const char *filename, const data_set_t *ds)
166 {
167 FILE *csv;
168 int i;
170 if (check_create_dir (filename))
171 return (-1);
173 csv = fopen (filename, "w");
174 if (csv == NULL)
175 {
176 char errbuf[1024];
177 ERROR ("csv plugin: fopen (%s) failed: %s",
178 filename,
179 sstrerror (errno, errbuf, sizeof (errbuf)));
180 return (-1);
181 }
183 fprintf (csv, "epoch");
184 for (i = 0; i < ds->ds_num; i++)
185 fprintf (csv, ",%s", ds->ds[i].name);
187 fprintf (csv, "\n");
188 fclose (csv);
190 return 0;
191 } /* int csv_create_file */
193 static int csv_config (const char *key, const char *value)
194 {
195 if (strcasecmp ("DataDir", key) == 0)
196 {
197 if (datadir != NULL)
198 free (datadir);
199 datadir = strdup (value);
200 if (datadir != NULL)
201 {
202 int len = strlen (datadir);
203 while ((len > 0) && (datadir[len - 1] == '/'))
204 {
205 len--;
206 datadir[len] = '\0';
207 }
208 if (len <= 0)
209 {
210 free (datadir);
211 datadir = NULL;
212 }
213 }
214 }
215 else if (strcasecmp ("StoreRates", key) == 0)
216 {
217 if ((strcasecmp ("True", value) == 0)
218 || (strcasecmp ("Yes", value) == 0)
219 || (strcasecmp ("On", value) == 0))
220 {
221 store_rates = 1;
222 }
223 else
224 {
225 store_rates = 0;
226 }
227 }
228 else
229 {
230 return (-1);
231 }
232 return (0);
233 } /* int csv_config */
235 static int csv_write (const data_set_t *ds, const value_list_t *vl)
236 {
237 struct stat statbuf;
238 char filename[512];
239 char values[512];
240 FILE *csv;
241 int csv_fd;
242 struct flock fl;
243 int status;
245 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
246 return (-1);
248 DEBUG ("csv plugin: csv_write: filename = %s;", filename);
250 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
251 return (-1);
253 if (stat (filename, &statbuf) == -1)
254 {
255 if (errno == ENOENT)
256 {
257 if (csv_create_file (filename, ds))
258 return (-1);
259 }
260 else
261 {
262 char errbuf[1024];
263 ERROR ("stat(%s) failed: %s", filename,
264 sstrerror (errno, errbuf,
265 sizeof (errbuf)));
266 return (-1);
267 }
268 }
269 else if (!S_ISREG (statbuf.st_mode))
270 {
271 ERROR ("stat(%s): Not a regular file!",
272 filename);
273 return (-1);
274 }
276 csv = fopen (filename, "a");
277 if (csv == NULL)
278 {
279 char errbuf[1024];
280 ERROR ("csv plugin: fopen (%s) failed: %s", filename,
281 sstrerror (errno, errbuf, sizeof (errbuf)));
282 return (-1);
283 }
284 csv_fd = fileno (csv);
286 memset (&fl, '\0', sizeof (fl));
287 fl.l_start = 0;
288 fl.l_len = 0; /* till end of file */
289 fl.l_pid = getpid ();
290 fl.l_type = F_WRLCK;
291 fl.l_whence = SEEK_SET;
293 status = fcntl (csv_fd, F_SETLK, &fl);
294 if (status != 0)
295 {
296 char errbuf[1024];
297 ERROR ("csv plugin: flock (%s) failed: %s", filename,
298 sstrerror (errno, errbuf, sizeof (errbuf)));
299 fclose (csv);
300 return (-1);
301 }
303 fprintf (csv, "%s\n", values);
305 /* The lock is implicitely released. I we don't release it explicitely
306 * because the `FILE *' may need to flush a cache first */
307 fclose (csv);
309 return (0);
310 } /* int csv_write */
312 void module_register (void)
313 {
314 plugin_register_config ("csv", csv_config,
315 config_keys, config_keys_num);
316 plugin_register_write ("csv", csv_write);
317 } /* void module_register */