1 /*
2 * SysDB - src/frontend/query.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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include "sysdb.h"
34 #include "core/plugin.h"
35 #include "frontend/connection-private.h"
36 #include "parser/ast.h"
37 #include "parser/parser.h"
38 #include "utils/error.h"
39 #include "utils/proto.h"
40 #include "utils/strbuf.h"
42 #include <errno.h>
43 #include <ctype.h>
44 #include <string.h>
46 /*
47 * private helper functions
48 */
50 static char *
51 sstrdup(const char *s)
52 {
53 return s ? strdup(s) : NULL;
54 } /* sstrdup */
56 static size_t
57 sstrlen(const char *s)
58 {
59 return s ? strlen(s) : 0;
60 } /* sstrlen */
62 static int
63 exec_store(sdb_ast_store_t *st, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf)
64 {
65 char name[sstrlen(st->hostname) + sstrlen(st->parent) + sstrlen(st->name) + 3];
66 sdb_metric_store_t metric_store;
67 int type = st->obj_type, status = -1;
69 switch (st->obj_type) {
70 case SDB_HOST:
71 strncpy(name, st->name, sizeof(name));
72 status = sdb_plugin_store_host(st->name, st->last_update);
73 break;
75 case SDB_SERVICE:
76 snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name);
77 status = sdb_plugin_store_service(st->hostname, st->name, st->last_update);
78 break;
80 case SDB_METRIC:
81 snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name);
82 metric_store.type = st->store_type;
83 metric_store.id = st->store_id;
84 status = sdb_plugin_store_metric(st->hostname, st->name,
85 &metric_store, st->last_update);
86 break;
88 case SDB_ATTRIBUTE:
89 type |= st->parent_type;
91 if (st->parent)
92 snprintf(name, sizeof(name), "%s.%s.%s",
93 st->hostname, st->parent, st->name);
94 else
95 snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name);
97 switch (st->parent_type) {
98 case 0:
99 type |= SDB_HOST;
100 status = sdb_plugin_store_attribute(st->hostname,
101 st->name, &st->value, st->last_update);
102 break;
104 case SDB_SERVICE:
105 status = sdb_plugin_store_service_attribute(st->hostname, st->parent,
106 st->name, &st->value, st->last_update);
107 break;
109 case SDB_METRIC:
110 status = sdb_plugin_store_metric_attribute(st->hostname, st->parent,
111 st->name, &st->value, st->last_update);
112 break;
114 default:
115 sdb_log(SDB_LOG_ERR, "store: Invalid parent type in STORE: %s",
116 SDB_STORE_TYPE_TO_NAME(st->parent_type));
117 return -1;
118 }
119 break;
121 default:
122 sdb_log(SDB_LOG_ERR, "store: Invalid object type in STORE: %s",
123 SDB_STORE_TYPE_TO_NAME(st->obj_type));
124 return -1;
125 }
127 if (status < 0) {
128 sdb_strbuf_sprintf(errbuf, "STORE: Failed to store %s object",
129 SDB_STORE_TYPE_TO_NAME(type));
130 return -1;
131 }
133 if (! status) {
134 sdb_strbuf_sprintf(buf, "Successfully stored %s %s",
135 SDB_STORE_TYPE_TO_NAME(type), name);
136 }
137 else {
138 char type_str[32];
139 strncpy(type_str, SDB_STORE_TYPE_TO_NAME(type), sizeof(type_str));
140 type_str[0] = (char)toupper((int)type_str[0]);
141 sdb_strbuf_sprintf(buf, "%s %s already up to date", type_str, name);
142 }
144 return SDB_CONNECTION_OK;
145 } /* exec_store */
147 static int
148 exec_query(sdb_conn_t *conn, sdb_ast_node_t *ast)
149 {
150 sdb_strbuf_t *buf;
151 int status;
153 if (! ast) {
154 sdb_strbuf_sprintf(conn->errbuf, "out of memory");
155 return -1;
156 }
158 buf = sdb_strbuf_create(1024);
159 if (! buf) {
160 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
161 return -1;
162 }
163 if (ast->type == SDB_AST_TYPE_STORE)
164 status = exec_store(SDB_AST_STORE(ast), buf, conn->errbuf);
165 else
166 status = sdb_plugin_query(ast, buf, conn->errbuf);
167 if (status < 0) {
168 char query[conn->cmd_len + 1];
169 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
170 query[sizeof(query) - 1] = '\0';
171 sdb_log(SDB_LOG_ERR, "frontend: failed to execute query '%s'", query);
172 }
173 else
174 sdb_connection_send(conn, status,
175 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
177 sdb_strbuf_destroy(buf);
178 return status < 0 ? status : 0;
179 } /* exec_query */
181 /*
182 * public API
183 */
185 int
186 sdb_conn_query(sdb_conn_t *conn)
187 {
188 sdb_llist_t *parsetree;
189 sdb_ast_node_t *ast = NULL;
190 int status = 0;
192 if ((! conn) || (conn->cmd != SDB_CONNECTION_QUERY))
193 return -1;
195 parsetree = sdb_parser_parse(sdb_strbuf_string(conn->buf),
196 (int)conn->cmd_len, conn->errbuf);
197 if (! parsetree) {
198 char query[conn->cmd_len + 1];
199 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
200 query[sizeof(query) - 1] = '\0';
201 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s': %s",
202 query, sdb_strbuf_string(conn->errbuf));
203 return -1;
204 }
206 switch (sdb_llist_len(parsetree)) {
207 case 0:
208 /* skipping empty command; send back an empty reply */
209 sdb_connection_send(conn, SDB_CONNECTION_DATA, 0, NULL);
210 break;
211 case 1:
212 ast = SDB_AST_NODE(sdb_llist_get(parsetree, 0));
213 break;
215 default:
216 {
217 char query[conn->cmd_len + 1];
218 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
219 query[sizeof(query) - 1] = '\0';
220 sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %zu command%s "
221 "in multi-statement query '%s'",
222 sdb_llist_len(parsetree) - 1,
223 sdb_llist_len(parsetree) == 2 ? "" : "s",
224 query);
225 ast = SDB_AST_NODE(sdb_llist_get(parsetree, 0));
226 }
227 }
229 if (ast) {
230 status = exec_query(conn, ast);
231 sdb_object_deref(SDB_OBJ(ast));
232 }
233 sdb_llist_destroy(parsetree);
234 return status;
235 } /* sdb_conn_query */
237 int
238 sdb_conn_fetch(sdb_conn_t *conn)
239 {
240 sdb_ast_node_t *ast;
241 char hostname[conn->cmd_len + 1];
242 char name[conn->cmd_len + 1];
243 uint32_t type;
244 int status;
246 if ((! conn) || (conn->cmd != SDB_CONNECTION_FETCH))
247 return -1;
249 if (conn->cmd_len < sizeof(uint32_t)) {
250 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
251 "FETCH command", conn->cmd_len);
252 sdb_strbuf_sprintf(conn->errbuf, "FETCH: Invalid command length %d",
253 conn->cmd_len);
254 return -1;
255 }
257 /* TODO: support other types besides hosts */
258 hostname[0] = '\0';
260 sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
261 strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(uint32_t),
262 conn->cmd_len - sizeof(uint32_t));
263 name[sizeof(name) - 1] = '\0';
265 ast = sdb_ast_fetch_create((int)type,
266 hostname[0] ? strdup(hostname) : NULL,
267 name[0] ? strdup(name) : NULL,
268 /* filter = */ NULL);
269 status = exec_query(conn, ast);
270 sdb_object_deref(SDB_OBJ(ast));
271 return status;
272 } /* sdb_conn_fetch */
274 int
275 sdb_conn_list(sdb_conn_t *conn)
276 {
277 sdb_ast_node_t *ast;
278 uint32_t type = SDB_HOST;
279 int status;
281 if ((! conn) || (conn->cmd != SDB_CONNECTION_LIST))
282 return -1;
284 if (conn->cmd_len == sizeof(uint32_t))
285 sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
286 else if (conn->cmd_len) {
287 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
288 "LIST command", conn->cmd_len);
289 sdb_strbuf_sprintf(conn->errbuf, "LIST: Invalid command length %d",
290 conn->cmd_len);
291 return -1;
292 }
294 ast = sdb_ast_list_create((int)type, /* filter = */ NULL);
295 status = exec_query(conn, ast);
296 sdb_object_deref(SDB_OBJ(ast));
297 return status;
298 } /* sdb_conn_list */
300 int
301 sdb_conn_lookup(sdb_conn_t *conn)
302 {
303 sdb_ast_node_t *ast, *m;
304 const char *matcher;
305 size_t matcher_len;
307 uint32_t type;
308 int status;
310 if ((! conn) || (conn->cmd != SDB_CONNECTION_LOOKUP))
311 return -1;
313 if (conn->cmd_len < sizeof(uint32_t)) {
314 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
315 "LOOKUP command", conn->cmd_len);
316 sdb_strbuf_sprintf(conn->errbuf, "LOOKUP: Invalid command length %d",
317 conn->cmd_len);
318 return -1;
319 }
320 sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
322 matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t);
323 matcher_len = conn->cmd_len - sizeof(uint32_t);
324 m = sdb_parser_parse_conditional((int)type,
325 matcher, (int)matcher_len, conn->errbuf);
326 if (! m) {
327 char expr[matcher_len + 1];
328 char err[sdb_strbuf_len(conn->errbuf) + sizeof(expr) + 64];
329 strncpy(expr, matcher, sizeof(expr));
330 expr[sizeof(expr) - 1] = '\0';
331 snprintf(err, sizeof(err), "Failed to parse lookup condition '%s': %s",
332 expr, sdb_strbuf_string(conn->errbuf));
333 sdb_log(SDB_LOG_ERR, "frontend: %s", err);
334 sdb_strbuf_sprintf(conn->errbuf, "%s", err);
335 return -1;
336 }
338 ast = sdb_ast_lookup_create((int)type, m, /* filter = */ NULL);
339 status = exec_query(conn, ast);
340 if (! ast)
341 sdb_object_deref(SDB_OBJ(m));
342 sdb_object_deref(SDB_OBJ(ast));
343 return status;
344 } /* sdb_conn_lookup */
346 int
347 sdb_conn_store(sdb_conn_t *conn)
348 {
349 sdb_ast_node_t *ast;
350 const char *buf = sdb_strbuf_string(conn->buf);
351 size_t len = conn->cmd_len;
352 uint32_t type;
353 ssize_t n;
354 int status;
356 if ((! conn) || (conn->cmd != SDB_CONNECTION_STORE))
357 return -1;
359 if ((n = sdb_proto_unmarshal_int32(buf, len, &type)) < 0) {
360 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %zu for "
361 "STORE command", len);
362 sdb_strbuf_sprintf(conn->errbuf,
363 "STORE: Invalid command length %zu", len);
364 return -1;
365 }
367 switch (type) {
368 case SDB_HOST:
369 {
370 sdb_proto_host_t host;
371 if (sdb_proto_unmarshal_host(buf, len, &host) < 0) {
372 sdb_strbuf_sprintf(conn->errbuf,
373 "STORE: Failed to unmarshal host object");
374 return -1;
375 }
376 ast = sdb_ast_store_create(SDB_HOST, /* host */ NULL,
377 /* parent */ 0, NULL, sstrdup(host.name), host.last_update,
378 /* metric store */ NULL, NULL, SDB_DATA_NULL);
379 }
380 break;
382 case SDB_SERVICE:
383 {
384 sdb_proto_service_t svc;
385 if (sdb_proto_unmarshal_service(buf, len, &svc) < 0) {
386 sdb_strbuf_sprintf(conn->errbuf,
387 "STORE: Failed to unmarshal service object");
388 return -1;
389 }
390 ast = sdb_ast_store_create(SDB_SERVICE, sstrdup(svc.hostname),
391 /* parent */ 0, NULL, sstrdup(svc.name), svc.last_update,
392 /* metric store */ NULL, NULL, SDB_DATA_NULL);
393 }
394 break;
396 case SDB_METRIC:
397 {
398 sdb_proto_metric_t metric;
399 if (sdb_proto_unmarshal_metric(buf, len, &metric) < 0) {
400 sdb_strbuf_sprintf(conn->errbuf,
401 "STORE: Failed to unmarshal metric object");
402 return -1;
403 }
404 ast = sdb_ast_store_create(SDB_METRIC, sstrdup(metric.hostname),
405 /* parent */ 0, NULL, sstrdup(metric.name), metric.last_update,
406 sstrdup(metric.store_type), sstrdup(metric.store_id),
407 SDB_DATA_NULL);
408 }
409 break;
410 }
412 if (type & SDB_ATTRIBUTE) {
413 sdb_proto_attribute_t attr;
414 const char *hostname, *parent;
415 int parent_type;
416 if (sdb_proto_unmarshal_attribute(buf, len, &attr) < 0) {
417 sdb_strbuf_sprintf(conn->errbuf,
418 "STORE: Failed to unmarshal attribute object");
419 return -1;
420 }
421 if (attr.parent_type == SDB_HOST) {
422 hostname = attr.parent;
423 parent_type = 0;
424 parent = NULL;
425 }
426 else {
427 hostname = attr.hostname;
428 parent_type = attr.parent_type;
429 parent = attr.parent;
430 }
431 ast = sdb_ast_store_create(SDB_ATTRIBUTE, sstrdup(hostname),
432 parent_type, sstrdup(parent), sstrdup(attr.key),
433 attr.last_update, /* metric store */ NULL, NULL,
434 attr.value);
435 }
437 if (! ast) {
438 sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d for "
439 "STORE COMMAND", type);
440 sdb_strbuf_sprintf(conn->errbuf, "STORE: Invalid object type %d", type);
441 return -1;
442 }
444 status = sdb_parser_analyze(ast, conn->errbuf);
445 if (! status)
446 status = exec_query(conn, ast);
447 sdb_object_deref(SDB_OBJ(ast));
448 return status;
449 } /* sdb_conn_store */
451 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */