3 * Copyright (C) 2007 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_debug.h"
30 static const char *config_keys[] =
34 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
36 static char *datadir = NULL;
38 static int value_list_to_string (char *buffer, int buffer_len,
39 const data_set_t *ds, const value_list_t *vl)
45 memset (buffer, '\0', sizeof (buffer_len));
47 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
48 if ((status < 1) || (status >= buffer_len))
52 for (i = 0; i < ds->ds_num; i++)
54 if ((ds->ds[i].type != DS_TYPE_COUNTER)
55 && (ds->ds[i].type != DS_TYPE_GAUGE))
58 if (ds->ds[i].type == DS_TYPE_COUNTER)
59 status = snprintf (buffer + offset, buffer_len - offset,
60 ",%llu", vl->values[i].counter);
62 status = snprintf (buffer + offset, buffer_len - offset,
63 ",%lf", vl->values[i].gauge);
65 if ((status < 1) || (status >= (buffer_len - offset)))
69 } /* for ds->ds_num */
72 } /* int value_list_to_string */
74 static int value_list_to_filename (char *buffer, int buffer_len,
75 const data_set_t *ds, const value_list_t *vl)
82 status = snprintf (buffer + offset, buffer_len - offset,
84 if ((status < 1) || (status >= buffer_len - offset))
89 status = snprintf (buffer + offset, buffer_len - offset,
91 if ((status < 1) || (status >= buffer_len - offset))
95 if (strlen (vl->plugin_instance) > 0)
96 status = snprintf (buffer + offset, buffer_len - offset,
97 "%s-%s/", vl->plugin, vl->plugin_instance);
99 status = snprintf (buffer + offset, buffer_len - offset,
101 if ((status < 1) || (status >= buffer_len - offset))
105 if (strlen (vl->type_instance) > 0)
106 status = snprintf (buffer + offset, buffer_len - offset,
107 "%s-%s", ds->type, vl->type_instance);
109 status = snprintf (buffer + offset, buffer_len - offset,
111 if ((status < 1) || (status >= buffer_len - offset))
119 /* TODO: Find a way to minimize the calls to `localtime', since
120 * they are pretty expensive.. */
122 tm = localtime (&now);
124 strftime (buffer + offset, buffer_len - offset,
127 /* `localtime(3)' returns a pointer to static data,
128 * therefore the pointer may not be free'd. */
132 } /* int value_list_to_filename */
134 static int csv_create_file (const char *filename, const data_set_t *ds)
139 if (check_create_dir (filename))
142 csv = fopen (filename, "w");
145 syslog (LOG_ERR, "csv plugin: fopen (%s) failed: %s",
146 filename, strerror(errno));
150 fprintf (csv, "epoch");
151 for (i = 0; i < ds->ds_num; i++)
152 fprintf (csv, ",%s", ds->ds[i].name);
158 } /* int csv_create_file */
160 static int csv_config (const char *key, const char *value)
162 if (strcasecmp ("DataDir", key) == 0)
166 datadir = strdup (value);
169 int len = strlen (datadir);
170 while ((len > 0) && (datadir[len - 1] == '/'))
187 } /* int csv_config */
189 static int csv_write (const data_set_t *ds, const value_list_t *vl)
199 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
202 DBG ("filename = %s;", filename);
204 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
207 if (stat (filename, &statbuf) == -1)
211 if (csv_create_file (filename, ds))
216 syslog (LOG_ERR, "stat(%s) failed: %s",
217 filename, strerror (errno));
221 else if (!S_ISREG (statbuf.st_mode))
223 syslog (LOG_ERR, "stat(%s): Not a regular file!",
228 csv = fopen (filename, "a");
231 syslog (LOG_ERR, "csv plugin: fopen (%s) failed: %s",
232 filename, strerror (errno));
235 csv_fd = fileno (csv);
237 memset (&fl, '\0', sizeof (fl));
239 fl.l_len = 0; /* till end of file */
240 fl.l_pid = getpid ();
242 fl.l_whence = SEEK_SET;
244 status = fcntl (csv_fd, F_SETLK, &fl);
247 syslog (LOG_ERR, "csv plugin: flock (%s) failed: %s",
248 filename, strerror (errno));
253 fprintf (csv, "%s\n", values);
255 /* The lock is implicitely released. I we don't release it explicitely
256 * because the `FILE *' may need to flush a cache first */
260 } /* int csv_write */
262 void module_register (void)
264 plugin_register_config ("csv", csv_config,
265 config_keys, config_keys_num);
266 plugin_register_write ("csv", csv_write);