Code

afb4f8338c5d7e2611e489338eda5a642f3c56b8
[collectd.git] / src / csv.c
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, size_t buffer_size,
125                 value_list_t const *vl)
127         int status;
129         char *ptr;
130         size_t ptr_size;
131         time_t now;
132         struct tm struct_tm;
134         status = FORMAT_VL (buffer, buffer_size, vl);
135         if (status != 0)
136                 return (status);
138         /* Skip all the time formatting stuff when printing to STDOUT or
139          * STDERR. */
140         if (use_stdio)
141                 return (0);
143         ptr_size = buffer_size - strlen (buffer);
144         ptr = buffer + strlen (buffer);
146         /* "-2013-07-12" => 11 bytes */
147         if (ptr_size < 12)
148         {
149                 ERROR ("csv plugin: Buffer too small.");
150                 return (ENOMEM);
151         }
153         /* TODO: Find a way to minimize the calls to `localtime_r',
154          * since they are pretty expensive.. */
155         now = time (NULL);
156         if (localtime_r (&now, &struct_tm) == NULL)
157         {
158                 ERROR ("csv plugin: localtime_r failed");
159                 return (-1);
160         }
162         status = strftime (ptr, ptr_size, "-%Y-%m-%d", &struct_tm);
163         if (status == 0) /* yep, it returns zero on error. */
164         {
165                 ERROR ("csv plugin: strftime failed");
166                 return (-1);
167         }
169         return (0);
170 } /* int value_list_to_filename */
172 static int csv_create_file (const char *filename, const data_set_t *ds)
174         FILE *csv;
175         int i;
177         if (check_create_dir (filename))
178                 return (-1);
180         csv = fopen (filename, "w");
181         if (csv == NULL)
182         {
183                 char errbuf[1024];
184                 ERROR ("csv plugin: fopen (%s) failed: %s",
185                                 filename,
186                                 sstrerror (errno, errbuf, sizeof (errbuf)));
187                 return (-1);
188         }
190         fprintf (csv, "epoch");
191         for (i = 0; i < ds->ds_num; i++)
192                 fprintf (csv, ",%s", ds->ds[i].name);
194         fprintf (csv, "\n");
195         fclose (csv);
197         return 0;
198 } /* int csv_create_file */
200 static int csv_config (const char *key, const char *value)
202         if (strcasecmp ("DataDir", key) == 0)
203         {
204                 if (datadir != NULL)
205                         free (datadir);
206                 if (strcasecmp ("stdout", value) == 0)
207                 {
208                         use_stdio = 1;
209                         return (0);
210                 }
211                 else if (strcasecmp ("stderr", value) == 0)
212                 {
213                         use_stdio = 2;
214                         return (0);
215                 }
216                 datadir = strdup (value);
217                 if (datadir != NULL)
218                 {
219                         int len = strlen (datadir);
220                         while ((len > 0) && (datadir[len - 1] == '/'))
221                         {
222                                 len--;
223                                 datadir[len] = '\0';
224                         }
225                         if (len <= 0)
226                         {
227                                 free (datadir);
228                                 datadir = NULL;
229                         }
230                 }
231         }
232         else if (strcasecmp ("StoreRates", key) == 0)
233         {
234                 if (IS_TRUE (value))
235                         store_rates = 1;
236                 else
237                         store_rates = 0;
238         }
239         else
240         {
241                 return (-1);
242         }
243         return (0);
244 } /* int csv_config */
246 static int csv_write (const data_set_t *ds, const value_list_t *vl,
247                 user_data_t __attribute__((unused)) *user_data)
249         struct stat  statbuf;
250         char         filename[512];
251         char         values[4096];
252         FILE        *csv;
253         int          csv_fd;
254         struct flock fl;
255         int          status;
257         if (0 != strcmp (ds->type, vl->type)) {
258                 ERROR ("csv plugin: DS type does not match value list type");
259                 return -1;
260         }
262         status = value_list_to_filename (filename, sizeof (filename), vl);
263         if (status != 0)
264                 return (-1);
266         DEBUG ("csv plugin: csv_write: filename = %s;", filename);
268         if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
269                 return (-1);
271         if (use_stdio)
272         {
273                 size_t i;
275                 escape_string (filename, sizeof (filename));
277                 /* Replace commas by colons for PUTVAL compatible output. */
278                 for (i = 0; i < sizeof (values); i++)
279                 {
280                         if (values[i] == 0)
281                                 break;
282                         else if (values[i] == ',')
283                                 values[i] = ':';
284                 }
286                 fprintf (use_stdio == 1 ? stdout : stderr,
287                          "PUTVAL %s interval=%.3f %s\n",
288                          filename,
289                          CDTIME_T_TO_DOUBLE (vl->interval),
290                          values);
291                 return (0);
292         }
294         if (stat (filename, &statbuf) == -1)
295         {
296                 if (errno == ENOENT)
297                 {
298                         if (csv_create_file (filename, ds))
299                                 return (-1);
300                 }
301                 else
302                 {
303                         char errbuf[1024];
304                         ERROR ("stat(%s) failed: %s", filename,
305                                         sstrerror (errno, errbuf,
306                                                 sizeof (errbuf)));
307                         return (-1);
308                 }
309         }
310         else if (!S_ISREG (statbuf.st_mode))
311         {
312                 ERROR ("stat(%s): Not a regular file!",
313                                 filename);
314                 return (-1);
315         }
317         csv = fopen (filename, "a");
318         if (csv == NULL)
319         {
320                 char errbuf[1024];
321                 ERROR ("csv plugin: fopen (%s) failed: %s", filename,
322                                 sstrerror (errno, errbuf, sizeof (errbuf)));
323                 return (-1);
324         }
325         csv_fd = fileno (csv);
327         memset (&fl, '\0', sizeof (fl));
328         fl.l_start  = 0;
329         fl.l_len    = 0; /* till end of file */
330         fl.l_pid    = getpid ();
331         fl.l_type   = F_WRLCK;
332         fl.l_whence = SEEK_SET;
334         status = fcntl (csv_fd, F_SETLK, &fl);
335         if (status != 0)
336         {
337                 char errbuf[1024];
338                 ERROR ("csv plugin: flock (%s) failed: %s", filename,
339                                 sstrerror (errno, errbuf, sizeof (errbuf)));
340                 fclose (csv);
341                 return (-1);
342         }
344         fprintf (csv, "%s\n", values);
346         /* The lock is implicitely released. I we don't release it explicitely
347          * because the `FILE *' may need to flush a cache first */
348         fclose (csv);
350         return (0);
351 } /* int csv_write */
353 void module_register (void)
355         plugin_register_config ("csv", csv_config,
356                         config_keys, config_keys_num);
357         plugin_register_write ("csv", csv_write, /* user_data = */ NULL);
358 } /* void module_register */