1 /*
2 * SysDB - src/core/store_json.c
3 * Copyright (C) 2013-2015 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_object_t super;
53 /* The string buffer to write to */
54 sdb_strbuf_t *buf;
56 /* The context describes the state of the formatter through
57 * the path pointing to the current object */
58 int context[8];
59 size_t current;
61 int type;
62 int flags;
63 };
64 #define F(obj) ((sdb_store_json_formatter_t *)(obj))
66 static int
67 formatter_init(sdb_object_t *obj, va_list ap)
68 {
69 sdb_store_json_formatter_t *f = F(obj);
71 f->buf = va_arg(ap, sdb_strbuf_t *);
72 if (! f->buf)
73 return -1;
75 f->type = va_arg(ap, int);
76 if ((f->type != SDB_HOST) && (f->type != SDB_SERVICE) && (f->type != SDB_METRIC))
77 return -1;
79 f->flags = va_arg(ap, int);
81 f->context[0] = 0;
82 f->current = 0;
83 return 0;
84 } /* formatter_init */
86 static sdb_type_t formatter_type = {
87 /* size = */ sizeof(sdb_store_json_formatter_t),
88 /* init = */ formatter_init,
89 /* destroy = */ NULL,
90 };
92 /*
93 * private helper functions
94 */
96 static void
97 escape_string(const char *src, char *dest)
98 {
99 size_t i = 1;
100 dest[0] = '"';
101 for ( ; *src; ++src) {
102 char c = *src;
103 if ((c == '"') || (c == '\\') || iscntrl((int)c)) {
104 dest[i] = '\\';
105 ++i;
106 }
107 switch (c) {
108 case '\a': dest[i] = 'a'; break;
109 case '\b': dest[i] = 'b'; break;
110 case '\t': dest[i] = 't'; break;
111 case '\n': dest[i] = 'n'; break;
112 case '\v': dest[i] = 'v'; break;
113 case '\f': dest[i] = 'f'; break;
114 case '\r': dest[i] = 'r'; break;
115 default: dest[i] = c; break;
116 }
117 ++i;
118 }
119 dest[i] = '"';
120 dest[i + 1] = '\0';
121 } /* escape_string */
123 static int
124 json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
125 {
126 char time_str[64];
127 char interval_str[64];
128 char name[2 * strlen(SDB_OBJ(obj)->name) + 3];
129 size_t i;
131 assert(f && obj);
133 escape_string(SDB_OBJ(obj)->name, name);
134 sdb_strbuf_append(f->buf, "{\"name\": %s, ", name);
135 if (obj->type == SDB_ATTRIBUTE) {
136 char tmp[sdb_data_strlen(&ATTR(obj)->value) + 1];
137 char val[2 * sizeof(tmp) + 3];
138 if (! sdb_data_format(&ATTR(obj)->value, tmp, sizeof(tmp),
139 SDB_DOUBLE_QUOTED))
140 snprintf(tmp, sizeof(tmp), "<error>");
142 if (tmp[0] == '"') {
143 /* a string; escape_string handles quoting */
144 tmp[strlen(tmp) - 1] = '\0';
145 escape_string(tmp + 1, val);
146 sdb_strbuf_append(f->buf, "\"value\": %s, ", val);
147 }
148 else
149 sdb_strbuf_append(f->buf, "\"value\": %s, ", tmp);
150 }
151 else if (obj->type == SDB_METRIC) {
152 if (METRIC(obj)->store.type != NULL)
153 sdb_strbuf_append(f->buf, "\"timeseries\": true, ");
154 else
155 sdb_strbuf_append(f->buf, "\"timeseries\": false, ");
156 }
158 /* TODO: make time and interval formats configurable */
159 if (! sdb_strftime(time_str, sizeof(time_str), obj->last_update))
160 snprintf(time_str, sizeof(time_str), "<error>");
161 time_str[sizeof(time_str) - 1] = '\0';
163 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
164 obj->interval))
165 snprintf(interval_str, sizeof(interval_str), "<error>");
166 interval_str[sizeof(interval_str) - 1] = '\0';
168 sdb_strbuf_append(f->buf, "\"last_update\": \"%s\", "
169 "\"update_interval\": \"%s\", \"backends\": [",
170 time_str, interval_str);
172 for (i = 0; i < obj->backends_num; ++i) {
173 sdb_strbuf_append(f->buf, "\"%s\"", obj->backends[i]);
174 if (i < obj->backends_num - 1)
175 sdb_strbuf_append(f->buf, ",");
176 }
177 sdb_strbuf_append(f->buf, "]");
178 return 0;
179 } /* json_emit */
181 /*
182 * public API
183 */
185 sdb_store_json_formatter_t *
186 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags)
187 {
188 return F(sdb_object_create("json-formatter", formatter_type,
189 buf, type, flags));
190 } /* sdb_store_json_formatter */
192 int
193 sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
194 {
195 if ((! f) || (! obj))
196 return -1;
198 /* first top-level object */
199 if (! f->context[0]) {
200 if ((obj->type != f->type) && (obj->type != SDB_HOST)) {
201 sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
202 "as the first element during %s JSON serialization",
203 SDB_STORE_TYPE_TO_NAME(obj->type),
204 SDB_STORE_TYPE_TO_NAME(f->type));
205 return -1;
206 }
207 if (f->flags & SDB_WANT_ARRAY)
208 sdb_strbuf_append(f->buf, "[");
209 assert(f->current == 0);
210 }
211 else {
212 if ((f->current >= 1) && (obj->type != SDB_ATTRIBUTE)) {
213 /* new entry of a previous type or a new type on the same level;
214 * rewind to the right state */
215 while (f->current > 0) {
216 if (f->context[f->current] == obj->type)
217 break;
218 sdb_strbuf_append(f->buf, "}]");
219 --f->current;
220 }
221 }
223 if (obj->type == f->context[f->current]) {
224 /* new entry of the same type */
225 sdb_strbuf_append(f->buf, "},");
226 }
227 else if ((f->context[f->current] == SDB_HOST)
228 || (obj->type == SDB_ATTRIBUTE)) {
229 assert(obj->type != SDB_HOST);
230 /* all object types may be children of a host;
231 * attributes may be children of any type */
232 sdb_strbuf_append(f->buf, ", \"%ss\": [",
233 SDB_STORE_TYPE_TO_NAME(obj->type));
234 ++f->current;
235 }
236 else {
237 sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
238 "on level %zu during JSON serialization",
239 SDB_STORE_TYPE_TO_NAME(obj->type), f->current);
240 return -1;
241 }
242 }
244 json_emit(f, obj);
245 assert(f->current < SDB_STATIC_ARRAY_LEN(f->context));
246 f->context[f->current] = obj->type;
247 return 0;
248 } /* sdb_store_json_emit */
250 int
251 sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
252 sdb_store_matcher_t *filter)
253 {
254 sdb_avltree_t *trees[] = { NULL, NULL, NULL };
255 size_t i;
257 if (sdb_store_json_emit(f, obj))
258 return -1;
260 if (obj->type == SDB_HOST) {
261 trees[0] = HOST(obj)->attributes;
262 trees[1] = HOST(obj)->metrics;
263 trees[2] = HOST(obj)->services;
264 }
265 else if (obj->type == SDB_SERVICE)
266 trees[0] = SVC(obj)->attributes;
267 else if (obj->type == SDB_METRIC)
268 trees[0] = METRIC(obj)->attributes;
269 else if (obj->type == SDB_ATTRIBUTE)
270 return 0;
271 else
272 return -1;
274 for (i = 0; i < SDB_STATIC_ARRAY_LEN(trees); ++i) {
275 sdb_avltree_iter_t *iter;
277 if (! trees[i])
278 continue;
280 iter = sdb_avltree_get_iter(trees[i]);
281 while (sdb_avltree_iter_has_next(iter)) {
282 sdb_store_obj_t *child;
283 child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
285 if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
286 continue;
288 if (sdb_store_json_emit_full(f, child, filter)) {
289 sdb_avltree_iter_destroy(iter);
290 return -1;
291 }
292 }
293 sdb_avltree_iter_destroy(iter);
294 }
295 return 0;
296 } /* sdb_store_json_emit_full */
298 int
299 sdb_store_json_finish(sdb_store_json_formatter_t *f)
300 {
301 if (! f)
302 return -1;
304 if (! f->context[0]) {
305 /* no content */
306 if (f->flags & SDB_WANT_ARRAY)
307 sdb_strbuf_append(f->buf, "[]");
308 return 0;
309 }
311 while (f->current > 0) {
312 sdb_strbuf_append(f->buf, "}]");
313 --f->current;
314 }
315 sdb_strbuf_append(f->buf, "}");
317 if (f->flags & SDB_WANT_ARRAY)
318 sdb_strbuf_append(f->buf, "]");
319 return 0;
320 } /* sdb_store_json_finish */
322 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */