Code

Merged branch 'master' of git://git.tokkee.org/sysdb.
authorSebastian Harl <sh@tokkee.org>
Tue, 1 Jul 2014 20:01:13 +0000 (22:01 +0200)
committerSebastian Harl <sh@tokkee.org>
Tue, 1 Jul 2014 20:01:13 +0000 (22:01 +0200)
src/core/plugin.c
src/core/store-private.h
src/core/store.c
src/include/core/plugin.h
t/unit/core/store_test.c

index 36f80f6e6c29296322fac9979632a0e32ea05f29..806e13c2f717f27b339ffd42b4cf43f4e2c3575b 100644 (file)
 
 #include <pthread.h>
 
+/* helper to access info attributes */
+#define INFO_GET(i, attr) \
+       ((i)->attr ? (i)->attr : #attr" not set")
+
 /*
  * private data types
  */
 
-struct sdb_plugin_info {
-       char *plugin_name;
-       char *filename;
-
-       /* public attributes */
-       char *description;
-       char *copyright;
-       char *license;
-
-       int   version;
-       int   plugin_version;
-};
-#define SDB_PLUGIN_INFO_INIT { \
-       /* plugin_name */ NULL, /* filename */ NULL, /* desc */ NULL, \
-       /* copyright */ NULL, /* license */ NULL, \
-       /* version */ -1, /* plugin_version */ -1 }
-#define INFO_GET(i, attr) \
-       ((i)->attr ? (i)->attr : #attr" not set")
-
 typedef struct {
        sdb_object_t super;
        sdb_plugin_ctx_t public;
@@ -829,6 +814,16 @@ sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
        return 0;
 } /* sdb_plugin_set_ctx */
 
+const sdb_plugin_info_t *
+sdb_plugin_current(void)
+{
+       ctx_t *ctx = ctx_get();
+
+       if (! ctx)
+               return NULL;
+       return &ctx->info;
+} /* sdb_plugin_current */
+
 int
 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
 {
index 14201fe071a77a65ba20b0b17b36a80f1a35d3fd..eea43164fed967c4b4e2bdec92e426c10d56e3d0 100644 (file)
@@ -50,6 +50,8 @@ struct sdb_store_obj {
        /* common meta information */
        sdb_time_t last_update;
        sdb_time_t interval; /* moving average */
+       char **backends;
+       size_t backends_num;
        sdb_store_obj_t *parent;
 };
 #define STORE_OBJ(obj) ((sdb_store_obj_t *)(obj))
index 1b8243b091d4149a98dabea39927b5eb661b122c..d185431f976de168213aa769ceee8bd2a7f16482 100644 (file)
@@ -69,6 +69,8 @@ store_obj_init(sdb_object_t *obj, va_list ap)
 
        sobj->last_update = va_arg(ap, sdb_time_t);
        sobj->interval = 0;
+       sobj->backends = NULL;
+       sobj->backends_num = 0;
        sobj->parent = NULL;
        return 0;
 } /* store_obj_init */
@@ -76,7 +78,14 @@ store_obj_init(sdb_object_t *obj, va_list ap)
 static void
 store_obj_destroy(sdb_object_t *obj)
 {
-       const sdb_store_obj_t *sobj = STORE_CONST_OBJ(obj);
+       sdb_store_obj_t *sobj = STORE_OBJ(obj);
+       size_t i;
+
+       for (i = 0; i < sobj->backends_num; ++i)
+               free(sobj->backends[i]);
+       free(sobj->backends);
+       sobj->backends = NULL;
+       sobj->backends_num = 0;
 
        if (sobj->parent)
                sdb_object_deref(SDB_OBJ(sobj->parent));
@@ -207,10 +216,14 @@ store_obj(const char *hostname, int type, const char *name,
                sdb_time_t last_update, sdb_store_obj_t **updated_obj)
 {
        char *host_cname = NULL, *cname = NULL;
+       char **tmp;
 
        sdb_llist_t *parent_list;
-       sdb_store_obj_t *old;
+       sdb_store_obj_t *old, *new;
+       const sdb_plugin_info_t *info;
+
        int status = 0;
+       size_t i;
 
        if (last_update <= 0)
                last_update = sdb_gettime();
@@ -293,12 +306,9 @@ store_obj(const char *hostname, int type, const char *name,
                        }
                }
 
-               if (updated_obj)
-                       *updated_obj = old;
+               new = old;
        }
        else {
-               sdb_store_obj_t *new;
-
                if (type == SDB_ATTRIBUTE) {
                        /* the value will be updated by the caller */
                        new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
@@ -310,30 +320,89 @@ store_obj(const char *hostname, int type, const char *name,
                        new = STORE_OBJ(sdb_object_create(name, t, type, last_update));
                }
 
-               if (! new) {
+               if (new) {
+                       status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
+                                       sdb_object_cmp_by_name);
+
+                       /* pass control to the list or destroy in case of an error */
+                       sdb_object_deref(SDB_OBJ(new));
+               }
+               else {
                        char errbuf[1024];
                        sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
                                        SDB_STORE_TYPE_TO_NAME(type), name,
                                        sdb_strerror(errno, errbuf, sizeof(errbuf)));
-                       free(host_cname);
-                       free(cname);
-                       return -1;
+                       status = -1;
                }
-
-               status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
-                               sdb_object_cmp_by_name);
-
-               /* pass control to the list or destroy in case of an error */
-               sdb_object_deref(SDB_OBJ(new));
-
-               if (updated_obj)
-                       *updated_obj = new;
        }
