csv plugin: Fixed the initialization of `struct flock' to be portable.
[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 static int value_list_to_string (char *buffer, int buffer_len,
28                 const data_set_t *ds, const value_list_t *vl)
29 {
30         int offset;
31         int status;
32         int i;
33
34         memset (buffer, '\0', sizeof (buffer_len));
35
36         status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
37         if ((status < 1) || (status >= buffer_len))
38                 return (-1);
39         offset = status;
40
41         for (i = 0; i < ds->ds_num; i++)
42         {
43                 if ((ds->ds[i].type != DS_TYPE_COUNTER)
44                                 && (ds->ds[i].type != DS_TYPE_GAUGE))
45                         return (-1);
46
47                 if (ds->ds[i].type == DS_TYPE_COUNTER)
48                         status = snprintf (buffer + offset, buffer_len - offset,
49                                         ",%llu", vl->values[i].counter);
50                 else
51                         status = snprintf (buffer + offset, buffer_len - offset,
52                                         ",%lf", vl->values[i].gauge);
53
54                 if ((status < 1) || (status >= (buffer_len - offset)))
55                         return (-1);
56
57                 offset += status;
58         } /* for ds->ds_num */
59
60         return (0);
61 } /* int value_list_to_string */
62
63 static int value_list_to_filename (char *buffer, int buffer_len,
64                 const data_set_t *ds, const value_list_t *vl)
65 {
66         int offset = 0;
67         int status;
68
69         status = snprintf (buffer + offset, buffer_len - offset,
70                         "%s/", vl->host);
71         if ((status < 1) || (status >= buffer_len - offset))
72                 return (-1);
73         offset += status;
74
75         if (strlen (vl->plugin_instance) > 0)
76                 status = snprintf (buffer + offset, buffer_len - offset,
77                                 "%s-%s/", vl->plugin, vl->plugin_instance);
78         else
79                 status = snprintf (buffer + offset, buffer_len - offset,
80                                 "%s/", vl->plugin);
81         if ((status < 1) || (status >= buffer_len - offset))
82                 return (-1);
83         offset += status;
84
85         if (strlen (vl->type_instance) > 0)
86                 status = snprintf (buffer + offset, buffer_len - offset,
87                                 "%s-%s", ds->type, vl->type_instance);
88         else
89                 status = snprintf (buffer + offset, buffer_len - offset,
90                                 "%s", ds->type);
91         if ((status < 1) || (status >= buffer_len - offset))
92                 return (-1);
93         offset += status;
94
95         {
96                 time_t now;
97                 struct tm *tm;
98
99                 /* TODO: Find a way to minimize the calls to `localtime', since
100                  * they are pretty expensive.. */
101                 now = time (NULL);
102                 tm = localtime (&now);
103
104                 strftime (buffer + offset, buffer_len - offset,
105                                 "-%Y-%m-%d", tm);
106
107                 /* `localtime(3)' returns a pointer to static data,
108                  * therefore the pointer may not be free'd. */
109         }
110
111         return (0);
112 } /* int value_list_to_filename */
113
114 static int csv_create_file (const char *filename, const data_set_t *ds)
115 {
116         FILE *csv;
117         int i;
118
119         if (check_create_dir (filename))
120                 return (-1);
121
122         csv = fopen (filename, "w");
123         if (csv == NULL)
124         {
125                 syslog (LOG_ERR, "csv plugin: fopen (%s) failed: %s",
126                                 filename, strerror(errno));
127                 return (-1);
128         }
129
130         fprintf (csv, "epoch");
131         for (i = 0; i < ds->ds_num; i++)
132                 fprintf (csv, ",%s", ds->ds[i].name);
133
134         fprintf (csv, "\n");
135         fclose (csv);
136
137         return 0;
138 } /* int csv_create_file */
139
140 static int csv_write (const data_set_t *ds, const value_list_t *vl)
141 {
142         struct stat  statbuf;
143         char         filename[512];
144         char         values[512];
145         FILE        *csv;
146         int          csv_fd;
147         struct flock fl;
148         int          status;
149
150         if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
151                 return (-1);
152
153         if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
154                 return (-1);
155
156         if (stat (filename, &statbuf) == -1)
157         {
158                 if (errno == ENOENT)
159                 {
160                         if (csv_create_file (filename, ds))
161                                 return (-1);
162                 }
163                 else
164                 {
165                         syslog (LOG_ERR, "stat(%s) failed: %s",
166                                         filename, strerror (errno));
167                         return (-1);
168                 }
169         }
170         else if (!S_ISREG (statbuf.st_mode))
171         {
172                 syslog (LOG_ERR, "stat(%s): Not a regular file!",
173                                 filename);
174                 return (-1);
175         }
176
177         csv = fopen (filename, "a");
178         if (csv == NULL)
179         {
180                 syslog (LOG_ERR, "csv plugin: fopen (%s) failed: %s",
181                                 filename, strerror (errno));
182                 return (-1);
183         }
184         csv_fd = fileno (csv);
185
186         memset (&fl, '\0', sizeof (fl));
187         fl.l_start  = 0;
188         fl.l_len    = 0; /* till end of file */
189         fl.l_pid    = getpid ();
190         fl.l_type   = F_WRLCK;
191         fl.l_whence = SEEK_SET;
192
193         status = fcntl (csv_fd, F_SETLK, &fl);
194         if (status != 0)
195         {
196                 syslog (LOG_ERR, "csv plugin: flock (%s) failed: %s",
197                                 filename, strerror (errno));
198                 fclose (csv);
199                 return (-1);
200         }
201
202         fprintf (csv, "%s\n", values);
203
204         /* The lock is implicitely released. I we don't release it explicitely
205          * because the `FILE *' may need to flush a cache first */
206         fclose (csv);
207
208         return (0);
209 } /* int csv_write */
210
211 void module_register (void)
212 {
213         plugin_register_write ("csv", csv_write);
214 }