2 * collectd - src/utils_format_graphite.c
3 * Copyright (C) 2012 Thomas Meson
4 * Copyright (C) 2012 Florian octo Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Thomas Meson <zllak at hycik.org>
21 * Florian octo Forster <octo at collectd.org>
29 #include "utils_cache.h"
30 #include "utils_format_graphite.h"
32 #define GRAPHITE_FORBIDDEN " \t\"\\:!/()\n\r"
34 /* Utils functions to format data sets in graphite format.
35 * Largely taken from write_graphite.c as it remains the same formatting */
37 static int gr_format_values(char *ret, size_t ret_len, int ds_num,
38 const data_set_t *ds, const value_list_t *vl,
39 gauge_t const *rates) {
43 assert(0 == strcmp(ds->type, vl->type));
45 memset(ret, 0, ret_len);
47 #define BUFFER_ADD(...) \
49 status = snprintf(ret + offset, ret_len - offset, __VA_ARGS__); \
52 } 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(GAUGE_FORMAT, vl->values[ds_num].gauge);
60 else if (rates != NULL)
61 BUFFER_ADD("%f", rates[ds_num]);
62 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
63 BUFFER_ADD("%" PRIu64, (uint64_t)vl->values[ds_num].counter);
64 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
65 BUFFER_ADD("%" PRIi64, vl->values[ds_num].derive);
66 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
67 BUFFER_ADD("%" PRIu64, vl->values[ds_num].absolute);
69 P_ERROR("gr_format_values: Unknown data source type: %i",
79 static void gr_copy_escape_part(char *dst, const char *src, size_t dst_len,
80 char escape_char, bool preserve_separator) {
81 memset(dst, 0, dst_len);
86 for (size_t i = 0; i < dst_len; i++) {
92 if ((!preserve_separator && (src[i] == '.')) || isspace((int)src[i]) ||
100 static int gr_format_name_tagged(char *ret, int ret_len, value_list_t const *vl,
101 char const *ds_name, char const *prefix,
102 char const *postfix, char const escape_char,
103 unsigned int flags) {
104 char n_host[DATA_MAX_NAME_LEN];
105 char n_plugin[DATA_MAX_NAME_LEN];
106 char n_plugin_instance[DATA_MAX_NAME_LEN];
107 char n_type[DATA_MAX_NAME_LEN];
108 char n_type_instance[DATA_MAX_NAME_LEN];
110 char tmp_plugin[DATA_MAX_NAME_LEN + 8];
111 char tmp_plugin_instance[DATA_MAX_NAME_LEN + 17];
112 char tmp_type[DATA_MAX_NAME_LEN + 6];
113 char tmp_type_instance[DATA_MAX_NAME_LEN + 15];
114 char tmp_metric[3 * DATA_MAX_NAME_LEN + 2];
115 char tmp_ds_name[DATA_MAX_NAME_LEN + 9];
123 gr_copy_escape_part(n_host, vl->host, sizeof(n_host), escape_char, 1);
124 gr_copy_escape_part(n_plugin, vl->plugin, sizeof(n_plugin), escape_char, 1);
125 gr_copy_escape_part(n_plugin_instance, vl->plugin_instance,
126 sizeof(n_plugin_instance), escape_char, 1);
127 gr_copy_escape_part(n_type, vl->type, sizeof(n_type), escape_char, 1);
128 gr_copy_escape_part(n_type_instance, vl->type_instance,
129 sizeof(n_type_instance), escape_char, 1);
131 snprintf(tmp_plugin, sizeof(tmp_plugin), ";plugin=%s", n_plugin);
133 if (n_plugin_instance[0] != '\0')
134 snprintf(tmp_plugin_instance, sizeof(tmp_plugin_instance),
135 ";plugin_instance=%s", n_plugin_instance);
137 tmp_plugin_instance[0] = '\0';
139 if (!(flags & GRAPHITE_DROP_DUPE_FIELDS) || strcmp(n_plugin, n_type) != 0)
140 snprintf(tmp_type, sizeof(tmp_type), ";type=%s", n_type);
144 if (n_type_instance[0] != '\0') {
145 if (!(flags & GRAPHITE_DROP_DUPE_FIELDS) ||
146 strcmp(n_plugin_instance, n_type_instance) != 0)
147 snprintf(tmp_type_instance, sizeof(tmp_type_instance),
148 ";type_instance=%s", n_type_instance);
150 tmp_type_instance[0] = '\0';
152 tmp_type_instance[0] = '\0';
154 /* Assert always_append_ds -> ds_name */
155 assert(!(flags & GRAPHITE_ALWAYS_APPEND_DS) || (ds_name != NULL));
156 if (ds_name != NULL) {
157 snprintf(tmp_ds_name, sizeof(tmp_ds_name), ";ds_name=%s", ds_name);
159 if ((flags & GRAPHITE_DROP_DUPE_FIELDS) && strcmp(n_plugin, n_type) == 0)
160 snprintf(tmp_metric, sizeof(tmp_metric), "%s.%s", n_plugin, ds_name);
162 snprintf(tmp_metric, sizeof(tmp_metric), "%s.%s.%s", n_plugin, n_type,
165 tmp_ds_name[0] = '\0';
167 if ((flags & GRAPHITE_DROP_DUPE_FIELDS) && strcmp(n_plugin, n_type) == 0)
168 snprintf(tmp_metric, sizeof(tmp_metric), "%s", n_plugin);
170 snprintf(tmp_metric, sizeof(tmp_metric), "%s.%s", n_plugin, n_type);
173 snprintf(ret, ret_len, "%s%s%s;host=%s%s%s%s%s%s", prefix, tmp_metric,
174 postfix, n_host, tmp_plugin, tmp_plugin_instance, tmp_type,
175 tmp_type_instance, tmp_ds_name);
180 static int gr_format_name(char *ret, int ret_len, value_list_t const *vl,
181 char const *ds_name, char const *prefix,
182 char const *postfix, char const escape_char,
183 unsigned int flags) {
184 char n_host[DATA_MAX_NAME_LEN];
185 char n_plugin[DATA_MAX_NAME_LEN];
186 char n_plugin_instance[DATA_MAX_NAME_LEN];
187 char n_type[DATA_MAX_NAME_LEN];
188 char n_type_instance[DATA_MAX_NAME_LEN];
190 char tmp_plugin[2 * DATA_MAX_NAME_LEN + 1];
191 char tmp_type[2 * DATA_MAX_NAME_LEN + 1];
199 bool preserve_separator = (flags & GRAPHITE_PRESERVE_SEPARATOR);
201 gr_copy_escape_part(n_host, vl->host, sizeof(n_host), escape_char,
203 gr_copy_escape_part(n_plugin, vl->plugin, sizeof(n_plugin), escape_char,
205 gr_copy_escape_part(n_plugin_instance, vl->plugin_instance,
206 sizeof(n_plugin_instance), escape_char,
208 gr_copy_escape_part(n_type, vl->type, sizeof(n_type), escape_char,
210 gr_copy_escape_part(n_type_instance, vl->type_instance,
211 sizeof(n_type_instance), escape_char, preserve_separator);
213 if (n_plugin_instance[0] != '\0')
214 snprintf(tmp_plugin, sizeof(tmp_plugin), "%s%c%s", n_plugin,
215 (flags & GRAPHITE_SEPARATE_INSTANCES) ? '.' : '-',
218 sstrncpy(tmp_plugin, n_plugin, sizeof(tmp_plugin));
220 if (n_type_instance[0] != '\0') {
221 if ((flags & GRAPHITE_DROP_DUPE_FIELDS) && strcmp(n_plugin, n_type) == 0)
222 sstrncpy(tmp_type, n_type_instance, sizeof(tmp_type));
224 snprintf(tmp_type, sizeof(tmp_type), "%s%c%s", n_type,
225 (flags & GRAPHITE_SEPARATE_INSTANCES) ? '.' : '-',
228 sstrncpy(tmp_type, n_type, sizeof(tmp_type));
230 /* Assert always_append_ds -> ds_name */
231 assert(!(flags & GRAPHITE_ALWAYS_APPEND_DS) || (ds_name != NULL));
232 if (ds_name != NULL) {
233 if ((flags & GRAPHITE_DROP_DUPE_FIELDS) &&
234 strcmp(tmp_plugin, tmp_type) == 0)
235 snprintf(ret, ret_len, "%s%s%s.%s.%s", prefix, n_host, postfix,
236 tmp_plugin, ds_name);
238 snprintf(ret, ret_len, "%s%s%s.%s.%s.%s", prefix, n_host, postfix,
239 tmp_plugin, tmp_type, ds_name);
241 snprintf(ret, ret_len, "%s%s%s.%s.%s", prefix, n_host, postfix, tmp_plugin,
247 static void escape_graphite_string(char *buffer, char escape_char) {
248 assert(strchr(GRAPHITE_FORBIDDEN, escape_char) == NULL);
250 for (char *head = buffer + strcspn(buffer, GRAPHITE_FORBIDDEN); *head != '\0';
251 head += strcspn(head, GRAPHITE_FORBIDDEN))
255 int format_graphite(char *buffer, size_t buffer_size, data_set_t const *ds,
256 value_list_t const *vl, char const *prefix,
257 char const *postfix, char const escape_char,
258 unsigned int flags) {
262 gauge_t *rates = NULL;
263 if (flags & GRAPHITE_STORE_RATES) {
264 rates = uc_get_rate(ds, vl);
266 P_ERROR("format_graphite: error with uc_get_rate");
271 for (size_t i = 0; i < ds->ds_num; i++) {
272 char const *ds_name = NULL;
273 char key[10 * DATA_MAX_NAME_LEN];
278 if ((flags & GRAPHITE_ALWAYS_APPEND_DS) || (ds->ds_num > 1))
279 ds_name = ds->ds[i].name;
281 /* Copy the identifier to `key' and escape it. */
282 if (flags & GRAPHITE_USE_TAGS) {
283 status = gr_format_name_tagged(key, sizeof(key), vl, ds_name, prefix,
284 postfix, escape_char, flags);
286 P_ERROR("format_graphite: error with gr_format_name_tagged");
291 status = gr_format_name(key, sizeof(key), vl, ds_name, prefix, postfix,
294 P_ERROR("format_graphite: error with gr_format_name");
300 escape_graphite_string(key, escape_char);
302 /* Convert the values to an ASCII representation and put that into
304 status = gr_format_values(values, sizeof(values), i, ds, vl, rates);
306 P_ERROR("format_graphite: error with gr_format_values");
311 /* Compute the graphite command */
313 (size_t)snprintf(message, sizeof(message), "%s %s %u\r\n", key, values,
314 (unsigned int)CDTIME_T_TO_TIME_T(vl->time));
315 if (message_len >= sizeof(message)) {
316 P_ERROR("format_graphite: message buffer too small: "
317 "Need %" PRIsz " bytes.",
323 /* Append it in case we got multiple data set */
324 if ((buffer_pos + message_len) >= buffer_size) {
325 P_ERROR("format_graphite: target buffer too small");
329 memcpy((void *)(buffer + buffer_pos), message, message_len);
330 buffer_pos += message_len;
331 buffer[buffer_pos] = '\0';
335 } /* int format_graphite */