Merge pull request #3339 from jkohen/patch-1
[collectd.git] / src / utils / format_graphite / format_graphite.c
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  **/
23
24 #include "collectd.h"
25
26 #include "plugin.h"
27 #include "utils/common/common.h"
28
29 #include "utils/format_graphite/format_graphite.h"
30 #include "utils_cache.h"
31
32 #define GRAPHITE_FORBIDDEN " \t\"\\:!/()\n\r"
33
34 /* Utils functions to format data sets in graphite format.
35  * Largely taken from write_graphite.c as it remains the same formatting */
36
37 /* helper function for reverse_hostname */
38 void reverse_string(char *r_host, int len) {
39   for (int i = 0, j = len - 1; i < j; i++, j--) {
40     char t = r_host[i];
41     r_host[i] = r_host[j];
42     r_host[j] = t;
43   }
44 }
45
46 void reverse_hostname(char *r_host, char const *orig_host) {
47   int len_host = strlen(orig_host);
48
49   /* put reversed hostname into working copy */
50   for (int i = 0; i < len_host; i++)
51     r_host[i] = orig_host[len_host - 1 - i];
52   r_host[len_host] = '\0';
53
54   /* reverse labels (except last) */
55   int p = 0;
56   for (int i = 0; i < len_host; i++)
57     if (r_host[i] == '.') {
58       reverse_string(&r_host[p], i - p);
59       p = i + 1;
60     }
61
62   /* reverse last label */
63   reverse_string(&r_host[p], len_host - p);
64 }
65
66 static int gr_format_values(char *ret, size_t ret_len, int ds_num,
67                             const data_set_t *ds, const value_list_t *vl,
68                             gauge_t const *rates) {
69   size_t offset = 0;
70   int status;
71
72   assert(0 == strcmp(ds->type, vl->type));
73
74   memset(ret, 0, ret_len);
75
76 #define BUFFER_ADD(...)                                                        \
77   do {                                                                         \
78     status = snprintf(ret + offset, ret_len - offset, __VA_ARGS__);            \
79     if (status < 1) {                                                          \
80       return -1;                                                               \
81     } else if (((size_t)status) >= (ret_len - offset)) {                       \
82       return -1;                                                               \
83     } else                                                                     \
84       offset += ((size_t)status);                                              \
85   } while (0)
86
87   if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
88     BUFFER_ADD(GAUGE_FORMAT, vl->values[ds_num].gauge);
89   else if (rates != NULL)
90     BUFFER_ADD("%f", rates[ds_num]);
91   else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
92     BUFFER_ADD("%" PRIu64, (uint64_t)vl->values[ds_num].counter);
93   else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
94     BUFFER_ADD("%" PRIi64, vl->values[ds_num].derive);
95   else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
96     BUFFER_ADD("%" PRIu64, vl->values[ds_num].absolute);
97   else {
98     P_ERROR("gr_format_values: Unknown data source type: %i",
99             ds->ds[ds_num].type);
100     return -1;
101   }
102
103 #undef BUFFER_ADD
104
105   return 0;
106 }
107
108 static void gr_copy_escape_part(char *dst, const char *src, size_t dst_len,
109                                 char escape_char, bool preserve_separator) {
110   memset(dst, 0, dst_len);
111
112   if (src == NULL)
113     return;
114
115   for (size_t i = 0; i < dst_len; i++) {
116     if (src[i] == 0) {
117       dst[i] = 0;
118       break;
119     }
120
121     if ((!preserve_separator && (src[i] == '.')) || isspace((int)src[i]) ||
122         iscntrl((int)src[i]))
123       dst[i] = escape_char;
124     else
125       dst[i] = src[i];
126   }
127 }
128
129 static int gr_format_name_tagged(char *ret, int ret_len, value_list_t const *vl,
130                                  char const *ds_name, char const *prefix,
131                                  char const *postfix, char const escape_char,
132                                  unsigned int flags) {
133   char n_host[DATA_MAX_NAME_LEN];
134   char n_plugin[DATA_MAX_NAME_LEN];
135   char n_plugin_instance[DATA_MAX_NAME_LEN];
136   char n_type[DATA_MAX_NAME_LEN];
137   char n_type_instance[DATA_MAX_NAME_LEN];
138
139   char tmp_plugin[DATA_MAX_NAME_LEN + 8];
140   char tmp_plugin_instance[DATA_MAX_NAME_LEN + 17];
141   char tmp_type[DATA_MAX_NAME_LEN + 6];
142   char tmp_type_instance[DATA_MAX_NAME_LEN + 15];
143   char tmp_metric[3 * DATA_MAX_NAME_LEN + 2];
144   char tmp_ds_name[DATA_MAX_NAME_LEN + 9];
145
146   if (prefix == NULL)
147     prefix = "";
148
149   if (postfix == NULL)
150     postfix = "";
151
152   if (flags & GRAPHITE_REVERSE_HOST) {
153     char r_host[DATA_MAX_NAME_LEN];
154     reverse_hostname(r_host, vl->host);
155     gr_copy_escape_part(n_host, r_host, sizeof(n_host), escape_char, 1);
156   } else {
157     gr_copy_escape_part(n_host, vl->host, sizeof(n_host), escape_char, 1);
158   }
159   gr_copy_escape_part(n_plugin, vl->plugin, sizeof(n_plugin), escape_char, 1);
160   gr_copy_escape_part(n_plugin_instance, vl->plugin_instance,
161                       sizeof(n_plugin_instance), escape_char, 1);
162   gr_copy_escape_part(n_type, vl->type, sizeof(n_type), escape_char, 1);
163   gr_copy_escape_part(n_type_instance, vl->type_instance,
164                       sizeof(n_type_instance), escape_char, 1);
165
166   snprintf(tmp_plugin, sizeof(tmp_plugin), ";plugin=%s", n_plugin);
167
168   if (n_plugin_instance[0] != '\0')
169     snprintf(tmp_plugin_instance, sizeof(tmp_plugin_instance),
170              ";plugin_instance=%s", n_plugin_instance);
171   else
172     tmp_plugin_instance[0] = '\0';
173
174   if (!(flags & GRAPHITE_DROP_DUPE_FIELDS) || strcmp(n_plugin, n_type) != 0)
175     snprintf(tmp_type, sizeof(tmp_type), ";type=%s", n_type);
176   else
177     tmp_type[0] = '\0';
178
179   if (n_type_instance[0] != '\0') {
180     if (!(flags & GRAPHITE_DROP_DUPE_FIELDS) ||
181         strcmp(n_plugin_instance, n_type_instance) != 0)
182       snprintf(tmp_type_instance, sizeof(tmp_type_instance),
183                ";type_instance=%s", n_type_instance);
184     else
185       tmp_type_instance[0] = '\0';
186   } else
187     tmp_type_instance[0] = '\0';
188
189   /* Assert always_append_ds -> ds_name */
190   assert(!(flags & GRAPHITE_ALWAYS_APPEND_DS) || (ds_name != NULL));
191   if (ds_name != NULL) {
192     snprintf(tmp_ds_name, sizeof(tmp_ds_name), ";ds_name=%s", ds_name);
193
194     if ((flags & GRAPHITE_DROP_DUPE_FIELDS) && strcmp(n_plugin, n_type) == 0)
195       snprintf(tmp_metric, sizeof(tmp_metric), "%s.%s", n_plugin, ds_name);
196     else
197       snprintf(tmp_metric, sizeof(tmp_metric), "%s.%s.%s", n_plugin, n_type,
198                ds_name);
199   } else {
200     tmp_ds_name[0] = '\0';
201
202     if ((flags & GRAPHITE_DROP_DUPE_FIELDS) && strcmp(n_plugin, n_type) == 0)
203       snprintf(tmp_metric, sizeof(tmp_metric), "%s", n_plugin);
204     else
205       snprintf(tmp_metric, sizeof(tmp_metric), "%s.%s", n_plugin, n_type);
206   }
207
208   snprintf(ret, ret_len, "%s%s%s;host=%s%s%s%s%s%s", prefix, tmp_metric,
209            postfix, n_host, tmp_plugin, tmp_plugin_instance, tmp_type,
210            tmp_type_instance, tmp_ds_name);
211
212   return 0;
213 }
214
215 static int gr_format_name(char *ret, int ret_len, value_list_t const *vl,
216                           char const *ds_name, char const *prefix,
217                           char const *postfix, char const escape_char,
218                           unsigned int flags) {
219   char n_host[DATA_MAX_NAME_LEN];
220   char n_plugin[DATA_MAX_NAME_LEN];
221   char n_plugin_instance[DATA_MAX_NAME_LEN];
222   char n_type[DATA_MAX_NAME_LEN];
223   char n_type_instance[DATA_MAX_NAME_LEN];
224
225   char tmp_plugin[2 * DATA_MAX_NAME_LEN + 1];
226   char tmp_type[2 * DATA_MAX_NAME_LEN + 1];
227
228   if (prefix == NULL)
229     prefix = "";
230
231   if (postfix == NULL)
232     postfix = "";
233
234   bool preserve_separator = (flags & GRAPHITE_PRESERVE_SEPARATOR);
235
236   if (flags & GRAPHITE_REVERSE_HOST) {
237     char r_host[DATA_MAX_NAME_LEN];
238     reverse_hostname(r_host, vl->host);
239     gr_copy_escape_part(n_host, r_host, sizeof(n_host), escape_char,
240                         preserve_separator);
241   } else {
242     gr_copy_escape_part(n_host, vl->host, sizeof(n_host), escape_char,
243                         preserve_separator);
244   }
245   gr_copy_escape_part(n_plugin, vl->plugin, sizeof(n_plugin), escape_char,
246                       preserve_separator);
247   gr_copy_escape_part(n_plugin_instance, vl->plugin_instance,
248                       sizeof(n_plugin_instance), escape_char,
249                       preserve_separator);
250   gr_copy_escape_part(n_type, vl->type, sizeof(n_type), escape_char,
251                       preserve_separator);
252   gr_copy_escape_part(n_type_instance, vl->type_instance,
253                       sizeof(n_type_instance), escape_char, preserve_separator);
254
255   if (n_plugin_instance[0] != '\0')
256     snprintf(tmp_plugin, sizeof(tmp_plugin), "%s%c%s", n_plugin,
257              (flags & GRAPHITE_SEPARATE_INSTANCES) ? '.' : '-',
258              n_plugin_instance);
259   else
260     sstrncpy(tmp_plugin, n_plugin, sizeof(tmp_plugin));
261
262   if (n_type_instance[0] != '\0') {
263     if ((flags & GRAPHITE_DROP_DUPE_FIELDS) && strcmp(n_plugin, n_type) == 0)
264       sstrncpy(tmp_type, n_type_instance, sizeof(tmp_type));
265     else
266       snprintf(tmp_type, sizeof(tmp_type), "%s%c%s", n_type,
267                (flags & GRAPHITE_SEPARATE_INSTANCES) ? '.' : '-',
268                n_type_instance);
269   } else
270     sstrncpy(tmp_type, n_type, sizeof(tmp_type));
271
272   /* Assert always_append_ds -> ds_name */
273   assert(!(flags & GRAPHITE_ALWAYS_APPEND_DS) || (ds_name != NULL));
274   if (ds_name != NULL) {
275     if ((flags & GRAPHITE_DROP_DUPE_FIELDS) &&
276         strcmp(tmp_plugin, tmp_type) == 0)
277       snprintf(ret, ret_len, "%s%s%s.%s.%s", prefix, n_host, postfix,
278                tmp_plugin, ds_name);
279     else
280       snprintf(ret, ret_len, "%s%s%s.%s.%s.%s", prefix, n_host, postfix,
281                tmp_plugin, tmp_type, ds_name);
282   } else
283     snprintf(ret, ret_len, "%s%s%s.%s.%s", prefix, n_host, postfix, tmp_plugin,
284              tmp_type);
285
286   return 0;
287 }
288
289 static void escape_graphite_string(char *buffer, char escape_char) {
290   assert(strchr(GRAPHITE_FORBIDDEN, escape_char) == NULL);
291
292   for (char *head = buffer + strcspn(buffer, GRAPHITE_FORBIDDEN); *head != '\0';
293        head += strcspn(head, GRAPHITE_FORBIDDEN))
294     *head = escape_char;
295 }
296
297 int format_graphite(char *buffer, size_t buffer_size, data_set_t const *ds,
298                     value_list_t const *vl, char const *prefix,
299                     char const *postfix, char const escape_char,
300                     unsigned int flags) {
301   int status = 0;
302   int buffer_pos = 0;
303
304   gauge_t *rates = NULL;
305   if (flags & GRAPHITE_STORE_RATES) {
306     rates = uc_get_rate(ds, vl);
307     if (rates == NULL) {
308       P_ERROR("format_graphite: error with uc_get_rate");
309       return -1;
310     }
311   }
312
313   for (size_t i = 0; i < ds->ds_num; i++) {
314     char const *ds_name = NULL;
315     char key[10 * DATA_MAX_NAME_LEN];
316     char values[512];
317     size_t message_len;
318     char message[1024];
319
320     if ((flags & GRAPHITE_ALWAYS_APPEND_DS) || (ds->ds_num > 1))
321       ds_name = ds->ds[i].name;
322
323     /* Copy the identifier to `key' and escape it. */
324     if (flags & GRAPHITE_USE_TAGS) {
325       status = gr_format_name_tagged(key, sizeof(key), vl, ds_name, prefix,
326                                      postfix, escape_char, flags);
327       if (status != 0) {
328         P_ERROR("format_graphite: error with gr_format_name_tagged");
329         sfree(rates);
330         return status;
331       }
332     } else {
333       status = gr_format_name(key, sizeof(key), vl, ds_name, prefix, postfix,
334                               escape_char, flags);
335       if (status != 0) {
336         P_ERROR("format_graphite: error with gr_format_name");
337         sfree(rates);
338         return status;
339       }
340     }
341
342     escape_graphite_string(key, escape_char);
343
344     /* Convert the values to an ASCII representation and put that into
345      * `values'. */
346     status = gr_format_values(values, sizeof(values), i, ds, vl, rates);
347     if (status != 0) {
348       P_ERROR("format_graphite: error with gr_format_values");
349       sfree(rates);
350       return status;
351     }
352
353     /* Compute the graphite command */
354     message_len =
355         (size_t)snprintf(message, sizeof(message), "%s %s %u\r\n", key, values,
356                          (unsigned int)CDTIME_T_TO_TIME_T(vl->time));
357     if (message_len >= sizeof(message)) {
358       P_ERROR("format_graphite: message buffer too small: "
359               "Need %" PRIsz " bytes.",
360               message_len + 1);
361       sfree(rates);
362       return -ENOMEM;
363     }
364
365     /* Append it in case we got multiple data set */
366     if ((buffer_pos + message_len) >= buffer_size) {
367       P_ERROR("format_graphite: target buffer too small");
368       sfree(rates);
369       return -ENOMEM;
370     }
371     memcpy((void *)(buffer + buffer_pos), message, message_len);
372     buffer_pos += message_len;
373     buffer[buffer_pos] = '\0';
374   }
375   sfree(rates);
376   return status;
377 } /* int format_graphite */