X-Git-Url: https://git.octo.it/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcsv.c;h=a94b7001025419602961980efa993506e9755b33;hb=b4d9bd23f3e04f9b42204d9954842524e2a7eb61;hp=5d64fb89f9d7d5f1aeb504020828473492700136;hpb=6dee31bca87a552017791aa97afa9bd40bc0ebfe;p=collectd.git diff --git a/src/csv.c b/src/csv.c index 5d64fb89..a94b7001 100644 --- a/src/csv.c +++ b/src/csv.c @@ -22,17 +22,20 @@ #include "collectd.h" #include "plugin.h" #include "common.h" +#include "utils_cache.h" /* * Private variables */ static const char *config_keys[] = { - "DataDir" + "DataDir", + "StoreRates" }; static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); static char *datadir = NULL; +static int store_rates = 0; static int value_list_to_string (char *buffer, int buffer_len, const data_set_t *ds, const value_list_t *vl) @@ -40,10 +43,13 @@ static int value_list_to_string (char *buffer, int buffer_len, int offset; int status; int i; + gauge_t *rates = NULL; - memset (buffer, '\0', sizeof (buffer_len)); + assert (0 == strcmp (ds->type, vl->type)); - status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time); + memset (buffer, '\0', buffer_len); + + status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time); if ((status < 1) || (status >= buffer_len)) return (-1); offset = status; @@ -55,18 +61,45 @@ static int value_list_to_string (char *buffer, int buffer_len, return (-1); if (ds->ds[i].type == DS_TYPE_COUNTER) - status = snprintf (buffer + offset, buffer_len - offset, - ",%llu", vl->values[i].counter); - else - status = snprintf (buffer + offset, buffer_len - offset, + { + if (store_rates == 0) + { + status = ssnprintf (buffer + offset, + buffer_len - offset, + ",%llu", + vl->values[i].counter); + } + else /* if (store_rates == 1) */ + { + if (rates == NULL) + rates = uc_get_rate (ds, vl); + if (rates == NULL) + { + WARNING ("csv plugin: " + "uc_get_rate failed."); + return (-1); + } + status = ssnprintf (buffer + offset, + buffer_len - offset, + ",%lf", rates[i]); + } + } + else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */ + { + status = ssnprintf (buffer + offset, buffer_len - offset, ",%lf", vl->values[i].gauge); + } if ((status < 1) || (status >= (buffer_len - offset))) + { + sfree (rates); return (-1); + } offset += status; } /* for ds->ds_num */ + sfree (rates); return (0); } /* int value_list_to_string */ @@ -76,37 +109,39 @@ 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; @@ -181,6 +216,19 @@ static int csv_config (const char *key, const char *value) } } } + else if (strcasecmp ("StoreRates", key) == 0) + { + if ((strcasecmp ("True", value) == 0) + || (strcasecmp ("Yes", value) == 0) + || (strcasecmp ("On", value) == 0)) + { + store_rates = 1; + } + else + { + store_rates = 0; + } + } else { return (-1); @@ -198,6 +246,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); @@ -265,12 +318,9 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl) return (0); } /* int csv_write */ -void module_register (modreg_e load) +void module_register (void) { - if (load & MR_WRITE) - { - plugin_register_config ("csv", csv_config, - config_keys, config_keys_num); - plugin_register_write ("csv", csv_write); - } + plugin_register_config ("csv", csv_config, + config_keys, config_keys_num); + plugin_register_write ("csv", csv_write); } /* void module_register */