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 <stdlib.h>
44 /*
45 * private data types
46 */
48 struct sdb_store_json_formatter {
49 sdb_strbuf_t *buf;
51 /* The context describes the state of the formatter through
52 * the path pointing to the current object */
53 int context[8];
54 size_t current;
55 };
57 /*
58 * private helper functions
59 */
61 static int
62 json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
63 {
64 char time_str[64];
65 char interval_str[64];
66 size_t i;
68 assert(f && obj);
70 sdb_strbuf_append(f->buf, "{\"name\": \"%s\", ", SDB_OBJ(obj)->name);
71 if (obj->type == SDB_ATTRIBUTE) {
72 char tmp[sdb_data_strlen(&ATTR(obj)->value) + 1];
73 if (sdb_data_format(&ATTR(obj)->value, tmp, sizeof(tmp),
74 SDB_DOUBLE_QUOTED) < 0)
75 snprintf(tmp, sizeof(tmp), "<error>");
76 sdb_strbuf_append(f->buf, "\"value\": %s, ", tmp);
77 }
79 /* TODO: make time and interval formats configurable */
80 if (! sdb_strftime(time_str, sizeof(time_str),
81 "%F %T %z", obj->last_update))
82 snprintf(time_str, sizeof(time_str), "<error>");
83 time_str[sizeof(time_str) - 1] = '\0';
85 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
86 obj->interval))
87 snprintf(interval_str, sizeof(interval_str), "<error>");
88 interval_str[sizeof(interval_str) - 1] = '\0';
90 sdb_strbuf_append(f->buf, "\"last_update\": \"%s\", "
91 "\"update_interval\": \"%s\", \"backends\": [",
92 time_str, interval_str);
94 for (i = 0; i < obj->backends_num; ++i) {
95 sdb_strbuf_append(f->buf, "\"%s\"", obj->backends[i]);
96 if (i < obj->backends_num - 1)
97 sdb_strbuf_append(f->buf, ",");
98 }
99 sdb_strbuf_append(f->buf, "]");
100 return 0;
101 } /* json_emit */
103 /*
104 * public API
105 */
107 sdb_store_json_formatter_t *
108 sdb_store_json_formatter(sdb_strbuf_t *buf)
109 {
110 sdb_store_json_formatter_t *f;
112 if (! buf)
113 return NULL;
115 f = calloc(1, sizeof(*f));
116 if (! f)
117 return NULL;
119 f->buf = buf;
120 f->context[0] = 0;
121 f->current = 0;
122 return f;
123 } /* sdb_store_json_formatter */
125 int
126 sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
127 {
128 if ((! f) || (! obj))
129 return -1;
131 /* first host */
132 if (! f->context[0]) {
133 assert(f->current == 0);
134 f->context[0] = SDB_HOST;
135 return json_emit(f, obj);
136 }
138 if (obj->type == f->context[f->current]) {
139 /* new entry of the same type */
140 sdb_strbuf_append(f->buf, "},");
141 }
142 else if ((f->context[f->current] == SDB_HOST)
143 || (obj->type == SDB_ATTRIBUTE)) {
144 assert(obj->type != SDB_HOST);
145 /* all object types may be children of a host;
146 * attributes may be children of any type */
147 sdb_strbuf_append(f->buf, ", \"%ss\": [",
148 SDB_STORE_TYPE_TO_NAME(obj->type));
149 ++f->current;
150 }
151 else if (f->current >= 1) {
152 /* new entry of a previous type or a new type on the same level
153 * -> rewind to the right state and then handle the new object */
154 assert(obj->type != SDB_ATTRIBUTE);
155 while (f->current > 0) {
156 if (f->context[f->current] == obj->type)
157 break;
158 assert(f->context[f->current] != SDB_HOST);
159 sdb_strbuf_append(f->buf, "}]");
160 --f->current;
161 }
162 return sdb_store_json_emit(f, obj);
163 }
164 else {
165 sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
166 "on level %zu during JSON serialization",
167 SDB_STORE_TYPE_TO_NAME(obj->type), f->current);
168 return -1;
169 }
171 json_emit(f, obj);
172 assert(f->current < SDB_STATIC_ARRAY_LEN(f->context));
173 f->context[f->current] = obj->type;
174 return 0;
175 } /* sdb_store_json_emit */
177 int
178 sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
179 sdb_store_matcher_t *filter)
180 {
181 sdb_avltree_t *trees[] = { NULL, NULL, NULL };
182 size_t i;
184 if (sdb_store_json_emit(f, obj))
185 return -1;
187 if (obj->type == SDB_HOST) {
188 trees[0] = HOST(obj)->attributes;
189 trees[1] = HOST(obj)->metrics;
190 trees[2] = HOST(obj)->services;
191 }
192 else if (obj->type == SDB_SERVICE)
193 trees[0] = SVC(obj)->attributes;
194 else if (obj->type == SDB_METRIC)
195 trees[0] = METRIC(obj)->attributes;
196 else if (obj->type == SDB_ATTRIBUTE)
197 return 0;
198 else
199 return -1;
201 for (i = 0; i < SDB_STATIC_ARRAY_LEN(trees); ++i) {
202 sdb_avltree_iter_t *iter;
204 if (! trees[i])
205 continue;
207 iter = sdb_avltree_get_iter(trees[i]);
208 while (sdb_avltree_iter_has_next(iter)) {
209 sdb_store_obj_t *child;
210 child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
212 if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
213 continue;
215 if (sdb_store_json_emit_full(f, child, filter)) {
216 sdb_avltree_iter_destroy(iter);
217 return -1;
218 }
219 }
220 sdb_avltree_iter_destroy(iter);
221 }
222 return 0;
223 } /* sdb_store_json_emit_full */
225 int
226 sdb_store_json_finish(sdb_store_json_formatter_t *f)
227 {
228 if (! f)
229 return -1;
231 while (f->current > 0) {
232 sdb_strbuf_append(f->buf, "}]");
233 --f->current;
234 }
235 if (f->context[0])
236 sdb_strbuf_append(f->buf, "}");
237 return 0;
238 } /* sdb_store_json_finish */
240 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */