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 ("%g", 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 ("%g", 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_to_json (char *buffer, size_t buffer_size, /* {{{ */
237 memset (buffer, 0, buffer_size);
239 #define BUFFER_ADD(...) do { \
240 status = ssnprintf (buffer + offset, buffer_size - offset, \
244 else if (((size_t) status) >= (buffer_size - offset)) \
247 offset += ((size_t) status); \
250 keys_num = meta_data_toc (meta, &keys);
251 for (i = 0; i < keys_num; ++i)
256 type = meta_data_type (meta, key);
257 if (type == MD_TYPE_STRING)
260 if (meta_data_get_string (meta, key, &value) == 0)
263 escape_string (temp, sizeof (temp), value);
265 BUFFER_ADD (",\"%s\":%s", key, temp);
268 else if (type == MD_TYPE_SIGNED_INT)
271 if (meta_data_get_signed_int (meta, key, &value) == 0)
272 BUFFER_ADD (",\"%s\":%"PRIi64, key, value);
274 else if (type == MD_TYPE_UNSIGNED_INT)
277 if (meta_data_get_unsigned_int (meta, key, &value) == 0)
278 BUFFER_ADD (",\"%s\":%"PRIu64, key, value);
280 else if (type == MD_TYPE_DOUBLE)
283 if (meta_data_get_double (meta, key, &value) == 0)
284 BUFFER_ADD (",\"%s\":%f", key, value);
286 else if (type == MD_TYPE_BOOLEAN)
289 if (meta_data_get_boolean (meta, key, &value) == 0)
290 BUFFER_ADD (",\"%s\":%s", key, value ? "true" : "false");
300 buffer[0] = '{'; /* replace leading ',' */
306 } /* int meta_data_to_json */
308 static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */
309 const data_set_t *ds, const value_list_t *vl, int store_rates)
315 memset (buffer, 0, buffer_size);
317 #define BUFFER_ADD(...) do { \
318 status = ssnprintf (buffer + offset, buffer_size - offset, \
322 else if (((size_t) status) >= (buffer_size - offset)) \
325 offset += ((size_t) status); \
328 /* All value lists have a leading comma. The first one will be replaced with
329 * a square bracket in `format_json_finalize'. */
332 status = values_to_json (temp, sizeof (temp), ds, vl, store_rates);
335 BUFFER_ADD ("\"values\":%s", temp);
337 status = dstypes_to_json (temp, sizeof (temp), ds);
340 BUFFER_ADD (",\"dstypes\":%s", temp);
342 status = dsnames_to_json (temp, sizeof (temp), ds);
345 BUFFER_ADD (",\"dsnames\":%s", temp);
347 BUFFER_ADD (",\"time\":%.3f", CDTIME_T_TO_DOUBLE (vl->time));
348 BUFFER_ADD (",\"interval\":%.3f", CDTIME_T_TO_DOUBLE (vl->interval));
350 #define BUFFER_ADD_KEYVAL(key, value) do { \
351 status = escape_string (temp, sizeof (temp), (value)); \
354 BUFFER_ADD (",\"%s\":%s", (key), temp); \
357 BUFFER_ADD_KEYVAL ("host", vl->host);
358 BUFFER_ADD_KEYVAL ("plugin", vl->plugin);
359 BUFFER_ADD_KEYVAL ("plugin_instance", vl->plugin_instance);
360 BUFFER_ADD_KEYVAL ("type", vl->type);
361 BUFFER_ADD_KEYVAL ("type_instance", vl->type_instance);
363 if (vl->meta != NULL)
365 char meta_buffer[buffer_size];
366 memset (meta_buffer, 0, sizeof (meta_buffer));
367 status = meta_data_to_json (meta_buffer, sizeof (meta_buffer), vl->meta);
371 BUFFER_ADD (",\"meta\":%s", meta_buffer);
372 } /* if (vl->meta != NULL) */
376 #undef BUFFER_ADD_KEYVAL
379 DEBUG ("format_json: value_list_to_json: buffer = %s;", buffer);
382 } /* }}} int value_list_to_json */
384 static int format_json_value_list_nocheck (char *buffer, /* {{{ */
385 size_t *ret_buffer_fill, size_t *ret_buffer_free,
386 const data_set_t *ds, const value_list_t *vl,
387 int store_rates, size_t temp_size)
389 char temp[temp_size];
392 status = value_list_to_json (temp, sizeof (temp), ds, vl, store_rates);
395 temp_size = strlen (temp);
397 memcpy (buffer + (*ret_buffer_fill), temp, temp_size + 1);
398 (*ret_buffer_fill) += temp_size;
399 (*ret_buffer_free) -= temp_size;
402 } /* }}} int format_json_value_list_nocheck */
404 int format_json_initialize (char *buffer, /* {{{ */
405 size_t *ret_buffer_fill, size_t *ret_buffer_free)
410 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
413 buffer_fill = *ret_buffer_fill;
414 buffer_free = *ret_buffer_free;
416 buffer_free = buffer_fill + buffer_free;
422 memset (buffer, 0, buffer_free);
423 *ret_buffer_fill = buffer_fill;
424 *ret_buffer_free = buffer_free;
427 } /* }}} int format_json_initialize */
429 int format_json_finalize (char *buffer, /* {{{ */
430 size_t *ret_buffer_fill, size_t *ret_buffer_free)
434 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
437 if (*ret_buffer_free < 2)
440 /* Replace the leading comma added in `value_list_to_json' with a square
442 if (buffer[0] != ',')
446 pos = *ret_buffer_fill;
450 (*ret_buffer_fill)++;
451 (*ret_buffer_free)--;
454 } /* }}} int format_json_finalize */
456 int format_json_value_list (char *buffer, /* {{{ */
457 size_t *ret_buffer_fill, size_t *ret_buffer_free,
458 const data_set_t *ds, const value_list_t *vl, int store_rates)
461 || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL)
462 || (ds == NULL) || (vl == NULL))
465 if (*ret_buffer_free < 3)
468 return (format_json_value_list_nocheck (buffer,
469 ret_buffer_fill, ret_buffer_free, ds, vl,
470 store_rates, (*ret_buffer_free) - 2));
471 } /* }}} int format_json_value_list */
473 /* vim: set sw=2 sts=2 et fdm=marker : */