Code

plugin, store: Add service/metric "hostname" attributes centrally.
[sysdb.git] / src / core / store.c
index e24d2ef0a5356bf0a2badc8aa5fe468f94983a4f..30b4d1c367cdc845714c98550501da3c1037f540 100644 (file)
@@ -59,8 +59,6 @@ struct sdb_store {
        pthread_rwlock_t host_lock;
 };
 
-sdb_store_t *global_store = NULL;
-
 /*
  * private types
  */
@@ -299,28 +297,6 @@ static sdb_type_t attribute_type = {
  * private helper functions
  */
 
-static sdb_host_t *
-lookup_host(sdb_store_t *st, const char *name, bool canonicalize)
-{
-       sdb_host_t *host;
-       char *cname;
-
-       assert(name);
-       if (! canonicalize)
-               return HOST(sdb_avltree_lookup(st->hosts, name));
-
-       cname = strdup(name);
-       cname = sdb_plugin_cname(cname);
-       if (! cname) {
-               sdb_log(SDB_LOG_ERR, "store: strdup failed");
-               return NULL;
-       }
-
-       host = HOST(sdb_avltree_lookup(st->hosts, cname));
-       free(cname);
-       return host;
-} /* lookup_host */
-
 static int
 record_backend(sdb_store_obj_t *obj)
 {
@@ -592,7 +568,7 @@ store_attribute(const char *hostname,
                return -1;
 
        pthread_rwlock_wrlock(&st->host_lock);
-       host = lookup_host(st, hostname, /* canonicalize = */ 1);
+       host = HOST(sdb_avltree_lookup(st->hosts, hostname));
        attrs = get_host_children(host, SDB_ATTRIBUTE);
        if (! attrs) {
                sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
@@ -613,25 +589,16 @@ static int
 store_host(const char *name, sdb_time_t last_update, sdb_object_t *user_data)
 {
        sdb_store_t *st = SDB_STORE(user_data);
-
-       char *cname = NULL;
        int status = 0;
 
        if (! name)
                return -1;
 
-       cname = sdb_plugin_cname(strdup(name));
-       if (! cname) {
-               sdb_log(SDB_LOG_ERR, "store: strdup failed");
-               return -1;
-       }
-
        pthread_rwlock_wrlock(&st->host_lock);
        status = store_obj(NULL, st->hosts,
-                       SDB_HOST, cname, last_update, NULL);
+                       SDB_HOST, name, last_update, NULL);
        pthread_rwlock_unlock(&st->host_lock);
 
-       free(cname);
        return status;
 } /* store_host */
 
@@ -651,7 +618,7 @@ store_service_attr(const char *hostname, const char *service,
                return -1;
 
        pthread_rwlock_wrlock(&st->host_lock);
-       host = lookup_host(st, hostname, /* canonicalize = */ 1);
+       host = HOST(sdb_avltree_lookup(st->hosts, hostname));
        services = get_host_children(host, SDB_SERVICE);
        sdb_object_deref(SDB_OBJ(host));
        if (! services) {
@@ -687,7 +654,6 @@ store_service(const char *hostname, const char *name,
 
        sdb_host_t *host;
        sdb_avltree_t *services;
-       sdb_data_t d;
 
        int status = 0;
 
@@ -695,7 +661,7 @@ store_service(const char *hostname, const char *name,
                return -1;
 
        pthread_rwlock_wrlock(&st->host_lock);
-       host = lookup_host(st, hostname, /* canonicalize = */ 1);
+       host = HOST(sdb_avltree_lookup(st->hosts, hostname));
        services = get_host_children(host, SDB_SERVICE);
        if (! services) {
                sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
@@ -709,15 +675,6 @@ store_service(const char *hostname, const char *name,
 
        sdb_object_deref(SDB_OBJ(host));
        pthread_rwlock_unlock(&st->host_lock);
-
-       if (status)
-               return status;
-
-       /* record the hostname as an attribute */
-       d.type = SDB_TYPE_STRING;
-       d.data.string = SDB_OBJ(host)->name;
-       if (store_service_attr(hostname, name, "hostname", &d, last_update, user_data))
-               status = -1;
        return status;
 } /* store_service */
 
@@ -737,7 +694,7 @@ store_metric_attr(const char *hostname, const char *metric,
                return -1;
 
        pthread_rwlock_wrlock(&st->host_lock);
-       host = lookup_host(st, hostname, /* canonicalize = */ 1);
+       host = HOST(sdb_avltree_lookup(st->hosts, hostname));
        metrics = get_host_children(host, SDB_METRIC);
        sdb_object_deref(SDB_OBJ(host));
        if (! metrics) {
@@ -775,7 +732,6 @@ store_metric(const char *hostname, const char *name,
        sdb_store_obj_t *obj = NULL;
        sdb_host_t *host;
        sdb_metric_t *metric;
-       sdb_data_t d;
 
        sdb_avltree_t *metrics;
 
@@ -792,7 +748,7 @@ store_metric(const char *hostname, const char *name,
        }
 
        pthread_rwlock_wrlock(&st->host_lock);
-       host = lookup_host(st, hostname, /* canonicalize = */ 1);
+       host = HOST(sdb_avltree_lookup(st->hosts, hostname));
        metrics = get_host_children(host, SDB_METRIC);
        if (! metrics) {
                sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
@@ -817,12 +773,6 @@ store_metric(const char *hostname, const char *name,
                if (store_metric_store(metric, store))
                        status = -1;
        pthread_rwlock_unlock(&st->host_lock);
-
-       /* record the hostname as an attribute */
-       d.type = SDB_TYPE_STRING;
-       d.data.string = SDB_OBJ(host)->name;
-       if (store_metric_attr(hostname, name, "hostname", &d, last_update, user_data))
-               status = -1;
        return status;
 } /* store_metric */
 
@@ -831,11 +781,6 @@ sdb_store_writer_t sdb_store_writer = {
        store_attribute, store_service_attr, store_metric_attr,
 };
 
-/*
- * TODO: let prepare and execute accept a store object as their user_data
- * object
- */
-
 static sdb_object_t *
 prepare_query(sdb_ast_node_t *ast,
                sdb_strbuf_t __attribute__((unused)) *errbuf,
@@ -867,32 +812,6 @@ sdb_store_create(void)
        return SDB_STORE(sdb_object_create("store", store_type));
 } /* sdb_store_create */
 
-int
-sdb_store_init(void)
-{
-       if (global_store)
-               return 0;
-
-       global_store = SDB_STORE(sdb_object_create("store", store_type));
-       if (! global_store) {
-               sdb_log(SDB_LOG_ERR, "store: Failed to allocate store");
-               return -1;
-       }
-       if (sdb_plugin_register_writer("memstore",
-                               &sdb_store_writer, SDB_OBJ(global_store)))
-               return -1;
-       return sdb_plugin_register_reader("memstore",
-                       &sdb_store_reader, SDB_OBJ(global_store));
-} /* sdb_store_init */
-
-void
-sdb_store_clear(void)
-{
-       if (! global_store)
-               return;
-       sdb_avltree_clear(global_store->hosts);
-} /* sdb_store_clear */
-
 int
 sdb_store_host(sdb_store_t *store, const char *name, sdb_time_t last_update)
 {
@@ -946,7 +865,7 @@ sdb_store_get_host(sdb_store_t *store, const char *name)
        if ((! store) || (! name))
                return NULL;
 
-       host = lookup_host(store, name, /* canonicalize = */ 0);
+       host = HOST(sdb_avltree_lookup(store->hosts, name));
        if (! host)
                return NULL;
 
@@ -1076,7 +995,7 @@ sdb_store_fetch_timeseries(sdb_store_t *store,
                return -1;
 
        pthread_rwlock_rdlock(&store->host_lock);
-       host = lookup_host(store, hostname, /* canonicalize = */ 1);
+       host = HOST(sdb_avltree_lookup(store->hosts, hostname));
        metrics = get_host_children(host, SDB_METRIC);
        sdb_object_deref(SDB_OBJ(host));
        if (! metrics) {