X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Futils%2Fformat_graphite%2Fformat_graphite.c;h=55823566f7ec9c5df241289880b81b6491ab6a35;hp=d0e047ff81aa257daae97ef207c998b4fee808ce;hb=7045c4b543304ffabaa30a2ca9183489e75eafe3;hpb=87f8f66a84e78fd37dd2767c032ce74924d31dfd diff --git a/src/utils/format_graphite/format_graphite.c b/src/utils/format_graphite/format_graphite.c index d0e047ff..55823566 100644 --- a/src/utils/format_graphite/format_graphite.c +++ b/src/utils/format_graphite/format_graphite.c @@ -34,6 +34,37 @@ /* Utils functions to format data sets in graphite format. * Largely taken from write_graphite.c as it remains the same formatting */ +/* helper function for reverse_hostname */ +void reverse_string(char *r_host, int len) { + char t; + for (int i = 0; i < len / 2; i++) { + int j = len - i - 1; + t = r_host[i]; + r_host[i] = r_host[j]; + r_host[j] = t; + } +} + +void reverse_hostname(char *r_host, char const *orig_host) { + int len_host = strlen(orig_host); + + /* put reversed hostname into working copy */ + for (int i = 0; i < len_host; i++) + r_host[i] = orig_host[len_host - 1 - i]; + r_host[len_host] = '\0'; + + /* reverse labels (except last) */ + int p = 0; + for (int i = 0; i < len_host; i++) + if (r_host[i] == '.') { + reverse_string(&r_host[p], i - p); + p = i + 1; + } + + /* reverse last label */ + reverse_string(&r_host[p], len_host - p); +} + static int gr_format_values(char *ret, size_t ret_len, int ds_num, const data_set_t *ds, const value_list_t *vl, gauge_t const *rates) { @@ -120,7 +151,14 @@ static int gr_format_name_tagged(char *ret, int ret_len, value_list_t const *vl, if (postfix == NULL) postfix = ""; - gr_copy_escape_part(n_host, vl->host, sizeof(n_host), escape_char, 1); + if (flags & GRAPHITE_REVERSE_HOST) { + int len_host = strlen(vl->host); + char r_host[len_host + 1]; + reverse_hostname(r_host, vl->host); + gr_copy_escape_part(n_host, r_host, sizeof(n_host), escape_char, 1); + } else { + gr_copy_escape_part(n_host, vl->host, sizeof(n_host), escape_char, 1); + } gr_copy_escape_part(n_plugin, vl->plugin, sizeof(n_plugin), escape_char, 1); gr_copy_escape_part(n_plugin_instance, vl->plugin_instance, sizeof(n_plugin_instance), escape_char, 1); @@ -198,8 +236,16 @@ static int gr_format_name(char *ret, int ret_len, value_list_t const *vl, bool preserve_separator = (flags & GRAPHITE_PRESERVE_SEPARATOR); - gr_copy_escape_part(n_host, vl->host, sizeof(n_host), escape_char, - preserve_separator); + if (flags & GRAPHITE_REVERSE_HOST) { + int len_host = strlen(vl->host); + char r_host[len_host + 1]; + reverse_hostname(r_host, vl->host); + gr_copy_escape_part(n_host, r_host, sizeof(n_host), escape_char, + preserve_separator); + } else { + gr_copy_escape_part(n_host, vl->host, sizeof(n_host), escape_char, + preserve_separator); + } gr_copy_escape_part(n_plugin, vl->plugin, sizeof(n_plugin), escape_char, preserve_separator); gr_copy_escape_part(n_plugin_instance, vl->plugin_instance,