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_format_json.h"
28 static int escape_string (char *buffer, size_t buffer_size, /* {{{ */
29 const char *string)
30 {
31 size_t src_pos;
32 size_t dst_pos;
34 if ((buffer == NULL) || (string == NULL))
35 return (-EINVAL);
37 if (buffer_size < 3)
38 return (-ENOMEM);
40 dst_pos = 0;
42 #define BUFFER_ADD(c) do { \
43 if (dst_pos >= (buffer_size - 1)) { \
44 buffer[buffer_size - 1] = 0; \
45 return (-ENOMEM); \
46 } \
47 buffer[dst_pos] = (c); \
48 dst_pos++; \
49 } while (0)
51 /* Escape special characters */
52 BUFFER_ADD ('"');
53 for (src_pos = 0; string[src_pos] != 0; src_pos++)
54 {
55 if ((string[src_pos] == '"')
56 || (string[src_pos] == '\\'))
57 {
58 BUFFER_ADD ('\\');
59 BUFFER_ADD (string[src_pos]);
60 }
61 else if (string[src_pos] <= 0x001F)
62 BUFFER_ADD ('?');
63 else
64 BUFFER_ADD (string[src_pos]);
65 } /* for */
66 BUFFER_ADD ('"');
67 buffer[dst_pos] = 0;
69 #undef BUFFER_ADD
71 return (0);
72 } /* }}} int buffer_add_string */
74 static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */
75 const data_set_t *ds, const value_list_t *vl)
76 {
77 size_t offset = 0;
78 int i;
80 memset (buffer, 0, buffer_size);
82 #define BUFFER_ADD(...) do { \
83 int status; \
84 status = ssnprintf (buffer + offset, buffer_size - offset, \
85 __VA_ARGS__); \
86 if (status < 1) \
87 return (-1); \
88 else if (((size_t) status) >= (buffer_size - offset)) \
89 return (-ENOMEM); \
90 else \
91 offset += ((size_t) status); \
92 } while (0)
94 BUFFER_ADD ("[");
95 for (i = 0; i < ds->ds_num; i++)
96 {
97 if (i > 0)
98 BUFFER_ADD (",");
100 if (ds->ds[i].type == DS_TYPE_GAUGE)
101 {
102 if(isfinite (vl->values[i].gauge))
103 BUFFER_ADD ("%g", vl->values[i].gauge);
104 else
105 BUFFER_ADD ("null");
106 }
107 else if (ds->ds[i].type == DS_TYPE_COUNTER)
108 BUFFER_ADD ("%llu", vl->values[i].counter);
109 else if (ds->ds[i].type == DS_TYPE_DERIVE)
110 BUFFER_ADD ("%"PRIi64, vl->values[i].derive);
111 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
112 BUFFER_ADD ("%"PRIu64, vl->values[i].absolute);
113 else
114 {
115 ERROR ("format_json: Unknown data source type: %i",
116 ds->ds[i].type);
117 return (-1);
118 }
119 } /* for ds->ds_num */
120 BUFFER_ADD ("]");
122 #undef BUFFER_ADD
124 DEBUG ("format_json: values_to_json: buffer = %s;", buffer);
126 return (0);
127 } /* }}} int values_to_json */
129 static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */
130 const data_set_t *ds, const value_list_t *vl)
131 {
132 char temp[512];
133 size_t offset = 0;
134 int status;
136 memset (buffer, 0, buffer_size);
138 #define BUFFER_ADD(...) do { \
139 status = ssnprintf (buffer + offset, buffer_size - offset, \
140 __VA_ARGS__); \
141 if (status < 1) \
142 return (-1); \
143 else if (((size_t) status) >= (buffer_size - offset)) \
144 return (-ENOMEM); \
145 else \
146 offset += ((size_t) status); \
147 } while (0)
149 /* All value lists have a leading comma. The first one will be replaced with
150 * a square bracket in `format_json_finalize'. */
151 BUFFER_ADD (",{");
153 status = values_to_json (temp, sizeof (temp), ds, vl);
154 if (status != 0)
155 return (status);
156 BUFFER_ADD ("\"values\":%s", temp);
158 BUFFER_ADD (",\"time\":%lu", (unsigned long) vl->time);
159 BUFFER_ADD (",\"interval\":%i", vl->interval);
161 #define BUFFER_ADD_KEYVAL(key, value) do { \
162 status = escape_string (temp, sizeof (temp), (value)); \
163 if (status != 0) \
164 return (status); \
165 BUFFER_ADD (",\"%s\":%s", (key), temp); \
166 } while (0)
168 BUFFER_ADD_KEYVAL ("host", vl->host);
169 BUFFER_ADD_KEYVAL ("plugin", vl->plugin);
170 BUFFER_ADD_KEYVAL ("plugin_instance", vl->plugin_instance);
171 BUFFER_ADD_KEYVAL ("type", vl->type);
172 BUFFER_ADD_KEYVAL ("type_instance", vl->type_instance);
174 BUFFER_ADD ("}");
176 #undef BUFFER_ADD_KEYVAL
177 #undef BUFFER_ADD
179 DEBUG ("format_json: value_list_to_json: buffer = %s;", buffer);
181 return (0);
182 } /* }}} int value_list_to_json */
184 static int format_json_value_list_nocheck (char *buffer, /* {{{ */
185 size_t *ret_buffer_fill, size_t *ret_buffer_free,
186 const data_set_t *ds, const value_list_t *vl,
187 size_t temp_size)
188 {
189 char temp[temp_size];
190 int status;
192 status = value_list_to_json (temp, sizeof (temp), ds, vl);
193 if (status != 0)
194 return (status);
195 temp_size = strlen (temp);
197 memcpy (buffer + (*ret_buffer_fill), temp, temp_size + 1);
198 (*ret_buffer_fill) += temp_size;
199 (*ret_buffer_free) -= temp_size;
201 return (0);
202 } /* }}} int format_json_value_list_nocheck */
204 int format_json_initialize (char *buffer, /* {{{ */
205 size_t *ret_buffer_fill, size_t *ret_buffer_free)
206 {
207 size_t buffer_fill;
208 size_t buffer_free;
210 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
211 return (-EINVAL);
213 buffer_fill = *ret_buffer_fill;
214 buffer_free = *ret_buffer_free;
216 buffer_free = buffer_fill + buffer_free;
217 buffer_fill = 0;
219 if (buffer_free < 3)
220 return (-ENOMEM);
222 memset (buffer, 0, buffer_free);
223 *ret_buffer_fill = buffer_fill;
224 *ret_buffer_free = buffer_free;
226 return (0);
227 } /* }}} int format_json_initialize */
229 int format_json_finalize (char *buffer, /* {{{ */
230 size_t *ret_buffer_fill, size_t *ret_buffer_free)
231 {
232 size_t pos;
234 if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL))
235 return (-EINVAL);
237 if (*ret_buffer_free < 2)
238 return (-ENOMEM);
240 /* Replace the leading comma added in `value_list_to_json' with a square
241 * bracket. */
242 if (buffer[0] != ',')
243 return (-EINVAL);
244 buffer[0] = '[';
246 pos = *ret_buffer_fill;
247 buffer[pos] = ']';
248 buffer[pos+1] = 0;
250 (*ret_buffer_fill)++;
251 (*ret_buffer_free)--;
253 return (0);
254 } /* }}} int format_json_finalize */
256 int format_json_value_list (char *buffer, /* {{{ */
257 size_t *ret_buffer_fill, size_t *ret_buffer_free,
258 const data_set_t *ds, const value_list_t *vl)
259 {
260 if ((buffer == NULL)
261 || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL)
262 || (ds == NULL) || (vl == NULL))
263 return (-EINVAL);
265 if (*ret_buffer_free < 3)
266 return (-ENOMEM);
268 return (format_json_value_list_nocheck (buffer,
269 ret_buffer_fill, ret_buffer_free, ds, vl,
270 (*ret_buffer_free) - 2));
271 } /* }}} int format_json_value_list */
273 /* vim: set sw=2 sts=2 et fdm=marker : */