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