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 size_t 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 (JSON_GAUGE_FORMAT, 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 (JSON_GAUGE_FORMAT, 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 size_t 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 size_t 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_keys_to_json (char *buffer, size_t buffer_size, /* {{{ */
234 meta_data_t *meta, char **keys, size_t keys_num)
235 {
236 size_t offset = 0;
237 int status;
238 size_t i;
240 buffer[0] = 0;
242 #define BUFFER_ADD(...) do { \
243 status = ssnprintf (buffer + offset, buffer_size - offset, \
244 __VA_ARGS__); \
245 if (status < 1) \
246 return (-1); \
247 else if (((size_t) status) >= (buffer_size - offset)) \
248 return (-ENOMEM); \
249 else \
250 offset += ((size_t) status); \
251 } while (0)
253 for (i = 0; i < keys_num; ++i)
254 {
255 int type;
256 char *key = keys[i];
258 type = meta_data_type (meta, key);
259 if (type == MD_TYPE_STRING)
260 {
261 char *value = NULL;
262 if (meta_data_get_string (meta, key, &value) == 0)
263 {
264 char temp[512] = "";
266 status = json_escape_string (temp, sizeof (temp), value);
267 sfree (value);
268 if (status != 0)
269 return status;
271 BUFFER_ADD (",\"%s\":%s", key, temp);
272 }
273 }
274 else if (type == MD_TYPE_SIGNED_INT)
275 {
276 int64_t value = 0;
277 if (meta_data_get_signed_int (meta, key, &value) == 0)
278 BUFFER_ADD (",\"%s\":%"PRIi64, key, value);
279 }
280 else if (type == MD_TYPE_UNSIGNED_INT)
281 {
282 uint64_t value = 0;
283 if (meta_data_get_unsigned_int (meta, key, &value) == 0)
284 BUFFER_ADD (",\"%s\":%"PRIu64, key, value);
285 }
286 else if (type == MD_TYPE_DOUBLE)
287 {
288 double value = 0.0;
289 if (meta_data_get_double (meta, key, &value) == 0)
290 BUFFER_ADD (",\"%s\":%f", key, value);
291 }
292 else if (type == MD_TYPE_BOOLEAN)
293 {
294 _Bool value = 0;
295 if (meta_data_get_boolean (meta, key, &value) == 0)
296 BUFFER_ADD (",\"%s\":%s", key, value ? "true" : "false");
297 }
298 } /* for (keys) */
300 if (offset == 0)
301 return (ENOENT);
303 buffer[0] = '{'; /* replace leading ',' */
304 BUFFER_ADD ("}");
306 #undef BUFFER_ADD
308 return (0);
309 } /* }}} int meta_data_keys_to_json */
311 static int meta_data_to_json (char *buffer, size_t buffer_size, /* {{{ */
312 meta_data_t *meta)
313 {
314 char **keys = NULL;
315 size_t keys_num;
316 int status;
317 size_t i;
319 if ((buffer == NULL) || (buffer_size == 0) || (meta == NULL))
320 return (EINVAL);
322 status = meta_data_toc (meta, &keys);
323 if (status <= 0)
324 return (status);
325 keys_num = (size_t) status;
327 status = meta_data_keys_to_json (buffer, buffer_size, meta, keys, keys_num);
329 for (i = 0; i < keys_num; ++i)
330 sfree (keys[i]);
331 sfree (keys);
333 return status;
334 } /* }}} int meta_data_to_json */
336 static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */
337 const data_set_t *ds, const value_list_t *vl, int store_rates)
338 {
339 char temp[512];
340 size_t offset = 0;
341 int status;
343 memset (buffer, 0, buffer_size);
345 #define BUFFER_ADD(...) do { \
346 status = ssnprintf (buffer + offset, buffer_size - offset, \
347 __VA_ARGS__); \
348 if (status < 1) \
349 return (-1); \
350 else if (((size_t) status) >= (buffer_size - offset)) \
351 return (-ENOMEM); \
352 else \
353 offset += ((size_t) status); \
354 } while (0)
356 /* All value lists have a leading comma. The first one will be replaced with
357 * a square bracket in `format_json_finalize'. */
358 BUFFER_ADD (",{");
360 status = values_to_json (temp, sizeof (temp), ds, vl, store_rates);
361 if (status != 0)
362 return (status);
363 BUFFER_ADD ("\"values\":%s", temp);
365 status = dstypes_to_json (temp, sizeof (temp), ds);
366 if (status != 0)
367 return (status);
368 BUFFER_ADD (",\"dstypes\":%s", temp);
370 status = dsnames_to_json (temp, sizeof (temp), ds);
371 if (status != 0)
372 return (status);
373 BUFFER_ADD (",\"dsnames\":%s", temp);
375 BUFFER_ADD (",\"time\":%.3f", CDTIME_T_TO_DOUBLE (vl->time));
376 BUFFER_ADD (",\"interval\":%.3f", CDTIME_T_TO_DOUBLE (vl->interval));
378 #define BUFFER_ADD_KEYVAL(key, value) do { \
379 status = json_escape_string (temp, sizeof (temp), (value)); \
380 if (status != 0) \
381 return (status); \
382 BUFFER_ADD (",\"%s\":%s", (key), temp); \
383 } while (0)
385 BUFFER_ADD_KEYVAL ("host", vl->host);
386 BUFFER_ADD_KEYVAL ("plugin", vl->plugin);
387 BUFFER_ADD_KEYVAL ("plugin_instance", vl->plugin_instance);
388 BUFFER_ADD_KEYVAL ("type", vl->type);
389 BUFFER_ADD_KEYVAL ("type_instance", vl->type_instance);
391 if (vl->meta != NULL)
392 {
393 char meta_buffer[buffer_size];
394 memset (meta_buffer, 0, sizeof (meta_buffer));
395 status = meta_data_to_json (meta_buffer, sizeof (meta_buffer), vl->meta);
396 if (status != 0)
397 return (status);
399 BUFFER_ADD (",\"meta\":%s", meta_buffer);
400 } /* if (vl->meta != NULL) */
402 BUFFER_ADD ("}");
404 #undef BUFFER_ADD_KEYVAL
405 #undef BUFFER_ADD
407 DEBUG ("format_json: value_list_to_json: buffer = %s;", buffer);
409 return (0);
410 } /* }}} int value_list_to_json */
412 static int format_json_value_list_nocheck (char *buffer, /* {{{ */
413 size_t *ret_buffer_fill, size_t *ret_buffer_free,
414 const data_set_t *ds, const value_list_t *vl,
415 int store_rates, size_t temp_size)
416 {
417 char temp[temp_size];
418 int status;
420 status = value_list_to_json (temp, sizeof (temp), ds, vl, store_rates);
421 if (status != 0)
422 return (status);
423 temp_size = strlen (temp);
425 memcpy (buffer + (*ret_buffer_fill), temp, temp_size + 1);
426 (*ret_buffer_fill) += temp_size;
427 (*ret_buffer_free) -= temp_size;
429 return (0);
430 } /* }}} int format_json_value_list_nocheck */
432 int format_json_initialize (char *buffer, /* {{{ */
433 size_t *ret_buffer_fill, size_t *ret_buffer_free)
434 {
435 size_t buffer_fill;
436 size_t buffer_free;
438 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
439 return (-EINVAL);
441 buffer_fill = *ret_buffer_fill;
442 buffer_free = *ret_buffer_free;
444 buffer_free = buffer_fill + buffer_free;
445 buffer_fill = 0;
447 if (buffer_free < 3)
448 return (-ENOMEM);
450 memset (buffer, 0, buffer_free);
451 *ret_buffer_fill = buffer_fill;
452 *ret_buffer_free = buffer_free;
454 return (0);
455 } /* }}} int format_json_initialize */
457 int format_json_finalize (char *buffer, /* {{{ */
458 size_t *ret_buffer_fill, size_t *ret_buffer_free)
459 {
460 size_t pos;
462 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
463 return (-EINVAL);
465 if (*ret_buffer_free < 2)
466 return (-ENOMEM);
468 /* Replace the leading comma added in `value_list_to_json' with a square
469 * bracket. */
470 if (buffer[0] != ',')
471 return (-EINVAL);
472 buffer[0] = '[';
474 pos = *ret_buffer_fill;
475 buffer[pos] = ']';
476 buffer[pos+1] = 0;
478 (*ret_buffer_fill)++;
479 (*ret_buffer_free)--;
481 return (0);
482 } /* }}} int format_json_finalize */
484 int format_json_value_list (char *buffer, /* {{{ */
485 size_t *ret_buffer_fill, size_t *ret_buffer_free,
486 const data_set_t *ds, const value_list_t *vl, int store_rates)
487 {
488 if ((buffer == NULL)
489 || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL)
490 || (ds == NULL) || (vl == NULL))
491 return (-EINVAL);
493 if (*ret_buffer_free < 3)
494 return (-ENOMEM);
496 return (format_json_value_list_nocheck (buffer,
497 ret_buffer_fill, ret_buffer_free, ds, vl,
498 store_rates, (*ret_buffer_free) - 2));
499 } /* }}} int format_json_value_list */
501 /* vim: set sw=2 sts=2 et fdm=marker : */