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