Replace all syslog-calls with one of the new logging-macros.
[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                 ERROR ("csv plugin: fopen (%s) failed: %s",
146                                 filename, strerror(errno));
147                 return (-1);
148         }
149
150         fprintf (csv, "epoch");
151         for (i = 0; i < ds->ds_num; i++)
152                 fprintf (csv, ",%s", ds->ds[i].name);
153
154         fprintf (csv, "\n");
155         fclose (csv);
156
157         return 0;
158 } /* int csv_create_file */
159
160 static int csv_config (const char *key, const char *value)
161 {
162         if (strcasecmp ("DataDir", key) == 0)
163         {
164                 if (datadir != NULL)
165                         free (datadir);
166                 datadir = strdup (value);
167                 if (datadir != NULL)
168                 {
169                         int len = strlen (datadir);
170                         while ((len > 0) && (datadir[len - 1] == '/'))
171                         {
172                                 len--;
173                                 datadir[len] = '\0';
174                         }
175                         if (len <= 0)
176                         {
177                                 free (datadir);
178                                 datadir = NULL;
179                         }
180                 }
181         }
182         else
183         {
184                 return (-1);
185         }
186         return (0);
187 } /* int csv_config */
188
189 static int csv_write (const data_set_t *ds, const value_list_t *vl)
190 {
191         struct stat  statbuf;
192         char         filename[512];
193         char         values[512];
194         FILE        *csv;
195         int          csv_fd;
196         struct flock fl;
197         int          status;
198
199         if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
200                 return (-1);
201
202         DEBUG ("filename = %s;", filename);
203
204         if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
205                 return (-1);
206
207         if (stat (filename, &statbuf) == -1)
208         {
209                 if (errno == ENOENT)
210                 {
211                         if (csv_create_file (filename, ds))
212                                 return (-1);
213                 }
214                 else
215                 {
216                         ERROR ("stat(%s) failed: %s",
217                                         filename, strerror (errno));
218                         return (-1);
219                 }
220         }
221         else if (!S_ISREG (statbuf.st_mode))
222         {
223                 ERROR ("stat(%s): Not a regular file!",
224                                 filename);
225                 return (-1);
226         }
227
228         csv = fopen (filename, "a");
229         if (csv == NULL)
230         {
231                 ERROR ("csv plugin: fopen (%s) failed: %s",
232                                 filename, strerror (errno));
233                 return (-1);
234         }
235         csv_fd = fileno (csv);
236
237         memset (&fl, '\0', sizeof (fl));
238         fl.l_start  = 0;
239         fl.l_len    = 0; /* till end of file */
240         fl.l_pid    = getpid ();
241         fl.l_type   = F_WRLCK;
242         fl.l_whence = SEEK_SET;
243
244         status = fcntl (csv_fd, F_SETLK, &fl);
245         if (status != 0)
246         {
247                 ERROR ("csv plugin: flock (%s) failed: %s",
248                                 filename, strerror (errno));
249                 fclose (csv);
250                 return (-1);
251         }
252
253         fprintf (csv, "%s\n", values);
254
255         /* The lock is implicitely released. I we don't release it explicitely
256          * because the `FILE *' may need to flush a cache first */
257         fclose (csv);
258
259         return (0);
260 } /* int csv_write */
261
262 void module_register (void)
263 {
264         plugin_register_config ("csv", csv_config,
265                         config_keys, config_keys_num);
266         plugin_register_write ("csv", csv_write);
267 }