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_format_json.h"
28 static int escape_string (char *buffer, size_t buffer_size, /* {{{ */
34 if ((buffer == NULL) || (string == NULL))
42 #define BUFFER_ADD(c) do { \
43 if (dst_pos >= (buffer_size - 1)) { \
44 buffer[buffer_size - 1] = 0; \
47 buffer[dst_pos] = (c); \
51 /* Escape special characters */
53 for (src_pos = 0; string[src_pos] != 0; src_pos++)
55 if ((string[src_pos] == '"')
56 || (string[src_pos] == '\\'))
59 BUFFER_ADD (string[src_pos]);
61 else if (string[src_pos] <= 0x001F)
64 BUFFER_ADD (string[src_pos]);
72 } /* }}} int buffer_add_string */
74 static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */
75 const data_set_t *ds, const value_list_t *vl)
80 memset (buffer, 0, buffer_size);
82 #define BUFFER_ADD(...) do { \
84 status = ssnprintf (buffer + offset, buffer_size - offset, \
88 else if (((size_t) status) >= (buffer_size - offset)) \
91 offset += ((size_t) status); \
95 for (i = 0; i < ds->ds_num; i++)
100 if (ds->ds[i].type == DS_TYPE_GAUGE)
101 BUFFER_ADD ("%g", vl->values[i].gauge);
102 else if (ds->ds[i].type == DS_TYPE_COUNTER)
103 BUFFER_ADD ("%llu", vl->values[i].counter);
104 else if (ds->ds[i].type == DS_TYPE_DERIVE)
105 BUFFER_ADD ("%"PRIi64, vl->values[i].derive);
106 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
107 BUFFER_ADD ("%"PRIu64, vl->values[i].absolute);
110 ERROR ("format_json: Unknown data source type: %i",
114 } /* for ds->ds_num */
119 DEBUG ("format_json: values_to_json: buffer = %s;", buffer);
122 } /* }}} int values_to_json */
124 static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */
125 const data_set_t *ds, const value_list_t *vl)
131 memset (buffer, 0, buffer_size);
133 #define BUFFER_ADD(...) do { \
134 status = ssnprintf (buffer + offset, buffer_size - offset, \
138 else if (((size_t) status) >= (buffer_size - offset)) \
141 offset += ((size_t) status); \
144 /* All value lists have a leading comma. The first one will be replaced with
145 * a square bracket in `format_json_finalize'. */
148 status = values_to_json (temp, sizeof (temp), ds, vl);
151 BUFFER_ADD ("\"values\":%s", temp);
153 BUFFER_ADD (",\"time\":%lu", (unsigned long) vl->time);
154 BUFFER_ADD (",\"interval\":%i", vl->interval);
156 #define BUFFER_ADD_KEYVAL(key, value) do { \
157 status = escape_string (temp, sizeof (temp), (value)); \
160 BUFFER_ADD (",\"%s\":%s", (key), temp); \
163 BUFFER_ADD_KEYVAL ("host", vl->host);
164 BUFFER_ADD_KEYVAL ("plugin", vl->plugin);
165 BUFFER_ADD_KEYVAL ("plugin_instance", vl->plugin_instance);
166 BUFFER_ADD_KEYVAL ("type", vl->type);
167 BUFFER_ADD_KEYVAL ("type_instance", vl->type_instance);
171 #undef BUFFER_ADD_KEYVAL
174 DEBUG ("format_json: value_list_to_json: buffer = %s;", buffer);
177 } /* }}} int value_list_to_json */
179 static int format_json_value_list_nocheck (char *buffer, /* {{{ */
180 size_t *ret_buffer_fill, size_t *ret_buffer_free,
181 const data_set_t *ds, const value_list_t *vl,
184 char temp[temp_size];
187 status = value_list_to_json (temp, sizeof (temp), ds, vl);
190 temp_size = strlen (temp);
192 memcpy (buffer + (*ret_buffer_fill), temp, temp_size + 1);
193 (*ret_buffer_fill) += temp_size;
194 (*ret_buffer_free) -= temp_size;
197 } /* }}} int format_json_value_list_nocheck */
199 int format_json_initialize (char *buffer, /* {{{ */
200 size_t *ret_buffer_fill, size_t *ret_buffer_free)
205 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
208 buffer_fill = *ret_buffer_fill;
209 buffer_free = *ret_buffer_free;
211 buffer_free = buffer_fill + buffer_free;
217 memset (buffer, 0, buffer_free);
218 *ret_buffer_fill = buffer_fill;
219 *ret_buffer_free = buffer_free;
222 } /* }}} int format_json_initialize */
224 int format_json_finalize (char *buffer, /* {{{ */
225 size_t *ret_buffer_fill, size_t *ret_buffer_free)
229 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
232 if (*ret_buffer_free < 2)
235 /* Replace the leading comma added in `value_list_to_json' with a square
237 if (buffer[0] != ',')
241 pos = *ret_buffer_fill;
245 (*ret_buffer_fill)++;
246 (*ret_buffer_free)--;
249 } /* }}} int format_json_finalize */
251 int format_json_value_list (char *buffer, /* {{{ */
252 size_t *ret_buffer_fill, size_t *ret_buffer_free,
253 const data_set_t *ds, const value_list_t *vl)
256 || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL)
257 || (ds == NULL) || (vl == NULL))
260 if (*ret_buffer_free < 3)
263 return (format_json_value_list_nocheck (buffer,
264 ret_buffer_fill, ret_buffer_free, ds, vl,
265 (*ret_buffer_free) - 2));
266 } /* }}} int format_json_value_list */
268 /* vim: set sw=2 sts=2 et fdm=marker : */