amqp plugin: add support for Graphite output
[collectd.git] / src / utils_format_graphite.c
1 /**
2  * collectd - src/utils_format_graphite.c
3  * Copyright (C) 2012  Thomas Meson
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Author:
19  *   Thomas Meson <zllak at hycik.org>
20  **/
21
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25
26 #include "utils_cache.h"
27 #include "utils_format_json.h"
28 #include "utils_parse_option.h"
29
30 /* Utils functions to format data sets in graphite format.
31  * Largely taken from write_graphite.c as it remains the same formatting */
32
33 static int gr_format_values (char *ret, size_t ret_len,
34         int ds_num, const data_set_t *ds, const value_list_t *vl)
35 {
36     size_t offset = 0;
37     int status;
38
39     assert (0 == strcmp (ds->type, vl->type));
40
41     memset (ret, 0, ret_len);
42
43 #define BUFFER_ADD(...) do { \
44     status = ssnprintf (ret + offset, ret_len - offset, \
45             __VA_ARGS__); \
46     if (status < 1) \
47     { \
48         return (-1); \
49     } \
50     else if (((size_t) status) >= (ret_len - offset)) \
51     { \
52         return (-1); \
53     } \
54     else \
55     offset += ((size_t) status); \
56 } while (0)
57
58     if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
59         BUFFER_ADD ("%f", vl->values[ds_num].gauge);
60     else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
61         BUFFER_ADD ("%llu", vl->values[ds_num].counter);
62     else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
63         BUFFER_ADD ("%"PRIi64, vl->values[ds_num].derive);
64     else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
65         BUFFER_ADD ("%"PRIu64, vl->values[ds_num].absolute);
66     else
67     {
68         ERROR ("gr_format_values plugin: Unknown data source type: %i",
69                 ds->ds[ds_num].type);
70         return (-1);
71     }
72
73 #undef BUFFER_ADD
74
75     return (0);
76 }
77
78 static void copy_escape_part (char *dst, const char *src, size_t dst_len,
79     char escape_char)
80 {
81     size_t i;
82
83     memset (dst, 0, dst_len);
84
85     if (src == NULL)
86         return;
87
88     for (i = 0; i < dst_len; i++)
89     {
90         if (src[i] == 0)
91         {
92             dst[i] = 0;
93             break;
94         }
95
96         if ((src[i] == '.')
97                 || isspace ((int) src[i])
98                 || iscntrl ((int) src[i]))
99             dst[i] = escape_char;
100         else
101             dst[i] = src[i];
102     }
103 }
104
105 static int gr_format_name (char *ret, int ret_len,
106         const value_list_t *vl,
107         const char *ds_name,
108         char *prefix,
109         char *postfix,
110         char escape_char)
111 {
112     char n_host[DATA_MAX_NAME_LEN];
113     char n_plugin[DATA_MAX_NAME_LEN];
114     char n_plugin_instance[DATA_MAX_NAME_LEN];
115     char n_type[DATA_MAX_NAME_LEN];
116     char n_type_instance[DATA_MAX_NAME_LEN];
117
118     char tmp_plugin[2 * DATA_MAX_NAME_LEN + 1];
119     char tmp_type[2 * DATA_MAX_NAME_LEN + 1];
120
121     if (prefix == NULL)
122         prefix = "";
123
124     if (postfix == NULL)
125         postfix = "";
126
127     copy_escape_part (n_host, vl->host,
128             sizeof (n_host), escape_char);
129     copy_escape_part (n_plugin, vl->plugin,
130             sizeof (n_plugin), escape_char);
131     copy_escape_part (n_plugin_instance, vl->plugin_instance,
132             sizeof (n_plugin_instance), escape_char);
133     copy_escape_part (n_type, vl->type,
134             sizeof (n_type), escape_char);
135     copy_escape_part (n_type_instance, vl->type_instance,
136             sizeof (n_type_instance), escape_char);
137
138     if (n_plugin_instance[0] != '\0')
139         ssnprintf (tmp_plugin, sizeof (tmp_plugin), "%s%c%s",
140             n_plugin,
141             '-',
142             n_plugin_instance);
143     else
144         sstrncpy (tmp_plugin, n_plugin, sizeof (tmp_plugin));
145
146     if (n_type_instance[0] != '\0')
147         ssnprintf (tmp_type, sizeof (tmp_type), "%s%c%s",
148             n_type,
149             '-',
150             n_type_instance);
151     else
152         sstrncpy (tmp_type, n_type, sizeof (tmp_type));
153
154     if (ds_name != NULL)
155         ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
156             prefix, n_host, postfix, tmp_plugin, tmp_type, ds_name);
157     else
158         ssnprintf (ret, ret_len, "%s%s%s.%s.%s",
159             prefix, n_host, postfix, tmp_plugin, tmp_type);
160
161     return (0);
162 }
163
164 int format_graphite (char *buffer, size_t buffer_size,
165     const data_set_t *ds, const value_list_t *vl, char *prefix,
166     char *postfix, char escape_char)
167 {
168     int status = 0;
169     int i;
170     int buffer_pos = 0;
171
172     for (i = 0; i < ds->ds_num; i++)
173     {
174         const char *ds_name = NULL;
175         char        key[10*DATA_MAX_NAME_LEN];
176         char        values[512];
177         size_t      message_len;
178         char        message[1024];
179
180         ds_name = ds->ds[i].name;
181
182         /* Copy the identifier to `key' and escape it. */
183         status = gr_format_name (key, sizeof (key), vl, ds_name,
184                     prefix, postfix, escape_char);
185         if (status != 0)
186         {
187             ERROR ("amqp plugin: error with gr_format_name");
188             return (status);
189         }
190
191         escape_string (key, sizeof (key));
192         /* Convert the values to an ASCII representation and put that into
193          * `values'. */
194         status = gr_format_values (values, sizeof (values), i, ds, vl);
195         if (status != 0)
196         {
197             ERROR ("format_graphite: error with gr_format_values");
198             return (status);
199         }
200
201         /* Compute the graphite command */
202         message_len = (size_t) ssnprintf (message, sizeof (message),
203                 "%s %s %u\r\n",
204                 key,
205                 values,
206                 (unsigned int) CDTIME_T_TO_TIME_T (vl->time));
207         if (message_len >= sizeof (message)) {
208             ERROR ("format_graphite: message buffer too small: "
209                     "Need %zu bytes.", message_len + 1);
210             return (-ENOMEM);
211         }
212
213         /* Append it in case we got multiple data set */
214         if ((buffer_pos + message_len) >= buffer_size)
215         {
216             ERROR ("format_graphite: target buffer too small");
217             return (-ENOMEM);
218         }
219         memcpy((void *) (buffer + buffer_pos), message, message_len);
220         buffer_pos += message_len;
221     }
222     return (status);
223 } /* int format_graphite */
224
225 /* vim: set sw=2 sts=2 et fdm=marker : */