From: Sebastian Harl Date: Wed, 12 Aug 2015 19:55:41 +0000 (+0200) Subject: plugin, store: Handle hostname canonicalization in the plugin module. X-Git-Tag: sysdb-0.8.0~45 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=502bbcd0731ca35b4f3d93653a818f5246f81e0b;p=sysdb.git plugin, store: Handle hostname canonicalization in the plugin module. This is a high-level functionality. Each store implementation should not have to worry about it. --- diff --git a/src/core/plugin.c b/src/core/plugin.c index da8a485..6b15603 100644 --- a/src/core/plugin.c +++ b/src/core/plugin.c @@ -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 */ diff --git a/src/core/store.c b/src/core/store.c index bea4d2b..498217b 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -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' - " diff --git a/src/include/core/store.h b/src/include/core/store.h index 16a094a..28b257e 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -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);