Code

28e1e31c2b006fc7fb947e073456f7c5afa79df3
[sysdb.git] / src / frontend / query.c
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)
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)
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)
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)
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)
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)
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                         name[0] ? strdup(name) : NULL,
398                         /* full */ 1, /* filter = */ NULL);
399         status = exec_cmd(conn, ast);
400         sdb_object_deref(SDB_OBJ(ast));
401         return status;
402 } /* sdb_conn_fetch */
404 int
405 sdb_conn_list(sdb_conn_t *conn)
407         sdb_ast_node_t *ast;
408         uint32_t type = SDB_HOST;
409         int status;
411         if ((! conn) || (conn->cmd != SDB_CONNECTION_LIST))
412                 return -1;
414         if (conn->cmd_len == sizeof(uint32_t))
415                 sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
416         else if (conn->cmd_len) {
417                 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
418                                 "LIST command", conn->cmd_len);
419                 sdb_strbuf_sprintf(conn->errbuf, "LIST: Invalid command length %d",
420                                 conn->cmd_len);
421                 return -1;
422         }
424         ast = sdb_ast_list_create((int)type, /* filter = */ NULL);
425         status = exec_cmd(conn, ast);
426         sdb_object_deref(SDB_OBJ(ast));
427         return status;
428 } /* sdb_conn_list */
430 int
431 sdb_conn_lookup(sdb_conn_t *conn)
433         sdb_ast_node_t *ast, *m;
434         const char *matcher;
435         size_t matcher_len;
437         uint32_t type;
438         int status;
440         if ((! conn) || (conn->cmd != SDB_CONNECTION_LOOKUP))
441                 return -1;
443         if (conn->cmd_len < sizeof(uint32_t)) {
444                 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
445                                 "LOOKUP command", conn->cmd_len);
446                 sdb_strbuf_sprintf(conn->errbuf, "LOOKUP: Invalid command length %d",
447                                 conn->cmd_len);
448                 return -1;
449         }
450         sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
452         matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t);
453         matcher_len = conn->cmd_len - sizeof(uint32_t);
454         m = sdb_parser_parse_conditional((int)type,
455                         matcher, (int)matcher_len, conn->errbuf);
456         if (! m) {
457                 char expr[matcher_len + 1];
458                 char err[sdb_strbuf_len(conn->errbuf) + sizeof(expr) + 64];
459                 strncpy(expr, matcher, sizeof(expr));
460                 expr[sizeof(expr) - 1] = '\0';
461                 snprintf(err, sizeof(err), "Failed to parse lookup condition '%s': %s",
462                                 expr, sdb_strbuf_string(conn->errbuf));
463                 sdb_log(SDB_LOG_ERR, "frontend: %s", err);
464                 sdb_strbuf_sprintf(conn->errbuf, "%s", err);
465                 return -1;
466         }
468         ast = sdb_ast_lookup_create((int)type, m, /* filter = */ NULL);
469         status = exec_cmd(conn, ast);
470         if (! ast)
471                 sdb_object_deref(SDB_OBJ(m));
472         sdb_object_deref(SDB_OBJ(ast));
473         return status;
474 } /* sdb_conn_lookup */
476 int
477 sdb_conn_store(sdb_conn_t *conn)
479         sdb_ast_node_t *ast;
480         const char *buf = sdb_strbuf_string(conn->buf);
481         size_t len = conn->cmd_len;
482         uint32_t type;
483         ssize_t n;
484         int status;
486         if ((! conn) || (conn->cmd != SDB_CONNECTION_STORE))
487                 return -1;
489         if ((n = sdb_proto_unmarshal_int32(buf, len, &type)) < 0) {
490                 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %zu for "
491                                 "STORE command", len);
492                 sdb_strbuf_sprintf(conn->errbuf,
493                                 "STORE: Invalid command length %zu", len);
494                 return -1;
495         }
497         switch (type) {
498                 case SDB_HOST:
499                 {
500                         sdb_proto_host_t host;
501                         if (sdb_proto_unmarshal_host(buf, len, &host) < 0) {
502                                 sdb_strbuf_sprintf(conn->errbuf,
503                                                 "STORE: Failed to unmarshal host object");
504                                 return -1;
505                         }
506                         ast = sdb_ast_store_create(SDB_HOST, /* host */ NULL,
507                                         /* parent */ 0, NULL, sstrdup(host.name), host.last_update,
508                                         /* metric store */ NULL, NULL, SDB_DATA_NULL);
509                 }
510                 break;
512                 case SDB_SERVICE:
513                 {
514                         sdb_proto_service_t svc;
515                         if (sdb_proto_unmarshal_service(buf, len, &svc) < 0) {
516                                 sdb_strbuf_sprintf(conn->errbuf,
517                                                 "STORE: Failed to unmarshal service object");
518                                 return -1;
519                         }
520                         ast = sdb_ast_store_create(SDB_SERVICE, sstrdup(svc.hostname),
521                                         /* parent */ 0, NULL, sstrdup(svc.name), svc.last_update,
522                                         /* metric store */ NULL, NULL, SDB_DATA_NULL);
523                 }
524                 break;
526                 case SDB_METRIC:
527                 {
528                         sdb_proto_metric_t metric;
529                         if (sdb_proto_unmarshal_metric(buf, len, &metric) < 0) {
530                                 sdb_strbuf_sprintf(conn->errbuf,
531                                                 "STORE: Failed to unmarshal metric object");
532                                 return -1;
533                         }
534                         ast = sdb_ast_store_create(SDB_METRIC, sstrdup(metric.hostname),
535                                         /* parent */ 0, NULL, sstrdup(metric.name), metric.last_update,
536                                         sstrdup(metric.store_type), sstrdup(metric.store_id),
537                                         SDB_DATA_NULL);
538                 }
539                 break;
540         }
542         if (type & SDB_ATTRIBUTE) {
543                 sdb_proto_attribute_t attr;
544                 const char *hostname, *parent;
545                 int parent_type;
546                 if (sdb_proto_unmarshal_attribute(buf, len, &attr) < 0) {
547                         sdb_strbuf_sprintf(conn->errbuf,
548                                         "STORE: Failed to unmarshal attribute object");
549                         return -1;
550                 }
551                 if (attr.parent_type == SDB_HOST) {
552                         hostname = attr.parent;
553                         parent_type = 0;
554                         parent = NULL;
555                 }
556                 else {
557                         hostname = attr.hostname;
558                         parent_type = attr.parent_type;
559                         parent = attr.parent;
560                 }
561                 ast = sdb_ast_store_create(SDB_ATTRIBUTE, sstrdup(hostname),
562                                 parent_type, sstrdup(parent), sstrdup(attr.key),
563                                 attr.last_update, /* metric store */ NULL, NULL,
564                                 attr.value);
565         }
567         if (! ast) {
568                 sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d for "
569                                 "STORE COMMAND", type);
570                 sdb_strbuf_sprintf(conn->errbuf, "STORE: Invalid object type %d", type);
571                 return -1;
572         }
574         status = sdb_parser_analyze(ast, conn->errbuf);
575         if (! status)
576                 status = exec_cmd(conn, ast);
577         sdb_object_deref(SDB_OBJ(ast));
578         return status;
579 } /* sdb_conn_store */
581 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */