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) < 0)
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 }
136 /* TODO: make time and interval formats configurable */
137 if (! sdb_strftime(time_str, sizeof(time_str),
138 "%F %T %z", obj->last_update))
139 snprintf(time_str, sizeof(time_str), "<error>");
140 time_str[sizeof(time_str) - 1] = '\0';
142 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
143 obj->interval))
144 snprintf(interval_str, sizeof(interval_str), "<error>");
145 interval_str[sizeof(interval_str) - 1] = '\0';
147 sdb_strbuf_append(f->buf, "\"last_update\": \"%s\", "
148 "\"update_interval\": \"%s\", \"backends\": [",
149 time_str, interval_str);
151 for (i = 0; i < obj->backends_num; ++i) {
152 sdb_strbuf_append(f->buf, "\"%s\"", obj->backends[i]);
153 if (i < obj->backends_num - 1)
154 sdb_strbuf_append(f->buf, ",");
155 }
156 sdb_strbuf_append(f->buf, "]");
157 return 0;
158 } /* json_emit */
160 /*
161 * public API
162 */
164 sdb_store_json_formatter_t *
165 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags)
166 {
167 sdb_store_json_formatter_t *f;
169 if (! buf)
170 return NULL;
172 if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC))
173 return NULL;
175 f = calloc(1, sizeof(*f));
176 if (! f)
177 return NULL;
179 f->buf = buf;
180 f->context[0] = 0;
181 f->current = 0;
183 f->current_host = NULL;
185 f->type = type;
186 f->flags = flags;
187 return f;
188 } /* sdb_store_json_formatter */
190 int
191 sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
192 {
193 if ((! f) || (! obj))
194 return -1;
196 if ((f->type != SDB_HOST) && (obj->type == SDB_HOST)) {
197 sdb_log(SDB_LOG_ERR, "store: Unexpected object of type host "
198 "during %s JSON serialization",
199 SDB_STORE_TYPE_TO_NAME(f->type));
200 return -1;
201 }
203 /* first top-level object */
204 if (! f->context[0]) {
205 if (obj->type != f->type) {
206 sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
207 "as the first element during %s JSON serialization",
208 SDB_STORE_TYPE_TO_NAME(obj->type),
209 SDB_STORE_TYPE_TO_NAME(f->type));
210 return -1;
211 }
212 if (f->flags & SDB_WANT_ARRAY)
213 sdb_strbuf_append(f->buf, "[");
214 assert(f->current == 0);
215 f->context[0] = obj->type;
216 return json_emit(f, obj);
217 }
219 if ((f->current >= 1) && (obj->type != SDB_ATTRIBUTE)) {
220 /* new entry of a previous type or a new type on the same level;
221 * rewind to the right state */
222 while (f->current > 0) {
223 if (f->context[f->current] == obj->type)
224 break;
225 sdb_strbuf_append(f->buf, "}]");
226 --f->current;
227 }
228 }
229 if ((obj->type == f->type) && (f->type != SDB_HOST))
230 if (obj->parent != f->current_host)
231 sdb_strbuf_append(f->buf, "}]");
233 if (obj->type == f->context[f->current]) {
234 /* new entry of the same type */
235 sdb_strbuf_append(f->buf, "},");
236 }
237 else if ((f->context[f->current] == SDB_HOST)
238 || (obj->type == SDB_ATTRIBUTE)) {
239 assert(obj->type != SDB_HOST);
240 /* all object types may be children of a host;
241 * attributes may be children of any type */
242 sdb_strbuf_append(f->buf, ", \"%ss\": [",
243 SDB_STORE_TYPE_TO_NAME(obj->type));
244 ++f->current;
245 }
246 else {
247 sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
248 "on level %zu during JSON serialization",
249 SDB_STORE_TYPE_TO_NAME(obj->type), f->current);
250 return -1;
251 }
253 json_emit(f, obj);
254 assert(f->current < SDB_STATIC_ARRAY_LEN(f->context));
255 f->context[f->current] = obj->type;
256 return 0;
257 } /* sdb_store_json_emit */
259 int
260 sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
261 sdb_store_matcher_t *filter)
262 {
263 sdb_avltree_t *trees[] = { NULL, NULL, NULL };
264 size_t i;
266 if (sdb_store_json_emit(f, obj))
267 return -1;
269 if (obj->type == SDB_HOST) {
270 trees[0] = HOST(obj)->attributes;
271 trees[1] = HOST(obj)->metrics;
272 trees[2] = HOST(obj)->services;
273 }
274 else if (obj->type == SDB_SERVICE)
275 trees[0] = SVC(obj)->attributes;
276 else if (obj->type == SDB_METRIC)
277 trees[0] = METRIC(obj)->attributes;
278 else if (obj->type == SDB_ATTRIBUTE)
279 return 0;
280 else
281 return -1;
283 for (i = 0; i < SDB_STATIC_ARRAY_LEN(trees); ++i) {
284 sdb_avltree_iter_t *iter;
286 if (! trees[i])
287 continue;
289 iter = sdb_avltree_get_iter(trees[i]);
290 while (sdb_avltree_iter_has_next(iter)) {
291 sdb_store_obj_t *child;
292 child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
294 if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
295 continue;
297 if (sdb_store_json_emit_full(f, child, filter)) {
298 sdb_avltree_iter_destroy(iter);
299 return -1;
300 }
301 }
302 sdb_avltree_iter_destroy(iter);
303 }
304 return 0;
305 } /* sdb_store_json_emit_full */
307 int
308 sdb_store_json_finish(sdb_store_json_formatter_t *f)
309 {
310 if (! f)
311 return -1;
313 if (! f->context[0]) {
314 /* no content */
315 if (f->flags & SDB_WANT_ARRAY)
316 sdb_strbuf_append(f->buf, "[]");
317 return 0;
318 }
320 while (f->current > 0) {
321 sdb_strbuf_append(f->buf, "}]");
322 --f->current;
323 }
324 if (f->context[0] != SDB_HOST) {
325 assert(f->type != SDB_HOST);
326 sdb_strbuf_append(f->buf, "}]}");
327 }
328 else
329 sdb_strbuf_append(f->buf, "}");
331 if (f->flags & SDB_WANT_ARRAY)
332 sdb_strbuf_append(f->buf, "]");
333 return 0;
334 } /* sdb_store_json_finish */
336 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */