1 /**
2 * collectd - src/utils_format_json.c
3 * Copyright (C) 2009 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "common.h"
31 #include "utils_cache.h"
32 #include "utils_format_json.h"
34 static int json_escape_string (char *buffer, size_t buffer_size, /* {{{ */
35 const char *string)
36 {
37 size_t src_pos;
38 size_t dst_pos;
40 if ((buffer == NULL) || (string == NULL))
41 return (-EINVAL);
43 if (buffer_size < 3)
44 return (-ENOMEM);
46 dst_pos = 0;
48 #define BUFFER_ADD(c) do { \
49 if (dst_pos >= (buffer_size - 1)) { \
50 buffer[buffer_size - 1] = 0; \
51 return (-ENOMEM); \
52 } \
53 buffer[dst_pos] = (c); \
54 dst_pos++; \
55 } while (0)
57 /* Escape special characters */
58 BUFFER_ADD ('"');
59 for (src_pos = 0; string[src_pos] != 0; src_pos++)
60 {
61 if ((string[src_pos] == '"')
62 || (string[src_pos] == '\\'))
63 {
64 BUFFER_ADD ('\\');
65 BUFFER_ADD (string[src_pos]);
66 }
67 else if (string[src_pos] <= 0x001F)
68 BUFFER_ADD ('?');
69 else
70 BUFFER_ADD (string[src_pos]);
71 } /* for */
72 BUFFER_ADD ('"');
73 buffer[dst_pos] = 0;
75 #undef BUFFER_ADD
77 return (0);
78 } /* }}} int json_escape_string */
80 static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */
81 const data_set_t *ds, const value_list_t *vl, int store_rates)
82 {
83 size_t offset = 0;
84 int i;
85 gauge_t *rates = NULL;
87 memset (buffer, 0, buffer_size);
89 #define BUFFER_ADD(...) do { \
90 int status; \
91 status = ssnprintf (buffer + offset, buffer_size - offset, \
92 __VA_ARGS__); \
93 if (status < 1) \
94 { \
95 sfree(rates); \
96 return (-1); \
97 } \
98 else if (((size_t) status) >= (buffer_size - offset)) \
99 { \
100 sfree(rates); \
101 return (-ENOMEM); \
102 } \
103 else \
104 offset += ((size_t) status); \
105 } while (0)
107 BUFFER_ADD ("[");
108 for (i = 0; i < ds->ds_num; i++)
109 {
110 if (i > 0)
111 BUFFER_ADD (",");
113 if (ds->ds[i].type == DS_TYPE_GAUGE)
114 {
115 if(isfinite (vl->values[i].gauge))
116 BUFFER_ADD ("%g", vl->values[i].gauge);
117 else
118 BUFFER_ADD ("null");
119 }
120 else if (store_rates)
121 {
122 if (rates == NULL)
123 rates = uc_get_rate (ds, vl);
124 if (rates == NULL)
125 {
126 WARNING ("utils_format_json: uc_get_rate failed.");
127 sfree(rates);
128 return (-1);
129 }
131 if(isfinite (rates[i]))
132 BUFFER_ADD ("%g", rates[i]);
133 else
134 BUFFER_ADD ("null");
135 }
136 else if (ds->ds[i].type == DS_TYPE_COUNTER)
137 BUFFER_ADD ("%llu", vl->values[i].counter);
138 else if (ds->ds[i].type == DS_TYPE_DERIVE)
139 BUFFER_ADD ("%"PRIi64, vl->values[i].derive);
140 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
141 BUFFER_ADD ("%"PRIu64, vl->values[i].absolute);
142 else
143 {
144 ERROR ("format_json: Unknown data source type: %i",
145 ds->ds[i].type);
146 sfree (rates);
147 return (-1);
148 }
149 } /* for ds->ds_num */
150 BUFFER_ADD ("]");
152 #undef BUFFER_ADD
154 DEBUG ("format_json: values_to_json: buffer = %s;", buffer);
155 sfree(rates);
156 return (0);
157 } /* }}} int values_to_json */
159 static int dstypes_to_json (char *buffer, size_t buffer_size, /* {{{ */
160 const data_set_t *ds)
161 {
162 size_t offset = 0;
163 int i;
165 memset (buffer, 0, buffer_size);
167 #define BUFFER_ADD(...) do { \
168 int status; \
169 status = ssnprintf (buffer + offset, buffer_size - offset, \
170 __VA_ARGS__); \
171 if (status < 1) \
172 return (-1); \
173 else if (((size_t) status) >= (buffer_size - offset)) \
174 return (-ENOMEM); \
175 else \
176 offset += ((size_t) status); \
177 } while (0)
179 BUFFER_ADD ("[");
180 for (i = 0; i < ds->ds_num; i++)
181 {
182 if (i > 0)
183 BUFFER_ADD (",");
185 BUFFER_ADD ("\"%s\"", DS_TYPE_TO_STRING (ds->ds[i].type));
186 } /* for ds->ds_num */
187 BUFFER_ADD ("]");
189 #undef BUFFER_ADD
191 DEBUG ("format_json: dstypes_to_json: buffer = %s;", buffer);
193 return (0);
194 } /* }}} int dstypes_to_json */
196 static int dsnames_to_json (char *buffer, size_t buffer_size, /* {{{ */
197 const data_set_t *ds)
198 {
199 size_t offset = 0;
200 int i;
202 memset (buffer, 0, buffer_size);
204 #define BUFFER_ADD(...) do { \
205 int status; \
206 status = ssnprintf (buffer + offset, buffer_size - offset, \
207 __VA_ARGS__); \
208 if (status < 1) \
209 return (-1); \
210 else if (((size_t) status) >= (buffer_size - offset)) \
211 return (-ENOMEM); \
212 else \
213 offset += ((size_t) status); \
214 } while (0)
216 BUFFER_ADD ("[");
217 for (i = 0; i < ds->ds_num; i++)
218 {
219 if (i > 0)
220 BUFFER_ADD (",");
222 BUFFER_ADD ("\"%s\"", ds->ds[i].name);
223 } /* for ds->ds_num */
224 BUFFER_ADD ("]");
226 #undef BUFFER_ADD
228 DEBUG ("format_json: dsnames_to_json: buffer = %s;", buffer);
230 return (0);
231 } /* }}} int dsnames_to_json */
233 static int meta_data_to_json (char *buffer, size_t buffer_size, /* {{{ */
234 meta_data_t *meta)
235 {
236 size_t offset = 0;
237 char **keys = NULL;
238 int keys_num;
239 int status;
240 int i;
242 buffer[0] = 0;
244 if (meta == NULL)
245 return (EINVAL);
247 #define BUFFER_ADD(...) do { \
248 status = ssnprintf (buffer + offset, buffer_size - offset, \
249 __VA_ARGS__); \
250 if (status < 1) \
251 return (-1); \
252 else if (((size_t) status) >= (buffer_size - offset)) \
253 return (-ENOMEM); \
254 else \
255 offset += ((size_t) status); \
256 } while (0)
258 keys_num = meta_data_toc (meta, &keys);
259 if (keys_num == 0)
260 {
261 sfree (keys);
262 return (0);
263 }
265 for (i = 0; i < keys_num; ++i)
266 {
267 int type;
268 char *key = keys[i];
270 type = meta_data_type (meta, key);
271 if (type == MD_TYPE_STRING)
272 {
273 char *value = NULL;
274 if (meta_data_get_string (meta, key, &value) == 0)
275 {
276 char temp[512] = "";
277 json_escape_string (temp, sizeof (temp), value);
278 sfree (value);
279 BUFFER_ADD (",\"%s\":%s", key, temp);
280 }
281 }
282 else if (type == MD_TYPE_SIGNED_INT)
283 {
284 int64_t value = 0;
285 if (meta_data_get_signed_int (meta, key, &value) == 0)
286 BUFFER_ADD (",\"%s\":%"PRIi64, key, value);
287 }
288 else if (type == MD_TYPE_UNSIGNED_INT)
289 {
290 uint64_t value = 0;
291 if (meta_data_get_unsigned_int (meta, key, &value) == 0)
292 BUFFER_ADD (",\"%s\":%"PRIu64, key, value);
293 }
294 else if (type == MD_TYPE_DOUBLE)
295 {
296 double value = 0.0;
297 if (meta_data_get_double (meta, key, &value) == 0)
298 BUFFER_ADD (",\"%s\":%f", key, value);
299 }
300 else if (type == MD_TYPE_BOOLEAN)
301 {
302 _Bool value = 0;
303 if (meta_data_get_boolean (meta, key, &value) == 0)
304 BUFFER_ADD (",\"%s\":%s", key, value ? "true" : "false");
305 }
307 free (key);
308 } /* for (keys) */
309 free (keys);
311 if (offset <= 0)
312 return (ENOENT);
314 buffer[0] = '{'; /* replace leading ',' */
315 BUFFER_ADD ("}");
317 #undef BUFFER_ADD
319 return (0);
320 } /* }}} int meta_data_to_json */
322 static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */
323 const data_set_t *ds, const value_list_t *vl, int store_rates)
324 {
325 char temp[512];
326 size_t offset = 0;
327 int status;
329 memset (buffer, 0, buffer_size);
331 #define BUFFER_ADD(...) do { \
332 status = ssnprintf (buffer + offset, buffer_size - offset, \
333 __VA_ARGS__); \
334 if (status < 1) \
335 return (-1); \
336 else if (((size_t) status) >= (buffer_size - offset)) \
337 return (-ENOMEM); \
338 else \
339 offset += ((size_t) status); \
340 } while (0)
342 /* All value lists have a leading comma. The first one will be replaced with
343 * a square bracket in `format_json_finalize'. */
344 BUFFER_ADD (",{");
346 status = values_to_json (temp, sizeof (temp), ds, vl, store_rates);
347 if (status != 0)
348 return (status);
349 BUFFER_ADD ("\"values\":%s", temp);
351 status = dstypes_to_json (temp, sizeof (temp), ds);
352 if (status != 0)
353 return (status);
354 BUFFER_ADD (",\"dstypes\":%s", temp);
356 status = dsnames_to_json (temp, sizeof (temp), ds);
357 if (status != 0)
358 return (status);
359 BUFFER_ADD (",\"dsnames\":%s", temp);
361 BUFFER_ADD (",\"time\":%.3f", CDTIME_T_TO_DOUBLE (vl->time));
362 BUFFER_ADD (",\"interval\":%.3f", CDTIME_T_TO_DOUBLE (vl->interval));
364 #define BUFFER_ADD_KEYVAL(key, value) do { \
365 status = json_escape_string (temp, sizeof (temp), (value)); \
366 if (status != 0) \
367 return (status); \
368 BUFFER_ADD (",\"%s\":%s", (key), temp); \
369 } while (0)
371 BUFFER_ADD_KEYVAL ("host", vl->host);
372 BUFFER_ADD_KEYVAL ("plugin", vl->plugin);
373 BUFFER_ADD_KEYVAL ("plugin_instance", vl->plugin_instance);
374 BUFFER_ADD_KEYVAL ("type", vl->type);
375 BUFFER_ADD_KEYVAL ("type_instance", vl->type_instance);
377 if (vl->meta != NULL)
378 {
379 char meta_buffer[buffer_size];
380 memset (meta_buffer, 0, sizeof (meta_buffer));
381 status = meta_data_to_json (meta_buffer, sizeof (meta_buffer), vl->meta);
382 if (status != 0)
383 return (status);
385 BUFFER_ADD (",\"meta\":%s", meta_buffer);
386 } /* if (vl->meta != NULL) */
388 BUFFER_ADD ("}");
390 #undef BUFFER_ADD_KEYVAL
391 #undef BUFFER_ADD
393 DEBUG ("format_json: value_list_to_json: buffer = %s;", buffer);
395 return (0);
396 } /* }}} int value_list_to_json */
398 static int format_json_value_list_nocheck (char *buffer, /* {{{ */
399 size_t *ret_buffer_fill, size_t *ret_buffer_free,
400 const data_set_t *ds, const value_list_t *vl,
401 int store_rates, size_t temp_size)
402 {
403 char temp[temp_size];
404 int status;
406 status = value_list_to_json (temp, sizeof (temp), ds, vl, store_rates);
407 if (status != 0)
408 return (status);
409 temp_size = strlen (temp);
411 memcpy (buffer + (*ret_buffer_fill), temp, temp_size + 1);
412 (*ret_buffer_fill) += temp_size;
413 (*ret_buffer_free) -= temp_size;
415 return (0);
416 } /* }}} int format_json_value_list_nocheck */
418 int format_json_initialize (char *buffer, /* {{{ */
419 size_t *ret_buffer_fill, size_t *ret_buffer_free)
420 {
421 size_t buffer_fill;
422 size_t buffer_free;
424 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
425 return (-EINVAL);
427 buffer_fill = *ret_buffer_fill;
428 buffer_free = *ret_buffer_free;
430 buffer_free = buffer_fill + buffer_free;
431 buffer_fill = 0;
433 if (buffer_free < 3)
434 return (-ENOMEM);
436 memset (buffer, 0, buffer_free);
437 *ret_buffer_fill = buffer_fill;
438 *ret_buffer_free = buffer_free;
440 return (0);
441 } /* }}} int format_json_initialize */
443 int format_json_finalize (char *buffer, /* {{{ */
444 size_t *ret_buffer_fill, size_t *ret_buffer_free)
445 {
446 size_t pos;
448 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
449 return (-EINVAL);
451 if (*ret_buffer_free < 2)
452 return (-ENOMEM);
454 /* Replace the leading comma added in `value_list_to_json' with a square
455 * bracket. */
456 if (buffer[0] != ',')
457 return (-EINVAL);
458 buffer[0] = '[';
460 pos = *ret_buffer_fill;
461 buffer[pos] = ']';
462 buffer[pos+1] = 0;
464 (*ret_buffer_fill)++;
465 (*ret_buffer_free)--;
467 return (0);
468 } /* }}} int format_json_finalize */
470 int format_json_value_list (char *buffer, /* {{{ */
471 size_t *ret_buffer_fill, size_t *ret_buffer_free,
472 const data_set_t *ds, const value_list_t *vl, int store_rates)
473 {
474 if ((buffer == NULL)
475 || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL)
476 || (ds == NULL) || (vl == NULL))
477 return (-EINVAL);
479 if (*ret_buffer_free < 3)
480 return (-ENOMEM);
482 return (format_json_value_list_nocheck (buffer,
483 ret_buffer_fill, ret_buffer_free, ds, vl,
484 store_rates, (*ret_buffer_free) - 2));
485 } /* }}} int format_json_value_list */
487 /* vim: set sw=2 sts=2 et fdm=marker : */