From: Chris Buben Date: Wed, 24 Mar 2010 03:53:30 +0000 (-0700) Subject: write_http: make StoreRates work for JSON format, too. X-Git-Tag: collectd-4.10.0~47 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=670442e38158af0eb6b4a4efa6becfa6696129e7;p=collectd.git write_http: make StoreRates work for JSON format, too. StoreRates for write_http was inconsistent in that it only functioned for WH_FORMAT_COMMAND. Extend StoreRates so that it works when using WH_FORMAT_JSON, too. Signed-off-by: Florian Forster --- diff --git a/src/utils_format_json.c b/src/utils_format_json.c index 75a73aee..e8fcb35b 100644 --- a/src/utils_format_json.c +++ b/src/utils_format_json.c @@ -23,6 +23,7 @@ #include "plugin.h" #include "common.h" +#include "utils_cache.h" #include "utils_format_json.h" static int escape_string (char *buffer, size_t buffer_size, /* {{{ */ @@ -72,10 +73,11 @@ static int escape_string (char *buffer, size_t buffer_size, /* {{{ */ } /* }}} int buffer_add_string */ static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */ - const data_set_t *ds, const value_list_t *vl) + const data_set_t *ds, const value_list_t *vl, int store_rates) { size_t offset = 0; int i; + gauge_t *rates = NULL; memset (buffer, 0, buffer_size); @@ -84,11 +86,17 @@ static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */ 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 (-ENOMEM); \ + } \ else \ - offset += ((size_t) status); \ + offset += ((size_t) status); \ } while (0) BUFFER_ADD ("["); @@ -105,7 +113,26 @@ static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */ BUFFER_ADD ("null"); } else if (ds->ds[i].type == DS_TYPE_COUNTER) - BUFFER_ADD ("%llu", vl->values[i].counter); + { + if (store_rates != 0) + { + if (rates == NULL) + rates = uc_get_rate (ds, vl); + if (rates == NULL) + { + WARNING ("utils_format_json: " + "uc_get_rate failed."); + sfree(rates); + return (-1); + } + if(isfinite (rates[i])) + BUFFER_ADD ("%g", rates[i]); + else + BUFFER_ADD ("null"); + } + else + 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) @@ -122,12 +149,12 @@ static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */ #undef BUFFER_ADD DEBUG ("format_json: values_to_json: buffer = %s;", buffer); - + sfree(rates); return (0); } /* }}} int values_to_json */ static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */ - const data_set_t *ds, const value_list_t *vl) + const data_set_t *ds, const value_list_t *vl, int store_rates) { char temp[512]; size_t offset = 0; @@ -150,7 +177,7 @@ static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */ * a square bracket in `format_json_finalize'. */ BUFFER_ADD (",{"); - status = values_to_json (temp, sizeof (temp), ds, vl); + status = values_to_json (temp, sizeof (temp), ds, vl, store_rates); if (status != 0) return (status); BUFFER_ADD ("\"values\":%s", temp); @@ -184,12 +211,12 @@ static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */ static int format_json_value_list_nocheck (char *buffer, /* {{{ */ size_t *ret_buffer_fill, size_t *ret_buffer_free, const data_set_t *ds, const value_list_t *vl, - size_t temp_size) + int store_rates, size_t temp_size) { char temp[temp_size]; int status; - status = value_list_to_json (temp, sizeof (temp), ds, vl); + status = value_list_to_json (temp, sizeof (temp), ds, vl, store_rates); if (status != 0) return (status); temp_size = strlen (temp); @@ -255,7 +282,7 @@ int format_json_finalize (char *buffer, /* {{{ */ int format_json_value_list (char *buffer, /* {{{ */ size_t *ret_buffer_fill, size_t *ret_buffer_free, - const data_set_t *ds, const value_list_t *vl) + const data_set_t *ds, const value_list_t *vl, int store_rates) { if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL) @@ -267,7 +294,7 @@ int format_json_value_list (char *buffer, /* {{{ */ return (format_json_value_list_nocheck (buffer, ret_buffer_fill, ret_buffer_free, ds, vl, - (*ret_buffer_free) - 2)); + store_rates, (*ret_buffer_free) - 2)); } /* }}} int format_json_value_list */ /* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/utils_format_json.h b/src/utils_format_json.h index 880a79dc..c902e272 100644 --- a/src/utils_format_json.h +++ b/src/utils_format_json.h @@ -29,7 +29,7 @@ int format_json_initialize (char *buffer, size_t *ret_buffer_fill, size_t *ret_buffer_free); int format_json_value_list (char *buffer, size_t *ret_buffer_fill, size_t *ret_buffer_free, - const data_set_t *ds, const value_list_t *vl); + const data_set_t *ds, const value_list_t *vl, int store_rates); int format_json_finalize (char *buffer, size_t *ret_buffer_fill, size_t *ret_buffer_free); diff --git a/src/write_http.c b/src/write_http.c index cc47b16a..ab8757ed 100644 --- a/src/write_http.c +++ b/src/write_http.c @@ -446,7 +446,7 @@ static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ * status = format_json_value_list (cb->send_buffer, &cb->send_buffer_fill, &cb->send_buffer_free, - ds, vl); + ds, vl, cb->store_rates); if (status == (-ENOMEM)) { status = wh_flush_nolock (/* timeout = */ -1, cb); @@ -460,7 +460,7 @@ static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ * status = format_json_value_list (cb->send_buffer, &cb->send_buffer_fill, &cb->send_buffer_free, - ds, vl); + ds, vl, cb->store_rates); } if (status != 0) {