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