Code

frontend, proto: Include object type in FETCH and LOOKUP messages.
[sysdb.git] / src / frontend / query.c
1 /*
2  * SysDB - src/frontend/query.c
3  * Copyright (C) 2013 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 #include "sysdb.h"
30 #include "core/store.h"
31 #include "frontend/connection-private.h"
32 #include "frontend/parser.h"
33 #include "utils/error.h"
34 #include "utils/proto.h"
35 #include "utils/strbuf.h"
37 #include <errno.h>
38 #include <string.h>
40 /*
41  * private helper functions
42  */
44 typedef struct {
45         sdb_strbuf_t *buf;
46         sdb_store_matcher_t *filter;
48         size_t last_len;
49 } tojson_data_t;
51 static int
52 lookup_tojson(sdb_store_obj_t *obj, void *user_data)
53 {
54         tojson_data_t *data = user_data;
55         int status;
57         if (data->filter && (! sdb_store_matcher_matches(data->filter, obj, NULL)))
58                 return 0;
60         if (sdb_strbuf_len(data->buf) > data->last_len)
61                 sdb_strbuf_append(data->buf, ",");
62         data->last_len = sdb_strbuf_len(data->buf);
63         status = sdb_store_host_tojson(obj, data->buf,
64                         data->filter, /* flags = */ 0);
65         return status;
66 } /* lookup_tojson */
68 /*
69  * public API
70  */
72 int
73 sdb_fe_query(sdb_conn_t *conn)
74 {
75         sdb_llist_t *parsetree;
76         sdb_conn_node_t *node = NULL;
77         int status = 0;
79         if ((! conn) || (conn->cmd != CONNECTION_QUERY))
80                 return -1;
82         parsetree = sdb_fe_parse(sdb_strbuf_string(conn->buf),
83                         (int)conn->cmd_len);
84         if (! parsetree) {
85                 char query[conn->cmd_len + 1];
86                 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
87                 query[sizeof(query) - 1] = '\0';
88                 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s'",
89                                 query);
90                 return -1;
91         }
93         switch (sdb_llist_len(parsetree)) {
94                 case 0:
95                         /* skipping empty command; send back an empty reply */
96                         sdb_connection_send(conn, CONNECTION_DATA, 0, NULL);
97                         break;
98                 case 1:
99                         node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
100                         break;
102                 default:
103                         {
104                                 char query[conn->cmd_len + 1];
105                                 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
106                                 query[sizeof(query) - 1] = '\0';
107                                 sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %zu command%s "
108                                                 "in multi-statement query '%s'",
109                                                 sdb_llist_len(parsetree) - 1,
110                                                 sdb_llist_len(parsetree) == 2 ? "" : "s",
111                                                 query);
112                                 node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
113                         }
114         }
116         if (node) {
117                 status = sdb_fe_exec(conn, node);
118                 sdb_object_deref(SDB_OBJ(node));
119         }
121         sdb_llist_destroy(parsetree);
122         return status;
123 } /* sdb_fe_query */
125 int
126 sdb_fe_fetch(sdb_conn_t *conn)
128         char name[conn->cmd_len + 1];
129         int type;
131         if ((! conn) || (conn->cmd != CONNECTION_FETCH))
132                 return -1;
134         if (conn->cmd_len < sizeof(type)) {
135                 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
136                                 "FETCH command", conn->cmd_len);
137                 sdb_strbuf_sprintf(conn->errbuf, "FETCH: Invalid command length %d",
138                                 conn->cmd_len);
139                 return -1;
140         }
142         type = sdb_proto_get_int(conn->buf, 0);
143         strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(type),
144                         conn->cmd_len - sizeof(type));
145         name[sizeof(name) - 1] = '\0';
146         return sdb_fe_exec_fetch(conn, type, name, /* filter = */ NULL);
147 } /* sdb_fe_fetch */
149 int
150 sdb_fe_list(sdb_conn_t *conn)
152         int type = SDB_HOST;
154         if ((! conn) || (conn->cmd != CONNECTION_LIST))
155                 return -1;
157         if (conn->cmd_len == sizeof(uint32_t))
158                 type = sdb_proto_get_int(conn->buf, 0);
159         else if (conn->cmd_len) {
160                 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
161                                 "LIST command", conn->cmd_len);
162                 sdb_strbuf_sprintf(conn->errbuf, "LIST: Invalid command length %d",
163                                 conn->cmd_len);
164                 return -1;
165         }
166         return sdb_fe_exec_list(conn, type, /* filter = */ NULL);
167 } /* sdb_fe_list */
169 int
170 sdb_fe_lookup(sdb_conn_t *conn)
172         sdb_store_matcher_t *m;
173         const char *matcher;
174         size_t matcher_len;
176         uint32_t type;
177         int status;
179         if ((! conn) || (conn->cmd != CONNECTION_LOOKUP))
180                 return -1;
182         if (conn->cmd_len < sizeof(type)) {
183                 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
184                                 "LOOKUP command", conn->cmd_len);
185                 sdb_strbuf_sprintf(conn->errbuf, "LOOKUP: Invalid command length %d",
186                                 conn->cmd_len);
187                 return -1;
188         }
189         type = sdb_proto_get_int(conn->buf, 0);
191         matcher = sdb_strbuf_string(conn->buf) + sizeof(type);
192         matcher_len = conn->cmd_len - sizeof(type);
193         m = sdb_fe_parse_matcher(matcher, (int)matcher_len);
194         if (! m) {
195                 char expr[matcher_len + 1];
196                 strncpy(expr, matcher, sizeof(expr));
197                 expr[sizeof(expr) - 1] = '\0';
198                 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse "
199                                 "lookup condition '%s'", expr);
200                 return -1;
201         }
203         status = sdb_fe_exec_lookup(conn, type, m, /* filter = */ NULL);
204         sdb_object_deref(SDB_OBJ(m));
205         return status;
206 } /* sdb_fe_lookup */
208 int
209 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
211         sdb_store_matcher_t *m = NULL, *filter = NULL;
213         if (! node)
214                 return -1;
216         switch (node->cmd) {
217                 case CONNECTION_FETCH:
218                         if (CONN_FETCH(node)->filter)
219                                 filter = CONN_FETCH(node)->filter->matcher;
220                         return sdb_fe_exec_fetch(conn, CONN_FETCH(node)->type,
221                                         CONN_FETCH(node)->name, filter);
222                 case CONNECTION_LIST:
223                         if (CONN_LIST(node)->filter)
224                                 filter = CONN_LIST(node)->filter->matcher;
225                         return sdb_fe_exec_list(conn, CONN_LIST(node)->type, filter);
226                 case CONNECTION_LOOKUP:
227                         if (CONN_LOOKUP(node)->matcher)
228                                 m = CONN_LOOKUP(node)->matcher->matcher;
229                         if (CONN_LOOKUP(node)->filter)
230                                 filter = CONN_LOOKUP(node)->filter->matcher;
231                         return sdb_fe_exec_lookup(conn,
232                                         CONN_LOOKUP(node)->type, m, filter);
233                 case CONNECTION_TIMESERIES:
234                         return sdb_fe_exec_timeseries(conn,
235                                         CONN_TS(node)->hostname, CONN_TS(node)->metric,
236                                         &CONN_TS(node)->opts);
238                 default:
239                         sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
240                         return -1;
241         }
242         return -1;
243 } /* sdb_fe_exec */
245 int
246 sdb_fe_exec_fetch(sdb_conn_t *conn, int type, const char *name,
247                 sdb_store_matcher_t *filter)
249         sdb_strbuf_t *buf;
250         sdb_store_obj_t *host;
251         uint32_t res_type = htonl(CONNECTION_FETCH);
253         /* XXX: support other types */
254         if (type != SDB_HOST) {
255                 sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d "
256                                 "in FETCH command", type);
257                 sdb_strbuf_sprintf(conn->errbuf,
258                                 "FETCH: Invalid object type %d", type);
259                 return -1;
260         }
262         host = sdb_store_get_host(name);
263         if (! host) {
264                 sdb_log(SDB_LOG_DEBUG, "frontend: Failed to fetch host '%s': "
265                                 "not found", name);
267                 sdb_strbuf_sprintf(conn->errbuf, "Host %s not found", name);
268                 return -1;
269         }
271         buf = sdb_strbuf_create(1024);
272         if (! buf) {
273                 char errbuf[1024];
274                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
275                                 "buffer to handle FETCH command: %s",
276                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
278                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
279                 sdb_strbuf_destroy(buf);
280                 sdb_object_deref(SDB_OBJ(host));
281                 return -1;
282         }
284         sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
285         if (sdb_store_host_tojson(host, buf, filter, /* flags = */ 0)) {
286                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
287                                 "host '%s' to JSON", name);
288                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
289                 sdb_strbuf_destroy(buf);
290                 sdb_object_deref(SDB_OBJ(host));
291                 return -1;
292         }
294         sdb_connection_send(conn, CONNECTION_DATA,
295                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
296         sdb_strbuf_destroy(buf);
297         sdb_object_deref(SDB_OBJ(host));
298         return 0;
299 } /* sdb_fe_exec_fetch */
301 int
302 sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter)
304         sdb_strbuf_t *buf;
305         uint32_t res_type = htonl(CONNECTION_LIST);
307         int flags;
309         if (type == SDB_HOST)
310                 flags = SDB_SKIP_ALL;
311         else if (type == SDB_SERVICE)
312                 flags = (SDB_SKIP_ALL & (~SDB_SKIP_SERVICES))
313                         | SDB_SKIP_EMPTY_SERVICES;
314         else if (type == SDB_METRIC)
315                 flags = (SDB_SKIP_ALL & (~SDB_SKIP_METRICS))
316                         | SDB_SKIP_EMPTY_METRICS;
317         else {
318                 sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d "
319                                 "for LIST command", type);
320                 sdb_strbuf_sprintf(conn->errbuf,
321                                 "LIST: Invalid object type %d", type);
322                 return -1;
323         }
325         buf = sdb_strbuf_create(1024);
326         if (! buf) {
327                 char errbuf[1024];
328                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
329                                 "buffer to handle LIST command: %s",
330                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
332                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
333                 sdb_strbuf_destroy(buf);
334                 return -1;
335         }
337         sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
338         if (sdb_store_tojson(buf, filter, flags)) {
339                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
340                                 "store to JSON");
341                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
342                 sdb_strbuf_destroy(buf);
343                 return -1;
344         }
346         sdb_connection_send(conn, CONNECTION_DATA,
347                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
348         sdb_strbuf_destroy(buf);
349         return 0;
350 } /* sdb_fe_exec_list */
352 int
353 sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
354                 sdb_store_matcher_t *m, sdb_store_matcher_t *filter)
356         tojson_data_t data = { NULL, filter, 0 };
357         uint32_t res_type = htonl(CONNECTION_LOOKUP);
359         /* XXX: support other types */
360         if (type != SDB_HOST) {
361                 sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d "
362                                 "in LOOKUP command", type);
363                 sdb_strbuf_sprintf(conn->errbuf,
364                                 "LOOKUP: Invalid object type %d", type);
365                 return -1;
366         }
368         data.buf = sdb_strbuf_create(1024);
369         if (! data.buf) {
370                 char errbuf[1024];
371                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
372                                 "buffer to handle LOOKUP command: %s",
373                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
375                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
376                 sdb_strbuf_destroy(data.buf);
377                 return -1;
378         }
380         sdb_strbuf_memcpy(data.buf, &res_type, sizeof(uint32_t));
381         sdb_strbuf_append(data.buf, "[");
383         /* Let the JSON serializer handle the filter instead of the scanner. Else,
384          * we'd have to filter twice -- once in the scanner and then again in the
385          * serializer. */
386         data.last_len = sdb_strbuf_len(data.buf);
387         if (sdb_store_scan(m, /* filter */ NULL, lookup_tojson, &data)) {
388                 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
389                 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
390                 sdb_strbuf_destroy(data.buf);
391                 return -1;
392         }
394         sdb_strbuf_append(data.buf, "]");
396         sdb_connection_send(conn, CONNECTION_DATA,
397                         (uint32_t)sdb_strbuf_len(data.buf), sdb_strbuf_string(data.buf));
398         sdb_strbuf_destroy(data.buf);
399         return 0;
400 } /* sdb_fe_exec_lookup */
402 int
403 sdb_fe_exec_timeseries(sdb_conn_t *conn,
404                 const char *hostname, const char *metric,
405                 sdb_timeseries_opts_t *opts)
407         sdb_strbuf_t *buf;
408         uint32_t res_type = htonl(CONNECTION_TIMESERIES);
410         buf = sdb_strbuf_create(1024);
411         if (! buf) {
412                 char errbuf[1024];
413                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
414                                 "buffer to handle TIMESERIES command: %s",
415                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
417                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
418                 return -1;
419         }
421         sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
422         if (sdb_store_fetch_timeseries(hostname, metric, opts, buf)) {
423                 sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series");
424                 sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch time-series");
425                 sdb_strbuf_destroy(buf);
426                 return -1;
427         }
429         sdb_connection_send(conn, CONNECTION_DATA,
430                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
431         sdb_strbuf_destroy(buf);
432         return 0;
433 } /* sdb_fe_exec_timeseries */
435 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */