2 * collectd - src/utils_format_kairosdb.c
3 * Copyright (C) 2016 Aurelien beorn Rougemont
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Aurelien beorn Rougemont <beorn at gandi dot net>
32 #include "utils_cache.h"
33 #include "utils_format_kairosdb.h"
35 /* This is the KAIROSDB format for write_http output
40 * "name":"collectd.vmem"
43 * [1453897164060, 97.000000]
47 * "host": "fqdn.domain.tld",
48 * "plugin_instance": "vmpage_number",
49 * "type": "kernel_stack",
57 static int kairosdb_escape_string(char *buffer, size_t buffer_size, /* {{{ */
61 if ((buffer == NULL) || (string == NULL))
69 #define BUFFER_ADD(c) \
71 if (dst_pos >= (buffer_size - 1)) { \
72 buffer[buffer_size - 1] = 0; \
75 buffer[dst_pos] = (c); \
79 /* Escape special characters */
80 /* authorize -_. and alpha num but also escapes " */
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]));
93 } /* }}} int kairosdb_escape_string */
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) {
99 gauge_t *rates = NULL;
101 memset(buffer, 0, buffer_size);
103 #define BUFFER_ADD(...) \
106 status = snprintf(buffer + offset, buffer_size - offset, __VA_ARGS__); \
110 } else if (((size_t)status) >= (buffer_size - offset)) { \
114 offset += ((size_t)status); \
117 if (ds->ds[ds_idx].type == DS_TYPE_GAUGE) {
118 if (isfinite(vl->values[ds_idx].gauge)) {
120 BUFFER_ADD("%" PRIu64, CDTIME_T_TO_MS(vl->time));
122 BUFFER_ADD(JSON_GAUGE_FORMAT, vl->values[ds_idx].gauge);
124 DEBUG("utils_format_kairosdb: invalid vl->values[ds_idx].gauge for "
126 vl->plugin, vl->plugin_instance, vl->type, vl->type_instance,
127 ds->ds[ds_idx].name);
130 } else if (store_rates) {
132 rates = uc_get_rate(ds, vl);
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);
141 if (isfinite(rates[ds_idx])) {
143 BUFFER_ADD("%" PRIu64, CDTIME_T_TO_MS(vl->time));
145 BUFFER_ADD(JSON_GAUGE_FORMAT, rates[ds_idx]);
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);
153 } else if (ds->ds[ds_idx].type == DS_TYPE_COUNTER) {
155 BUFFER_ADD("%" PRIu64, CDTIME_T_TO_MS(vl->time));
157 BUFFER_ADD("%" PRIu64, (uint64_t)vl->values[ds_idx].counter);
158 } else if (ds->ds[ds_idx].type == DS_TYPE_DERIVE) {
160 BUFFER_ADD("%" PRIu64, CDTIME_T_TO_MS(vl->time));
162 BUFFER_ADD("%" PRIi64, vl->values[ds_idx].derive);
163 } else if (ds->ds[ds_idx].type == DS_TYPE_ABSOLUTE) {
165 BUFFER_ADD("%" PRIu64, CDTIME_T_TO_MS(vl->time));
167 BUFFER_ADD("%" PRIu64, vl->values[ds_idx].absolute);
169 ERROR("format_kairosdb: Unknown data source type: %i", ds->ds[ds_idx].type);
177 DEBUG("format_kairosdb: values_to_kairosdb: buffer = %s;", buffer);
180 } /* }}} int values_to_kairosdb */
182 static int value_list_to_kairosdb(char *buffer, size_t buffer_size, /* {{{ */
183 const data_set_t *ds, const value_list_t *vl,
185 char const *const *http_attrs,
186 size_t http_attrs_num, int data_ttl,
187 char const *metrics_prefix) {
192 memset(buffer, 0, buffer_size);
194 #define BUFFER_ADD(...) \
196 status = snprintf(buffer + offset, buffer_size - offset, __VA_ARGS__); \
199 else if (((size_t)status) >= (buffer_size - offset)) \
202 offset += ((size_t)status); \
205 #define BUFFER_ADD_KEYVAL(key, value) \
207 status = kairosdb_escape_string(temp, sizeof(temp), (value)); \
210 BUFFER_ADD(",\"%s\": %s", (key), temp); \
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\":\"");
218 if (metrics_prefix != NULL) {
219 BUFFER_ADD("%s.", metrics_prefix);
222 BUFFER_ADD("%s", vl->plugin);
224 status = values_to_kairosdb(temp, sizeof(temp), ds, vl, store_rates, i);
228 BUFFER_ADD("\", \"datapoints\": %s", temp);
231 * Now adds meta data to metric as tags
234 memset(temp, 0, sizeof(temp));
237 BUFFER_ADD(", \"ttl\": %i", data_ttl);
239 BUFFER_ADD(", \"tags\":\{");
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]);
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);
253 BUFFER_ADD_KEYVAL("ds", ds->ds[i].name);
255 } /* for ds->ds_num */
257 #undef BUFFER_ADD_KEYVAL
260 DEBUG("format_kairosdb: value_list_to_kairosdb: buffer = %s;", buffer);
263 } /* }}} int value_list_to_kairosdb */
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];
274 status = value_list_to_kairosdb(temp, sizeof(temp), ds, vl, store_rates,
275 http_attrs, http_attrs_num, data_ttl,
279 temp_size = strlen(temp);
281 memcpy(buffer + (*ret_buffer_fill), temp, temp_size + 1);
282 (*ret_buffer_fill) += temp_size;
283 (*ret_buffer_free) -= temp_size;
286 } /* }}} int format_kairosdb_value_list_nocheck */
288 int format_kairosdb_initialize(char *buffer, /* {{{ */
289 size_t *ret_buffer_fill,
290 size_t *ret_buffer_free) {
294 if ((buffer == NULL) || (ret_buffer_fill == NULL) ||
295 (ret_buffer_free == NULL))
298 buffer_fill = *ret_buffer_fill;
299 buffer_free = *ret_buffer_free;
301 buffer_free = buffer_fill + buffer_free;
307 memset(buffer, 0, buffer_free);
308 *ret_buffer_fill = buffer_fill;
309 *ret_buffer_free = buffer_free;
312 } /* }}} int format_kairosdb_initialize */
314 int format_kairosdb_finalize(char *buffer, /* {{{ */
315 size_t *ret_buffer_fill, size_t *ret_buffer_free) {
318 if ((buffer == NULL) || (ret_buffer_fill == NULL) ||
319 (ret_buffer_free == NULL))
322 if (*ret_buffer_free < 2)
325 /* Replace the leading comma added in `value_list_to_kairosdb' with a square
327 if (buffer[0] != ',')
331 pos = *ret_buffer_fill;
335 (*ret_buffer_fill)++;
336 (*ret_buffer_free)--;
339 } /* }}} int format_kairosdb_finalize */
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))
351 if (*ret_buffer_free < 3)
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,
358 } /* }}} int format_kairosdb_value_list */
360 /* vim: set sw=2 sts=2 et fdm=marker : */