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