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