X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Futils_format_graphite.c;h=e23d5d92c65599fecc29ac575ca48c68af2b3369;hb=d3db12b73a1c3c17a2dfc0ba96ec99490088c957;hp=49a59c506959fa4938d3cdc60fb6b1fe281fb9b7;hpb=59dded4cc1a2077ad11c6e9b507e15ad19e0e38c;p=collectd.git diff --git a/src/utils_format_graphite.c b/src/utils_format_graphite.c index 49a59c50..e23d5d92 100644 --- a/src/utils_format_graphite.c +++ b/src/utils_format_graphite.c @@ -25,15 +25,18 @@ #include "plugin.h" #include "common.h" +#include "utils_format_graphite.h" #include "utils_cache.h" -#include "utils_format_json.h" #include "utils_parse_option.h" +#define GRAPHITE_FORBIDDEN " \t\"\\:!/()\n\r" + /* Utils functions to format data sets in graphite format. * Largely taken from write_graphite.c as it remains the same formatting */ static int gr_format_values (char *ret, size_t ret_len, - int ds_num, const data_set_t *ds, const value_list_t *vl) + int ds_num, const data_set_t *ds, const value_list_t *vl, + gauge_t const *rates) { size_t offset = 0; int status; @@ -58,7 +61,9 @@ static int gr_format_values (char *ret, size_t ret_len, } while (0) if (ds->ds[ds_num].type == DS_TYPE_GAUGE) - BUFFER_ADD ("%f", vl->values[ds_num].gauge); + BUFFER_ADD (GAUGE_FORMAT, vl->values[ds_num].gauge); + else if (rates != NULL) + BUFFER_ADD ("%f", rates[ds_num]); else if (ds->ds[ds_num].type == DS_TYPE_COUNTER) BUFFER_ADD ("%llu", vl->values[ds_num].counter); else if (ds->ds[ds_num].type == DS_TYPE_DERIVE) @@ -105,11 +110,12 @@ static void gr_copy_escape_part (char *dst, const char *src, size_t dst_len, } static int gr_format_name (char *ret, int ret_len, - const value_list_t *vl, - const char *ds_name, - char *prefix, - char *postfix, - char escape_char) + value_list_t const *vl, + char const *ds_name, + char const *prefix, + char const *postfix, + char const escape_char, + unsigned int flags) { char n_host[DATA_MAX_NAME_LEN]; char n_plugin[DATA_MAX_NAME_LEN]; @@ -140,7 +146,7 @@ static int gr_format_name (char *ret, int ret_len, if (n_plugin_instance[0] != '\0') ssnprintf (tmp_plugin, sizeof (tmp_plugin), "%s%c%s", n_plugin, - '-', + (flags & GRAPHITE_SEPARATE_INSTANCES) ? '.' : '-', n_plugin_instance); else sstrncpy (tmp_plugin, n_plugin, sizeof (tmp_plugin)); @@ -148,11 +154,13 @@ static int gr_format_name (char *ret, int ret_len, if (n_type_instance[0] != '\0') ssnprintf (tmp_type, sizeof (tmp_type), "%s%c%s", n_type, - '-', + (flags & GRAPHITE_SEPARATE_INSTANCES) ? '.' : '-', n_type_instance); else sstrncpy (tmp_type, n_type, sizeof (tmp_type)); + /* Assert always_append_ds -> ds_name */ + assert (!(flags & GRAPHITE_ALWAYS_APPEND_DS) || (ds_name != NULL)); if (ds_name != NULL) ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s", prefix, n_host, postfix, tmp_plugin, tmp_type, ds_name); @@ -163,40 +171,61 @@ static int gr_format_name (char *ret, int ret_len, return (0); } +static void escape_graphite_string (char *buffer, char escape_char) +{ + char *head; + + assert (strchr(GRAPHITE_FORBIDDEN, escape_char) == NULL); + + for (head = buffer + strcspn(buffer, GRAPHITE_FORBIDDEN); + *head != '\0'; + head += strcspn(head, GRAPHITE_FORBIDDEN)) + *head = escape_char; +} + int format_graphite (char *buffer, size_t buffer_size, - const data_set_t *ds, const value_list_t *vl, char *prefix, - char *postfix, char escape_char) + data_set_t const *ds, value_list_t const *vl, + char const *prefix, char const *postfix, char const escape_char, + unsigned int flags) { int status = 0; int i; int buffer_pos = 0; + gauge_t *rates = NULL; + if (flags & GRAPHITE_STORE_RATES) + rates = uc_get_rate (ds, vl); + for (i = 0; i < ds->ds_num; i++) { - const char *ds_name = NULL; + char const *ds_name = NULL; char key[10*DATA_MAX_NAME_LEN]; char values[512]; size_t message_len; char message[1024]; - ds_name = ds->ds[i].name; + if ((flags & GRAPHITE_ALWAYS_APPEND_DS) + || (ds->ds_num > 1)) + ds_name = ds->ds[i].name; /* Copy the identifier to `key' and escape it. */ status = gr_format_name (key, sizeof (key), vl, ds_name, - prefix, postfix, escape_char); + prefix, postfix, escape_char, flags); if (status != 0) { - ERROR ("amqp plugin: error with gr_format_name"); + ERROR ("format_graphite: error with gr_format_name"); + sfree (rates); return (status); } - escape_string (key, sizeof (key)); + escape_graphite_string (key, escape_char); /* Convert the values to an ASCII representation and put that into * `values'. */ - status = gr_format_values (values, sizeof (values), i, ds, vl); + status = gr_format_values (values, sizeof (values), i, ds, vl, rates); if (status != 0) { ERROR ("format_graphite: error with gr_format_values"); + sfree (rates); return (status); } @@ -209,6 +238,7 @@ int format_graphite (char *buffer, size_t buffer_size, if (message_len >= sizeof (message)) { ERROR ("format_graphite: message buffer too small: " "Need %zu bytes.", message_len + 1); + sfree (rates); return (-ENOMEM); } @@ -216,11 +246,13 @@ int format_graphite (char *buffer, size_t buffer_size, if ((buffer_pos + message_len) >= buffer_size) { ERROR ("format_graphite: target buffer too small"); + sfree (rates); return (-ENOMEM); } memcpy((void *) (buffer + buffer_pos), message, message_len); buffer_pos += message_len; } + sfree (rates); return (status); } /* int format_graphite */