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