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