2 * collectd - src/utils_format_json.c
3 * Copyright (C) 2009 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
26 #include "utils_cache.h"
27 #include "utils_format_json.h"
29 static int escape_string (char *buffer, size_t buffer_size, /* {{{ */
35 if ((buffer == NULL) || (string == NULL))
43 #define BUFFER_ADD(c) do { \
44 if (dst_pos >= (buffer_size - 1)) { \
45 buffer[buffer_size - 1] = 0; \
48 buffer[dst_pos] = (c); \
52 /* Escape special characters */
54 for (src_pos = 0; string[src_pos] != 0; src_pos++)
56 if ((string[src_pos] == '"')
57 || (string[src_pos] == '\\'))
60 BUFFER_ADD (string[src_pos]);
62 else if (string[src_pos] <= 0x001F)
65 BUFFER_ADD (string[src_pos]);
73 } /* }}} int escape_string */
75 static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */
76 const data_set_t *ds, const value_list_t *vl, int store_rates)
80 gauge_t *rates = NULL;
82 memset (buffer, 0, buffer_size);
84 #define BUFFER_ADD(...) do { \
86 status = ssnprintf (buffer + offset, buffer_size - offset, \
93 else if (((size_t) status) >= (buffer_size - offset)) \
99 offset += ((size_t) status); \
103 for (i = 0; i < ds->ds_num; i++)
108 if (ds->ds[i].type == DS_TYPE_GAUGE)
110 if(isfinite (vl->values[i].gauge))
111 BUFFER_ADD (JSON_GAUGE_FORMAT, vl->values[i].gauge);
115 else if (store_rates)
118 rates = uc_get_rate (ds, vl);
121 WARNING ("utils_format_json: uc_get_rate failed.");
126 if(isfinite (rates[i]))
127 BUFFER_ADD (JSON_GAUGE_FORMAT, rates[i]);
131 else if (ds->ds[i].type == DS_TYPE_COUNTER)
132 BUFFER_ADD ("%llu", vl->values[i].counter);
133 else if (ds->ds[i].type == DS_TYPE_DERIVE)
134 BUFFER_ADD ("%"PRIi64, vl->values[i].derive);
135 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
136 BUFFER_ADD ("%"PRIu64, vl->values[i].absolute);
139 ERROR ("format_json: Unknown data source type: %i",
144 } /* for ds->ds_num */
149 DEBUG ("format_json: values_to_json: buffer = %s;", buffer);
152 } /* }}} int values_to_json */
154 static int dstypes_to_json (char *buffer, size_t buffer_size, /* {{{ */
155 const data_set_t *ds)
160 memset (buffer, 0, buffer_size);
162 #define BUFFER_ADD(...) do { \
164 status = ssnprintf (buffer + offset, buffer_size - offset, \
168 else if (((size_t) status) >= (buffer_size - offset)) \
171 offset += ((size_t) status); \
175 for (i = 0; i < ds->ds_num; i++)
180 BUFFER_ADD ("\"%s\"", DS_TYPE_TO_STRING (ds->ds[i].type));
181 } /* for ds->ds_num */
186 DEBUG ("format_json: dstypes_to_json: buffer = %s;", buffer);
189 } /* }}} int dstypes_to_json */
191 static int dsnames_to_json (char *buffer, size_t buffer_size, /* {{{ */
192 const data_set_t *ds)
197 memset (buffer, 0, buffer_size);
199 #define BUFFER_ADD(...) do { \
201 status = ssnprintf (buffer + offset, buffer_size - offset, \
205 else if (((size_t) status) >= (buffer_size - offset)) \
208 offset += ((size_t) status); \
212 for (i = 0; i < ds->ds_num; i++)
217 BUFFER_ADD ("\"%s\"", ds->ds[i].name);
218 } /* for ds->ds_num */
223 DEBUG ("format_json: dsnames_to_json: buffer = %s;", buffer);
226 } /* }}} int dsnames_to_json */
228 static int meta_data_keys_to_json (char *buffer, size_t buffer_size, /* {{{ */
229 meta_data_t *meta, char **keys, size_t keys_num)
237 #define BUFFER_ADD(...) do { \
238 status = ssnprintf (buffer + offset, buffer_size - offset, \
242 else if (((size_t) status) >= (buffer_size - offset)) \
245 offset += ((size_t) status); \
248 for (i = 0; i < keys_num; ++i)
253 type = meta_data_type (meta, key);
254 if (type == MD_TYPE_STRING)
257 if (meta_data_get_string (meta, key, &value) == 0)
260 escape_string (temp, sizeof (temp), value);
262 BUFFER_ADD (",\"%s\":%s", key, temp);
265 else if (type == MD_TYPE_SIGNED_INT)
268 if (meta_data_get_signed_int (meta, key, &value) == 0)
269 BUFFER_ADD (",\"%s\":%"PRIi64, key, value);
271 else if (type == MD_TYPE_UNSIGNED_INT)
274 if (meta_data_get_unsigned_int (meta, key, &value) == 0)
275 BUFFER_ADD (",\"%s\":%"PRIu64, key, value);
277 else if (type == MD_TYPE_DOUBLE)
280 if (meta_data_get_double (meta, key, &value) == 0)
281 BUFFER_ADD (",\"%s\":%f", key, value);
283 else if (type == MD_TYPE_BOOLEAN)
286 if (meta_data_get_boolean (meta, key, &value) == 0)
287 BUFFER_ADD (",\"%s\":%s", key, value ? "true" : "false");
294 buffer[0] = '{'; /* replace leading ',' */
300 } /* }}} int meta_data_keys_to_json */
302 static int meta_data_to_json (char *buffer, size_t buffer_size, /* {{{ */
310 if ((buffer == NULL) || (buffer_size == 0) || (meta == NULL))
313 status = meta_data_toc (meta, &keys);
316 keys_num = (size_t) status;
318 status = meta_data_keys_to_json (buffer, buffer_size, meta, keys, keys_num);
320 for (i = 0; i < keys_num; ++i)
325 } /* }}} int meta_data_to_json */
327 static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */
328 const data_set_t *ds, const value_list_t *vl, int store_rates)
334 memset (buffer, 0, buffer_size);
336 #define BUFFER_ADD(...) do { \
337 status = ssnprintf (buffer + offset, buffer_size - offset, \
341 else if (((size_t) status) >= (buffer_size - offset)) \
344 offset += ((size_t) status); \
347 /* All value lists have a leading comma. The first one will be replaced with
348 * a square bracket in `format_json_finalize'. */
351 status = values_to_json (temp, sizeof (temp), ds, vl, store_rates);
354 BUFFER_ADD ("\"values\":%s", temp);
356 status = dstypes_to_json (temp, sizeof (temp), ds);
359 BUFFER_ADD (",\"dstypes\":%s", temp);
361 status = dsnames_to_json (temp, sizeof (temp), ds);
364 BUFFER_ADD (",\"dsnames\":%s", temp);
366 BUFFER_ADD (",\"time\":%.3f", CDTIME_T_TO_DOUBLE (vl->time));
367 BUFFER_ADD (",\"interval\":%.3f", CDTIME_T_TO_DOUBLE (vl->interval));
369 #define BUFFER_ADD_KEYVAL(key, value) do { \
370 status = escape_string (temp, sizeof (temp), (value)); \
373 BUFFER_ADD (",\"%s\":%s", (key), temp); \
376 BUFFER_ADD_KEYVAL ("host", vl->host);
377 BUFFER_ADD_KEYVAL ("plugin", vl->plugin);
378 BUFFER_ADD_KEYVAL ("plugin_instance", vl->plugin_instance);
379 BUFFER_ADD_KEYVAL ("type", vl->type);
380 BUFFER_ADD_KEYVAL ("type_instance", vl->type_instance);
382 if (vl->meta != NULL)
384 char meta_buffer[buffer_size];
385 memset (meta_buffer, 0, sizeof (meta_buffer));
386 status = meta_data_to_json (meta_buffer, sizeof (meta_buffer), vl->meta);
390 BUFFER_ADD (",\"meta\":%s", meta_buffer);
391 } /* if (vl->meta != NULL) */
395 #undef BUFFER_ADD_KEYVAL
398 DEBUG ("format_json: value_list_to_json: buffer = %s;", buffer);
401 } /* }}} int value_list_to_json */
403 static int format_json_value_list_nocheck (char *buffer, /* {{{ */
404 size_t *ret_buffer_fill, size_t *ret_buffer_free,
405 const data_set_t *ds, const value_list_t *vl,
406 int store_rates, size_t temp_size)
408 char temp[temp_size];
411 status = value_list_to_json (temp, sizeof (temp), ds, vl, store_rates);
414 temp_size = strlen (temp);
416 memcpy (buffer + (*ret_buffer_fill), temp, temp_size + 1);
417 (*ret_buffer_fill) += temp_size;
418 (*ret_buffer_free) -= temp_size;
421 } /* }}} int format_json_value_list_nocheck */
423 int format_json_initialize (char *buffer, /* {{{ */
424 size_t *ret_buffer_fill, size_t *ret_buffer_free)
429 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
432 buffer_fill = *ret_buffer_fill;
433 buffer_free = *ret_buffer_free;
435 buffer_free = buffer_fill + buffer_free;
441 memset (buffer, 0, buffer_free);
442 *ret_buffer_fill = buffer_fill;
443 *ret_buffer_free = buffer_free;
446 } /* }}} int format_json_initialize */
448 int format_json_finalize (char *buffer, /* {{{ */
449 size_t *ret_buffer_fill, size_t *ret_buffer_free)
453 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
456 if (*ret_buffer_free < 2)
459 /* Replace the leading comma added in `value_list_to_json' with a square
461 if (buffer[0] != ',')
465 pos = *ret_buffer_fill;
469 (*ret_buffer_fill)++;
470 (*ret_buffer_free)--;
473 } /* }}} int format_json_finalize */
475 int format_json_value_list (char *buffer, /* {{{ */
476 size_t *ret_buffer_fill, size_t *ret_buffer_free,
477 const data_set_t *ds, const value_list_t *vl, int store_rates)
480 || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL)
481 || (ds == NULL) || (vl == NULL))
484 if (*ret_buffer_free < 3)
487 return (format_json_value_list_nocheck (buffer,
488 ret_buffer_fill, ret_buffer_free, ds, vl,
489 store_rates, (*ret_buffer_free) - 2));
490 } /* }}} int format_json_value_list */
492 /* vim: set sw=2 sts=2 et fdm=marker : */