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 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)
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)
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)
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 meta_data_to_json (char *buffer, size_t buffer_size, /* {{{ */
229 meta_data_t *meta)
230 {
231 size_t offset = 0;
232 char **keys = NULL;
233 int keys_num;
234 int status;
235 int i;
237 memset (buffer, 0, buffer_size);
239 #define BUFFER_ADD(...) do { \
240 status = ssnprintf (buffer + offset, buffer_size - offset, \
241 __VA_ARGS__); \
242 if (status < 1) \
243 return (-1); \
244 else if (((size_t) status) >= (buffer_size - offset)) \
245 return (-ENOMEM); \
246 else \
247 offset += ((size_t) status); \
248 } while (0)
250 keys_num = meta_data_toc (meta, &keys);
251 for (i = 0; i < keys_num; ++i)
252 {
253 int type;
254 char *key = keys[i];
256 type = meta_data_type (meta, key);
257 if (type == MD_TYPE_STRING)
258 {
259 char *value = NULL;
260 if (meta_data_get_string (meta, key, &value) == 0)
261 {
262 char temp[512] = "";
263 escape_string (temp, sizeof (temp), value);
264 sfree (value);
265 BUFFER_ADD (",\"%s\":%s", key, temp);
266 }
267 }
268 else if (type == MD_TYPE_SIGNED_INT)
269 {
270 int64_t value = 0;
271 if (meta_data_get_signed_int (meta, key, &value) == 0)
272 BUFFER_ADD (",\"%s\":%"PRIi64, key, value);
273 }
274 else if (type == MD_TYPE_UNSIGNED_INT)
275 {
276 uint64_t value = 0;
277 if (meta_data_get_unsigned_int (meta, key, &value) == 0)
278 BUFFER_ADD (",\"%s\":%"PRIu64, key, value);
279 }
280 else if (type == MD_TYPE_DOUBLE)
281 {
282 double value = 0.0;
283 if (meta_data_get_double (meta, key, &value) == 0)
284 BUFFER_ADD (",\"%s\":%f", key, value);
285 }
286 else if (type == MD_TYPE_BOOLEAN)
287 {
288 _Bool value = 0;
289 if (meta_data_get_boolean (meta, key, &value) == 0)
290 BUFFER_ADD (",\"%s\":%s", key, value ? "true" : "false");
291 }
293 free (key);
294 } /* for (keys) */
295 free (keys);
297 if (offset <= 0)
298 return (ENOENT);
300 buffer[0] = '{'; /* replace leading ',' */
301 BUFFER_ADD ("}");
303 #undef BUFFER_ADD
305 return (0);
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)
310 {
311 char temp[512];
312 size_t offset = 0;
313 int status;
315 memset (buffer, 0, buffer_size);
317 #define BUFFER_ADD(...) do { \
318 status = ssnprintf (buffer + offset, buffer_size - offset, \
319 __VA_ARGS__); \
320 if (status < 1) \
321 return (-1); \
322 else if (((size_t) status) >= (buffer_size - offset)) \
323 return (-ENOMEM); \
324 else \
325 offset += ((size_t) status); \
326 } while (0)
328 /* All value lists have a leading comma. The first one will be replaced with
329 * a square bracket in `format_json_finalize'. */
330 BUFFER_ADD (",{");
332 status = values_to_json (temp, sizeof (temp), ds, vl, store_rates);
333 if (status != 0)
334 return (status);
335 BUFFER_ADD ("\"values\":%s", temp);
337 status = dstypes_to_json (temp, sizeof (temp), ds);
338 if (status != 0)
339 return (status);
340 BUFFER_ADD (",\"dstypes\":%s", temp);
342 status = dsnames_to_json (temp, sizeof (temp), ds);
343 if (status != 0)
344 return (status);
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)); \
352 if (status != 0) \
353 return (status); \
354 BUFFER_ADD (",\"%s\":%s", (key), temp); \
355 } while (0)
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)
364 {
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);
368 if (status != 0)
369 return (status);
371 BUFFER_ADD (",\"meta\":%s", meta_buffer);
372 } /* if (vl->meta != NULL) */
374 BUFFER_ADD ("}");
376 #undef BUFFER_ADD_KEYVAL
377 #undef BUFFER_ADD
379 DEBUG ("format_json: value_list_to_json: buffer = %s;", buffer);
381 return (0);
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)
388 {
389 char temp[temp_size];
390 int status;
392 status = value_list_to_json (temp, sizeof (temp), ds, vl, store_rates);
393 if (status != 0)
394 return (status);
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;
401 return (0);
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)
406 {
407 size_t buffer_fill;
408 size_t buffer_free;
410 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
411 return (-EINVAL);
413 buffer_fill = *ret_buffer_fill;
414 buffer_free = *ret_buffer_free;
416 buffer_free = buffer_fill + buffer_free;
417 buffer_fill = 0;
419 if (buffer_free < 3)
420 return (-ENOMEM);
422 memset (buffer, 0, buffer_free);
423 *ret_buffer_fill = buffer_fill;
424 *ret_buffer_free = buffer_free;
426 return (0);
427 } /* }}} int format_json_initialize */
429 int format_json_finalize (char *buffer, /* {{{ */
430 size_t *ret_buffer_fill, size_t *ret_buffer_free)
431 {
432 size_t pos;
434 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
435 return (-EINVAL);
437 if (*ret_buffer_free < 2)
438 return (-ENOMEM);
440 /* Replace the leading comma added in `value_list_to_json' with a square
441 * bracket. */
442 if (buffer[0] != ',')
443 return (-EINVAL);
444 buffer[0] = '[';
446 pos = *ret_buffer_fill;
447 buffer[pos] = ']';
448 buffer[pos+1] = 0;
450 (*ret_buffer_fill)++;
451 (*ret_buffer_free)--;
453 return (0);
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)
459 {
460 if ((buffer == NULL)
461 || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL)
462 || (ds == NULL) || (vl == NULL))
463 return (-EINVAL);
465 if (*ret_buffer_free < 3)
466 return (-ENOMEM);
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 : */