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"
25 #include "plugin.h"
26 #include "common.h"
28 #include "utils_cache.h"
29 #include "utils_format_json.h"
30 #include "utils_parse_option.h"
32 /* Utils functions to format data sets in graphite format.
33 * Largely taken from write_graphite.c as it remains the same formatting */
35 static int gr_format_values (char *ret, size_t ret_len,
36 int ds_num, const data_set_t *ds, const value_list_t *vl,
37 gauge_t const *rates)
38 {
39 size_t offset = 0;
40 int status;
42 assert (0 == strcmp (ds->type, vl->type));
44 memset (ret, 0, ret_len);
46 #define BUFFER_ADD(...) do { \
47 status = ssnprintf (ret + offset, ret_len - offset, \
48 __VA_ARGS__); \
49 if (status < 1) \
50 { \
51 return (-1); \
52 } \
53 else if (((size_t) status) >= (ret_len - offset)) \
54 { \
55 return (-1); \
56 } \
57 else \
58 offset += ((size_t) status); \
59 } while (0)
61 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
62 BUFFER_ADD ("%f", vl->values[ds_num].gauge);
63 else if (rates != NULL)
64 BUFFER_ADD ("%f", rates[ds_num]);
65 else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
66 BUFFER_ADD ("%llu", vl->values[ds_num].counter);
67 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
68 BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
69 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
70 BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
71 else
72 {
73 ERROR ("gr_format_values plugin: Unknown data source type: %i",
74 ds->ds[ds_num].type);
75 return (-1);
76 }
78 #undef BUFFER_ADD
80 return (0);
81 }
83 static void gr_copy_escape_part (char *dst, const char *src, size_t dst_len,
84 char escape_char)
85 {
86 size_t i;
88 memset (dst, 0, dst_len);
90 if (src == NULL)
91 return;
93 for (i = 0; i < dst_len; i++)
94 {
95 if (src[i] == 0)
96 {
97 dst[i] = 0;
98 break;
99 }
101 if ((src[i] == '.')
102 || isspace ((int) src[i])
103 || iscntrl ((int) src[i]))
104 dst[i] = escape_char;
105 else
106 dst[i] = src[i];
107 }
108 }
110 static int gr_format_name (char *ret, int ret_len,
111 const value_list_t *vl,
112 const char *ds_name,
113 char *prefix,
114 char *postfix,
115 char escape_char)
116 {
117 char n_host[DATA_MAX_NAME_LEN];
118 char n_plugin[DATA_MAX_NAME_LEN];
119 char n_plugin_instance[DATA_MAX_NAME_LEN];
120 char n_type[DATA_MAX_NAME_LEN];
121 char n_type_instance[DATA_MAX_NAME_LEN];
123 char tmp_plugin[2 * DATA_MAX_NAME_LEN + 1];
124 char tmp_type[2 * DATA_MAX_NAME_LEN + 1];
126 if (prefix == NULL)
127 prefix = "";
129 if (postfix == NULL)
130 postfix = "";
132 gr_copy_escape_part (n_host, vl->host,
133 sizeof (n_host), escape_char);
134 gr_copy_escape_part (n_plugin, vl->plugin,
135 sizeof (n_plugin), escape_char);
136 gr_copy_escape_part (n_plugin_instance, vl->plugin_instance,
137 sizeof (n_plugin_instance), escape_char);
138 gr_copy_escape_part (n_type, vl->type,
139 sizeof (n_type), escape_char);
140 gr_copy_escape_part (n_type_instance, vl->type_instance,
141 sizeof (n_type_instance), escape_char);
143 if (n_plugin_instance[0] != '\0')
144 ssnprintf (tmp_plugin, sizeof (tmp_plugin), "%s%c%s",
145 n_plugin,
146 '-',
147 n_plugin_instance);
148 else
149 sstrncpy (tmp_plugin, n_plugin, sizeof (tmp_plugin));
151 if (n_type_instance[0] != '\0')
152 ssnprintf (tmp_type, sizeof (tmp_type), "%s%c%s",
153 n_type,
154 '-',
155 n_type_instance);
156 else
157 sstrncpy (tmp_type, n_type, sizeof (tmp_type));
159 if (ds_name != NULL)
160 ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
161 prefix, n_host, postfix, tmp_plugin, tmp_type, ds_name);
162 else
163 ssnprintf (ret, ret_len, "%s%s%s.%s.%s",
164 prefix, n_host, postfix, tmp_plugin, tmp_type);
166 return (0);
167 }
169 int format_graphite (char *buffer, size_t buffer_size,
170 const data_set_t *ds, const value_list_t *vl, char *prefix,
171 char *postfix, char escape_char,
172 _Bool store_rates)
173 {
174 int status = 0;
175 int i;
176 int buffer_pos = 0;
178 gauge_t *rates = NULL;
179 if (store_rates)
180 rates = uc_get_rate (ds, vl);
182 for (i = 0; i < ds->ds_num; i++)
183 {
184 const char *ds_name = NULL;
185 char key[10*DATA_MAX_NAME_LEN];
186 char values[512];
187 size_t message_len;
188 char message[1024];
190 ds_name = ds->ds[i].name;
192 /* Copy the identifier to `key' and escape it. */
193 status = gr_format_name (key, sizeof (key), vl, ds_name,
194 prefix, postfix, escape_char);
195 if (status != 0)
196 {
197 ERROR ("format_graphite: error with gr_format_name");
198 sfree (rates);
199 return (status);
200 }
202 escape_string (key, sizeof (key));
203 /* Convert the values to an ASCII representation and put that into
204 * `values'. */
205 status = gr_format_values (values, sizeof (values), i, ds, vl, rates);
206 if (status != 0)
207 {
208 ERROR ("format_graphite: error with gr_format_values");
209 sfree (rates);
210 return (status);
211 }
213 /* Compute the graphite command */
214 message_len = (size_t) ssnprintf (message, sizeof (message),
215 "%s %s %u\r\n",
216 key,
217 values,
218 (unsigned int) CDTIME_T_TO_TIME_T (vl->time));
219 if (message_len >= sizeof (message)) {
220 ERROR ("format_graphite: message buffer too small: "
221 "Need %zu bytes.", message_len + 1);
222 sfree (rates);
223 return (-ENOMEM);
224 }
226 /* Append it in case we got multiple data set */
227 if ((buffer_pos + message_len) >= buffer_size)
228 {
229 ERROR ("format_graphite: target buffer too small");
230 sfree (rates);
231 return (-ENOMEM);
232 }
233 memcpy((void *) (buffer + buffer_pos), message, message_len);
234 buffer_pos += message_len;
235 }
236 sfree (rates);
237 return (status);
238 } /* int format_graphite */
240 /* vim: set sw=2 sts=2 et fdm=marker : */