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 buffer_add_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 value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */
155 const data_set_t *ds, const value_list_t *vl, int store_rates)
161 memset (buffer, 0, buffer_size);
163 #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); \
174 /* All value lists have a leading comma. The first one will be replaced with
175 * a square bracket in `format_json_finalize'. */
178 status = values_to_json (temp, sizeof (temp), ds, vl, store_rates);
181 BUFFER_ADD ("\"values\":%s", temp);
183 BUFFER_ADD (",\"time\":%lu", (unsigned long) vl->time);
184 BUFFER_ADD (",\"interval\":%i", vl->interval);
186 #define BUFFER_ADD_KEYVAL(key, value) do { \
187 status = escape_string (temp, sizeof (temp), (value)); \
190 BUFFER_ADD (",\"%s\":%s", (key), temp); \
193 BUFFER_ADD_KEYVAL ("host", vl->host);
194 BUFFER_ADD_KEYVAL ("plugin", vl->plugin);
195 BUFFER_ADD_KEYVAL ("plugin_instance", vl->plugin_instance);
196 BUFFER_ADD_KEYVAL ("type", vl->type);
197 BUFFER_ADD_KEYVAL ("type_instance", vl->type_instance);
201 #undef BUFFER_ADD_KEYVAL
204 DEBUG ("format_json: value_list_to_json: buffer = %s;", buffer);
207 } /* }}} int value_list_to_json */
209 static int format_json_value_list_nocheck (char *buffer, /* {{{ */
210 size_t *ret_buffer_fill, size_t *ret_buffer_free,
211 const data_set_t *ds, const value_list_t *vl,
212 int store_rates, size_t temp_size)
214 char temp[temp_size];
217 status = value_list_to_json (temp, sizeof (temp), ds, vl, store_rates);
220 temp_size = strlen (temp);
222 memcpy (buffer + (*ret_buffer_fill), temp, temp_size + 1);
223 (*ret_buffer_fill) += temp_size;
224 (*ret_buffer_free) -= temp_size;
227 } /* }}} int format_json_value_list_nocheck */
229 int format_json_initialize (char *buffer, /* {{{ */
230 size_t *ret_buffer_fill, size_t *ret_buffer_free)
235 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
238 buffer_fill = *ret_buffer_fill;
239 buffer_free = *ret_buffer_free;
241 buffer_free = buffer_fill + buffer_free;
247 memset (buffer, 0, buffer_free);
248 *ret_buffer_fill = buffer_fill;
249 *ret_buffer_free = buffer_free;
252 } /* }}} int format_json_initialize */
254 int format_json_finalize (char *buffer, /* {{{ */
255 size_t *ret_buffer_fill, size_t *ret_buffer_free)
259 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
262 if (*ret_buffer_free < 2)
265 /* Replace the leading comma added in `value_list_to_json' with a square
267 if (buffer[0] != ',')
271 pos = *ret_buffer_fill;
275 (*ret_buffer_fill)++;
276 (*ret_buffer_free)--;
279 } /* }}} int format_json_finalize */
281 int format_json_value_list (char *buffer, /* {{{ */
282 size_t *ret_buffer_fill, size_t *ret_buffer_free,
283 const data_set_t *ds, const value_list_t *vl, int store_rates)
286 || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL)
287 || (ds == NULL) || (vl == NULL))
290 if (*ret_buffer_free < 3)
293 return (format_json_value_list_nocheck (buffer,
294 ret_buffer_fill, ret_buffer_free, ds, vl,
295 store_rates, (*ret_buffer_free) - 2));
296 } /* }}} int format_json_value_list */
298 /* vim: set sw=2 sts=2 et fdm=marker : */