2 * collectd - src/utils_format_json.c
3 * Copyright (C) 2009 Florian octo Forster
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 * Florian octo Forster <octo at collectd.org>
31 #include "utils_cache.h"
32 #include "utils_format_json.h"
34 static int json_escape_string (char *buffer, size_t buffer_size, /* {{{ */
40 if ((buffer == NULL) || (string == NULL))
48 #define BUFFER_ADD(c) do { \
49 if (dst_pos >= (buffer_size - 1)) { \
50 buffer[buffer_size - 1] = 0; \
53 buffer[dst_pos] = (c); \
57 /* Escape special characters */
59 for (src_pos = 0; string[src_pos] != 0; src_pos++)
61 if ((string[src_pos] == '"')
62 || (string[src_pos] == '\\'))
65 BUFFER_ADD (string[src_pos]);
67 else if (string[src_pos] <= 0x001F)
70 BUFFER_ADD (string[src_pos]);
78 } /* }}} int json_escape_string */
80 static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */
81 const data_set_t *ds, const value_list_t *vl, int store_rates)
85 gauge_t *rates = NULL;
87 memset (buffer, 0, buffer_size);
89 #define BUFFER_ADD(...) do { \
91 status = ssnprintf (buffer + offset, buffer_size - offset, \
98 else if (((size_t) status) >= (buffer_size - offset)) \
104 offset += ((size_t) status); \
108 for (i = 0; i < ds->ds_num; i++)
113 if (ds->ds[i].type == DS_TYPE_GAUGE)
115 if(isfinite (vl->values[i].gauge))
116 BUFFER_ADD (JSON_GAUGE_FORMAT, vl->values[i].gauge);
120 else if (store_rates)
123 rates = uc_get_rate (ds, vl);
126 WARNING ("utils_format_json: uc_get_rate failed.");
131 if(isfinite (rates[i]))
132 BUFFER_ADD (JSON_GAUGE_FORMAT, rates[i]);
136 else if (ds->ds[i].type == DS_TYPE_COUNTER)
137 BUFFER_ADD ("%llu", vl->values[i].counter);
138 else if (ds->ds[i].type == DS_TYPE_DERIVE)
139 BUFFER_ADD ("%"PRIi64, vl->values[i].derive);
140 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
141 BUFFER_ADD ("%"PRIu64, vl->values[i].absolute);
144 ERROR ("format_json: Unknown data source type: %i",
149 } /* for ds->ds_num */
154 DEBUG ("format_json: values_to_json: buffer = %s;", buffer);
157 } /* }}} int values_to_json */
159 static int dstypes_to_json (char *buffer, size_t buffer_size, /* {{{ */
160 const data_set_t *ds)
165 memset (buffer, 0, buffer_size);
167 #define BUFFER_ADD(...) do { \
169 status = ssnprintf (buffer + offset, buffer_size - offset, \
173 else if (((size_t) status) >= (buffer_size - offset)) \
176 offset += ((size_t) status); \
180 for (i = 0; i < ds->ds_num; i++)
185 BUFFER_ADD ("\"%s\"", DS_TYPE_TO_STRING (ds->ds[i].type));
186 } /* for ds->ds_num */
191 DEBUG ("format_json: dstypes_to_json: buffer = %s;", buffer);
194 } /* }}} int dstypes_to_json */
196 static int dsnames_to_json (char *buffer, size_t buffer_size, /* {{{ */
197 const data_set_t *ds)
202 memset (buffer, 0, buffer_size);
204 #define BUFFER_ADD(...) do { \
206 status = ssnprintf (buffer + offset, buffer_size - offset, \
210 else if (((size_t) status) >= (buffer_size - offset)) \
213 offset += ((size_t) status); \
217 for (i = 0; i < ds->ds_num; i++)
222 BUFFER_ADD ("\"%s\"", ds->ds[i].name);
223 } /* for ds->ds_num */
228 DEBUG ("format_json: dsnames_to_json: buffer = %s;", buffer);
231 } /* }}} int dsnames_to_json */
233 static int meta_data_keys_to_json (char *buffer, size_t buffer_size, /* {{{ */
234 meta_data_t *meta, char **keys, size_t keys_num)
242 #define BUFFER_ADD(...) do { \
243 status = ssnprintf (buffer + offset, buffer_size - offset, \
247 else if (((size_t) status) >= (buffer_size - offset)) \
250 offset += ((size_t) status); \
253 for (i = 0; i < keys_num; ++i)
258 type = meta_data_type (meta, key);
259 if (type == MD_TYPE_STRING)
262 if (meta_data_get_string (meta, key, &value) == 0)
266 status = json_escape_string (temp, sizeof (temp), value);
271 BUFFER_ADD (",\"%s\":%s", key, temp);
274 else if (type == MD_TYPE_SIGNED_INT)
277 if (meta_data_get_signed_int (meta, key, &value) == 0)
278 BUFFER_ADD (",\"%s\":%"PRIi64, key, value);
280 else if (type == MD_TYPE_UNSIGNED_INT)
283 if (meta_data_get_unsigned_int (meta, key, &value) == 0)
284 BUFFER_ADD (",\"%s\":%"PRIu64, key, value);
286 else if (type == MD_TYPE_DOUBLE)
289 if (meta_data_get_double (meta, key, &value) == 0)
290 BUFFER_ADD (",\"%s\":%f", key, value);
292 else if (type == MD_TYPE_BOOLEAN)
295 if (meta_data_get_boolean (meta, key, &value) == 0)
296 BUFFER_ADD (",\"%s\":%s", key, value ? "true" : "false");
303 buffer[0] = '{'; /* replace leading ',' */
309 } /* }}} int meta_data_keys_to_json */
311 static int meta_data_to_json (char *buffer, size_t buffer_size, /* {{{ */
319 if ((buffer == NULL) || (buffer_size == 0) || (meta == NULL))
322 status = meta_data_toc (meta, &keys);
325 keys_num = (size_t) status;
327 status = meta_data_keys_to_json (buffer, buffer_size, meta, keys, keys_num);
329 for (i = 0; i < keys_num; ++i)
334 } /* }}} int meta_data_to_json */
336 static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */
337 const data_set_t *ds, const value_list_t *vl, int store_rates)
343 memset (buffer, 0, buffer_size);
345 #define BUFFER_ADD(...) do { \
346 status = ssnprintf (buffer + offset, buffer_size - offset, \
350 else if (((size_t) status) >= (buffer_size - offset)) \
353 offset += ((size_t) status); \
356 /* All value lists have a leading comma. The first one will be replaced with
357 * a square bracket in `format_json_finalize'. */
360 status = values_to_json (temp, sizeof (temp), ds, vl, store_rates);
363 BUFFER_ADD ("\"values\":%s", temp);
365 status = dstypes_to_json (temp, sizeof (temp), ds);
368 BUFFER_ADD (",\"dstypes\":%s", temp);
370 status = dsnames_to_json (temp, sizeof (temp), ds);
373 BUFFER_ADD (",\"dsnames\":%s", temp);
375 BUFFER_ADD (",\"time\":%.3f", CDTIME_T_TO_DOUBLE (vl->time));
376 BUFFER_ADD (",\"interval\":%.3f", CDTIME_T_TO_DOUBLE (vl->interval));
378 #define BUFFER_ADD_KEYVAL(key, value) do { \
379 status = json_escape_string (temp, sizeof (temp), (value)); \
382 BUFFER_ADD (",\"%s\":%s", (key), temp); \
385 BUFFER_ADD_KEYVAL ("host", vl->host);
386 BUFFER_ADD_KEYVAL ("plugin", vl->plugin);
387 BUFFER_ADD_KEYVAL ("plugin_instance", vl->plugin_instance);
388 BUFFER_ADD_KEYVAL ("type", vl->type);
389 BUFFER_ADD_KEYVAL ("type_instance", vl->type_instance);
391 if (vl->meta != NULL)
393 char meta_buffer[buffer_size];
394 memset (meta_buffer, 0, sizeof (meta_buffer));
395 status = meta_data_to_json (meta_buffer, sizeof (meta_buffer), vl->meta);
399 BUFFER_ADD (",\"meta\":%s", meta_buffer);
400 } /* if (vl->meta != NULL) */
404 #undef BUFFER_ADD_KEYVAL
407 DEBUG ("format_json: value_list_to_json: buffer = %s;", buffer);
410 } /* }}} int value_list_to_json */
412 static int format_json_value_list_nocheck (char *buffer, /* {{{ */
413 size_t *ret_buffer_fill, size_t *ret_buffer_free,
414 const data_set_t *ds, const value_list_t *vl,
415 int store_rates, size_t temp_size)
417 char temp[temp_size];
420 status = value_list_to_json (temp, sizeof (temp), ds, vl, store_rates);
423 temp_size = strlen (temp);
425 memcpy (buffer + (*ret_buffer_fill), temp, temp_size + 1);
426 (*ret_buffer_fill) += temp_size;
427 (*ret_buffer_free) -= temp_size;
430 } /* }}} int format_json_value_list_nocheck */
432 int format_json_initialize (char *buffer, /* {{{ */
433 size_t *ret_buffer_fill, size_t *ret_buffer_free)
438 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
441 buffer_fill = *ret_buffer_fill;
442 buffer_free = *ret_buffer_free;
444 buffer_free = buffer_fill + buffer_free;
450 memset (buffer, 0, buffer_free);
451 *ret_buffer_fill = buffer_fill;
452 *ret_buffer_free = buffer_free;
455 } /* }}} int format_json_initialize */
457 int format_json_finalize (char *buffer, /* {{{ */
458 size_t *ret_buffer_fill, size_t *ret_buffer_free)
462 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
465 if (*ret_buffer_free < 2)
468 /* Replace the leading comma added in `value_list_to_json' with a square
470 if (buffer[0] != ',')
474 pos = *ret_buffer_fill;
478 (*ret_buffer_fill)++;
479 (*ret_buffer_free)--;
482 } /* }}} int format_json_finalize */
484 int format_json_value_list (char *buffer, /* {{{ */
485 size_t *ret_buffer_fill, size_t *ret_buffer_free,
486 const data_set_t *ds, const value_list_t *vl, int store_rates)
489 || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL)
490 || (ds == NULL) || (vl == NULL))
493 if (*ret_buffer_free < 3)
496 return (format_json_value_list_nocheck (buffer,
497 ret_buffer_fill, ret_buffer_free, ds, vl,
498 store_rates, (*ret_buffer_free) - 2));
499 } /* }}} int format_json_value_list */
501 /* vim: set sw=2 sts=2 et fdm=marker : */