2 * collectd - src/utils_format_graphite.c
3 * Copyright (C) 2012 Thomas Meson
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Thomas Meson <zllak at hycik.org>
26 #include "utils_cache.h"
27 #include "utils_format_json.h"
28 #include "utils_parse_option.h"
30 /* Utils functions to format data sets in graphite format.
31 * Largely taken from write_graphite.c as it remains the same formatting */
33 static int gr_format_values (char *ret, size_t ret_len,
34 int ds_num, const data_set_t *ds, const value_list_t *vl)
39 assert (0 == strcmp (ds->type, vl->type));
41 memset (ret, 0, ret_len);
43 #define BUFFER_ADD(...) do { \
44 status = ssnprintf (ret + offset, ret_len - offset, \
50 else if (((size_t) status) >= (ret_len - offset)) \
55 offset += ((size_t) status); \
58 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
59 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
60 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
61 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
62 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
63 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
64 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
65 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
68 ERROR ("gr_format_values plugin: Unknown data source type: %i",
78 static void copy_escape_part (char *dst, const char *src, size_t dst_len,
83 memset (dst, 0, dst_len);
88 for (i = 0; i < dst_len; i++)
97 || isspace ((int) src[i])
98 || iscntrl ((int) src[i]))
105 static int gr_format_name (char *ret, int ret_len,
106 const value_list_t *vl,
112 char n_host[DATA_MAX_NAME_LEN];
113 char n_plugin[DATA_MAX_NAME_LEN];
114 char n_plugin_instance[DATA_MAX_NAME_LEN];
115 char n_type[DATA_MAX_NAME_LEN];
116 char n_type_instance[DATA_MAX_NAME_LEN];
118 char tmp_plugin[2 * DATA_MAX_NAME_LEN + 1];
119 char tmp_type[2 * DATA_MAX_NAME_LEN + 1];
127 copy_escape_part (n_host, vl->host,
128 sizeof (n_host), escape_char);
129 copy_escape_part (n_plugin, vl->plugin,
130 sizeof (n_plugin), escape_char);
131 copy_escape_part (n_plugin_instance, vl->plugin_instance,
132 sizeof (n_plugin_instance), escape_char);
133 copy_escape_part (n_type, vl->type,
134 sizeof (n_type), escape_char);
135 copy_escape_part (n_type_instance, vl->type_instance,
136 sizeof (n_type_instance), escape_char);
138 if (n_plugin_instance[0] != '\0')
139 ssnprintf (tmp_plugin, sizeof (tmp_plugin), "%s%c%s",
144 sstrncpy (tmp_plugin, n_plugin, sizeof (tmp_plugin));
146 if (n_type_instance[0] != '\0')
147 ssnprintf (tmp_type, sizeof (tmp_type), "%s%c%s",
152 sstrncpy (tmp_type, n_type, sizeof (tmp_type));
155 ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
156 prefix, n_host, postfix, tmp_plugin, tmp_type, ds_name);
158 ssnprintf (ret, ret_len, "%s%s%s.%s.%s",
159 prefix, n_host, postfix, tmp_plugin, tmp_type);
164 int format_graphite (char *buffer, size_t buffer_size,
165 const data_set_t *ds, const value_list_t *vl, char *prefix,
166 char *postfix, char escape_char)
172 for (i = 0; i < ds->ds_num; i++)
174 const char *ds_name = NULL;
175 char key[10*DATA_MAX_NAME_LEN];
180 ds_name = ds->ds[i].name;
182 /* Copy the identifier to `key' and escape it. */
183 status = gr_format_name (key, sizeof (key), vl, ds_name,
184 prefix, postfix, escape_char);
187 ERROR ("amqp plugin: error with gr_format_name");
191 escape_string (key, sizeof (key));
192 /* Convert the values to an ASCII representation and put that into
194 status = gr_format_values (values, sizeof (values), i, ds, vl);
197 ERROR ("format_graphite: error with gr_format_values");
201 /* Compute the graphite command */
202 message_len = (size_t) ssnprintf (message, sizeof (message),
206 (unsigned int) CDTIME_T_TO_TIME_T (vl->time));
207 if (message_len >= sizeof (message)) {
208 ERROR ("format_graphite: message buffer too small: "
209 "Need %zu bytes.", message_len + 1);
213 /* Append it in case we got multiple data set */
214 if ((buffer_pos + message_len) >= buffer_size)
216 ERROR ("format_graphite: target buffer too small");
219 memcpy((void *) (buffer + buffer_pos), message, message_len);
220 buffer_pos += message_len;
223 } /* int format_graphite */
225 /* vim: set sw=2 sts=2 et fdm=marker : */