Code

plugin, store: Handle hostname canonicalization in the plugin module.
authorSebastian Harl <sh@tokkee.org>
Wed, 12 Aug 2015 19:55:41 +0000 (21:55 +0200)
committerSebastian Harl <sh@tokkee.org>
Wed, 12 Aug 2015 19:55:41 +0000 (21:55 +0200)
This is a high-level functionality. Each store implementation should not have
to worry about it.

src/core/plugin.c
src/core/store.c
src/include/core/store.h

index da8a485b960a4786b914102e9f9fbc741378170a..6b1560336b311a659c35566333d78a147d8d39c2 100644 (file)
@@ -1459,6 +1459,8 @@ sdb_plugin_query(sdb_ast_node_t *ast, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf)
 int
 sdb_plugin_store_host(const char *name, sdb_time_t last_update)
 {
+       char *cname;
+
        sdb_llist_iter_t *iter;
        int status = 0;
 
@@ -1471,16 +1473,23 @@ sdb_plugin_store_host(const char *name, sdb_time_t last_update)
                return -1;
        }
 
+       cname = sdb_plugin_cname(strdup(name));
+       if (! cname) {
+               sdb_log(SDB_LOG_ERR, "core: strdup failed");
+               return -1;
+       }
+
        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(cname, last_update, 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 */
 
@@ -1488,6 +1497,8 @@ int
 sdb_plugin_store_service(const char *hostname, const char *name,
                sdb_time_t last_update)
 {
+       char *cname;
+
        sdb_llist_iter_t *iter;
        int status = 0;
 
@@ -1500,17 +1511,24 @@ sdb_plugin_store_service(const char *hostname, const char *name,
                return -1;
        }
 
+       cname = sdb_plugin_cname(strdup(hostname));
+       if (! cname) {
+               sdb_log(SDB_LOG_ERR, "core: strdup failed");
+               return -1;
+       }
+
        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,
+               s = writer->impl.store_service(cname, name, last_update,
                                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 */
 
@@ -1518,6 +1536,8 @@ int
 sdb_plugin_store_metric(const char *hostname, const char *name,
                sdb_metric_store_t *store, sdb_time_t last_update)
 {
+       char *cname;
+
        sdb_llist_iter_t *iter;
        int status = 0;
 
@@ -1530,6 +1550,12 @@ sdb_plugin_store_metric(const char *hostname, const char *name,
                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;
 
@@ -1538,12 +1564,13 @@ sdb_plugin_store_metric(const char *hostname, const char *name,
                writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
                int s;
                assert(writer);
-               s = writer->impl.store_metric(hostname, name, store, last_update,
+               s = writer->impl.store_metric(cname, name, store, last_update,
                                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 */
 
@@ -1551,6 +1578,8 @@ int
 sdb_plugin_store_attribute(const char *hostname, const char *key,
                const sdb_data_t *value, sdb_time_t last_update)
 {
+       char *cname;
+
        sdb_llist_iter_t *iter;
        int status = 0;
 
@@ -1563,17 +1592,24 @@ sdb_plugin_store_attribute(const char *hostname, const char *key,
                return -1;
        }
 
+       cname = sdb_plugin_cname(strdup(hostname));
+       if (! cname) {
+               sdb_log(SDB_LOG_ERR, "core: strdup failed");
+               return -1;
+       }
+
        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,
+               s = writer->impl.store_attribute(cname, key, value, last_update,
                                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 */
 
@@ -1581,6 +1617,8 @@ 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)
 {
+       char *cname;
+
        sdb_llist_iter_t *iter;
        int status = 0;
 
@@ -1593,17 +1631,24 @@ sdb_plugin_store_service_attribute(const char *hostname, const char *service,
                return -1;
        }
 
+       cname = sdb_plugin_cname(strdup(hostname));
+       if (! cname) {
+               sdb_log(SDB_LOG_ERR, "core: strdup failed");
+               return -1;
+       }
+
        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,
+               s = writer->impl.store_service_attr(cname, service,
                                key, value, last_update, 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 */
 
@@ -1611,6 +1656,8 @@ 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)
 {
+       char *cname;
+
        sdb_llist_iter_t *iter;
        int status = 0;
 
@@ -1623,17 +1670,24 @@ sdb_plugin_store_metric_attribute(const char *hostname, const char *metric,
                return -1;
        }
 
+       cname = sdb_plugin_cname(strdup(hostname));
+       if (! cname) {
+               sdb_log(SDB_LOG_ERR, "core: strdup failed");
+               return -1;
+       }
+
        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,
+               s = writer->impl.store_metric_attr(cname, metric,
                                key, value, last_update, 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 */
 
index bea4d2b578c421ed7706791e2aee0db95524de96..498217b394c092ce1a356a98d1f31e7bcffcdc3f 100644 (file)
@@ -590,7 +590,7 @@ store_attribute(const char *hostname,
                return -1;
 
        pthread_rwlock_wrlock(&st->host_lock);
-       host = lookup_host(st, hostname, /* canonicalize = */ 1);
+       host = lookup_host(st, hostname, /* canonicalize = */ 0);
        attrs = get_host_children(host, SDB_ATTRIBUTE);
        if (! attrs) {
                sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
@@ -611,25 +611,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 */
 
@@ -649,7 +640,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 = lookup_host(st, hostname, /* canonicalize = */ 0);
        services = get_host_children(host, SDB_SERVICE);
        sdb_object_deref(SDB_OBJ(host));
        if (! services) {
@@ -693,7 +684,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 = lookup_host(st, hostname, /* canonicalize = */ 0);
        services = get_host_children(host, SDB_SERVICE);
        if (! services) {
                sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
@@ -735,7 +726,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 = lookup_host(st, hostname, /* canonicalize = */ 0);
        metrics = get_host_children(host, SDB_METRIC);
        sdb_object_deref(SDB_OBJ(host));
        if (! metrics) {
@@ -790,7 +781,7 @@ store_metric(const char *hostname, const char *name,
        }
 
        pthread_rwlock_wrlock(&st->host_lock);
-       host = lookup_host(st, hostname, /* canonicalize = */ 1);
+       host = lookup_host(st, hostname, /* canonicalize = */ 0);
        metrics = get_host_children(host, SDB_METRIC);
        if (! metrics) {
                sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
index 16a094adbf8e347b9278d94142a3ff4d0242632b..28b257e056b10f6912b793c95ee592afba6ed59d 100644 (file)
@@ -287,7 +287,8 @@ sdb_store_create(void);
 /*
  * sdb_store_host, sdb_store_service, sdb_store_metric, sdb_store_attribute,
  * sdb_store_metric_attr:
- * Store an object in the specified store.
+ * Store an object in the specified store. The hostname is expected to be
+ * canonical.
  */
 int
 sdb_store_host(sdb_store_t *store, const char *name, sdb_time_t last_update);