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 **/
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "utils_debug.h"
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;
34 memset (buffer, '\0', sizeof (buffer_len));
36 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
37 if ((status < 1) || (status >= buffer_len))
38 return (-1);
39 offset = status;
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);
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);
54 if ((status < 1) || (status >= (buffer_len - offset)))
55 return (-1);
57 offset += status;
58 } /* for ds->ds_num */
60 return (0);
61 } /* int value_list_to_string */
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;
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;
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;
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;
95 {
96 time_t now;
97 struct tm *tm;
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);
104 strftime (buffer + offset, buffer_len - offset,
105 "-%Y-%m-%d", tm);
107 /* `localtime(3)' returns a pointer to static data,
108 * therefore the pointer may not be free'd. */
109 }
111 return (0);
112 } /* int value_list_to_filename */
114 static int csv_create_file (const char *filename, const data_set_t *ds)
115 {
116 FILE *csv;
117 int i;
119 if (check_create_dir (filename))
120 return (-1);
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 }
130 fprintf (csv, "epoch");
131 for (i = 0; i < ds->ds_num; i++)
132 fprintf (csv, ",%s", ds->ds[i].name);
134 fprintf (csv, "\n");
135 fclose (csv);
137 return 0;
138 } /* int csv_create_file */
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 = { F_WRLCK, SEEK_SET, 0, 0, getpid () };
148 int status;
150 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
151 return (-1);
153 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
154 return (-1);
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 }
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);
186 status = fcntl (csv_fd, F_SETLK, &fl);
187 if (status != 0)
188 {
189 syslog (LOG_ERR, "csv plugin: flock (%s) failed: %s",
190 filename, strerror (errno));
191 fclose (csv);
192 return (-1);
193 }
195 fprintf (csv, "%s\n", values);
197 /* The lock is implicitely released. I we don't release it explicitely
198 * because the `FILE *' may need to flush a cache first */
199 fclose (csv);
201 return (0);
202 } /* int csv_write */
204 void module_register (void)
205 {
206 plugin_register_write ("csv", csv_write);
207 }