Code

plugin, store: Add service/metric "hostname" attributes centrally.
[sysdb.git] / src / core / store.c
index 498217b394c092ce1a356a98d1f31e7bcffcdc3f..30b4d1c367cdc845714c98550501da3c1037f540 100644 (file)
@@ -297,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)
 {
@@ -590,7 +568,7 @@ store_attribute(const char *hostname,
                return -1;
 
        pthread_rwlock_wrlock(&st->host_lock);
-       host = lookup_host(st, hostname, /* canonicalize = */ 0);
+       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' - "
@@ -640,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 = */ 0);
+       host = HOST(sdb_avltree_lookup(st->hosts, hostname));
        services = get_host_children(host, SDB_SERVICE);
        sdb_object_deref(SDB_OBJ(host));
        if (! services) {
@@ -676,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;
 
@@ -684,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 = */ 0);
+       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' - "
@@ -698,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 */
 
@@ -726,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 = */ 0);
+       host = HOST(sdb_avltree_lookup(st->hosts, hostname));
        metrics = get_host_children(host, SDB_METRIC);
        sdb_object_deref(SDB_OBJ(host));
        if (! metrics) {
@@ -764,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;
 
@@ -781,7 +748,7 @@ store_metric(const char *hostname, const char *name,
        }
 
        pthread_rwlock_wrlock(&st->host_lock);
-       host = lookup_host(st, hostname, /* canonicalize = */ 0);
+       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' - "
@@ -806,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 */
 
@@ -820,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,
@@ -909,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;
 
@@ -1039,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) {