3 * Copyright (C) 2007-2009 Florian octo Forster
4 * Copyright (C) 2009 Doug MacEachern
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at collectd.org>
21 * Doug MacEachern <dougm@hyperic.com>
28 #include "utils_cache.h"
33 static const char *config_keys[] = {"DataDir", "StoreRates"};
34 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
36 static char *datadir = NULL;
37 static int store_rates = 0;
38 static int use_stdio = 0;
40 static int value_list_to_string(char *buffer, int buffer_len,
41 const data_set_t *ds, const value_list_t *vl) {
44 gauge_t *rates = NULL;
46 assert(0 == strcmp(ds->type, vl->type));
48 memset(buffer, '\0', buffer_len);
50 status = ssnprintf(buffer, buffer_len, "%.3f", CDTIME_T_TO_DOUBLE(vl->time));
51 if ((status < 1) || (status >= buffer_len))
55 for (size_t i = 0; i < ds->ds_num; i++) {
56 if ((ds->ds[i].type != DS_TYPE_COUNTER) &&
57 (ds->ds[i].type != DS_TYPE_GAUGE) &&
58 (ds->ds[i].type != DS_TYPE_DERIVE) &&
59 (ds->ds[i].type != DS_TYPE_ABSOLUTE)) {
64 if (ds->ds[i].type == DS_TYPE_GAUGE) {
65 status = ssnprintf(buffer + offset, buffer_len - offset, ",%lf",
67 } else if (store_rates != 0) {
69 rates = uc_get_rate(ds, vl);
71 WARNING("csv plugin: "
72 "uc_get_rate failed.");
76 ssnprintf(buffer + offset, buffer_len - offset, ",%lf", rates[i]);
77 } else if (ds->ds[i].type == DS_TYPE_COUNTER) {
78 status = ssnprintf(buffer + offset, buffer_len - offset, ",%llu",
79 vl->values[i].counter);
80 } else if (ds->ds[i].type == DS_TYPE_DERIVE) {
81 status = ssnprintf(buffer + offset, buffer_len - offset, ",%" PRIi64,
82 vl->values[i].derive);
83 } else if (ds->ds[i].type == DS_TYPE_ABSOLUTE) {
84 status = ssnprintf(buffer + offset, buffer_len - offset, ",%" PRIu64,
85 vl->values[i].absolute);
88 if ((status < 1) || (status >= (buffer_len - offset))) {
94 } /* for ds->ds_num */
98 } /* int value_list_to_string */
100 static int value_list_to_filename(char *buffer, size_t buffer_size,
101 value_list_t const *vl) {
105 size_t ptr_size = buffer_size;
109 if (datadir != NULL) {
110 size_t len = strlen(datadir) + 1;
115 memcpy(ptr, datadir, len);
121 status = FORMAT_VL(ptr, ptr_size, vl);
125 /* Skip all the time formatting stuff when printing to STDOUT or
130 ptr_size -= strlen(ptr);
133 /* "-2013-07-12" => 11 bytes */
135 ERROR("csv plugin: Buffer too small.");
139 /* TODO: Find a way to minimize the calls to `localtime_r',
140 * since they are pretty expensive.. */
142 if (localtime_r(&now, &struct_tm) == NULL) {
143 ERROR("csv plugin: localtime_r failed");
147 status = strftime(ptr, ptr_size, "-%Y-%m-%d", &struct_tm);
148 if (status == 0) /* yep, it returns zero on error. */
150 ERROR("csv plugin: strftime failed");
155 } /* int value_list_to_filename */
157 static int csv_create_file(const char *filename, const data_set_t *ds) {
160 if (check_create_dir(filename))
163 csv = fopen(filename, "w");
166 ERROR("csv plugin: fopen (%s) failed: %s", filename,
167 sstrerror(errno, errbuf, sizeof(errbuf)));
171 fprintf(csv, "epoch");
172 for (size_t i = 0; i < ds->ds_num; i++)
173 fprintf(csv, ",%s", ds->ds[i].name);
179 } /* int csv_create_file */
181 static int csv_config(const char *key, const char *value) {
182 if (strcasecmp("DataDir", key) == 0) {
183 if (datadir != NULL) {
187 if (strcasecmp("stdout", value) == 0) {
190 } else if (strcasecmp("stderr", value) == 0) {
194 datadir = strdup(value);
195 if (datadir != NULL) {
196 int len = strlen(datadir);
197 while ((len > 0) && (datadir[len - 1] == '/')) {
206 } else if (strcasecmp("StoreRates", key) == 0) {
215 } /* int csv_config */
217 static int csv_write(const data_set_t *ds, const value_list_t *vl,
218 user_data_t __attribute__((unused)) * user_data) {
224 struct flock fl = {0};
227 if (0 != strcmp(ds->type, vl->type)) {
228 ERROR("csv plugin: DS type does not match value list type");
232 status = value_list_to_filename(filename, sizeof(filename), vl);
236 DEBUG("csv plugin: csv_write: filename = %s;", filename);
238 if (value_list_to_string(values, sizeof(values), ds, vl) != 0)
242 escape_string(filename, sizeof(filename));
244 /* Replace commas by colons for PUTVAL compatible output. */
245 for (size_t i = 0; i < sizeof(values); i++) {
248 else if (values[i] == ',')
252 fprintf(use_stdio == 1 ? stdout : stderr, "PUTVAL %s interval=%.3f %s\n",
253 filename, CDTIME_T_TO_DOUBLE(vl->interval), values);
257 if (stat(filename, &statbuf) == -1) {
258 if (errno == ENOENT) {
259 if (csv_create_file(filename, ds))
263 ERROR("stat(%s) failed: %s", filename,
264 sstrerror(errno, errbuf, sizeof(errbuf)));
267 } else if (!S_ISREG(statbuf.st_mode)) {
268 ERROR("stat(%s): Not a regular file!", filename);
272 csv = fopen(filename, "a");
275 ERROR("csv plugin: fopen (%s) failed: %s", filename,
276 sstrerror(errno, errbuf, sizeof(errbuf)));
279 csv_fd = fileno(csv);
283 fl.l_whence = SEEK_SET;
285 status = fcntl(csv_fd, F_SETLK, &fl);
288 ERROR("csv plugin: flock (%s) failed: %s", filename,
289 sstrerror(errno, errbuf, sizeof(errbuf)));
294 fprintf(csv, "%s\n", values);
296 /* The lock is implicitely released. I we don't release it explicitely
297 * because the `FILE *' may need to flush a cache first */
301 } /* int csv_write */
303 void module_register(void) {
304 plugin_register_config("csv", csv_config, config_keys, config_keys_num);
305 plugin_register_write("csv", csv_write, /* user_data = */ NULL);
306 } /* void module_register */