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 uint32_t res_type = htonl(SDB_CONNECTION_TIMESERIES);
253 sdb_strbuf_memcpy(buf, &res_type, sizeof(res_type));
254 sdb_timeseries_tojson(series, buf);
255 sdb_timeseries_destroy(series);
256 }
257 else {
258 sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series '%s/%s' "
259 "- %s fetcher callback returned no data for '%s'",
260 ts->hostname, ts->metric, st.type, st.id);
261 status = -1;
262 }
263 }
265 free(fetch.hostname);
266 free(fetch.name);
267 if (st.type)
268 free(st.type);
269 if (st.id)
270 free(st.id);
271 return status;
272 } /* exec_timeseries */
274 static int
275 exec_cmd(sdb_conn_t *conn, sdb_ast_node_t *ast)
276 {
277 sdb_strbuf_t *buf;
278 int status;
280 if (! ast) {
281 sdb_strbuf_sprintf(conn->errbuf, "out of memory");
282 return -1;
283 }
285 buf = sdb_strbuf_create(1024);
286 if (! buf) {
287 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
288 return -1;
289 }
291 if (ast->type == SDB_AST_TYPE_STORE)
292 status = exec_store(SDB_AST_STORE(ast), buf, conn->errbuf);
293 else if (ast->type == SDB_AST_TYPE_TIMESERIES)
294 status = exec_timeseries(SDB_AST_TIMESERIES(ast), buf, conn->errbuf);
295 else
296 status = exec_query(ast, buf, conn->errbuf);
298 if (status < 0) {
299 char query[conn->cmd_len + 1];
300 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
301 query[sizeof(query) - 1] = '\0';
302 sdb_log(SDB_LOG_ERR, "frontend: failed to execute query '%s'", query);
303 }
304 else
305 sdb_connection_send(conn, status,
306 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
308 sdb_strbuf_destroy(buf);
309 return status < 0 ? status : 0;
310 } /* exec_cmd */
312 /*
313 * public API
314 */
316 int
317 sdb_conn_query(sdb_conn_t *conn)
318 {
319 sdb_llist_t *parsetree;
320 sdb_ast_node_t *ast = NULL;
321 int status = 0;
323 if ((! conn) || (conn->cmd != SDB_CONNECTION_QUERY))
324 return -1;
326 parsetree = sdb_parser_parse(sdb_strbuf_string(conn->buf),
327 (int)conn->cmd_len, conn->errbuf);
328 if (! parsetree) {
329 char query[conn->cmd_len + 1];
330 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
331 query[sizeof(query) - 1] = '\0';
332 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s': %s",
333 query, sdb_strbuf_string(conn->errbuf));
334 return -1;
335 }
337 switch (sdb_llist_len(parsetree)) {
338 case 0:
339 /* skipping empty command; send back an empty reply */
340 sdb_connection_send(conn, SDB_CONNECTION_DATA, 0, NULL);
341 break;
342 case 1:
343 ast = SDB_AST_NODE(sdb_llist_get(parsetree, 0));
344 break;
346 default:
347 {
348 char query[conn->cmd_len + 1];
349 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
350 query[sizeof(query) - 1] = '\0';
351 sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %zu command%s "
352 "in multi-statement query '%s'",
353 sdb_llist_len(parsetree) - 1,
354 sdb_llist_len(parsetree) == 2 ? "" : "s",
355 query);
356 ast = SDB_AST_NODE(sdb_llist_get(parsetree, 0));
357 }
358 }
360 if (ast) {
361 status = exec_cmd(conn, ast);
362 sdb_object_deref(SDB_OBJ(ast));
363 }
364 sdb_llist_destroy(parsetree);
365 return status;
366 } /* sdb_conn_query */
368 int
369 sdb_conn_fetch(sdb_conn_t *conn)
370 {
371 sdb_ast_node_t *ast;
372 char hostname[conn->cmd_len + 1];
373 char name[conn->cmd_len + 1];
374 uint32_t type;
375 int status;
377 if ((! conn) || (conn->cmd != SDB_CONNECTION_FETCH))
378 return -1;
380 if (conn->cmd_len < sizeof(uint32_t)) {
381 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
382 "FETCH command", conn->cmd_len);
383 sdb_strbuf_sprintf(conn->errbuf, "FETCH: Invalid command length %d",
384 conn->cmd_len);
385 return -1;
386 }
388 /* TODO: support other types besides hosts */
389 hostname[0] = '\0';
391 sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
392 strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(uint32_t),
393 conn->cmd_len - sizeof(uint32_t));
394 name[sizeof(name) - 1] = '\0';
396 ast = sdb_ast_fetch_create((int)type,
397 hostname[0] ? strdup(hostname) : NULL,
398 -1, NULL,
399 name[0] ? strdup(name) : NULL,
400 /* full */ 1, /* filter = */ NULL);
401 status = exec_cmd(conn, ast);
402 sdb_object_deref(SDB_OBJ(ast));
403 return status;
404 } /* sdb_conn_fetch */
406 int
407 sdb_conn_list(sdb_conn_t *conn)
408 {
409 sdb_ast_node_t *ast;
410 uint32_t type = SDB_HOST;
411 int status;
413 if ((! conn) || (conn->cmd != SDB_CONNECTION_LIST))
414 return -1;
416 if (conn->cmd_len == sizeof(uint32_t))
417 sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
418 else if (conn->cmd_len) {
419 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
420 "LIST command", conn->cmd_len);
421 sdb_strbuf_sprintf(conn->errbuf, "LIST: Invalid command length %d",
422 conn->cmd_len);
423 return -1;
424 }
426 ast = sdb_ast_list_create((int)type, /* filter = */ NULL);
427 status = exec_cmd(conn, ast);
428 sdb_object_deref(SDB_OBJ(ast));
429 return status;
430 } /* sdb_conn_list */
432 int
433 sdb_conn_lookup(sdb_conn_t *conn)
434 {
435 sdb_ast_node_t *ast, *m;
436 const char *matcher;
437 size_t matcher_len;
439 uint32_t type;
440 int status;
442 if ((! conn) || (conn->cmd != SDB_CONNECTION_LOOKUP))
443 return -1;
445 if (conn->cmd_len < sizeof(uint32_t)) {
446 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
447 "LOOKUP command", conn->cmd_len);
448 sdb_strbuf_sprintf(conn->errbuf, "LOOKUP: Invalid command length %d",
449 conn->cmd_len);
450 return -1;
451 }
452 sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
454 matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t);
455 matcher_len = conn->cmd_len - sizeof(uint32_t);
456 m = sdb_parser_parse_conditional((int)type,
457 matcher, (int)matcher_len, conn->errbuf);
458 if (! m) {
459 char expr[matcher_len + 1];
460 char err[sdb_strbuf_len(conn->errbuf) + sizeof(expr) + 64];
461 strncpy(expr, matcher, sizeof(expr));
462 expr[sizeof(expr) - 1] = '\0';
463 snprintf(err, sizeof(err), "Failed to parse lookup condition '%s': %s",
464 expr, sdb_strbuf_string(conn->errbuf));
465 sdb_log(SDB_LOG_ERR, "frontend: %s", err);
466 sdb_strbuf_sprintf(conn->errbuf, "%s", err);
467 return -1;
468 }
470 ast = sdb_ast_lookup_create((int)type, m, /* filter = */ NULL);
471 status = exec_cmd(conn, ast);
472 if (! ast)
473 sdb_object_deref(SDB_OBJ(m));
474 sdb_object_deref(SDB_OBJ(ast));
475 return status;
476 } /* sdb_conn_lookup */
478 int
479 sdb_conn_store(sdb_conn_t *conn)
480 {
481 sdb_ast_node_t *ast = NULL;
482 const char *buf = sdb_strbuf_string(conn->buf);
483 size_t len = conn->cmd_len;
484 uint32_t type;
485 ssize_t n;
486 int status;
488 if ((! conn) || (conn->cmd != SDB_CONNECTION_STORE))
489 return -1;
491 if ((n = sdb_proto_unmarshal_int32(buf, len, &type)) < 0) {
492 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %zu for "
493 "STORE command", len);
494 sdb_strbuf_sprintf(conn->errbuf,
495 "STORE: Invalid command length %zu", len);
496 return -1;
497 }
499 switch (type) {
500 case SDB_HOST:
501 {
502 sdb_proto_host_t host;
503 if (sdb_proto_unmarshal_host(buf, len, &host) < 0) {
504 sdb_strbuf_sprintf(conn->errbuf,
505 "STORE: Failed to unmarshal host object");
506 return -1;
507 }
508 ast = sdb_ast_store_create(SDB_HOST, /* host */ NULL,
509 /* parent */ 0, NULL, sstrdup(host.name), host.last_update,
510 /* metric store */ NULL, NULL, SDB_DATA_NULL);
511 }
512 break;
514 case SDB_SERVICE:
515 {
516 sdb_proto_service_t svc;
517 if (sdb_proto_unmarshal_service(buf, len, &svc) < 0) {
518 sdb_strbuf_sprintf(conn->errbuf,
519 "STORE: Failed to unmarshal service object");
520 return -1;
521 }
522 ast = sdb_ast_store_create(SDB_SERVICE, sstrdup(svc.hostname),
523 /* parent */ 0, NULL, sstrdup(svc.name), svc.last_update,
524 /* metric store */ NULL, NULL, SDB_DATA_NULL);
525 }
526 break;
528 case SDB_METRIC:
529 {
530 sdb_proto_metric_t metric;
531 if (sdb_proto_unmarshal_metric(buf, len, &metric) < 0) {
532 sdb_strbuf_sprintf(conn->errbuf,
533 "STORE: Failed to unmarshal metric object");
534 return -1;
535 }
536 ast = sdb_ast_store_create(SDB_METRIC, sstrdup(metric.hostname),
537 /* parent */ 0, NULL, sstrdup(metric.name), metric.last_update,
538 sstrdup(metric.store_type), sstrdup(metric.store_id),
539 SDB_DATA_NULL);
540 }
541 break;
542 }
544 if (type & SDB_ATTRIBUTE) {
545 sdb_proto_attribute_t attr;
546 const char *hostname, *parent;
547 int parent_type;
548 if (sdb_proto_unmarshal_attribute(buf, len, &attr) < 0) {
549 sdb_strbuf_sprintf(conn->errbuf,
550 "STORE: Failed to unmarshal attribute object");
551 return -1;
552 }
553 if (attr.parent_type == SDB_HOST) {
554 hostname = attr.parent;
555 parent_type = 0;
556 parent = NULL;
557 }
558 else {
559 hostname = attr.hostname;
560 parent_type = attr.parent_type;
561 parent = attr.parent;
562 }
563 ast = sdb_ast_store_create(SDB_ATTRIBUTE, sstrdup(hostname),
564 parent_type, sstrdup(parent), sstrdup(attr.key),
565 attr.last_update, /* metric store */ NULL, NULL,
566 attr.value);
567 }
569 if (! ast) {
570 sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d for "
571 "STORE COMMAND", type);
572 sdb_strbuf_sprintf(conn->errbuf, "STORE: Invalid object type %d", type);
573 return -1;
574 }
576 status = sdb_parser_analyze(ast, conn->errbuf);
577 if (! status)
578 status = exec_cmd(conn, ast);
579 sdb_object_deref(SDB_OBJ(ast));
580 return status;
581 } /* sdb_conn_store */
583 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */