1 /**
2 * collectd - src/utils_format_json.c
3 * Copyright (C) 2009 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
26 #include "utils_cache.h"
27 #include "utils_format_json.h"
29 static int escape_string (char *buffer, size_t buffer_size, /* {{{ */
30 const char *string)
31 {
32 size_t src_pos;
33 size_t dst_pos;
35 if ((buffer == NULL) || (string == NULL))
36 return (-EINVAL);
38 if (buffer_size < 3)
39 return (-ENOMEM);
41 dst_pos = 0;
43 #define BUFFER_ADD(c) do { \
44 if (dst_pos >= (buffer_size - 1)) { \
45 buffer[buffer_size - 1] = 0; \
46 return (-ENOMEM); \
47 } \
48 buffer[dst_pos] = (c); \
49 dst_pos++; \
50 } while (0)
52 /* Escape special characters */
53 BUFFER_ADD ('"');
54 for (src_pos = 0; string[src_pos] != 0; src_pos++)
55 {
56 if ((string[src_pos] == '"')
57 || (string[src_pos] == '\\'))
58 {
59 BUFFER_ADD ('\\');
60 BUFFER_ADD (string[src_pos]);
61 }
62 else if (string[src_pos] <= 0x001F)
63 BUFFER_ADD ('?');
64 else
65 BUFFER_ADD (string[src_pos]);
66 } /* for */
67 BUFFER_ADD ('"');
68 buffer[dst_pos] = 0;
70 #undef BUFFER_ADD
72 return (0);
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)
77 {
78 size_t offset = 0;
79 int i;
80 gauge_t *rates = NULL;
82 memset (buffer, 0, buffer_size);
84 #define BUFFER_ADD(...) do { \
85 int status; \
86 status = ssnprintf (buffer + offset, buffer_size - offset, \
87 __VA_ARGS__); \
88 if (status < 1) \
89 { \
90 sfree(rates); \
91 return (-1); \
92 } \
93 else if (((size_t) status) >= (buffer_size - offset)) \
94 { \
95 sfree(rates); \
96 return (-ENOMEM); \
97 } \
98 else \
99 offset += ((size_t) status); \
100 } while (0)
102 BUFFER_ADD ("[");
103 for (i = 0; i < ds->ds_num; i++)
104 {
105 if (i > 0)
106 BUFFER_ADD (",");
108 if (ds->ds[i].type == DS_TYPE_GAUGE)
109 {
110 if(isfinite (vl->values[i].gauge))
111 BUFFER_ADD ("%g", vl->values[i].gauge);
112 else
113 BUFFER_ADD ("null");
114 }
115 else if (store_rates)
116 {
117 if (rates == NULL)
118 rates = uc_get_rate (ds, vl);
119 if (rates == NULL)
120 {
121 WARNING ("utils_format_json: uc_get_rate failed.");
122 sfree(rates);
123 return (-1);
124 }
126 if(isfinite (rates[i]))
127 BUFFER_ADD ("%g", rates[i]);
128 else
129 BUFFER_ADD ("null");
130 }
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);
137 else
138 {
139 ERROR ("format_json: Unknown data source type: %i",
140 ds->ds[i].type);
141 sfree (rates);
142 return (-1);
143 }
144 } /* for ds->ds_num */
145 BUFFER_ADD ("]");
147 #undef BUFFER_ADD
149 DEBUG ("format_json: values_to_json: buffer = %s;", buffer);
150 sfree(rates);
151 return (0);
152 } /* }}} int values_to_json */
154 static int dstypes_to_json (char *buffer, size_t buffer_size, /* {{{ */
155 const data_set_t *ds, const value_list_t *vl)
156 {
157 size_t offset = 0;
158 int i;
160 memset (buffer, 0, buffer_size);
162 #define BUFFER_ADD(...) do { \
163 int status; \
164 status = ssnprintf (buffer + offset, buffer_size - offset, \
165 __VA_ARGS__); \
166 if (status < 1) \
167 return (-1); \
168 else if (((size_t) status) >= (buffer_size - offset)) \
169 return (-ENOMEM); \
170 else \
171 offset += ((size_t) status); \
172 } while (0)
174 BUFFER_ADD ("[");
175 for (i = 0; i < ds->ds_num; i++)
176 {
177 if (i > 0)
178 BUFFER_ADD (",");
180 BUFFER_ADD ("\"%s\"", DS_TYPE_TO_STRING (ds->ds[i].type));
181 } /* for ds->ds_num */
182 BUFFER_ADD ("]");
184 #undef BUFFER_ADD
186 DEBUG ("format_json: dstypes_to_json: buffer = %s;", buffer);
188 return (0);
189 } /* }}} int dstypes_to_json */
191 static int dsnames_to_json (char *buffer, size_t buffer_size, /* {{{ */
192 const data_set_t *ds, const value_list_t *vl)
193 {
194 size_t offset = 0;
195 int i;
197 memset (buffer, 0, buffer_size);
199 #define BUFFER_ADD(...) do { \
200 int status; \
201 status = ssnprintf (buffer + offset, buffer_size - offset, \
202 __VA_ARGS__); \
203 if (status < 1) \
204 return (-1); \
205 else if (((size_t) status) >= (buffer_size - offset)) \
206 return (-ENOMEM); \
207 else \
208 offset += ((size_t) status); \
209 } while (0)
211 BUFFER_ADD ("[");
212 for (i = 0; i < ds->ds_num; i++)
213 {
214 if (i > 0)
215 BUFFER_ADD (",");
217 BUFFER_ADD ("\"%s\"", ds->ds[i].name);
218 } /* for ds->ds_num */
219 BUFFER_ADD ("]");
221 #undef BUFFER_ADD
223 DEBUG ("format_json: dsnames_to_json: buffer = %s;", buffer);
225 return (0);
226 } /* }}} int dsnames_to_json */
228 static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */
229 const data_set_t *ds, const value_list_t *vl, int store_rates)
230 {
231 char temp[512];
232 size_t offset = 0;
233 int status;
235 memset (buffer, 0, buffer_size);
237 #define BUFFER_ADD(...) do { \
238 status = ssnprintf (buffer + offset, buffer_size - offset, \
239 __VA_ARGS__); \
240 if (status < 1) \
241 return (-1); \
242 else if (((size_t) status) >= (buffer_size - offset)) \
243 return (-ENOMEM); \
244 else \
245 offset += ((size_t) status); \
246 } while (0)
248 /* All value lists have a leading comma. The first one will be replaced with
249 * a square bracket in `format_json_finalize'. */
250 BUFFER_ADD (",{");
252 status = values_to_json (temp, sizeof (temp), ds, vl, store_rates);
253 if (status != 0)
254 return (status);
255 BUFFER_ADD ("\"values\":%s", temp);
257 status = dstypes_to_json (temp, sizeof (temp), ds, vl);
258 if (status != 0)
259 return (status);
260 BUFFER_ADD (",\"dstypes\":%s", temp);
262 status = dsnames_to_json (temp, sizeof (temp), ds, vl);
263 if (status != 0)
264 return (status);
265 BUFFER_ADD (",\"dsnames\":%s", temp);
267 BUFFER_ADD (",\"time\":%lu", (unsigned long) vl->time);
268 BUFFER_ADD (",\"interval\":%.3f", CDTIME_T_TO_DOUBLE (vl->interval));
270 #define BUFFER_ADD_KEYVAL(key, value) do { \
271 status = escape_string (temp, sizeof (temp), (value)); \
272 if (status != 0) \
273 return (status); \
274 BUFFER_ADD (",\"%s\":%s", (key), temp); \
275 } while (0)
277 BUFFER_ADD_KEYVAL ("host", vl->host);
278 BUFFER_ADD_KEYVAL ("plugin", vl->plugin);
279 BUFFER_ADD_KEYVAL ("plugin_instance", vl->plugin_instance);
280 BUFFER_ADD_KEYVAL ("type", vl->type);
281 BUFFER_ADD_KEYVAL ("type_instance", vl->type_instance);
283 BUFFER_ADD ("}");
285 #undef BUFFER_ADD_KEYVAL
286 #undef BUFFER_ADD
288 DEBUG ("format_json: value_list_to_json: buffer = %s;", buffer);
290 return (0);
291 } /* }}} int value_list_to_json */
293 static int format_json_value_list_nocheck (char *buffer, /* {{{ */
294 size_t *ret_buffer_fill, size_t *ret_buffer_free,
295 const data_set_t *ds, const value_list_t *vl,
296 int store_rates, size_t temp_size)
297 {
298 char temp[temp_size];
299 int status;
301 status = value_list_to_json (temp, sizeof (temp), ds, vl, store_rates);
302 if (status != 0)
303 return (status);
304 temp_size = strlen (temp);
306 memcpy (buffer + (*ret_buffer_fill), temp, temp_size + 1);
307 (*ret_buffer_fill) += temp_size;
308 (*ret_buffer_free) -= temp_size;
310 return (0);
311 } /* }}} int format_json_value_list_nocheck */
313 int format_json_initialize (char *buffer, /* {{{ */
314 size_t *ret_buffer_fill, size_t *ret_buffer_free)
315 {
316 size_t buffer_fill;
317 size_t buffer_free;
319 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
320 return (-EINVAL);
322 buffer_fill = *ret_buffer_fill;
323 buffer_free = *ret_buffer_free;
325 buffer_free = buffer_fill + buffer_free;
326 buffer_fill = 0;
328 if (buffer_free < 3)
329 return (-ENOMEM);
331 memset (buffer, 0, buffer_free);
332 *ret_buffer_fill = buffer_fill;
333 *ret_buffer_free = buffer_free;
335 return (0);
336 } /* }}} int format_json_initialize */
338 int format_json_finalize (char *buffer, /* {{{ */
339 size_t *ret_buffer_fill, size_t *ret_buffer_free)
340 {
341 size_t pos;
343 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
344 return (-EINVAL);
346 if (*ret_buffer_free < 2)
347 return (-ENOMEM);
349 /* Replace the leading comma added in `value_list_to_json' with a square
350 * bracket. */
351 if (buffer[0] != ',')
352 return (-EINVAL);
353 buffer[0] = '[';
355 pos = *ret_buffer_fill;
356 buffer[pos] = ']';
357 buffer[pos+1] = 0;
359 (*ret_buffer_fill)++;
360 (*ret_buffer_free)--;
362 return (0);
363 } /* }}} int format_json_finalize */
365 int format_json_value_list (char *buffer, /* {{{ */
366 size_t *ret_buffer_fill, size_t *ret_buffer_free,
367 const data_set_t *ds, const value_list_t *vl, int store_rates)
368 {
369 if ((buffer == NULL)
370 || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL)
371 || (ds == NULL) || (vl == NULL))
372 return (-EINVAL);
374 if (*ret_buffer_free < 3)
375 return (-ENOMEM);
377 return (format_json_value_list_nocheck (buffer,
378 ret_buffer_fill, ret_buffer_free, ds, vl,
379 store_rates, (*ret_buffer_free) - 2));
380 } /* }}} int format_json_value_list */
382 /* vim: set sw=2 sts=2 et fdm=marker : */