#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, /* {{{ */
} /* }}} 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);
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 ("[");
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)
#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;
* 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);
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);
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)
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 : */