Switch branches/tags
svn-trunk collectd-5.7.2 collectd-5.7.0 collectd-5.6.3 collectd-5.6.2 collectd-5.6.1 collectd-5.6.0 collectd-5.5.3 collectd-5.5.2 collectd-5.5.1 collectd-5.5.0 collectd-5.4.3 collectd-5.4.2 collectd-5.4.1 collectd-5.4.0 collectd-5.3.2 collectd-5.3.1 collectd-5.3.0 collectd-5.2.2 collectd-5.2.1 collectd-5.2.0 collectd-5.1.3 collectd-5.1.2 collectd-5.1.1 collectd-5.1.0 collectd-5.0.5 collectd-5.0.4 collectd-5.0.3 collectd-5.0.2 collectd-5.0.1 collectd-5.0.0 collectd-5.0.0-beta1 collectd-5.0.0-beta0 collectd-4.10.9 collectd-4.10.8 collectd-4.10.7 collectd-4.10.6 collectd-4.10.5 collectd-4.10.4 collectd-4.10.3 collectd-4.10.2 collectd-4.10.1 collectd-4.10.0 collectd-4.9.5 collectd-4.9.4 collectd-4.9.3 collectd-4.9.2 collectd-4.9.1 collectd-4.9.0 collectd-4.8.5 collectd-4.8.4 collectd-4.8.3 collectd-4.8.2 collectd-4.8.1 collectd-4.8.0 collectd-4.7.5 collectd-4.7.4 collectd-4.7.3 collectd-4.7.2 collectd-4.7.1 collectd-4.7.0 collectd-4.6.5 collectd-4.6.4 collectd-4.6.3 collectd-4.6.2 collectd-4.6.1 collectd-4.6.0 collectd-4.5.4 collectd-4.5.3 collectd-4.5.2 collectd-4.5.1 collectd-4.5.0 collectd-4.4.5 collectd-4.4.4 collectd-4.4.3 collectd-4.4.2 collectd-4.4.1 collectd-4.4.0 collectd-4.3.4 collectd-4.3.3 collectd-4.3.2 collectd-4.3.1 collectd-4.3.0 collectd-4.3.0beta1 collectd-4.3.0beta0 collectd-4.2.7 collectd-4.2.6 collectd-4.2.5 collectd-4.2.4 collectd-4.2.3 collectd-4.2.2 collectd-4.2.1 collectd-4.2.0 collectd-4.1.6 collectd-4.1.5 collectd-4.1.4 collectd-4.1.3 collectd-4.1.2 collectd-4.1.1 collectd-4.1.0
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
228 lines (195 sloc) 6.63 KB
* collectd - src/utils_format_graphite.c
* Copyright (C) 2012 Thomas Meson
* Copyright (C) 2012 Florian octo Forster
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License is applicable.
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* General Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* Authors:
* Thomas Meson <zllak at>
* Florian octo Forster <octo at>
#include "collectd.h"
#include "plugin.h"
#include "common.h"
#include "utils_cache.h"
#include "utils_format_json.h"
#include "utils_parse_option.h"
/* 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)
size_t offset = 0;
int status;
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) \
{ \
return (-1); \
} \
else if (((size_t) status) >= (ret_len - offset)) \
{ \
return (-1); \
} \
else \
offset += ((size_t) status); \
} while (0)
if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
BUFFER_ADD ("%f", vl->values[ds_num].gauge);
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)
BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
ERROR ("gr_format_values plugin: Unknown data source type: %i",
return (-1);
return (0);
static void gr_copy_escape_part (char *dst, const char *src, size_t dst_len,
char escape_char)
size_t i;
memset (dst, 0, dst_len);
if (src == NULL)
for (i = 0; i < dst_len; i++)
if (src[i] == 0)
dst[i] = 0;
if ((src[i] == '.')
|| isspace ((int) src[i])
|| iscntrl ((int) src[i]))
dst[i] = escape_char;
dst[i] = src[i];
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)
char n_host[DATA_MAX_NAME_LEN];
char n_plugin[DATA_MAX_NAME_LEN];
char n_plugin_instance[DATA_MAX_NAME_LEN];
char n_type[DATA_MAX_NAME_LEN];
char n_type_instance[DATA_MAX_NAME_LEN];
char tmp_plugin[2 * DATA_MAX_NAME_LEN + 1];
char tmp_type[2 * DATA_MAX_NAME_LEN + 1];
if (prefix == NULL)
prefix = "";
if (postfix == NULL)
postfix = "";
gr_copy_escape_part (n_host, vl->host,
sizeof (n_host), escape_char);
gr_copy_escape_part (n_plugin, vl->plugin,
sizeof (n_plugin), escape_char);
gr_copy_escape_part (n_plugin_instance, vl->plugin_instance,
sizeof (n_plugin_instance), escape_char);
gr_copy_escape_part (n_type, vl->type,
sizeof (n_type), escape_char);
gr_copy_escape_part (n_type_instance, vl->type_instance,
sizeof (n_type_instance), escape_char);
if (n_plugin_instance[0] != '\0')
ssnprintf (tmp_plugin, sizeof (tmp_plugin), "%s%c%s",
sstrncpy (tmp_plugin, n_plugin, sizeof (tmp_plugin));
if (n_type_instance[0] != '\0')
ssnprintf (tmp_type, sizeof (tmp_type), "%s%c%s",
sstrncpy (tmp_type, n_type, sizeof (tmp_type));
if (ds_name != NULL)
ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
prefix, n_host, postfix, tmp_plugin, tmp_type, ds_name);
ssnprintf (ret, ret_len, "%s%s%s.%s.%s",
prefix, n_host, postfix, tmp_plugin, tmp_type);
return (0);
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)
int status = 0;
int i;
int buffer_pos = 0;
for (i = 0; i < ds->ds_num; i++)
const char *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;
/* Copy the identifier to `key' and escape it. */
status = gr_format_name (key, sizeof (key), vl, ds_name,
prefix, postfix, escape_char);
if (status != 0)
ERROR ("amqp plugin: error with gr_format_name");
return (status);
escape_string (key, sizeof (key));
/* Convert the values to an ASCII representation and put that into
* `values'. */
status = gr_format_values (values, sizeof (values), i, ds, vl);
if (status != 0)
ERROR ("format_graphite: error with gr_format_values");
return (status);
/* Compute the graphite command */
message_len = (size_t) ssnprintf (message, sizeof (message),
"%s %s %u\r\n",
(unsigned int) CDTIME_T_TO_TIME_T (vl->time));
if (message_len >= sizeof (message)) {
ERROR ("format_graphite: message buffer too small: "
"Need %zu bytes.", message_len + 1);
return (-ENOMEM);
/* Append it in case we got multiple data set */
if ((buffer_pos + message_len) >= buffer_size)
ERROR ("format_graphite: target buffer too small");
return (-ENOMEM);
memcpy((void *) (buffer + buffer_pos), message, message_len);
buffer_pos += message_len;
return (status);
} /* int format_graphite */
/* vim: set sw=2 sts=2 et fdm=marker : */