1 /**
2 * collectd - src/utils_format_graphite.c
3 * Copyright (C) 2012 Thomas Meson
4 * Copyright (C) 2012 Florian octo Forster
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Thomas Meson <zllak at hycik.org>
21 * Florian octo Forster <octo at collectd.org>
22 **/
24 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
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) {
40 size_t offset = 0;
41 int status;
43 assert(0 == strcmp(ds->type, vl->type));
45 memset(ret, 0, ret_len);
47 #define BUFFER_ADD(...) \
48 do { \
49 status = ssnprintf(ret + offset, ret_len - offset, __VA_ARGS__); \
50 if (status < 1) { \
51 return (-1); \
52 } else if (((size_t)status) >= (ret_len - offset)) { \
53 return (-1); \
54 } else \
55 offset += ((size_t)status); \
56 } while (0)
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("%llu", 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);
68 else {
69 ERROR("gr_format_values plugin: Unknown data source type: %i",
70 ds->ds[ds_num].type);
71 return (-1);
72 }
74 #undef BUFFER_ADD
76 return (0);
77 }
79 static void gr_copy_escape_part(char *dst, const char *src, size_t dst_len,
80 char escape_char) {
81 memset(dst, 0, dst_len);
83 if (src == NULL)
84 return;
86 for (size_t i = 0; i < dst_len; i++) {
87 if (src[i] == 0) {
88 dst[i] = 0;
89 break;
90 }
92 if ((src[i] == '.') || isspace((int)src[i]) || iscntrl((int)src[i]))
93 dst[i] = escape_char;
94 else
95 dst[i] = src[i];
96 }
97 }
99 static int gr_format_name(char *ret, int ret_len, value_list_t const *vl,
100 char const *ds_name, char const *prefix,
101 char const *postfix, char const escape_char,
102 unsigned int flags) {
103 char n_host[DATA_MAX_NAME_LEN];
104 char n_plugin[DATA_MAX_NAME_LEN];
105 char n_plugin_instance[DATA_MAX_NAME_LEN];
106 char n_type[DATA_MAX_NAME_LEN];
107 char n_type_instance[DATA_MAX_NAME_LEN];
109 char tmp_plugin[2 * DATA_MAX_NAME_LEN + 1];
110 char tmp_type[2 * DATA_MAX_NAME_LEN + 1];
112 if (prefix == NULL)
113 prefix = "";
115 if (postfix == NULL)
116 postfix = "";
118 gr_copy_escape_part(n_host, vl->host, sizeof(n_host), escape_char);
119 gr_copy_escape_part(n_plugin, vl->plugin, sizeof(n_plugin), escape_char);
120 gr_copy_escape_part(n_plugin_instance, vl->plugin_instance,
121 sizeof(n_plugin_instance), escape_char);
122 gr_copy_escape_part(n_type, vl->type, sizeof(n_type), escape_char);
123 gr_copy_escape_part(n_type_instance, vl->type_instance,
124 sizeof(n_type_instance), escape_char);
126 if (n_plugin_instance[0] != '\0')
127 ssnprintf(tmp_plugin, sizeof(tmp_plugin), "%s%c%s", n_plugin,
128 (flags & GRAPHITE_SEPARATE_INSTANCES) ? '.' : '-',
129 n_plugin_instance);
130 else
131 sstrncpy(tmp_plugin, n_plugin, sizeof(tmp_plugin));
133 if (n_type_instance[0] != '\0')
134 ssnprintf(tmp_type, sizeof(tmp_type), "%s%c%s", n_type,
135 (flags & GRAPHITE_SEPARATE_INSTANCES) ? '.' : '-',
136 n_type_instance);
137 else
138 sstrncpy(tmp_type, n_type, sizeof(tmp_type));
140 /* Assert always_append_ds -> ds_name */
141 assert(!(flags & GRAPHITE_ALWAYS_APPEND_DS) || (ds_name != NULL));
142 if (ds_name != NULL)
143 ssnprintf(ret, ret_len, "%s%s%s.%s.%s.%s", prefix, n_host, postfix,
144 tmp_plugin, tmp_type, ds_name);
145 else
146 ssnprintf(ret, ret_len, "%s%s%s.%s.%s", prefix, n_host, postfix, tmp_plugin,
147 tmp_type);
149 return (0);
150 }
152 static void escape_graphite_string(char *buffer, char escape_char) {
153 assert(strchr(GRAPHITE_FORBIDDEN, escape_char) == NULL);
155 for (char *head = buffer + strcspn(buffer, GRAPHITE_FORBIDDEN); *head != '\0';
156 head += strcspn(head, GRAPHITE_FORBIDDEN))
157 *head = escape_char;
158 }
160 int format_graphite(char *buffer, size_t buffer_size, data_set_t const *ds,
161 value_list_t const *vl, char const *prefix,
162 char const *postfix, char const escape_char,
163 unsigned int flags) {
164 int status = 0;
165 int buffer_pos = 0;
167 gauge_t *rates = NULL;
168 if (flags & GRAPHITE_STORE_RATES)
169 rates = uc_get_rate(ds, vl);
171 for (size_t i = 0; i < ds->ds_num; i++) {
172 char const *ds_name = NULL;
173 char key[10 * DATA_MAX_NAME_LEN];
174 char values[512];
175 size_t message_len;
176 char message[1024];
178 if ((flags & GRAPHITE_ALWAYS_APPEND_DS) || (ds->ds_num > 1))
179 ds_name = ds->ds[i].name;
181 /* Copy the identifier to `key' and escape it. */
182 status = gr_format_name(key, sizeof(key), vl, ds_name, prefix, postfix,
183 escape_char, flags);
184 if (status != 0) {
185 ERROR("format_graphite: error with gr_format_name");
186 sfree(rates);
187 return (status);
188 }
190 escape_graphite_string(key, escape_char);
191 /* Convert the values to an ASCII representation and put that into
192 * `values'. */
193 status = gr_format_values(values, sizeof(values), i, ds, vl, rates);
194 if (status != 0) {
195 ERROR("format_graphite: error with gr_format_values");
196 sfree(rates);
197 return (status);
198 }
200 /* Compute the graphite command */
201 message_len =
202 (size_t)ssnprintf(message, sizeof(message), "%s %s %u\r\n", key, values,
203 (unsigned int)CDTIME_T_TO_TIME_T(vl->time));
204 if (message_len >= sizeof(message)) {
205 ERROR("format_graphite: message buffer too small: "
206 "Need %zu bytes.",
207 message_len + 1);
208 sfree(rates);
209 return (-ENOMEM);
210 }
212 /* Append it in case we got multiple data set */
213 if ((buffer_pos + message_len) >= buffer_size) {
214 ERROR("format_graphite: target buffer too small");
215 sfree(rates);
216 return (-ENOMEM);
217 }
218 memcpy((void *)(buffer + buffer_pos), message, message_len);
219 buffer_pos += message_len;
220 buffer[buffer_pos] = '\0';
221 }
222 sfree(rates);
223 return (status);
224 } /* int format_graphite */
226 /* vim: set sw=2 sts=2 et fdm=marker : */