Merge pull request #2796 from elfiesmelfie/feat_ipmi_SEL_ignore_list
[collectd.git] / src / utils_format_kairosdb.c
1 /**
2  * collectd - src/utils_format_kairosdb.c
3  * Copyright (C) 2016       Aurelien beorn Rougemont
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Aurelien beorn Rougemont <beorn at gandi dot net>
25  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 #include "utils_cache.h"
33 #include "utils_format_kairosdb.h"
34
35 /* This is the KAIROSDB format for write_http output
36  *
37  * Target format
38  * [
39  *   {
40  *     "name":"collectd.vmem"
41  *     "datapoints":
42  *       [
43  *         [1453897164060, 97.000000]
44  *       ],
45  *      "tags":
46  *        {
47  *          "host": "fqdn.domain.tld",
48  *          "plugin_instance": "vmpage_number",
49  *          "type": "kernel_stack",
50  *          "ds": "value"
51  *          ""
52  *        }
53  *   }
54  * ]
55  */
56
57 static int kairosdb_escape_string(char *buffer, size_t buffer_size, /* {{{ */
58                                   const char *string) {
59   size_t dst_pos;
60
61   if ((buffer == NULL) || (string == NULL))
62     return -EINVAL;
63
64   if (buffer_size < 3)
65     return -ENOMEM;
66
67   dst_pos = 0;
68
69 #define BUFFER_ADD(c)                                                          \
70   do {                                                                         \
71     if (dst_pos >= (buffer_size - 1)) {                                        \
72       buffer[buffer_size - 1] = 0;                                             \
73       return -ENOMEM;                                                          \
74     }                                                                          \
75     buffer[dst_pos] = (c);                                                     \
76     dst_pos++;                                                                 \
77   } while (0)
78
79   /* Escape special characters */
80   /* authorize -_. and alpha num but also escapes " */
81   BUFFER_ADD('"');
82   for (size_t src_pos = 0; string[src_pos] != 0; src_pos++) {
83     if (isalnum(string[src_pos]) || 0x2d == string[src_pos] ||
84         0x2e == string[src_pos] || 0x5f == string[src_pos])
85       BUFFER_ADD(tolower(string[src_pos]));
86   } /* for */
87   BUFFER_ADD('"');
88   buffer[dst_pos] = 0;
89
90 #undef BUFFER_ADD
91
92   return 0;
93 } /* }}} int kairosdb_escape_string */
94
95 static int values_to_kairosdb(char *buffer, size_t buffer_size, /* {{{ */
96                               const data_set_t *ds, const value_list_t *vl,
97                               int store_rates, size_t ds_idx) {
98   size_t offset = 0;
99   gauge_t *rates = NULL;
100
101   memset(buffer, 0, buffer_size);
102
103 #define BUFFER_ADD(...)                                                        \
104   do {                                                                         \
105     int status;                                                                \
106     status = snprintf(buffer + offset, buffer_size - offset, __VA_ARGS__);     \
107     if (status < 1) {                                                          \
108       sfree(rates);                                                            \
109       return -1;                                                               \
110     } else if (((size_t)status) >= (buffer_size - offset)) {                   \
111       sfree(rates);                                                            \
112       return -ENOMEM;                                                          \
113     } else                                                                     \
114       offset += ((size_t)status);                                              \
115   } while (0)
116
117   if (ds->ds[ds_idx].type == DS_TYPE_GAUGE) {
118     if (isfinite(vl->values[ds_idx].gauge)) {
119       BUFFER_ADD("[[");
120       BUFFER_ADD("%" PRIu64, CDTIME_T_TO_MS(vl->time));
121       BUFFER_ADD(",");
122       BUFFER_ADD(JSON_GAUGE_FORMAT, vl->values[ds_idx].gauge);
123     } else {
124       DEBUG("utils_format_kairosdb: invalid vl->values[ds_idx].gauge for "
125             "%s|%s|%s|%s|%s",
126             vl->plugin, vl->plugin_instance, vl->type, vl->type_instance,
127             ds->ds[ds_idx].name);
128       return -1;
129     }
130   } else if (store_rates) {
131     if (rates == NULL)
132       rates = uc_get_rate(ds, vl);
133     if (rates == NULL) {
134       WARNING("utils_format_kairosdb: uc_get_rate failed for %s|%s|%s|%s|%s",
135               vl->plugin, vl->plugin_instance, vl->type, vl->type_instance,
136               ds->ds[ds_idx].name);
137
138       return -1;
139     }
140
141     if (isfinite(rates[ds_idx])) {
142       BUFFER_ADD("[[");
143       BUFFER_ADD("%" PRIu64, CDTIME_T_TO_MS(vl->time));
144       BUFFER_ADD(",");
145       BUFFER_ADD(JSON_GAUGE_FORMAT, rates[ds_idx]);
146     } else {
147       WARNING("utils_format_kairosdb: invalid rates[ds_idx] for %s|%s|%s|%s|%s",
148               vl->plugin, vl->plugin_instance, vl->type, vl->type_instance,
149               ds->ds[ds_idx].name);
150       sfree(rates);
151       return -1;
152     }
153   } else if (ds->ds[ds_idx].type == DS_TYPE_COUNTER) {
154     BUFFER_ADD("[[");
155     BUFFER_ADD("%" PRIu64, CDTIME_T_TO_MS(vl->time));
156     BUFFER_ADD(",");
157     BUFFER_ADD("%" PRIu64, (uint64_t)vl->values[ds_idx].counter);
158   } else if (ds->ds[ds_idx].type == DS_TYPE_DERIVE) {
159     BUFFER_ADD("[[");
160     BUFFER_ADD("%" PRIu64, CDTIME_T_TO_MS(vl->time));
161     BUFFER_ADD(",");
162     BUFFER_ADD("%" PRIi64, vl->values[ds_idx].derive);
163   } else if (ds->ds[ds_idx].type == DS_TYPE_ABSOLUTE) {
164     BUFFER_ADD("[[");
165     BUFFER_ADD("%" PRIu64, CDTIME_T_TO_MS(vl->time));
166     BUFFER_ADD(",");
167     BUFFER_ADD("%" PRIu64, vl->values[ds_idx].absolute);
168   } else {
169     ERROR("format_kairosdb: Unknown data source type: %i", ds->ds[ds_idx].type);
170     sfree(rates);
171     return -1;
172   }
173   BUFFER_ADD("]]");
174
175 #undef BUFFER_ADD
176
177   DEBUG("format_kairosdb: values_to_kairosdb: buffer = %s;", buffer);
178   sfree(rates);
179   return 0;
180 } /* }}} int values_to_kairosdb */
181
182 static int value_list_to_kairosdb(char *buffer, size_t buffer_size, /* {{{ */
183                                   const data_set_t *ds, const value_list_t *vl,
184                                   int store_rates,
185                                   char const *const *http_attrs,
186                                   size_t http_attrs_num, int data_ttl,
187                                   char const *metrics_prefix) {
188   char temp[512];
189   size_t offset = 0;
190   int status;
191
192   memset(buffer, 0, buffer_size);
193
194 #define BUFFER_ADD(...)                                                        \
195   do {                                                                         \
196     status = snprintf(buffer + offset, buffer_size - offset, __VA_ARGS__);     \
197     if (status < 1)                                                            \
198       return -1;                                                               \
199     else if (((size_t)status) >= (buffer_size - offset))                       \
200       return -ENOMEM;                                                          \
201     else                                                                       \
202       offset += ((size_t)status);                                              \
203   } while (0)
204
205 #define BUFFER_ADD_KEYVAL(key, value)                                          \
206   do {                                                                         \
207     status = kairosdb_escape_string(temp, sizeof(temp), (value));              \
208     if (status != 0)                                                           \
209       return status;                                                           \
210     BUFFER_ADD(",\"%s\": %s", (key), temp);                                    \
211   } while (0)
212
213   for (size_t i = 0; i < ds->ds_num; i++) {
214     /* All value lists have a leading comma. The first one will be replaced with
215      * a square bracket in `format_kairosdb_finalize'. */
216     BUFFER_ADD(",{\"name\":\"");
217
218     if (metrics_prefix != NULL) {
219       BUFFER_ADD("%s.", metrics_prefix);
220     }
221
222     BUFFER_ADD("%s", vl->plugin);
223
224     status = values_to_kairosdb(temp, sizeof(temp), ds, vl, store_rates, i);
225     if (status != 0)
226       return status;
227
228     BUFFER_ADD("\", \"datapoints\": %s", temp);
229
230     /*
231      * Now adds meta data to metric as tags
232      */
233
234     memset(temp, 0, sizeof(temp));
235
236     if (data_ttl != 0)
237       BUFFER_ADD(", \"ttl\": %i", data_ttl);
238
239     BUFFER_ADD(", \"tags\":\{");
240
241     BUFFER_ADD("\"host\": \"%s\"", vl->host);
242     for (size_t j = 0; j < http_attrs_num; j += 2) {
243       BUFFER_ADD(", \"%s\":", http_attrs[j]);
244       BUFFER_ADD(" \"%s\"", http_attrs[j + 1]);
245     }
246
247     if (strlen(vl->plugin_instance))
248       BUFFER_ADD_KEYVAL("plugin_instance", vl->plugin_instance);
249     BUFFER_ADD_KEYVAL("type", vl->type);
250     if (strlen(vl->type_instance))
251       BUFFER_ADD_KEYVAL("type_instance", vl->type_instance);
252     if (ds->ds_num != 1)
253       BUFFER_ADD_KEYVAL("ds", ds->ds[i].name);
254     BUFFER_ADD("}}");
255   } /* for ds->ds_num */
256
257 #undef BUFFER_ADD_KEYVAL
258 #undef BUFFER_ADD
259
260   DEBUG("format_kairosdb: value_list_to_kairosdb: buffer = %s;", buffer);
261
262   return 0;
263 } /* }}} int value_list_to_kairosdb */
264
265 static int format_kairosdb_value_list_nocheck(
266     char *buffer, /* {{{ */
267     size_t *ret_buffer_fill, size_t *ret_buffer_free, const data_set_t *ds,
268     const value_list_t *vl, int store_rates, size_t temp_size,
269     char const *const *http_attrs, size_t http_attrs_num, int data_ttl,
270     char const *metrics_prefix) {
271   char temp[temp_size];
272   int status;
273
274   status = value_list_to_kairosdb(temp, sizeof(temp), ds, vl, store_rates,
275                                   http_attrs, http_attrs_num, data_ttl,
276                                   metrics_prefix);
277   if (status != 0)
278     return status;
279   temp_size = strlen(temp);
280
281   memcpy(buffer + (*ret_buffer_fill), temp, temp_size + 1);
282   (*ret_buffer_fill) += temp_size;
283   (*ret_buffer_free) -= temp_size;
284
285   return 0;
286 } /* }}} int format_kairosdb_value_list_nocheck */
287
288 int format_kairosdb_initialize(char *buffer, /* {{{ */
289                                size_t *ret_buffer_fill,
290                                size_t *ret_buffer_free) {
291   size_t buffer_fill;
292   size_t buffer_free;
293
294   if ((buffer == NULL) || (ret_buffer_fill == NULL) ||
295       (ret_buffer_free == NULL))
296     return -EINVAL;
297
298   buffer_fill = *ret_buffer_fill;
299   buffer_free = *ret_buffer_free;
300
301   buffer_free = buffer_fill + buffer_free;
302   buffer_fill = 0;
303
304   if (buffer_free < 3)
305     return -ENOMEM;
306
307   memset(buffer, 0, buffer_free);
308   *ret_buffer_fill = buffer_fill;
309   *ret_buffer_free = buffer_free;
310
311   return 0;
312 } /* }}} int format_kairosdb_initialize */
313
314 int format_kairosdb_finalize(char *buffer, /* {{{ */
315                              size_t *ret_buffer_fill, size_t *ret_buffer_free) {
316   size_t pos;
317
318   if ((buffer == NULL) || (ret_buffer_fill == NULL) ||
319       (ret_buffer_free == NULL))
320     return -EINVAL;
321
322   if (*ret_buffer_free < 2)
323     return -ENOMEM;
324
325   /* Replace the leading comma added in `value_list_to_kairosdb' with a square
326    * bracket. */
327   if (buffer[0] != ',')
328     return -EINVAL;
329   buffer[0] = '[';
330
331   pos = *ret_buffer_fill;
332   buffer[pos] = ']';
333   buffer[pos + 1] = 0;
334
335   (*ret_buffer_fill)++;
336   (*ret_buffer_free)--;
337
338   return 0;
339 } /* }}} int format_kairosdb_finalize */
340
341 int format_kairosdb_value_list(char *buffer, /* {{{ */
342                                size_t *ret_buffer_fill, size_t *ret_buffer_free,
343                                const data_set_t *ds, const value_list_t *vl,
344                                int store_rates, char const *const *http_attrs,
345                                size_t http_attrs_num, int data_ttl,
346                                char const *metrics_prefix) {
347   if ((buffer == NULL) || (ret_buffer_fill == NULL) ||
348       (ret_buffer_free == NULL) || (ds == NULL) || (vl == NULL))
349     return -EINVAL;
350
351   if (*ret_buffer_free < 3)
352     return -ENOMEM;
353
354   return format_kairosdb_value_list_nocheck(
355       buffer, ret_buffer_fill, ret_buffer_free, ds, vl, store_rates,
356       (*ret_buffer_free) - 2, http_attrs, http_attrs_num, data_ttl,
357       metrics_prefix);
358 } /* }}} int format_kairosdb_value_list */
359
360 /* vim: set sw=2 sts=2 et fdm=marker : */