1 /*
2 * SysDB - src/core/store_json.c
3 * Copyright (C) 2013-2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 /*
29 * This module implements JSON support.
30 */
32 #if HAVE_CONFIG_H
33 # include "config.h"
34 #endif /* HAVE_CONFIG_H */
36 #include "sysdb.h"
37 #include "core/store-private.h"
38 #include "utils/error.h"
40 #include <assert.h>
42 #include <ctype.h>
43 #include <stdlib.h>
44 #include <string.h>
46 /*
47 * private data types
48 */
50 struct sdb_store_json_formatter {
51 sdb_strbuf_t *buf;
53 /* The context describes the state of the formatter through
54 * the path pointing to the current object */
55 int context[8];
56 size_t current;
58 /* The current host context when processing non-host objects */
59 sdb_store_obj_t *current_host;
61 int type;
62 int flags;
63 };
65 /*
66 * private helper functions
67 */
69 static void
70 escape_string(const char *src, char *dest)
71 {
72 size_t i = 1;
73 dest[0] = '"';
74 for ( ; *src; ++src) {
75 char c = *src;
76 if ((c == '"') || (c == '\\') || iscntrl((int)c)) {
77 dest[i] = '\\';
78 ++i;
79 }
80 switch (c) {
81 case '\a': dest[i] = 'a'; break;
82 case '\b': dest[i] = 'b'; break;
83 case '\t': dest[i] = 't'; break;
84 case '\n': dest[i] = 'n'; break;
85 case '\v': dest[i] = 'v'; break;
86 case '\f': dest[i] = 'f'; break;
87 case '\r': dest[i] = 'r'; break;
88 default: dest[i] = c; break;
89 }
90 ++i;
91 }
92 dest[i] = '"';
93 dest[i + 1] = '\0';
94 } /* escape_string */
96 static int
97 json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
98 {
99 char time_str[64];
100 char interval_str[64];
101 char name[2 * strlen(SDB_OBJ(obj)->name) + 3];
102 size_t i;
104 assert(f && obj);
106 if ((f->type != SDB_HOST) && (f->type == obj->type)) {
107 /* create the host for the current entry first */
108 assert(obj->parent && (obj->parent->type == SDB_HOST));
109 if (f->current_host != obj->parent) {
110 json_emit(f, obj->parent);
111 sdb_strbuf_append(f->buf, ", \"%ss\": [",
112 SDB_STORE_TYPE_TO_NAME(obj->type));
113 f->current_host = obj->parent;
114 }
115 }
117 escape_string(SDB_OBJ(obj)->name, name);
118 sdb_strbuf_append(f->buf, "{\"name\": %s, ", name);
119 if (obj->type == SDB_ATTRIBUTE) {
120 char tmp[sdb_data_strlen(&ATTR(obj)->value) + 1];
121 char val[2 * sizeof(tmp) + 3];
122 if (! sdb_data_format(&ATTR(obj)->value, tmp, sizeof(tmp),
123 SDB_DOUBLE_QUOTED))
124 snprintf(tmp, sizeof(tmp), "<error>");
126 if (tmp[0] == '"') {
127 /* a string; escape_string handles quoting */
128 tmp[strlen(tmp) - 1] = '\0';
129 escape_string(tmp + 1, val);
130 sdb_strbuf_append(f->buf, "\"value\": %s, ", val);
131 }
132 else
133 sdb_strbuf_append(f->buf, "\"value\": %s, ", tmp);
134 }
135 else if (obj->type == SDB_METRIC) {
136 if (METRIC(obj)->store.type != NULL)
137 sdb_strbuf_append(f->buf, "\"timeseries\": true, ");
138 else
139 sdb_strbuf_append(f->buf, "\"timeseries\": false, ");
140 }
142 /* TODO: make time and interval formats configurable */
143 if (! sdb_strftime(time_str, sizeof(time_str), obj->last_update))
144 snprintf(time_str, sizeof(time_str), "<error>");
145 time_str[sizeof(time_str) - 1] = '\0';
147 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
148 obj->interval))
149 snprintf(interval_str, sizeof(interval_str), "<error>");
150 interval_str[sizeof(interval_str) - 1] = '\0';
152 sdb_strbuf_append(f->buf, "\"last_update\": \"%s\", "
153 "\"update_interval\": \"%s\", \"backends\": [",
154 time_str, interval_str);
156 for (i = 0; i < obj->backends_num; ++i) {
157 sdb_strbuf_append(f->buf, "\"%s\"", obj->backends[i]);
158 if (i < obj->backends_num - 1)
159 sdb_strbuf_append(f->buf, ",");
160 }
161 sdb_strbuf_append(f->buf, "]");
162 return 0;
163 } /* json_emit */
165 /*
166 * public API
167 */
169 sdb_store_json_formatter_t *
170 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags)
171 {
172 sdb_store_json_formatter_t *f;
174 if (! buf)
175 return NULL;
177 if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC))
178 return NULL;
180 f = calloc(1, sizeof(*f));
181 if (! f)
182 return NULL;
184 f->buf = buf;
185 f->context[0] = 0;
186 f->current = 0;
188 f->current_host = NULL;
190 f->type = type;
191 f->flags = flags;
192 return f;
193 } /* sdb_store_json_formatter */
195 int
196 sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
197 {
198 if ((! f) || (! obj))
199 return -1;
201 if ((f->type != SDB_HOST) && (obj->type == SDB_HOST)) {
202 sdb_log(SDB_LOG_ERR, "store: Unexpected object of type host "
203 "during %s JSON serialization",
204 SDB_STORE_TYPE_TO_NAME(f->type));
205 return -1;
206 }
208 /* first top-level object */
209 if (! f->context[0]) {
210 if (obj->type != f->type) {
211 sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
212 "as the first element during %s JSON serialization",
213 SDB_STORE_TYPE_TO_NAME(obj->type),
214 SDB_STORE_TYPE_TO_NAME(f->type));
215 return -1;
216 }
217 if (f->flags & SDB_WANT_ARRAY)
218 sdb_strbuf_append(f->buf, "[");
219 assert(f->current == 0);
220 f->context[0] = obj->type;
221 return json_emit(f, obj);
222 }
224 if ((f->current >= 1) && (obj->type != SDB_ATTRIBUTE)) {
225 /* new entry of a previous type or a new type on the same level;
226 * rewind to the right state */
227 while (f->current > 0) {
228 if (f->context[f->current] == obj->type)
229 break;
230 sdb_strbuf_append(f->buf, "}]");
231 --f->current;
232 }
233 }
234 if ((obj->type == f->type) && (f->type != SDB_HOST))
235 if (obj->parent != f->current_host)
236 sdb_strbuf_append(f->buf, "}]");
238 if (obj->type == f->context[f->current]) {
239 /* new entry of the same type */
240 sdb_strbuf_append(f->buf, "},");
241 }
242 else if ((f->context[f->current] == SDB_HOST)
243 || (obj->type == SDB_ATTRIBUTE)) {
244 assert(obj->type != SDB_HOST);
245 /* all object types may be children of a host;
246 * attributes may be children of any type */
247 sdb_strbuf_append(f->buf, ", \"%ss\": [",
248 SDB_STORE_TYPE_TO_NAME(obj->type));
249 ++f->current;
250 }
251 else {
252 sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
253 "on level %zu during JSON serialization",
254 SDB_STORE_TYPE_TO_NAME(obj->type), f->current);
255 return -1;
256 }
258 json_emit(f, obj);
259 assert(f->current < SDB_STATIC_ARRAY_LEN(f->context));
260 f->context[f->current] = obj->type;
261 return 0;
262 } /* sdb_store_json_emit */
264 int
265 sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
266 sdb_store_matcher_t *filter)
267 {
268 sdb_avltree_t *trees[] = { NULL, NULL, NULL };
269 size_t i;
271 if (sdb_store_json_emit(f, obj))
272 return -1;
274 if (obj->type == SDB_HOST) {
275 trees[0] = HOST(obj)->attributes;
276 trees[1] = HOST(obj)->metrics;
277 trees[2] = HOST(obj)->services;
278 }
279 else if (obj->type == SDB_SERVICE)
280 trees[0] = SVC(obj)->attributes;
281 else if (obj->type == SDB_METRIC)
282 trees[0] = METRIC(obj)->attributes;
283 else if (obj->type == SDB_ATTRIBUTE)
284 return 0;
285 else
286 return -1;
288 for (i = 0; i < SDB_STATIC_ARRAY_LEN(trees); ++i) {
289 sdb_avltree_iter_t *iter;
291 if (! trees[i])
292 continue;
294 iter = sdb_avltree_get_iter(trees[i]);
295 while (sdb_avltree_iter_has_next(iter)) {
296 sdb_store_obj_t *child;
297 child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
299 if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
300 continue;
302 if (sdb_store_json_emit_full(f, child, filter)) {
303 sdb_avltree_iter_destroy(iter);
304 return -1;
305 }
306 }
307 sdb_avltree_iter_destroy(iter);
308 }
309 return 0;
310 } /* sdb_store_json_emit_full */
312 int
313 sdb_store_json_finish(sdb_store_json_formatter_t *f)
314 {
315 if (! f)
316 return -1;
318 if (! f->context[0]) {
319 /* no content */
320 if (f->flags & SDB_WANT_ARRAY)
321 sdb_strbuf_append(f->buf, "[]");
322 return 0;
323 }
325 while (f->current > 0) {
326 sdb_strbuf_append(f->buf, "}]");
327 --f->current;
328 }
329 if (f->context[0] != SDB_HOST) {
330 assert(f->type != SDB_HOST);
331 sdb_strbuf_append(f->buf, "}]}");
332 }
333 else
334 sdb_strbuf_append(f->buf, "}");
336 if (f->flags & SDB_WANT_ARRAY)
337 sdb_strbuf_append(f->buf, "]");
338 return 0;
339 } /* sdb_store_json_finish */
341 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */