From 75a0b125784546894d2d5ecb5d21b6a26dbd640b Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 6 Aug 2010 14:40:55 +0200 Subject: [PATCH] write_http plugin: Move "wh_value_list_to_string" to src/common.[ch]. --- src/common.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++ src/common.h | 3 ++ src/write_http.c | 72 +----------------------------------------------- 3 files changed, 74 insertions(+), 71 deletions(-) diff --git a/src/common.c b/src/common.c index 2598036d..08653dcd 100644 --- a/src/common.c +++ b/src/common.c @@ -29,6 +29,7 @@ #include "collectd.h" #include "common.h" #include "plugin.h" +#include "utils_cache.h" #if HAVE_PTHREAD_H # include @@ -802,6 +803,75 @@ int format_name (char *ret, int ret_len, return (0); } /* int format_name */ +int format_values (char *ret, size_t ret_len, /* {{{ */ + const data_set_t *ds, const value_list_t *vl, + _Bool store_rates) +{ + size_t offset = 0; + int status; + int i; + gauge_t *rates = NULL; + + assert (0 == strcmp (ds->type, vl->type)); + + memset (ret, 0, ret_len); + +#define BUFFER_ADD(...) do { \ + status = ssnprintf (ret + offset, ret_len - offset, \ + __VA_ARGS__); \ + if (status < 1) \ + { \ + sfree (rates); \ + return (-1); \ + } \ + else if (((size_t) status) >= (ret_len - offset)) \ + { \ + sfree (rates); \ + return (-1); \ + } \ + else \ + offset += ((size_t) status); \ +} while (0) + + BUFFER_ADD ("%lu", (unsigned long) vl->time); + + for (i = 0; i < ds->ds_num; i++) + { + if (ds->ds[i].type == DS_TYPE_GAUGE) + BUFFER_ADD (":%f", vl->values[i].gauge); + else if (store_rates) + { + if (rates == NULL) + rates = uc_get_rate (ds, vl); + if (rates == NULL) + { + WARNING ("format_values: " + "uc_get_rate failed."); + return (-1); + } + BUFFER_ADD (":%g", rates[i]); + } + else if (ds->ds[i].type == DS_TYPE_COUNTER) + BUFFER_ADD (":%llu", vl->values[i].counter); + else if (ds->ds[i].type == DS_TYPE_DERIVE) + BUFFER_ADD (":%"PRIi64, vl->values[i].derive); + else if (ds->ds[i].type == DS_TYPE_ABSOLUTE) + BUFFER_ADD (":%"PRIu64, vl->values[i].absolute); + else + { + ERROR ("format_values plugin: Unknown data source type: %i", + ds->ds[i].type); + sfree (rates); + return (-1); + } + } /* for ds->ds_num */ + +#undef BUFFER_ADD + + sfree (rates); + return (0); +} /* }}} int format_values */ + int parse_identifier (char *str, char **ret_host, char **ret_plugin, char **ret_plugin_instance, char **ret_type, char **ret_type_instance) diff --git a/src/common.h b/src/common.h index 229f7098..63ecca39 100644 --- a/src/common.h +++ b/src/common.h @@ -258,6 +258,9 @@ int format_name (char *ret, int ret_len, #define FORMAT_VL(ret, ret_len, vl) \ format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \ (vl)->type, (vl)->type_instance) +int format_values (char *ret, size_t ret_len, + const data_set_t *ds, const value_list_t *vl, + _Bool store_rates); int parse_identifier (char *str, char **ret_host, char **ret_plugin, char **ret_plugin_instance, diff --git a/src/write_http.c b/src/write_http.c index ab8757ed..bac8e986 100644 --- a/src/write_http.c +++ b/src/write_http.c @@ -270,76 +270,6 @@ static void wh_callback_free (void *data) /* {{{ */ sfree (cb); } /* }}} void wh_callback_free */ -static int wh_value_list_to_string (char *buffer, /* {{{ */ - size_t buffer_size, - const data_set_t *ds, const value_list_t *vl, - wh_callback_t *cb) -{ - size_t offset = 0; - int status; - int i; - gauge_t *rates = NULL; - - assert (0 == strcmp (ds->type, vl->type)); - - memset (buffer, 0, buffer_size); - -#define BUFFER_ADD(...) do { \ - status = ssnprintf (buffer + offset, buffer_size - offset, \ - __VA_ARGS__); \ - if (status < 1) \ - { \ - sfree (rates); \ - return (-1); \ - } \ - else if (((size_t) status) >= (buffer_size - offset)) \ - { \ - sfree (rates); \ - return (-1); \ - } \ - else \ - offset += ((size_t) status); \ -} while (0) - - BUFFER_ADD ("%lu", (unsigned long) vl->time); - - for (i = 0; i < ds->ds_num; i++) - { - if (ds->ds[i].type == DS_TYPE_GAUGE) - BUFFER_ADD (":%f", vl->values[i].gauge); - else if (cb->store_rates) - { - if (rates == NULL) - rates = uc_get_rate (ds, vl); - if (rates == NULL) - { - WARNING ("write_http plugin: " - "uc_get_rate failed."); - return (-1); - } - BUFFER_ADD (":%g", rates[i]); - } - else if (ds->ds[i].type == DS_TYPE_COUNTER) - BUFFER_ADD (":%llu", vl->values[i].counter); - else if (ds->ds[i].type == DS_TYPE_DERIVE) - BUFFER_ADD (":%"PRIi64, vl->values[i].derive); - else if (ds->ds[i].type == DS_TYPE_ABSOLUTE) - BUFFER_ADD (":%"PRIu64, vl->values[i].absolute); - else - { - ERROR ("write_http plugin: Unknown data source type: %i", - ds->ds[i].type); - sfree (rates); - return (-1); - } - } /* for ds->ds_num */ - -#undef BUFFER_ADD - - sfree (rates); - return (0); -} /* }}} int wh_value_list_to_string */ - static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */ wh_callback_t *cb) { @@ -366,7 +296,7 @@ static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{ /* Convert the values to an ASCII representation and put that into * `values'. */ - status = wh_value_list_to_string (values, sizeof (values), ds, vl, cb); + status = format_values (values, sizeof (values), ds, vl, cb->store_rates); if (status != 0) { ERROR ("write_http plugin: error with " "wh_value_list_to_string"); -- 2.30.2