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 * metric fetcher:
48 * Implements the callbacks necessary to read a metric object.
49 */
51 typedef struct {
52 char *type;
53 char *id;
54 } metric_store_t;
56 static int
57 metric_fetcher_host(sdb_store_host_t __attribute__((unused)) *host,
58 sdb_object_t __attribute__((unused)) *user_data)
59 {
60 return 0;
61 } /* metric_fetcher_host */
63 static int
64 metric_fetcher_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
65 {
66 metric_store_t *st = SDB_OBJ_WRAPPER(user_data)->data;
68 if ((! metric->store.type) || (! metric->store.id))
69 return 0;
71 st->type = strdup(metric->store.type);
72 st->id = strdup(metric->store.id);
73 if ((! st->type) || (! st->id))
74 return -1;
75 return 0;
76 } /* metric_fetcher_metric */
78 static sdb_store_writer_t metric_fetcher = {
79 metric_fetcher_host, NULL, metric_fetcher_metric, NULL,
80 };
82 /*
83 * private helper functions
84 */
86 static char *
87 sstrdup(const char *s)
88 {
89 return s ? strdup(s) : NULL;
90 } /* sstrdup */
92 static size_t
93 sstrlen(const char *s)
94 {
95 return s ? strlen(s) : 0;
96 } /* sstrlen */
98 static int
99 exec_query(sdb_ast_node_t *ast, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf)
100 {
101 sdb_store_json_formatter_t *f;
102 int type = 0, flags = 0;
103 uint32_t res_type = 0;
104 int status;
106 switch (ast->type) {
107 case SDB_AST_TYPE_FETCH:
108 type = SDB_AST_FETCH(ast)->obj_type;
109 res_type = htonl(SDB_CONNECTION_FETCH);
110 break;
111 case SDB_AST_TYPE_LIST:
112 type = SDB_AST_LIST(ast)->obj_type;
113 flags = SDB_WANT_ARRAY;
114 res_type = htonl(SDB_CONNECTION_LIST);
115 break;
116 case SDB_AST_TYPE_LOOKUP:
117 type = SDB_AST_LOOKUP(ast)->obj_type;
118 flags = SDB_WANT_ARRAY;
119 res_type = htonl(SDB_CONNECTION_LOOKUP);
120 break;
121 default:
122 sdb_strbuf_sprintf(errbuf, "invalid command %s (%#x)",
123 SDB_AST_TYPE_TO_STRING(ast), ast->type);
124 return -1;
125 }
127 f = sdb_store_json_formatter(buf, type, flags);
128 sdb_strbuf_memcpy(buf, &res_type, sizeof(res_type));
129 status = sdb_plugin_query(ast, &sdb_store_json_writer, SDB_OBJ(f), errbuf);
130 if (status < 0)
131 sdb_strbuf_clear(buf);
132 sdb_store_json_finish(f);
133 sdb_object_deref(SDB_OBJ(f));
134 return status;
135 } /* exec_query */
137 static int
138 exec_store(sdb_ast_store_t *st, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf)
139 {
140 char name[sstrlen(st->hostname) + sstrlen(st->parent) + sstrlen(st->name) + 3];
141 sdb_metric_store_t metric_store;
142 int type = st->obj_type, status = -1;
144 switch (st->obj_type) {
145 case SDB_HOST:
146 strncpy(name, st->name, sizeof(name));
147 status = sdb_plugin_store_host(st->name, st->last_update);
148 break;
150 case SDB_SERVICE:
151 snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name);
152 status = sdb_plugin_store_service(st->hostname, st->name, st->last_update);
153 break;
155 case SDB_METRIC:
156 snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name);
157 metric_store.type = st->store_type;
158 metric_store.id = st->store_id;
159 status = sdb_plugin_store_metric(st->hostname, st->name,
160 &metric_store, st->last_update);
161 break;
163 case SDB_ATTRIBUTE:
164 type |= st->parent_type;
166 if (st->parent)
167 snprintf(name, sizeof(name), "%s.%s.%s",
168 st->hostname, st->parent, st->name);
169 else
170 snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name);
172 switch (st->parent_type) {
173 case 0:
174 type |= SDB_HOST;
175 status = sdb_plugin_store_attribute(st->hostname,
176 st->name, &st->value, st->last_update);
177 break;
179 case SDB_SERVICE:
180 status = sdb_plugin_store_service_attribute(st->hostname, st->parent,
181 st->name, &st->value, st->last_update);
182 break;
184 case SDB_METRIC:
185 status = sdb_plugin_store_metric_attribute(st->hostname, st->parent,
186 st->name, &st->value, st->last_update);
187 break;
189 default:
190 sdb_log(SDB_LOG_ERR, "store: Invalid parent type in STORE: %s",
191 SDB_STORE_TYPE_TO_NAME(st->parent_type));
192 return -1;
193 }
194 break;
196 default:
197 sdb_log(SDB_LOG_ERR, "store: Invalid object type in STORE: %s",
198 SDB_STORE_TYPE_TO_NAME(st->obj_type));
199 return -1;
200 }
202 if (status < 0) {
203 sdb_strbuf_sprintf(errbuf, "STORE: Failed to store %s object",
204 SDB_STORE_TYPE_TO_NAME(type));
205 return -1;
206 }
208 if (! status) {
209 sdb_strbuf_sprintf(buf, "Successfully stored %s %s",
210 SDB_STORE_TYPE_TO_NAME(type), name);
211 }
212 else {
213 char type_str[32];
214 strncpy(type_str, SDB_STORE_TYPE_TO_NAME(type), sizeof(type_str));
215 type_str[0] = (char)toupper((int)type_str[0]);
216 sdb_strbuf_sprintf(buf, "%s %s already up to date", type_str, name);
217 }
219 return SDB_CONNECTION_OK;
220 } /* exec_store */
222 static int
223 exec_timeseries(sdb_ast_timeseries_t *ts, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf)
224 {
225 metric_store_t st = { NULL, NULL };
226 sdb_object_wrapper_t obj = SDB_OBJECT_WRAPPER_STATIC(&st);
227 sdb_ast_fetch_t fetch = SDB_AST_FETCH_INIT;
228 sdb_timeseries_opts_t opts = { 0, 0 };
229 sdb_timeseries_t *series = NULL;
230 int status;
232 if ((! ts) || (! ts->hostname) || (! ts->metric))
233 return -1;
235 fetch.obj_type = SDB_METRIC;
236 fetch.hostname = strdup(ts->hostname);
237 fetch.name = strdup(ts->metric);
238 opts.start = ts->start;
239 opts.end = ts->end;
241 status = sdb_plugin_query(SDB_AST_NODE(&fetch),
242 &metric_fetcher, SDB_OBJ(&obj), errbuf);
243 if ((status < 0) || (! st.type) || (! st.id)) {
244 sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series '%s/%s' "
245 "- no data-store configured for the stored metric",
246 ts->hostname, ts->metric);
247 status = -1;
248 }
249 if (status >= 0) {
250 series = sdb_plugin_fetch_timeseries(st.type, st.id, &opts);
251 if (! series) {
252 sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series '%s/%s' "
253 "- %s fetcher callback returned no data for '%s'",
254 ts->hostname, ts->metric, st.type, st.id);
255 status = -1;
256 }
257 }
259 if (status >= 0) {
260 sdb_timeseries_tojson(series, buf);
261 sdb_timeseries_destroy(series);
262 }
264 free(fetch.hostname);
265 free(fetch.name);
266 if (st.type)
267 free(st.type);
268 if (st.id)
269 free(st.id);
270 return status;
271 } /* exec_timeseries */
273 static int
274 exec_cmd(sdb_conn_t *conn, sdb_ast_node_t *ast)
275 {
276 sdb_strbuf_t *buf;
277 int status;
279 if (! ast) {
280 sdb_strbuf_sprintf(conn->errbuf, "out of memory");
281 return -1;
282 }
284 buf = sdb_strbuf_create(1024);
285 if (! buf) {
286 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
287 return -1;
288 }
290 if (ast->type == SDB_AST_TYPE_STORE)
291 status = exec_store(SDB_AST_STORE(ast), buf, conn->errbuf);
292 else if (ast->type == SDB_AST_TYPE_TIMESERIES)
293 status = exec_timeseries(SDB_AST_TIMESERIES(ast), buf, conn->errbuf);
294 else
295 status = exec_query(ast, buf, conn->errbuf);
297 if (status < 0) {
298 char query[conn->cmd_len + 1];
299 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
300 query[sizeof(query) - 1] = '\0';
301 sdb_log(SDB_LOG_ERR, "frontend: failed to execute query '%s'", query);
302 }
303 else
304 sdb_connection_send(conn, status,
305 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
307 sdb_strbuf_destroy(buf);
308 return status < 0 ? status : 0;
309 } /* exec_cmd */
311 /*
312 * public API
313 */
315 int
316 sdb_conn_query(sdb_conn_t *conn)
317 {
318 sdb_llist_t *parsetree;
319 sdb_ast_node_t *ast = NULL;
320 int status = 0;
322 if ((! conn) || (conn->cmd != SDB_CONNECTION_QUERY))
323 return -1;
325 parsetree = sdb_parser_parse(sdb_strbuf_string(conn->buf),
326 (int)conn->cmd_len, conn->errbuf);
327 if (! parsetree) {
328 char query[conn->cmd_len + 1];
329 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
330 query[sizeof(query) - 1] = '\0';
331 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s': %s",
332 query, sdb_strbuf_string(conn->errbuf));
333 return -1;
334 }
336 switch (sdb_llist_len(parsetree)) {
337 case 0:
338 /* skipping empty command; send back an empty reply */
339 sdb_connection_send(conn, SDB_CONNECTION_DATA, 0, NULL);
340 break;
341 case 1:
342 ast = SDB_AST_NODE(sdb_llist_get(parsetree, 0));
343 break;
345 default:
346 {
347 char query[conn->cmd_len + 1];
348 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
349 query[sizeof(query) - 1] = '\0';
350 sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %zu command%s "
351 "in multi-statement query '%s'",
352 sdb_llist_len(parsetree) - 1,
353 sdb_llist_len(parsetree) == 2 ? "" : "s",
354 query);
355 ast = SDB_AST_NODE(sdb_llist_get(parsetree, 0));
356 }
357 }
359 if (ast) {
360 status = exec_cmd(conn, ast);
361 sdb_object_deref(SDB_OBJ(ast));
362 }
363 sdb_llist_destroy(parsetree);
364 return status;
365 } /* sdb_conn_query */
367 int
368 sdb_conn_fetch(sdb_conn_t *conn)
369 {
370 sdb_ast_node_t *ast;
371 char hostname[conn->cmd_len + 1];
372 char name[conn->cmd_len + 1];
373 uint32_t type;
374 int status;
376 if ((! conn) || (conn->cmd != SDB_CONNECTION_FETCH))
377 return -1;
379 if (conn->cmd_len < sizeof(uint32_t)) {
380 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
381 "FETCH command", conn->cmd_len);
382 sdb_strbuf_sprintf(conn->errbuf, "FETCH: Invalid command length %d",
383 conn->cmd_len);
384 return -1;
385 }
387 /* TODO: support other types besides hosts */
388 hostname[0] = '\0';
390 sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
391 strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(uint32_t),
392 conn->cmd_len - sizeof(uint32_t));
393 name[sizeof(name) - 1] = '\0';
395 ast = sdb_ast_fetch_create((int)type,
396 hostname[0] ? strdup(hostname) : NULL,
397 -1, NULL,
398 name[0] ? strdup(name) : NULL,
399 /* full */ 1, /* filter = */ NULL);
400 status = exec_cmd(conn, ast);
401 sdb_object_deref(SDB_OBJ(ast));
402 return status;
403 } /* sdb_conn_fetch */
405 int
406 sdb_conn_list(sdb_conn_t *conn)
407 {
408 sdb_ast_node_t *ast;
409 uint32_t type = SDB_HOST;
410 int status;
412 if ((! conn) || (conn->cmd != SDB_CONNECTION_LIST))
413 return -1;
415 if (conn->cmd_len == sizeof(uint32_t))
416 sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
417 else if (conn->cmd_len) {
418 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
419 "LIST command", conn->cmd_len);
420 sdb_strbuf_sprintf(conn->errbuf, "LIST: Invalid command length %d",
421 conn->cmd_len);
422 return -1;
423 }
425 ast = sdb_ast_list_create((int)type, /* filter = */ NULL);
426 status = exec_cmd(conn, ast);
427 sdb_object_deref(SDB_OBJ(ast));
428 return status;
429 } /* sdb_conn_list */
431 int
432 sdb_conn_lookup(sdb_conn_t *conn)
433 {
434 sdb_ast_node_t *ast, *m;
435 const char *matcher;
436 size_t matcher_len;
438 uint32_t type;
439 int status;
441 if ((! conn) || (conn->cmd != SDB_CONNECTION_LOOKUP))
442 return -1;
444 if (conn->cmd_len < sizeof(uint32_t)) {
445 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
446 "LOOKUP command", conn->cmd_len);
447 sdb_strbuf_sprintf(conn->errbuf, "LOOKUP: Invalid command length %d",
448 conn->cmd_len);
449 return -1;
450 }
451 sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
453 matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t);
454 matcher_len = conn->cmd_len - sizeof(uint32_t);
455 m = sdb_parser_parse_conditional((int)type,
456 matcher, (int)matcher_len, conn->errbuf);
457 if (! m) {
458 char expr[matcher_len + 1];
459 char err[sdb_strbuf_len(conn->errbuf) + sizeof(expr) + 64];
460 strncpy(expr, matcher, sizeof(expr));
461 expr[sizeof(expr) - 1] = '\0';
462 snprintf(err, sizeof(err), "Failed to parse lookup condition '%s': %s",
463 expr, sdb_strbuf_string(conn->errbuf));
464 sdb_log(SDB_LOG_ERR, "frontend: %s", err);
465 sdb_strbuf_sprintf(conn->errbuf, "%s", err);
466 return -1;
467 }
469 ast = sdb_ast_lookup_create((int)type, m, /* filter = */ NULL);
470 status = exec_cmd(conn, ast);
471 if (! ast)
472 sdb_object_deref(SDB_OBJ(m));
473 sdb_object_deref(SDB_OBJ(ast));
474 return status;
475 } /* sdb_conn_lookup */
477 int
478 sdb_conn_store(sdb_conn_t *conn)
479 {
480 sdb_ast_node_t *ast;
481 const char *buf = sdb_strbuf_string(conn->buf);
482 size_t len = conn->cmd_len;
483 uint32_t type;
484 ssize_t n;
485 int status;
487 if ((! conn) || (conn->cmd != SDB_CONNECTION_STORE))
488 return -1;
490 if ((n = sdb_proto_unmarshal_int32(buf, len, &type)) < 0) {
491 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %zu for "
492 "STORE command", len);
493 sdb_strbuf_sprintf(conn->errbuf,
494 "STORE: Invalid command length %zu", len);
495 return -1;
496 }
498 switch (type) {
499 case SDB_HOST:
500 {
501 sdb_proto_host_t host;
502 if (sdb_proto_unmarshal_host(buf, len, &host) < 0) {
503 sdb_strbuf_sprintf(conn->errbuf,
504 "STORE: Failed to unmarshal host object");
505 return -1;
506 }
507 ast = sdb_ast_store_create(SDB_HOST, /* host */ NULL,
508 /* parent */ 0, NULL, sstrdup(host.name), host.last_update,
509 /* metric store */ NULL, NULL, SDB_DATA_NULL);
510 }
511 break;
513 case SDB_SERVICE:
514 {
515 sdb_proto_service_t svc;
516 if (sdb_proto_unmarshal_service(buf, len, &svc) < 0) {
517 sdb_strbuf_sprintf(conn->errbuf,
518 "STORE: Failed to unmarshal service object");
519 return -1;
520 }
521 ast = sdb_ast_store_create(SDB_SERVICE, sstrdup(svc.hostname),
522 /* parent */ 0, NULL, sstrdup(svc.name), svc.last_update,
523 /* metric store */ NULL, NULL, SDB_DATA_NULL);
524 }
525 break;
527 case SDB_METRIC:
528 {
529 sdb_proto_metric_t metric;
530 if (sdb_proto_unmarshal_metric(buf, len, &metric) < 0) {
531 sdb_strbuf_sprintf(conn->errbuf,
532 "STORE: Failed to unmarshal metric object");
533 return -1;
534 }
535 ast = sdb_ast_store_create(SDB_METRIC, sstrdup(metric.hostname),
536 /* parent */ 0, NULL, sstrdup(metric.name), metric.last_update,
537 sstrdup(metric.store_type), sstrdup(metric.store_id),
538 SDB_DATA_NULL);
539 }
540 break;
541 }
543 if (type & SDB_ATTRIBUTE) {
544 sdb_proto_attribute_t attr;
545 const char *hostname, *parent;
546 int parent_type;
547 if (sdb_proto_unmarshal_attribute(buf, len, &attr) < 0) {
548 sdb_strbuf_sprintf(conn->errbuf,
549 "STORE: Failed to unmarshal attribute object");
550 return -1;
551 }
552 if (attr.parent_type == SDB_HOST) {
553 hostname = attr.parent;
554 parent_type = 0;
555 parent = NULL;
556 }
557 else {
558 hostname = attr.hostname;
559 parent_type = attr.parent_type;
560 parent = attr.parent;
561 }
562 ast = sdb_ast_store_create(SDB_ATTRIBUTE, sstrdup(hostname),
563 parent_type, sstrdup(parent), sstrdup(attr.key),
564 attr.last_update, /* metric store */ NULL, NULL,
565 attr.value);
566 }
568 if (! ast) {
569 sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d for "
570 "STORE COMMAND", type);
571 sdb_strbuf_sprintf(conn->errbuf, "STORE: Invalid object type %d", type);
572 return -1;
573 }
575 status = sdb_parser_analyze(ast, conn->errbuf);
576 if (! status)
577 status = exec_cmd(conn, ast);
578 sdb_object_deref(SDB_OBJ(ast));
579 return status;
580 } /* sdb_conn_store */
582 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */