f4b56fe0dc9254755f626bc5bf41b9acb2127510
[collectd.git] / src / csv.c
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  **/
21
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "utils_debug.h"
26
27 /*
28  * Private variables
29  */
30 static const char *config_keys[] =
31 {
32         "DataDir"
33 };
34 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
35
36 static char *datadir   = NULL;
37
38 static int value_list_to_string (char *buffer, int buffer_len,
39                 const data_set_t *ds, const value_list_t *vl)
40 {
41         int offset;
42         int status;
43         int i;
44
45         memset (buffer, '\0', sizeof (buffer_len));
46
47         status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
48         if ((status < 1) || (status >= buffer_len))
49                 return (-1);
50         offset = status;
51
52         for (i = 0; i < ds->ds_num; i++)
53         {
54                 if ((ds->ds[i].type != DS_TYPE_COUNTER)
55                                 && (ds->ds[i].type != DS_TYPE_GAUGE))
56                         return (-1);
57
58                 if (ds->ds[i].type == DS_TYPE_COUNTER)
59                         status = snprintf (buffer + offset, buffer_len - offset,
60                                         ",%llu", vl->values[i].counter);
61                 else
62                         status = snprintf (buffer + offset, buffer_len - offset,
63                                         ",%lf", vl->values[i].gauge);
64
65                 if ((status < 1) || (status >= (buffer_len - offset)))
66                         return (-1);
67
68                 offset += status;
69         } /* for ds->ds_num */
70
71         return (0);
72 } /* int value_list_to_string */
73
74 static int value_list_to_filename (char *buffer, int buffer_len,
75                 const data_set_t *ds, const value_list_t *vl)
76 {
77         int offset = 0;
78         int status;
79
80         if (datadir != NULL)
81         {
82                 status = snprintf (buffer + offset, buffer_len - offset,
83                                 "%s/", datadir);
84                 if ((status < 1) || (status >= buffer_len - offset))
85                         return (-1);
86                 offset += status;
87         }
88
89         status = snprintf (buffer + offset, buffer_len - offset,
90                         "%s/", vl->host);
91         if ((status < 1) || (status >= buffer_len - offset))
92                 return (-1);
93         offset += status;
94
95         if (strlen (vl->plugin_instance) > 0)
96                 status = snprintf (buffer + offset, buffer_len - offset,
97                                 "%s-%s/", vl->plugin, vl->plugin_instance);
98         else
99                 status = snprintf (buffer + offset, buffer_len - offset,
100                                 "%s/", vl->plugin);
101         if ((status < 1) || (status >= buffer_len - offset))
102                 return (-1);
103         offset += status;
104
105         if (strlen (vl->type_instance) > 0)
106                 status = snprintf (buffer + offset, buffer_len - offset,
107                                 "%s-%s", ds->type, vl->type_instance);
108         else
109                 status = snprintf (buffer + offset, buffer_len - offset,
110                                 "%s", ds->type);
111         if ((status < 1) || (status >= buffer_len - offset))
112                 return (-1);
113         offset += status;
114
115         {
116                 time_t now;
117                 struct tm stm;
118
119                 /* TODO: Find a way to minimize the calls to `localtime_r',
120                  * since they are pretty expensive.. */
121                 now = time (NULL);
122                 if (localtime_r (&now, &stm) == NULL)
123                 {
124                         syslog (LOG_ERR, "csv plugin: localtime_r failed");
125                         return (1);
126                 }
127
128                 strftime (buffer + offset, buffer_len - offset,
129                                 "-%Y-%m-%d", &stm);
130         }
131
132         return (0);
133 } /* int value_list_to_filename */
134
135 static int csv_create_file (const char *filename, const data_set_t *ds)
136 {
137         FILE *csv;
138         int i;
139
140         if (check_create_dir (filename))
141                 return (-1);
142
143         csv = fopen (filename, "w");
144         if (csv == NULL)
145         {
146                 syslog (LOG_ERR, "csv plugin: fopen (%s) failed: %s",
147                                 filename, strerror(errno));
148                 return (-1);
149         }
150
151         fprintf (csv, "epoch");
152         for (i = 0; i < ds->ds_num; i++)
153                 fprintf (csv, ",%s", ds->ds[i].name);
154
155         fprintf (csv, "\n");
156         fclose (csv);
157
158         return 0;
159 } /* int csv_create_file */
160
161 static int csv_config (const char *key, const char *value)
162 {
163         if (strcasecmp ("DataDir", key) == 0)
164         {
165                 if (datadir != NULL)
166                         free (datadir);
167                 datadir = strdup (value);
168                 if (datadir != NULL)
169                 {
170                         int len = strlen (datadir);
171                         while ((len > 0) && (datadir[len - 1] == '/'))
172                         {
173                                 len--;
174                                 datadir[len] = '\0';
175                         }
176                         if (len <= 0)
177                         {
178                                 free (datadir);
179                                 datadir = NULL;
180                         }
181                 }
182         }
183         else
184         {
185                 return (-1);
186         }
187         return (0);
188 } /* int csv_config */
189
190 static int csv_write (const data_set_t *ds, const value_list_t *vl)
191 {
192         struct stat  statbuf;
193         char         filename[512];
194         char         values[512];
195         FILE        *csv;
196         int          csv_fd;
197         struct flock fl;
198         int          status;
199
200         if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
201                 return (-1);
202
203         DBG ("filename = %s;", filename);
204
205         if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
206                 return (-1);
207
208         if (stat (filename, &statbuf) == -1)
209         {
210                 if (errno == ENOENT)
211                 {
212                         if (csv_create_file (filename, ds))
213                                 return (-1);
214                 }
215                 else
216                 {
217                         syslog (LOG_ERR, "stat(%s) failed: %s",
218                                         filename, strerror (errno));
219                         return (-1);
220                 }
221         }
222         else if (!S_ISREG (statbuf.st_mode))
223         {
224                 syslog (LOG_ERR, "stat(%s): Not a regular file!",
225                                 filename);
226                 return (-1);
227         }
228
229         csv = fopen (filename, "a");
230         if (csv == NULL)
231         {
232                 syslog (LOG_ERR, "csv plugin: fopen (%s) failed: %s",
233                                 filename, strerror (errno));
234                 return (-1);
235         }
236         csv_fd = fileno (csv);
237
238         memset (&fl, '\0', sizeof (fl));
239         fl.l_start  = 0;
240         fl.l_len    = 0; /* till end of file */
241         fl.l_pid    = getpid ();
242         fl.l_type   = F_WRLCK;
243         fl.l_whence = SEEK_SET;
244
245         status = fcntl (csv_fd, F_SETLK, &fl);
246         if (status != 0)
247         {
248                 syslog (LOG_ERR, "csv plugin: flock (%s) failed: %s",
249                                 filename, strerror (errno));
250                 fclose (csv);
251                 return (-1);
252         }
253
254         fprintf (csv, "%s\n", values);
255
256         /* The lock is implicitely released. I we don't release it explicitely
257          * because the `FILE *' may need to flush a cache first */
258         fclose (csv);
259
260         return (0);
261 } /* int csv_write */
262
263 void module_register (void)
264 {
265         plugin_register_config ("csv", csv_config,
266                         config_keys, config_keys_num);
267         plugin_register_write ("csv", csv_write);
268 }