Code

store, plugin: Let the plugin module determine an objects backends.
[sysdb.git] / src / core / plugin.c
index 3ba98df263cb7a3e257fbcac2a7dd364005ca979..83ef6ead9cb150aa447515ba60036691eedfd451 100644 (file)
@@ -106,6 +106,14 @@ typedef struct {
 } writer_t;
 #define WRITER(obj) ((writer_t *)(obj))
 
+typedef struct {
+       callback_t super; /* cb_callback will always be NULL */
+#define r_user_data super.cb_user_data
+#define r_ctx super.cb_ctx
+       sdb_store_reader_t impl;
+} reader_t;
+#define READER(obj) ((reader_t *)(obj))
+
 /*
  * private variables
  */
@@ -127,6 +135,7 @@ static sdb_llist_t      *shutdown_list = NULL;
 static sdb_llist_t      *log_list = NULL;
 static sdb_llist_t      *ts_fetcher_list = NULL;
 static sdb_llist_t      *writer_list = NULL;
+static sdb_llist_t      *reader_list = NULL;
 
 static struct {
        const char   *type;
@@ -140,6 +149,7 @@ static struct {
        { "log",                &log_list },
        { "timeseries fetcher", &ts_fetcher_list },
        { "store writer",       &writer_list },
+       { "store reader",       &reader_list },
 };
 
 /*
@@ -396,8 +406,7 @@ plugin_writer_init(sdb_object_t *obj, va_list ap)
        assert(impl);
 
        if ((! impl->store_host) || (! impl->store_service)
-                       || (! impl->store_metric) || (! impl->store_attribute)
-                       || (! impl->store_service_attr) || (! impl->store_metric_attr)) {
+                       || (! impl->store_metric) || (! impl->store_attribute)) {
                sdb_log(SDB_LOG_ERR, "core: store writer callback '%s' "
                                "does not fully implement the writer interface.",
                                obj->name);
@@ -436,6 +445,53 @@ static sdb_type_t writer_type = {
        plugin_writer_destroy
 };
 
+static int
+plugin_reader_init(sdb_object_t *obj, va_list ap)
+{
+       sdb_store_reader_t *impl = va_arg(ap, sdb_store_reader_t *);
+       sdb_object_t       *ud   = va_arg(ap, sdb_object_t *);
+
+       assert(impl);
+
+       if ((! impl->prepare_query) || (! impl->execute_query)) {
+               sdb_log(SDB_LOG_ERR, "core: store reader callback '%s' "
+                               "does not fully implement the reader interface.",
+                               obj->name);
+               return -1;
+       }
+       if (sdb_llist_search_by_name(reader_list, obj->name)) {
+               sdb_log(SDB_LOG_WARNING, "core: store reader callback '%s' "
+                               "has already been registered. Ignoring newly "
+                               "registered version.", obj->name);
+               return -1;
+       }
+
+       /* ctx may be NULL if the callback was not registered by a plugin */
+
+       READER(obj)->impl = *impl;
+       READER(obj)->r_ctx  = ctx_get();
+       sdb_object_ref(SDB_OBJ(READER(obj)->r_ctx));
+
+       sdb_object_ref(ud);
+       READER(obj)->r_user_data = ud;
+       return 0;
+} /* plugin_reader_init */
+
+static void
+plugin_reader_destroy(sdb_object_t *obj)
+{
+       assert(obj);
+       sdb_object_deref(READER(obj)->r_user_data);
+       sdb_object_deref(SDB_OBJ(READER(obj)->r_ctx));
+} /* plugin_reader_destroy */
+
+static sdb_type_t reader_type = {
+       sizeof(reader_t),
+
+       plugin_reader_init,
+       plugin_reader_destroy
+};
+
 static int
 module_init(const char *name, lt_dlhandle lh, sdb_plugin_info_t *info)
 {
@@ -605,6 +661,21 @@ plugin_add_callback(sdb_llist_t **list, const char *type,
        return 0;
 } /* plugin_add_callback */
 
+static void
+get_backend(const char **backends, size_t *backends_num)
+{
+       const sdb_plugin_info_t *info;
+
+       info = sdb_plugin_current();
+       if ((! info) || (! info->plugin_name) || (! *info->plugin_name)) {
+               *backends_num = 0;
+               return;
+       }
+
+       backends[0] = info->plugin_name;
+       *backends_num = 1;
+} /* get_backend */
+
 /*
  * public API
  */
@@ -867,6 +938,41 @@ sdb_plugin_register_writer(const char *name,
        return 0;
 } /* sdb_store_register_writer */
 
+int
+sdb_plugin_register_reader(const char *name,
+               sdb_store_reader_t *reader, sdb_object_t *user_data)
+{
+       char cb_name[1024];
+       sdb_object_t *obj;
+
+       if ((! name) || (! reader))
+               return -1;
+
+       if (! reader_list)
+               reader_list = sdb_llist_create();
+       if (! reader_list)
+               return -1;
+
+       plugin_get_name(name, cb_name, sizeof(cb_name));
+
+       obj = sdb_object_create(cb_name, reader_type,
+                       reader, user_data);
+       if (! obj)
+               return -1;
+
+       if (sdb_llist_append(reader_list, obj)) {
+               sdb_object_deref(obj);
+               return -1;
+       }
+
+       /* pass control to the list */
+       sdb_object_deref(obj);
+
+       sdb_log(SDB_LOG_INFO, "core: Registered store reader callback '%s'.",
+                       cb_name);
+       return 0;
+} /* sdb_plugin_register_reader */
+
 void
 sdb_plugin_unregister_all(void)
 {
@@ -1330,25 +1436,92 @@ sdb_plugin_fetch_timeseries(const char *type, const char *id,
        return ts;
 } /* sdb_plugin_fetch_timeseries */
 
+int
+sdb_plugin_query(sdb_ast_node_t *ast, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf)
+{
+       size_t n = sdb_llist_len(reader_list);
+       reader_t *reader;
+       sdb_object_t *q;
+       int status = 0;
+
+       if (! ast)
+               return 0;
+
+       if ((ast->type != SDB_AST_TYPE_FETCH)
+                       && (ast->type != SDB_AST_TYPE_LIST)
+                       && (ast->type != SDB_AST_TYPE_LOOKUP)
+                       && (ast->type != SDB_AST_TYPE_TIMESERIES)) {
+               sdb_log(SDB_LOG_ERR, "core: Cannot execute query of type %s",
+                               SDB_AST_TYPE_TO_STRING(ast));
+               sdb_strbuf_sprintf(errbuf, "Cannot execute query of type %s",
+                               SDB_AST_TYPE_TO_STRING(ast));
+               return -1;
+       }
+
+       if (n != 1) {
+               char *msg = (n > 0)
+                       ? "Cannot execute query: multiple readers not supported"
+                       : "Cannot execute query: no readers registered";
+               sdb_strbuf_sprintf(errbuf, "%s", msg);
+               sdb_log(SDB_LOG_ERR, "core: %s", msg);
+               return -1;
+       }
+
+       reader = READER(sdb_llist_get(reader_list, 0));
+       assert(reader);
+
+       q = reader->impl.prepare_query(ast, errbuf, reader->r_user_data);
+       if (q)
+               status = reader->impl.execute_query(q, buf, errbuf, reader->r_user_data);
+       else
+               status = -1;
+
+       sdb_object_deref(SDB_OBJ(q));
+       sdb_object_deref(SDB_OBJ(reader));
+       return status;
+} /* sdb_plugin_query */
+
 int
 sdb_plugin_store_host(const char *name, sdb_time_t last_update)
 {
+       sdb_store_host_t host = { 0 };
+       const char *backends[1];
+       char *cname;
+
        sdb_llist_iter_t *iter;
        int status = 0;
 
        if (! name)
                return -1;
 
+       if (! sdb_llist_len(writer_list)) {
+               sdb_log(SDB_LOG_ERR, "core: Cannot store host: "
+                               "no writers registered");
+               return -1;
+       }
+
+       cname = sdb_plugin_cname(strdup(name));
+       if (! cname) {
+               sdb_log(SDB_LOG_ERR, "core: strdup failed");
+               return -1;
+       }
+
+       host.name = cname;
+       host.last_update = last_update;
+       host.backends = backends;
+       get_backend(host.backends, &host.backends_num);
+
        iter = sdb_llist_get_iter(writer_list);
        while (sdb_llist_iter_has_next(iter)) {
                writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
                int s;
                assert(writer);
-               s = writer->impl.store_host(name, last_update, writer->w_user_data);
+               s = writer->impl.store_host(&host, writer->w_user_data);
                if (((s > 0) && (status >= 0)) || (s < 0))
                        status = s;
        }
        sdb_llist_iter_destroy(iter);
+       free(cname);
        return status;
 } /* sdb_plugin_store_host */
 
@@ -1356,23 +1529,57 @@ int
 sdb_plugin_store_service(const char *hostname, const char *name,
                sdb_time_t last_update)
 {
+       sdb_store_service_t service = { 0 };
+       const char *backends[1];
+       char *cname;
+
        sdb_llist_iter_t *iter;
+       sdb_data_t d;
+
        int status = 0;
 
        if ((! hostname) || (! name))
                return -1;
 
+       if (! sdb_llist_len(writer_list)) {
+               sdb_log(SDB_LOG_ERR, "core: Cannot store service: "
+                               "no writers registered");
+               return -1;
+       }
+
+       cname = sdb_plugin_cname(strdup(hostname));
+       if (! cname) {
+               sdb_log(SDB_LOG_ERR, "core: strdup failed");
+               return -1;
+       }
+
+       service.hostname = cname;
+       service.name = name;
+       service.last_update = last_update;
+       service.backends = backends;
+       get_backend(service.backends, &service.backends_num);
+
        iter = sdb_llist_get_iter(writer_list);
        while (sdb_llist_iter_has_next(iter)) {
                writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
                int s;
                assert(writer);
-               s = writer->impl.store_service(hostname, name, last_update,
-                               writer->w_user_data);
+               s = writer->impl.store_service(&service, writer->w_user_data);
                if (((s > 0) && (status >= 0)) || (s < 0))
                        status = s;
        }
        sdb_llist_iter_destroy(iter);
+
+       if (! status) {
+               /* record the hostname as an attribute */
+               d.type = SDB_TYPE_STRING;
+               d.data.string = cname;
+               if (sdb_plugin_store_service_attribute(cname, name,
+                                       "hostname", &d, last_update))
+                       status = -1;
+       }
+
+       free(cname);
        return status;
 } /* sdb_plugin_store_service */
 
@@ -1380,26 +1587,64 @@ int
 sdb_plugin_store_metric(const char *hostname, const char *name,
                sdb_metric_store_t *store, sdb_time_t last_update)
 {
+       sdb_store_metric_t metric = { 0 };
+       const char *backends[1];
+       char *cname;
+
        sdb_llist_iter_t *iter;
+       sdb_data_t d;
+
        int status = 0;
 
        if ((! hostname) || (! name))
                return -1;
 
+       if (! sdb_llist_len(writer_list)) {
+               sdb_log(SDB_LOG_ERR, "core: Cannot store metric: "
+                               "no writers registered");
+               return -1;
+       }
+
+       cname = sdb_plugin_cname(strdup(hostname));
+       if (! cname) {
+               sdb_log(SDB_LOG_ERR, "core: strdup failed");
+               return -1;
+       }
+
        if (store && ((! store->type) || (! store->id)))
                store = NULL;
 
+       metric.hostname = cname;
+       metric.name = name;
+       if (store) {
+               metric.store.type = store->type;
+               metric.store.id = store->id;
+       }
+       metric.last_update = last_update;
+       metric.backends = backends;
+       get_backend(metric.backends, &metric.backends_num);
+
        iter = sdb_llist_get_iter(writer_list);
        while (sdb_llist_iter_has_next(iter)) {
                writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
                int s;
                assert(writer);
-               s = writer->impl.store_metric(hostname, name, store, last_update,
-                               writer->w_user_data);
+               s = writer->impl.store_metric(&metric, writer->w_user_data);
                if (((s > 0) && (status >= 0)) || (s < 0))
                        status = s;
        }
        sdb_llist_iter_destroy(iter);
+
+       if (! status) {
+               /* record the hostname as an attribute */
+               d.type = SDB_TYPE_STRING;
+               d.data.string = cname;
+               if (sdb_plugin_store_metric_attribute(cname, name,
+                                       "hostname", &d, last_update))
+                       status = -1;
+       }
+
+       free(cname);
        return status;
 } /* sdb_plugin_store_metric */
 
@@ -1407,23 +1652,47 @@ int
 sdb_plugin_store_attribute(const char *hostname, const char *key,
                const sdb_data_t *value, sdb_time_t last_update)
 {
+       sdb_store_attribute_t attr = { 0 };
+       const char *backends[1];
+       char *cname;
+
        sdb_llist_iter_t *iter;
        int status = 0;
 
        if ((! hostname) || (! key) || (! value))
                return -1;
 
+       if (! sdb_llist_len(writer_list)) {
+               sdb_log(SDB_LOG_ERR, "core: Cannot store attribute: "
+                               "no writers registered");
+               return -1;
+       }
+
+       cname = sdb_plugin_cname(strdup(hostname));
+       if (! cname) {
+               sdb_log(SDB_LOG_ERR, "core: strdup failed");
+               return -1;
+       }
+
+       attr.parent_type = SDB_HOST;
+       attr.parent = cname;
+       attr.key = key;
+       attr.value = *value;
+       attr.last_update = last_update;
+       attr.backends = backends;
+       get_backend(attr.backends, &attr.backends_num);
+
        iter = sdb_llist_get_iter(writer_list);
        while (sdb_llist_iter_has_next(iter)) {
                writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
                int s;
                assert(writer);
-               s = writer->impl.store_attribute(hostname, key, value, last_update,
-                               writer->w_user_data);
+               s = writer->impl.store_attribute(&attr, writer->w_user_data);
                if (((s > 0) && (status >= 0)) || (s < 0))
                        status = s;
        }
        sdb_llist_iter_destroy(iter);
+       free(cname);
        return status;
 } /* sdb_plugin_store_attribute */
 
@@ -1431,23 +1700,48 @@ int
 sdb_plugin_store_service_attribute(const char *hostname, const char *service,
                const char *key, const sdb_data_t *value, sdb_time_t last_update)
 {
+       sdb_store_attribute_t attr = { 0 };
+       const char *backends[1];
+       char *cname;
+
        sdb_llist_iter_t *iter;
        int status = 0;
 
        if ((! hostname) || (! service) || (! key) || (! value))
                return -1;
 
+       if (! sdb_llist_len(writer_list)) {
+               sdb_log(SDB_LOG_ERR, "core: Cannot store service attribute: "
+                               "no writers registered");
+               return -1;
+       }
+
+       cname = sdb_plugin_cname(strdup(hostname));
+       if (! cname) {
+               sdb_log(SDB_LOG_ERR, "core: strdup failed");
+               return -1;
+       }
+
+       attr.hostname = cname;
+       attr.parent_type = SDB_SERVICE;
+       attr.parent = service;
+       attr.key = key;
+       attr.value = *value;
+       attr.last_update = last_update;
+       attr.backends = backends;
+       get_backend(attr.backends, &attr.backends_num);
+
        iter = sdb_llist_get_iter(writer_list);
        while (sdb_llist_iter_has_next(iter)) {
                writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
                int s;
                assert(writer);
-               s = writer->impl.store_service_attr(hostname, service,
-                               key, value, last_update, writer->w_user_data);
+               s = writer->impl.store_attribute(&attr, writer->w_user_data);
                if (((s > 0) && (status >= 0)) || (s < 0))
                        status = s;
        }
        sdb_llist_iter_destroy(iter);
+       free(cname);
        return status;
 } /* sdb_plugin_store_service_attribute */
 
@@ -1455,23 +1749,48 @@ int
 sdb_plugin_store_metric_attribute(const char *hostname, const char *metric,
                const char *key, const sdb_data_t *value, sdb_time_t last_update)
 {
+       sdb_store_attribute_t attr = { 0 };
+       const char *backends[1];
+       char *cname;
+
        sdb_llist_iter_t *iter;
        int status = 0;
 
        if ((! hostname) || (! metric) || (! key) || (! value))
                return -1;
 
+       if (! sdb_llist_len(writer_list)) {
+               sdb_log(SDB_LOG_ERR, "core: Cannot store metric attribute: "
+                               "no writers registered");
+               return -1;
+       }
+
+       cname = sdb_plugin_cname(strdup(hostname));
+       if (! cname) {
+               sdb_log(SDB_LOG_ERR, "core: strdup failed");
+               return -1;
+       }
+
+       attr.hostname = cname;
+       attr.parent_type = SDB_METRIC;
+       attr.parent = metric;
+       attr.key = key;
+       attr.value = *value;
+       attr.last_update = last_update;
+       attr.backends = backends;
+       get_backend(attr.backends, &attr.backends_num);
+
        iter = sdb_llist_get_iter(writer_list);
        while (sdb_llist_iter_has_next(iter)) {
                writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
                int s;
                assert(writer);
-               s = writer->impl.store_metric_attr(hostname, metric,
-                               key, value, last_update, writer->w_user_data);
+               s = writer->impl.store_attribute(&attr, writer->w_user_data);
                if (((s > 0) && (status >= 0)) || (s < 0))
                        status = s;
        }
        sdb_llist_iter_destroy(iter);
+       free(cname);
        return status;
 } /* sdb_plugin_store_metric_attribute */