+
        free(host_cname);
        free(cname);
+
+       if (status < 0)
+               return status;
+       assert(new);
+
+       if (updated_obj)
+               *updated_obj = new;
+
+       info = sdb_plugin_current();
+       if (! info)
+               return status;
+
+       for (i = 0; i < new->backends_num; ++i)
+               if (!strcasecmp(new->backends[i], info->plugin_name))
+                       return status;
+
+       tmp = realloc(new->backends,
+                       (new->backends_num + 1) * sizeof(*new->backends));
+       if (! tmp)
+               return -1;
+
+       new->backends = tmp;
+       new->backends[new->backends_num] = strdup(info->plugin_name);
+       if (! new->backends[new->backends_num])
+               return -1;
+
+       ++new->backends_num;
        return status;
 } /* store_obj */
 
+/*
+ * store_common_tojson serializes common object attributes to JSON.
+ *
+ * The function never returns an error. Rather, an error message will be part
+ * of the serialized data.
+ */
+static void
+store_common_tojson(sdb_store_obj_t *obj, sdb_strbuf_t *buf)
+{
+       char time_str[64];
+       char interval_str[64];
+       size_t i;
+
+       if (! sdb_strftime(time_str, sizeof(time_str),
+                               "%F %T %z", obj->last_update))
+               snprintf(time_str, sizeof(time_str), "<error>");
+       time_str[sizeof(time_str) - 1] = '\0';
+
+       if (! sdb_strfinterval(interval_str, sizeof(interval_str),
+                               obj->interval))
+               snprintf(interval_str, sizeof(interval_str), "<error>");
+       interval_str[sizeof(interval_str) - 1] = '\0';
+
+       sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
+                       "\"update_interval\": \"%s\", \"backends\": [",
+                       time_str, interval_str);
+
+       for (i = 0; i < obj->backends_num; ++i) {
+               sdb_strbuf_append(buf, "\"%s\"", obj->backends[i]);
+               if (i < obj->backends_num - 1)
+                       sdb_strbuf_append(buf, ",");
+       }
+       sdb_strbuf_append(buf, "]");
+} /* store_common_tojson */
+
 /*
  * store_obj_tojson serializes attribute / service objects to JSON.
  *
@@ -344,13 +413,10 @@ static void
 store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
 {
        sdb_llist_iter_t *iter;
-       char time_str[64];
-       char interval_str[64];
 
        assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
 
        sdb_strbuf_append(buf, "[");
-
        iter = sdb_llist_get_iter(list);
        if (! iter) {
                char errbuf[1024];
@@ -367,28 +433,16 @@ store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
                assert(sobj);
                assert(sobj->type == type);
 
-               if (! sdb_strftime(time_str, sizeof(time_str),
-                                       "%F %T %z", sobj->last_update))
-                       snprintf(time_str, sizeof(time_str), "<error>");
-               time_str[sizeof(time_str) - 1] = '\0';
-
-               if (! sdb_strfinterval(interval_str, sizeof(interval_str),
-                                       sobj->interval))
-                       snprintf(interval_str, sizeof(interval_str), "<error>");
-               interval_str[sizeof(interval_str) - 1] = '\0';
-
                sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
                if (sobj->type == SDB_ATTRIBUTE) {
                        char tmp[sdb_data_strlen(&ATTR(sobj)->value) + 1];
                        sdb_data_format(&ATTR(sobj)->value, tmp, sizeof(tmp),
                                        SDB_DOUBLE_QUOTED);
-                       sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\", "
-                                       "\"update_interval\": \"%s\"}", tmp, time_str,
-                                       interval_str);
+                       sdb_strbuf_append(buf, "\"value\": %s, ", tmp);
                }
-               else
-                       sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
-                                       "\"update_interval\": \"%s\"}", time_str, interval_str);
+
+               store_common_tojson(sobj, buf);
+               sdb_strbuf_append(buf, "}");
 
                if (sdb_llist_iter_has_next(iter))
                        sdb_strbuf_append(buf, ",");
@@ -504,27 +558,14 @@ int
 sdb_store_host_tojson(sdb_store_obj_t *h, sdb_strbuf_t *buf, int flags)
 {
        sdb_host_t *host;
-       char time_str[64];
-       char interval_str[64];
 
        if ((! h) || (h->type != SDB_HOST) || (! buf))
                return -1;
 
        host = HOST(h);
 
-       if (! sdb_strftime(time_str, sizeof(time_str),
-                               "%F %T %z", host->_last_update))
-               snprintf(time_str, sizeof(time_str), "<error>");
-       time_str[sizeof(time_str) - 1] = '\0';
-
-       if (! sdb_strfinterval(interval_str, sizeof(interval_str),
-                               host->_interval))
-               snprintf(interval_str, sizeof(interval_str), "<error>");
-       interval_str[sizeof(interval_str) - 1] = '\0';
-
-       sdb_strbuf_append(buf, "{\"name\": \"%s\", "
-                       "\"last_update\": \"%s\", \"update_interval\": \"%s\"",
-                       SDB_OBJ(host)->name, time_str, interval_str);
+       sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(host)->name);
+       store_common_tojson(h, buf);
 
        if (! (flags & SDB_SKIP_ATTRIBUTES)) {
                sdb_strbuf_append(buf, ", \"attributes\": ");
index 3d938807f61d2c1098dae31429157946b908b2f8..f75788ad2359a479c5be92d276366d41bf2fa21b 100644 (file)
@@ -45,8 +45,22 @@ typedef struct {
 } sdb_plugin_ctx_t;
 #define SDB_PLUGIN_CTX_INIT { 0 }
 
-struct sdb_plugin_info;
-typedef struct sdb_plugin_info sdb_plugin_info_t;
+typedef struct {
+       char *plugin_name;
+       char *filename;
+
+       /* public attributes */
+       char *description;
+       char *copyright;
+       char *license;
+
+       int   version;
+       int   plugin_version;
+} sdb_plugin_info_t;
+#define SDB_PLUGIN_INFO_INIT { \
+       /* plugin_name */ NULL, /* filename */ NULL, /* desc */ NULL, \
+       /* copyright */ NULL, /* license */ NULL, \
+       /* version */ -1, /* plugin_version */ -1 }
 
 /* this should be used in the header of a plugin to avoid
  * missing prototype warnings/errors for the plugin init
@@ -234,6 +248,19 @@ sdb_plugin_get_ctx(void);
 int
 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old);
 
+/*
+ * sdb_plugin_current:
+ * Retrieve information about the plugin (if any) from which the current call
+ * into the core originated. The return value may not be modified.
+ *
+ * Returns:
+ *  - information about the current plugin if we were called from some
+ *    plugin's callback function
+ *  - NULL else
+ */
+const sdb_plugin_info_t *
+sdb_plugin_current(void);
+
 /*
  * sdb_plugin_configure:
  * Configure the plugin called 'name' using the config tree 'ci'. The plugin
index cca3968c4674df076a948d641a71d211c1257e3d..4e40743d85ce60b28dcb965ac770b72935285927 100644 (file)
@@ -278,72 +278,72 @@ START_TEST(test_store_tojson)
        } golden_data[] = {
                { 0, "{\"hosts\":["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"attributes\": ["
                                                "{\"name\": \"k1\", \"value\": \"v1\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"},"
+                                                       "\"update_interval\": \"0s\", \"backends\": []},"
                                                "{\"name\": \"k2\", \"value\": \"v2\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"},"
+                                                       "\"update_interval\": \"0s\", \"backends\": []},"
                                                "{\"name\": \"k3\", \"value\": \"v3\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"}"
+                                                       "\"update_interval\": \"0s\", \"backends\": []}"
                                        "], "
                                        "\"services\": []},"
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"attributes\": [], "
                                        "\"services\": ["
                                                "{\"name\": \"s1\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"},"
+                                                       "\"update_interval\": \"0s\", \"backends\": []},"
                                                "{\"name\": \"s2\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"}"
+                                                       "\"update_interval\": \"0s\", \"backends\": []}"
                                        "]}"
                        "]}" },
                { SDB_SKIP_SERVICES,
                        "{\"hosts\":["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"attributes\": ["
                                                "{\"name\": \"k1\", \"value\": \"v1\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"},"
+                                                       "\"update_interval\": \"0s\", \"backends\": []},"
                                                "{\"name\": \"k2\", \"value\": \"v2\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"},"
+                                                       "\"update_interval\": \"0s\", \"backends\": []},"
                                                "{\"name\": \"k3\", \"value\": \"v3\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"}"
+                                                       "\"update_interval\": \"0s\", \"backends\": []}"
                                        "]},"
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"attributes\": []}"
                        "]}" },
                { SDB_SKIP_ATTRIBUTES,
                        "{\"hosts\":["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"services\": []},"
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"services\": ["
                                                "{\"name\": \"s1\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"},"
+                                                       "\"update_interval\": \"0s\", \"backends\": []},"
                                                "{\"name\": \"s2\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"}"
+                                                       "\"update_interval\": \"0s\", \"backends\": []}"
                                        "]}"
                        "]}" },
                { SDB_SKIP_SERVICES | SDB_SKIP_ATTRIBUTES,
                        "{\"hosts\":["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\"},"
+                                       "\"update_interval\": \"0s\", \"backends\": []},"
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\"}"
+                                       "\"update_interval\": \"0s\", \"backends\": []}"
                        "]}" },
        };