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"
27 static int value_list_to_string (char *buffer, int buffer_len,
28 const data_set_t *ds, const value_list_t *vl)
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))
41 for (i = 0; i < ds->ds_num; i++)
43 if ((ds->ds[i].type != DS_TYPE_COUNTER)
44 && (ds->ds[i].type != DS_TYPE_GAUGE))
47 if (ds->ds[i].type == DS_TYPE_COUNTER)
48 status = snprintf (buffer + offset, buffer_len - offset,
49 ",%llu", vl->values[i].counter);
51 status = snprintf (buffer + offset, buffer_len - offset,
52 ",%lf", vl->values[i].gauge);
54 if ((status < 1) || (status >= (buffer_len - offset)))
58 } /* for ds->ds_num */
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)
69 status = snprintf (buffer + offset, buffer_len - offset,
71 if ((status < 1) || (status >= buffer_len - offset))
75 if (strlen (vl->plugin_instance) > 0)
76 status = snprintf (buffer + offset, buffer_len - offset,
77 "%s-%s/", vl->plugin, vl->plugin_instance);
79 status = snprintf (buffer + offset, buffer_len - offset,
81 if ((status < 1) || (status >= buffer_len - offset))
85 if (strlen (vl->type_instance) > 0)
86 status = snprintf (buffer + offset, buffer_len - offset,
87 "%s-%s", ds->type, vl->type_instance);
89 status = snprintf (buffer + offset, buffer_len - offset,
91 if ((status < 1) || (status >= buffer_len - offset))
99 /* TODO: Find a way to minimize the calls to `localtime', since
100 * they are pretty expensive.. */
102 tm = localtime (&now);
104 strftime (buffer + offset, buffer_len - offset,
107 /* `localtime(3)' returns a pointer to static data,
108 * therefore the pointer may not be free'd. */
112 } /* int value_list_to_filename */
114 static int csv_create_file (const char *filename, const data_set_t *ds)
119 if (check_create_dir (filename))
122 csv = fopen (filename, "w");
125 syslog (LOG_ERR, "csv plugin: fopen (%s) failed: %s",
126 filename, strerror(errno));
130 fprintf (csv, "epoch");
131 for (i = 0; i < ds->ds_num; i++)
132 fprintf (csv, ",%s", ds->ds[i].name);
138 } /* int csv_create_file */
140 static int csv_write (const data_set_t *ds, const value_list_t *vl)
150 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
153 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
156 if (stat (filename, &statbuf) == -1)
160 if (csv_create_file (filename, ds))
165 syslog (LOG_ERR, "stat(%s) failed: %s",
166 filename, strerror (errno));
170 else if (!S_ISREG (statbuf.st_mode))
172 syslog (LOG_ERR, "stat(%s): Not a regular file!",
177 csv = fopen (filename, "a");
180 syslog (LOG_ERR, "csv plugin: fopen (%s) failed: %s",
181 filename, strerror (errno));
184 csv_fd = fileno (csv);
186 memset (&fl, '\0', sizeof (fl));
188 fl.l_len = 0; /* till end of file */
189 fl.l_pid = getpid ();
191 fl.l_whence = SEEK_SET;
193 status = fcntl (csv_fd, F_SETLK, &fl);
196 syslog (LOG_ERR, "csv plugin: flock (%s) failed: %s",
197 filename, strerror (errno));
202 fprintf (csv, "%s\n", values);
204 /* The lock is implicitely released. I we don't release it explicitely
205 * because the `FILE *' may need to flush a cache first */
209 } /* int csv_write */
211 void module_register (void)
213 plugin_register_write ("csv", csv_write);