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_to_json (char *buffer, size_t buffer_size, /* {{{ */
247 #define BUFFER_ADD(...) do { \
248 status = ssnprintf (buffer + offset, buffer_size - offset, \
252 else if (((size_t) status) >= (buffer_size - offset)) \
255 offset += ((size_t) status); \
258 keys_num = meta_data_toc (meta, &keys);
265 for (i = 0; i < keys_num; ++i)
270 type = meta_data_type (meta, key);
271 if (type == MD_TYPE_STRING)
274 if (meta_data_get_string (meta, key, &value) == 0)
277 json_escape_string (temp, sizeof (temp), value);
279 BUFFER_ADD (",\"%s\":%s", key, temp);
282 else if (type == MD_TYPE_SIGNED_INT)
285 if (meta_data_get_signed_int (meta, key, &value) == 0)
286 BUFFER_ADD (",\"%s\":%"PRIi64, key, value);
288 else if (type == MD_TYPE_UNSIGNED_INT)
291 if (meta_data_get_unsigned_int (meta, key, &value) == 0)
292 BUFFER_ADD (",\"%s\":%"PRIu64, key, value);
294 else if (type == MD_TYPE_DOUBLE)
297 if (meta_data_get_double (meta, key, &value) == 0)
298 BUFFER_ADD (",\"%s\":%f", key, value);
300 else if (type == MD_TYPE_BOOLEAN)
303 if (meta_data_get_boolean (meta, key, &value) == 0)
304 BUFFER_ADD (",\"%s\":%s", key, value ? "true" : "false");
314 buffer[0] = '{'; /* replace leading ',' */
320 } /* }}} int meta_data_to_json */
322 static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */
323 const data_set_t *ds, const value_list_t *vl, int store_rates)
329 memset (buffer, 0, buffer_size);
331 #define BUFFER_ADD(...) do { \
332 status = ssnprintf (buffer + offset, buffer_size - offset, \
336 else if (((size_t) status) >= (buffer_size - offset)) \
339 offset += ((size_t) status); \
342 /* All value lists have a leading comma. The first one will be replaced with
343 * a square bracket in `format_json_finalize'. */
346 status = values_to_json (temp, sizeof (temp), ds, vl, store_rates);
349 BUFFER_ADD ("\"values\":%s", temp);
351 status = dstypes_to_json (temp, sizeof (temp), ds);
354 BUFFER_ADD (",\"dstypes\":%s", temp);
356 status = dsnames_to_json (temp, sizeof (temp), ds);
359 BUFFER_ADD (",\"dsnames\":%s", temp);
361 BUFFER_ADD (",\"time\":%.3f", CDTIME_T_TO_DOUBLE (vl->time));
362 BUFFER_ADD (",\"interval\":%.3f", CDTIME_T_TO_DOUBLE (vl->interval));
364 #define BUFFER_ADD_KEYVAL(key, value) do { \
365 status = json_escape_string (temp, sizeof (temp), (value)); \
368 BUFFER_ADD (",\"%s\":%s", (key), temp); \
371 BUFFER_ADD_KEYVAL ("host", vl->host);
372 BUFFER_ADD_KEYVAL ("plugin", vl->plugin);
373 BUFFER_ADD_KEYVAL ("plugin_instance", vl->plugin_instance);
374 BUFFER_ADD_KEYVAL ("type", vl->type);
375 BUFFER_ADD_KEYVAL ("type_instance", vl->type_instance);
377 if (vl->meta != NULL)
379 char meta_buffer[buffer_size];
380 memset (meta_buffer, 0, sizeof (meta_buffer));
381 status = meta_data_to_json (meta_buffer, sizeof (meta_buffer), vl->meta);
385 BUFFER_ADD (",\"meta\":%s", meta_buffer);
386 } /* if (vl->meta != NULL) */
390 #undef BUFFER_ADD_KEYVAL
393 DEBUG ("format_json: value_list_to_json: buffer = %s;", buffer);
396 } /* }}} int value_list_to_json */
398 static int format_json_value_list_nocheck (char *buffer, /* {{{ */
399 size_t *ret_buffer_fill, size_t *ret_buffer_free,
400 const data_set_t *ds, const value_list_t *vl,
401 int store_rates, size_t temp_size)
403 char temp[temp_size];
406 status = value_list_to_json (temp, sizeof (temp), ds, vl, store_rates);
409 temp_size = strlen (temp);
411 memcpy (buffer + (*ret_buffer_fill), temp, temp_size + 1);
412 (*ret_buffer_fill) += temp_size;
413 (*ret_buffer_free) -= temp_size;
416 } /* }}} int format_json_value_list_nocheck */
418 int format_json_initialize (char *buffer, /* {{{ */
419 size_t *ret_buffer_fill, size_t *ret_buffer_free)
424 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
427 buffer_fill = *ret_buffer_fill;
428 buffer_free = *ret_buffer_free;
430 buffer_free = buffer_fill + buffer_free;
436 memset (buffer, 0, buffer_free);
437 *ret_buffer_fill = buffer_fill;
438 *ret_buffer_free = buffer_free;
441 } /* }}} int format_json_initialize */
443 int format_json_finalize (char *buffer, /* {{{ */
444 size_t *ret_buffer_fill, size_t *ret_buffer_free)
448 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
451 if (*ret_buffer_free < 2)
454 /* Replace the leading comma added in `value_list_to_json' with a square
456 if (buffer[0] != ',')
460 pos = *ret_buffer_fill;
464 (*ret_buffer_fill)++;
465 (*ret_buffer_free)--;
468 } /* }}} int format_json_finalize */
470 int format_json_value_list (char *buffer, /* {{{ */
471 size_t *ret_buffer_fill, size_t *ret_buffer_free,
472 const data_set_t *ds, const value_list_t *vl, int store_rates)
475 || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL)
476 || (ds == NULL) || (vl == NULL))
479 if (*ret_buffer_free < 3)
482 return (format_json_value_list_nocheck (buffer,
483 ret_buffer_fill, ret_buffer_free, ds, vl,
484 store_rates, (*ret_buffer_free) - 2));
485 } /* }}} int format_json_value_list */
487 /* vim: set sw=2 sts=2 et fdm=marker : */