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