X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Ffrontend%2Fquery.c;h=2a1954abc9a87ebc966dff97e1af47244f76645a;hp=724144aafb91a63369efb98cd0c97a55b5a7f8a9;hb=HEAD;hpb=ef3a4a955deddc45d5f381ce20653cac3ca51656 diff --git a/src/frontend/query.c b/src/frontend/query.c index 724144a..2a1954a 100644 --- a/src/frontend/query.c +++ b/src/frontend/query.c @@ -40,8 +40,55 @@ #include "utils/strbuf.h" #include +#include #include +/* + * metric fetcher: + * Implements the callbacks necessary to read a metric object. + */ + +typedef struct { + char *type; + char *id; + sdb_time_t last_update; +} metric_store_t; + +static int +metric_fetcher_host(sdb_store_host_t __attribute__((unused)) *host, + sdb_object_t __attribute__((unused)) *user_data) +{ + return 0; +} /* metric_fetcher_host */ + +static int +metric_fetcher_metric(sdb_store_metric_t *metric, sdb_object_t *user_data) +{ + metric_store_t *st = SDB_OBJ_WRAPPER(user_data)->data; + sdb_time_t last_update = 0; + size_t idx = 0, i; + + if (! metric->stores_num) + return -1; + + /* Find the most up to date data store. + * TODO: Consider merging multiple results? */ + for (i = 0; i < metric->stores_num; ++i) { + if (metric->stores[i].last_update > last_update) { + last_update = metric->stores[i].last_update; + idx = i; + } + } + st->type = strdup(metric->stores[idx].type); + st->id = strdup(metric->stores[idx].id); + st->last_update = metric->stores[idx].last_update; + return 0; +} /* metric_fetcher_metric */ + +static sdb_store_writer_t metric_fetcher = { + metric_fetcher_host, NULL, metric_fetcher_metric, NULL, +}; + /* * private helper functions */ @@ -52,8 +99,195 @@ sstrdup(const char *s) return s ? strdup(s) : NULL; } /* sstrdup */ +static size_t +sstrlen(const char *s) +{ + return s ? strlen(s) : 0; +} /* sstrlen */ + +static int +exec_query(sdb_ast_node_t *ast, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf) +{ + sdb_store_json_formatter_t *f; + int type = 0, flags = 0; + uint32_t res_type = 0; + int status; + + switch (ast->type) { + case SDB_AST_TYPE_FETCH: + type = SDB_AST_FETCH(ast)->obj_type; + res_type = htonl(SDB_CONNECTION_FETCH); + break; + case SDB_AST_TYPE_LIST: + type = SDB_AST_LIST(ast)->obj_type; + flags = SDB_WANT_ARRAY; + res_type = htonl(SDB_CONNECTION_LIST); + break; + case SDB_AST_TYPE_LOOKUP: + type = SDB_AST_LOOKUP(ast)->obj_type; + flags = SDB_WANT_ARRAY; + res_type = htonl(SDB_CONNECTION_LOOKUP); + break; + default: + sdb_strbuf_sprintf(errbuf, "invalid command %s (%#x)", + SDB_AST_TYPE_TO_STRING(ast), ast->type); + return -1; + } + + f = sdb_store_json_formatter(buf, type, flags); + sdb_strbuf_memcpy(buf, &res_type, sizeof(res_type)); + status = sdb_plugin_query(ast, &sdb_store_json_writer, SDB_OBJ(f), + &(sdb_query_opts_t){ true }, errbuf); + if (status < 0) + sdb_strbuf_clear(buf); + sdb_store_json_finish(f); + sdb_object_deref(SDB_OBJ(f)); + return status; +} /* exec_query */ + +static int +exec_store(sdb_ast_store_t *st, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf) +{ + char name[sstrlen(st->hostname) + sstrlen(st->parent) + sstrlen(st->name) + 3]; + sdb_metric_store_t metric_store; + int type = st->obj_type, status = -1; + + switch (st->obj_type) { + case SDB_HOST: + strncpy(name, st->name, sizeof(name)); + status = sdb_plugin_store_host(st->name, st->last_update); + break; + + case SDB_SERVICE: + snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name); + status = sdb_plugin_store_service(st->hostname, st->name, st->last_update); + break; + + case SDB_METRIC: + snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name); + metric_store.type = st->store_type; + metric_store.id = st->store_id; + metric_store.last_update = st->store_last_update; + status = sdb_plugin_store_metric(st->hostname, st->name, + &metric_store, st->last_update); + break; + + case SDB_ATTRIBUTE: + type |= st->parent_type; + + if (st->parent) + snprintf(name, sizeof(name), "%s.%s.%s", + st->hostname, st->parent, st->name); + else + snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name); + + switch (st->parent_type) { + case 0: + type |= SDB_HOST; + status = sdb_plugin_store_attribute(st->hostname, + st->name, &st->value, st->last_update); + break; + + case SDB_SERVICE: + status = sdb_plugin_store_service_attribute(st->hostname, st->parent, + st->name, &st->value, st->last_update); + break; + + case SDB_METRIC: + status = sdb_plugin_store_metric_attribute(st->hostname, st->parent, + st->name, &st->value, st->last_update); + break; + + default: + sdb_log(SDB_LOG_ERR, "store: Invalid parent type in STORE: %s", + SDB_STORE_TYPE_TO_NAME(st->parent_type)); + return -1; + } + break; + + default: + sdb_log(SDB_LOG_ERR, "store: Invalid object type in STORE: %s", + SDB_STORE_TYPE_TO_NAME(st->obj_type)); + return -1; + } + + if (status < 0) { + sdb_strbuf_sprintf(errbuf, "STORE: Failed to store %s object", + SDB_STORE_TYPE_TO_NAME(type)); + return -1; + } + + if (! status) { + sdb_strbuf_sprintf(buf, "Successfully stored %s %s", + SDB_STORE_TYPE_TO_NAME(type), name); + } + else { + char type_str[32]; + strncpy(type_str, SDB_STORE_TYPE_TO_NAME(type), sizeof(type_str)); + type_str[0] = (char)toupper((int)type_str[0]); + sdb_strbuf_sprintf(buf, "%s %s already up to date", type_str, name); + } + + return SDB_CONNECTION_OK; +} /* exec_store */ + static int -query_exec(sdb_conn_t *conn, sdb_ast_node_t *ast) +exec_timeseries(sdb_ast_timeseries_t *ts, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf) +{ + metric_store_t st = { NULL, NULL, 0 }; + sdb_object_wrapper_t obj = SDB_OBJECT_WRAPPER_STATIC(&st); + sdb_ast_fetch_t fetch = SDB_AST_FETCH_INIT; + sdb_timeseries_opts_t opts = { 0, 0, NULL, 0 }; + sdb_timeseries_t *series = NULL; + int status; + + if ((! ts) || (! ts->hostname) || (! ts->metric)) + return -1; + + fetch.obj_type = SDB_METRIC; + fetch.hostname = strdup(ts->hostname); + fetch.name = strdup(ts->metric); + opts.data_names = (const char * const *)ts->data_names; + opts.data_names_len = ts->data_names_len; + opts.start = ts->start; + opts.end = ts->end; + + status = sdb_plugin_query(SDB_AST_NODE(&fetch), + &metric_fetcher, SDB_OBJ(&obj), + &(sdb_query_opts_t){ true }, errbuf); + if ((status < 0) || (! st.type) || (! st.id)) { + sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series '%s/%s' " + "- no data-store configured for the stored metric", + ts->hostname, ts->metric); + status = -1; + } + if (status >= 0) { + series = sdb_plugin_fetch_timeseries(st.type, st.id, &opts); + if (series) { + uint32_t res_type = htonl(SDB_CONNECTION_TIMESERIES); + sdb_strbuf_memcpy(buf, &res_type, sizeof(res_type)); + sdb_timeseries_tojson(series, buf); + sdb_timeseries_destroy(series); + } + else { + sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series '%s/%s' " + "- %s fetcher callback returned no data for '%s'", + ts->hostname, ts->metric, st.type, st.id); + status = -1; + } + } + + free(fetch.hostname); + free(fetch.name); + if (st.type) + free(st.type); + if (st.id) + free(st.id); + return status; +} /* exec_timeseries */ + +static int +exec_cmd(sdb_conn_t *conn, sdb_ast_node_t *ast) { sdb_strbuf_t *buf; int status; @@ -68,7 +302,14 @@ query_exec(sdb_conn_t *conn, sdb_ast_node_t *ast) sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); return -1; } - status = sdb_plugin_query(ast, buf, conn->errbuf); + + if (ast->type == SDB_AST_TYPE_STORE) + status = exec_store(SDB_AST_STORE(ast), buf, conn->errbuf); + else if (ast->type == SDB_AST_TYPE_TIMESERIES) + status = exec_timeseries(SDB_AST_TIMESERIES(ast), buf, conn->errbuf); + else + status = exec_query(ast, buf, conn->errbuf); + if (status < 0) { char query[conn->cmd_len + 1]; strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len); @@ -81,7 +322,7 @@ query_exec(sdb_conn_t *conn, sdb_ast_node_t *ast) sdb_strbuf_destroy(buf); return status < 0 ? status : 0; -} /* query_exec */ +} /* exec_cmd */ /* * public API @@ -132,7 +373,7 @@ sdb_conn_query(sdb_conn_t *conn) } if (ast) { - status = query_exec(conn, ast); + status = exec_cmd(conn, ast); sdb_object_deref(SDB_OBJ(ast)); } sdb_llist_destroy(parsetree); @@ -169,9 +410,10 @@ sdb_conn_fetch(sdb_conn_t *conn) ast = sdb_ast_fetch_create((int)type, hostname[0] ? strdup(hostname) : NULL, + -1, NULL, name[0] ? strdup(name) : NULL, - /* filter = */ NULL); - status = query_exec(conn, ast); + /* full */ 1, /* filter = */ NULL); + status = exec_cmd(conn, ast); sdb_object_deref(SDB_OBJ(ast)); return status; } /* sdb_conn_fetch */ @@ -197,7 +439,7 @@ sdb_conn_list(sdb_conn_t *conn) } ast = sdb_ast_list_create((int)type, /* filter = */ NULL); - status = query_exec(conn, ast); + status = exec_cmd(conn, ast); sdb_object_deref(SDB_OBJ(ast)); return status; } /* sdb_conn_list */ @@ -226,30 +468,22 @@ sdb_conn_lookup(sdb_conn_t *conn) matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t); matcher_len = conn->cmd_len - sizeof(uint32_t); - m = sdb_parser_parse_conditional(matcher, (int)matcher_len, conn->errbuf); + m = sdb_parser_parse_conditional((int)type, + matcher, (int)matcher_len, conn->errbuf); if (! m) { - char expr[matcher_len + 1]; - strncpy(expr, matcher, sizeof(expr)); - expr[sizeof(expr) - 1] = '\0'; - sdb_log(SDB_LOG_ERR, "frontend: Failed to parse lookup condition '%s': %s", - expr, sdb_strbuf_string(conn->errbuf)); - return -1; - } - - ast = sdb_ast_lookup_create((int)type, m, /* filter = */ NULL); - /* run analyzer using the full context */ - if (ast && sdb_parser_analyze(ast, conn->errbuf)) { char expr[matcher_len + 1]; char err[sdb_strbuf_len(conn->errbuf) + sizeof(expr) + 64]; strncpy(expr, matcher, sizeof(expr)); expr[sizeof(expr) - 1] = '\0'; snprintf(err, sizeof(err), "Failed to parse lookup condition '%s': %s", expr, sdb_strbuf_string(conn->errbuf)); + sdb_log(SDB_LOG_ERR, "frontend: %s", err); sdb_strbuf_sprintf(conn->errbuf, "%s", err); - status = -1; + return -1; } - else - status = query_exec(conn, ast); + + ast = sdb_ast_lookup_create((int)type, m, /* filter = */ NULL); + status = exec_cmd(conn, ast); if (! ast) sdb_object_deref(SDB_OBJ(m)); sdb_object_deref(SDB_OBJ(ast)); @@ -259,7 +493,7 @@ sdb_conn_lookup(sdb_conn_t *conn) int sdb_conn_store(sdb_conn_t *conn) { - sdb_ast_node_t *ast; + sdb_ast_node_t *ast = NULL; const char *buf = sdb_strbuf_string(conn->buf); size_t len = conn->cmd_len; uint32_t type; @@ -288,7 +522,7 @@ sdb_conn_store(sdb_conn_t *conn) } ast = sdb_ast_store_create(SDB_HOST, /* host */ NULL, /* parent */ 0, NULL, sstrdup(host.name), host.last_update, - /* metric store */ NULL, NULL, SDB_DATA_NULL); + /* metric store */ NULL, NULL, 0, SDB_DATA_NULL); } break; @@ -302,7 +536,7 @@ sdb_conn_store(sdb_conn_t *conn) } ast = sdb_ast_store_create(SDB_SERVICE, sstrdup(svc.hostname), /* parent */ 0, NULL, sstrdup(svc.name), svc.last_update, - /* metric store */ NULL, NULL, SDB_DATA_NULL); + /* metric store */ NULL, NULL, 0, SDB_DATA_NULL); } break; @@ -317,7 +551,7 @@ sdb_conn_store(sdb_conn_t *conn) ast = sdb_ast_store_create(SDB_METRIC, sstrdup(metric.hostname), /* parent */ 0, NULL, sstrdup(metric.name), metric.last_update, sstrdup(metric.store_type), sstrdup(metric.store_id), - SDB_DATA_NULL); + metric.store_last_update, SDB_DATA_NULL); } break; } @@ -343,7 +577,7 @@ sdb_conn_store(sdb_conn_t *conn) } ast = sdb_ast_store_create(SDB_ATTRIBUTE, sstrdup(hostname), parent_type, sstrdup(parent), sstrdup(attr.key), - attr.last_update, /* metric store */ NULL, NULL, + attr.last_update, /* metric store */ NULL, NULL, 0, attr.value); } @@ -356,7 +590,7 @@ sdb_conn_store(sdb_conn_t *conn) status = sdb_parser_analyze(ast, conn->errbuf); if (! status) - status = query_exec(conn, ast); + status = exec_cmd(conn, ast); sdb_object_deref(SDB_OBJ(ast)); return status; } /* sdb_conn_store */