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);
37 static int store_rates;
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 = snprintf(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 = snprintf(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.");
75 status = snprintf(buffer + offset, buffer_len - offset, ",%lf", rates[i]);
76 } else if (ds->ds[i].type == DS_TYPE_COUNTER) {
77 status = snprintf(buffer + offset, buffer_len - offset, ",%" PRIu64,
78 (uint64_t)vl->values[i].counter);
79 } else if (ds->ds[i].type == DS_TYPE_DERIVE) {
80 status = snprintf(buffer + offset, buffer_len - offset, ",%" PRIi64,
81 vl->values[i].derive);
82 } else if (ds->ds[i].type == DS_TYPE_ABSOLUTE) {
83 status = snprintf(buffer + offset, buffer_len - offset, ",%" PRIu64,
84 vl->values[i].absolute);
87 if ((status < 1) || (status >= (buffer_len - offset))) {
93 } /* for ds->ds_num */
97 } /* int value_list_to_string */
99 static int value_list_to_filename(char *buffer, size_t buffer_size,
100 value_list_t const *vl) {
104 size_t ptr_size = buffer_size;
108 if (datadir != NULL) {
109 size_t len = strlen(datadir) + 1;
114 memcpy(ptr, datadir, len);
120 status = FORMAT_VL(ptr, ptr_size, vl);
124 /* Skip all the time formatting stuff when printing to STDOUT or
129 ptr_size -= strlen(ptr);
132 /* "-2013-07-12" => 11 bytes */
134 ERROR("csv plugin: Buffer too small.");
138 /* TODO: Find a way to minimize the calls to `localtime_r',
139 * since they are pretty expensive.. */
141 if (localtime_r(&now, &struct_tm) == NULL) {
142 ERROR("csv plugin: localtime_r failed");
146 status = strftime(ptr, ptr_size, "-%Y-%m-%d", &struct_tm);
147 if (status == 0) /* yep, it returns zero on error. */
149 ERROR("csv plugin: strftime failed");
154 } /* int value_list_to_filename */
156 static int csv_create_file(const char *filename, const data_set_t *ds) {
159 if (check_create_dir(filename))
162 csv = fopen(filename, "w");
164 ERROR("csv plugin: fopen (%s) failed: %s", filename, STRERRNO);
168 fprintf(csv, "epoch");
169 for (size_t i = 0; i < ds->ds_num; i++)
170 fprintf(csv, ",%s", ds->ds[i].name);
176 } /* int csv_create_file */
178 static int csv_config(const char *key, const char *value) {
179 if (strcasecmp("DataDir", key) == 0) {
180 if (datadir != NULL) {
184 if (strcasecmp("stdout", value) == 0) {
187 } else if (strcasecmp("stderr", value) == 0) {
191 datadir = strdup(value);
192 if (datadir != NULL) {
193 int len = strlen(datadir);
194 while ((len > 0) && (datadir[len - 1] == '/')) {
203 } else if (strcasecmp("StoreRates", key) == 0) {
212 } /* int csv_config */
214 static int csv_write(const data_set_t *ds, const value_list_t *vl,
215 user_data_t __attribute__((unused)) * user_data) {
221 struct flock fl = {0};
224 if (0 != strcmp(ds->type, vl->type)) {
225 ERROR("csv plugin: DS type does not match value list type");
229 status = value_list_to_filename(filename, sizeof(filename), vl);
233 DEBUG("csv plugin: csv_write: filename = %s;", filename);
235 if (value_list_to_string(values, sizeof(values), ds, vl) != 0)
239 escape_string(filename, sizeof(filename));
241 /* Replace commas by colons for PUTVAL compatible output. */
242 for (size_t i = 0; i < sizeof(values); i++) {
245 else if (values[i] == ',')
249 fprintf(use_stdio == 1 ? stdout : stderr, "PUTVAL %s interval=%.3f %s\n",
250 filename, CDTIME_T_TO_DOUBLE(vl->interval), values);
254 if (stat(filename, &statbuf) == -1) {
255 if (errno == ENOENT) {
256 if (csv_create_file(filename, ds))
259 ERROR("stat(%s) failed: %s", filename, STRERRNO);
262 } else if (!S_ISREG(statbuf.st_mode)) {
263 ERROR("stat(%s): Not a regular file!", filename);
267 csv = fopen(filename, "a");
269 ERROR("csv plugin: fopen (%s) failed: %s", filename, STRERRNO);
272 csv_fd = fileno(csv);
276 fl.l_whence = SEEK_SET;
278 status = fcntl(csv_fd, F_SETLK, &fl);
280 ERROR("csv plugin: flock (%s) failed: %s", filename, STRERRNO);
285 fprintf(csv, "%s\n", values);
287 /* The lock is implicitely released. I we don't release it explicitely
288 * because the `FILE *' may need to flush a cache first */
292 } /* int csv_write */
294 void module_register(void) {
295 plugin_register_config("csv", csv_config, config_keys, config_keys_num);
296 plugin_register_write("csv", csv_write, /* user_data = */ NULL);
297 } /* void module_register */