X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcsv.c;h=d01f47de605de0fc8a8da4650ee23c1683901836;hb=2239c03fe46755183c756e81ac19cd416a8f0184;hp=ff59f91c006e81511b6aeec738000398ed945bdc;hpb=3da24ea8c48268a64025dc4aae5bdf223d7dad0c;p=collectd.git diff --git a/src/csv.c b/src/csv.c index ff59f91c..d01f47de 100644 --- a/src/csv.c +++ b/src/csv.c @@ -1,6 +1,6 @@ /** * collectd - src/csv.c - * Copyright (C) 2007 Florian octo Forster + * Copyright (C) 2007-2009 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -23,6 +23,7 @@ #include "plugin.h" #include "common.h" #include "utils_cache.h" +#include "utils_parse_option.h" /* * Private variables @@ -36,6 +37,7 @@ static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); static char *datadir = NULL; static int store_rates = 0; +static int use_stdio = 0; static int value_list_to_string (char *buffer, int buffer_len, const data_set_t *ds, const value_list_t *vl) @@ -45,9 +47,11 @@ static int value_list_to_string (char *buffer, int buffer_len, int i; gauge_t *rates = NULL; + assert (0 == strcmp (ds->type, vl->type)); + memset (buffer, '\0', buffer_len); - status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time); + status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time); if ((status < 1) || (status >= buffer_len)) return (-1); offset = status; @@ -62,7 +66,7 @@ static int value_list_to_string (char *buffer, int buffer_len, { if (store_rates == 0) { - status = snprintf (buffer + offset, + status = ssnprintf (buffer + offset, buffer_len - offset, ",%llu", vl->values[i].counter); @@ -77,14 +81,14 @@ static int value_list_to_string (char *buffer, int buffer_len, "uc_get_rate failed."); return (-1); } - status = snprintf (buffer + offset, + status = ssnprintf (buffer + offset, buffer_len - offset, ",%lf", rates[i]); } } else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */ { - status = snprintf (buffer + offset, buffer_len - offset, + status = ssnprintf (buffer + offset, buffer_len - offset, ",%lf", vl->values[i].gauge); } @@ -107,41 +111,44 @@ static int value_list_to_filename (char *buffer, int buffer_len, int offset = 0; int status; + assert (0 == strcmp (ds->type, vl->type)); + if (datadir != NULL) { - status = snprintf (buffer + offset, buffer_len - offset, + status = ssnprintf (buffer + offset, buffer_len - offset, "%s/", datadir); if ((status < 1) || (status >= buffer_len - offset)) return (-1); offset += status; } - status = snprintf (buffer + offset, buffer_len - offset, + status = ssnprintf (buffer + offset, buffer_len - offset, "%s/", vl->host); if ((status < 1) || (status >= buffer_len - offset)) return (-1); offset += status; if (strlen (vl->plugin_instance) > 0) - status = snprintf (buffer + offset, buffer_len - offset, + status = ssnprintf (buffer + offset, buffer_len - offset, "%s-%s/", vl->plugin, vl->plugin_instance); else - status = snprintf (buffer + offset, buffer_len - offset, + status = ssnprintf (buffer + offset, buffer_len - offset, "%s/", vl->plugin); if ((status < 1) || (status >= buffer_len - offset)) return (-1); offset += status; if (strlen (vl->type_instance) > 0) - status = snprintf (buffer + offset, buffer_len - offset, - "%s-%s", ds->type, vl->type_instance); + status = ssnprintf (buffer + offset, buffer_len - offset, + "%s-%s", vl->type, vl->type_instance); else - status = snprintf (buffer + offset, buffer_len - offset, - "%s", ds->type); + status = ssnprintf (buffer + offset, buffer_len - offset, + "%s", vl->type); if ((status < 1) || (status >= buffer_len - offset)) return (-1); offset += status; + if (!use_stdio) { time_t now; struct tm stm; @@ -196,6 +203,16 @@ static int csv_config (const char *key, const char *value) { if (datadir != NULL) free (datadir); + if (strcasecmp ("stdout", value) == 0) + { + use_stdio = 1; + return (0); + } + else if (strcasecmp ("stderr", value) == 0) + { + use_stdio = 2; + return (0); + } datadir = strdup (value); if (datadir != NULL) { @@ -232,7 +249,8 @@ static int csv_config (const char *key, const char *value) return (0); } /* int csv_config */ -static int csv_write (const data_set_t *ds, const value_list_t *vl) +static int csv_write (const data_set_t *ds, const value_list_t *vl, + user_data_t __attribute__((unused)) *user_data) { struct stat statbuf; char filename[512]; @@ -242,6 +260,11 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl) struct flock fl; int status; + if (0 != strcmp (ds->type, vl->type)) { + ERROR ("csv plugin: DS type does not match value list type"); + return -1; + } + if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0) return (-1); @@ -250,6 +273,27 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl) if (value_list_to_string (values, sizeof (values), ds, vl) != 0) return (-1); + if (use_stdio) + { + size_t i; + + escape_string (filename, sizeof (filename)); + + /* Replace commas by colons for PUTVAL compatible output. */ + for (i = 0; i < sizeof (values); i++) + { + if (values[i] == 0) + break; + else if (values[i] == ',') + values[i] = ':'; + } + + fprintf (use_stdio == 1 ? stdout : stderr, + "PUTVAL %s interval=%i %s\n", + filename, interval_g, values); + return (0); + } + if (stat (filename, &statbuf) == -1) { if (errno == ENOENT) @@ -313,5 +357,5 @@ void module_register (void) { plugin_register_config ("csv", csv_config, config_keys, config_keys_num); - plugin_register_write ("csv", csv_write); + plugin_register_write ("csv", csv_write, /* user_data = */ NULL); } /* void module_register */