From af74f327da5e6ebe01d5e5e6f8fa19646e203eed Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Fri, 16 Jan 2015 22:10:46 +0100 Subject: [PATCH] store: Pass on all stored objects to store writer plugins. --- src/core/plugin.c | 139 ++++++++++++++++++++++++++++++++++++++ src/core/store.c | 20 ++++++ src/include/core/plugin.h | 29 ++++++++ 3 files changed, 188 insertions(+) diff --git a/src/core/plugin.c b/src/core/plugin.c index 4de1c3a..7404b03 100644 --- a/src/core/plugin.c +++ b/src/core/plugin.c @@ -1332,5 +1332,144 @@ sdb_plugin_fetch_timeseries(const char *type, const char *id, return ts; } /* sdb_plugin_fetch_timeseries */ +int +sdb_plugin_store_host(const char *name, sdb_time_t last_update) +{ + sdb_llist_iter_t *iter; + int status = 0; + + if (! name) + return -1; + + iter = sdb_llist_get_iter(writer_list); + while (sdb_llist_iter_has_next(iter)) { + sdb_plugin_writer_t *writer; + writer = SDB_PLUGIN_WRITER(sdb_llist_iter_get_next(iter)); + assert(writer); + if (writer->impl.store_host(name, last_update, writer->user_data)) + status = -1; + } + sdb_llist_iter_destroy(iter); + return status; +} /* sdb_plugin_store_host */ + +int +sdb_plugin_store_service(const char *hostname, const char *name, + sdb_time_t last_update) +{ + sdb_llist_iter_t *iter; + int status = 0; + + if ((! hostname) || (! name)) + return -1; + + iter = sdb_llist_get_iter(writer_list); + while (sdb_llist_iter_has_next(iter)) { + sdb_plugin_writer_t *writer; + writer = SDB_PLUGIN_WRITER(sdb_llist_iter_get_next(iter)); + assert(writer); + if (writer->impl.store_service(hostname, name, last_update, + writer->user_data)) + status = -1; + } + sdb_llist_iter_destroy(iter); + return status; +} /* sdb_plugin_store_service */ + +int +sdb_plugin_store_metric(const char *hostname, const char *name, + sdb_metric_store_t *store, sdb_time_t last_update) +{ + sdb_llist_iter_t *iter; + int status = 0; + + if ((! hostname) || (! name)) + return -1; + + if ((! store->type) || (! store->id)) + store = NULL; + + iter = sdb_llist_get_iter(writer_list); + while (sdb_llist_iter_has_next(iter)) { + sdb_plugin_writer_t *writer; + writer = SDB_PLUGIN_WRITER(sdb_llist_iter_get_next(iter)); + assert(writer); + if (writer->impl.store_metric(hostname, name, store, last_update, + writer->user_data)) + status = -1; + } + sdb_llist_iter_destroy(iter); + return status; +} /* sdb_plugin_store_metric */ + +int +sdb_plugin_store_attribute(const char *hostname, const char *key, + const sdb_data_t *value, sdb_time_t last_update) +{ + sdb_llist_iter_t *iter; + int status = 0; + + if ((! hostname) || (! key) || (! value)) + return -1; + + iter = sdb_llist_get_iter(writer_list); + while (sdb_llist_iter_has_next(iter)) { + sdb_plugin_writer_t *writer; + writer = SDB_PLUGIN_WRITER(sdb_llist_iter_get_next(iter)); + assert(writer); + if (writer->impl.store_attribute(hostname, key, value, last_update, + writer->user_data)) + status = -1; + } + sdb_llist_iter_destroy(iter); + return status; +} /* sdb_plugin_store_attribute */ + +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) +{ + sdb_llist_iter_t *iter; + int status = 0; + + if ((! hostname) || (! service) || (! key) || (! value)) + return -1; + + iter = sdb_llist_get_iter(writer_list); + while (sdb_llist_iter_has_next(iter)) { + sdb_plugin_writer_t *writer; + writer = SDB_PLUGIN_WRITER(sdb_llist_iter_get_next(iter)); + assert(writer); + if (writer->impl.store_service_attr(hostname, service, + key, value, last_update, writer->user_data)) + status = -1; + } + sdb_llist_iter_destroy(iter); + return status; +} /* sdb_plugin_store_service_attribute */ + +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) +{ + sdb_llist_iter_t *iter; + int status = 0; + + if ((! hostname) || (! metric) || (! key) || (! value)) + return -1; + + iter = sdb_llist_get_iter(writer_list); + while (sdb_llist_iter_has_next(iter)) { + sdb_plugin_writer_t *writer; + writer = SDB_PLUGIN_WRITER(sdb_llist_iter_get_next(iter)); + assert(writer); + if (writer->impl.store_metric_attr(hostname, metric, + key, value, last_update, writer->user_data)) + status = -1; + } + sdb_llist_iter_destroy(iter); + return status; +} /* sdb_plugin_store_metric_attribute */ + /* vim: set tw=78 sw=4 ts=4 noexpandtab : */ diff --git a/src/core/store.c b/src/core/store.c index 98b3a96..e6aeaf6 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -536,6 +536,9 @@ sdb_store_host(const char *name, sdb_time_t last_update) status = store_obj(NULL, hosts, SDB_HOST, cname, last_update, NULL); pthread_rwlock_unlock(&host_lock); + if (sdb_plugin_store_host(name, last_update)) + status = -1; + free(cname); return status; } /* sdb_store_host */ @@ -594,6 +597,9 @@ sdb_store_attribute(const char *hostname, sdb_object_deref(SDB_OBJ(host)); pthread_rwlock_unlock(&host_lock); + + if (sdb_plugin_store_attribute(hostname, key, value, last_update)) + status = -1; return status; } /* sdb_store_attribute */ @@ -624,6 +630,9 @@ sdb_store_service(const char *hostname, const char *name, sdb_object_deref(SDB_OBJ(host)); pthread_rwlock_unlock(&host_lock); + + if (sdb_plugin_store_service(hostname, name, last_update)) + status = -1; return status; } /* sdb_store_service */ @@ -664,6 +673,10 @@ sdb_store_service_attr(const char *hostname, const char *service, sdb_object_deref(SDB_OBJ(svc)); pthread_rwlock_unlock(&host_lock); + + if (sdb_plugin_store_service_attribute(hostname, service, + key, value, last_update)) + status = -1; return status; } /* sdb_store_service_attr */ @@ -731,6 +744,9 @@ sdb_store_metric(const char *hostname, const char *name, status = -1; } pthread_rwlock_unlock(&host_lock); + + if (sdb_plugin_store_metric(hostname, name, store, last_update)) + status = -1; return status; } /* sdb_store_metric */ @@ -771,6 +787,10 @@ sdb_store_metric_attr(const char *hostname, const char *metric, sdb_object_deref(SDB_OBJ(m)); pthread_rwlock_unlock(&host_lock); + + if (sdb_plugin_store_metric_attribute(hostname, metric, + key, value, last_update)) + status = -1; return status; } /* sdb_store_metric_attr */ diff --git a/src/include/core/plugin.h b/src/include/core/plugin.h index e09bf0f..f48980a 100644 --- a/src/include/core/plugin.h +++ b/src/include/core/plugin.h @@ -413,6 +413,35 @@ sdb_timeseries_t * sdb_plugin_fetch_timeseries(const char *type, const char *id, sdb_timeseries_opts_t *opts); +/* + * sdb_plugin_store_host, sdb_plugin_store_service, sdb_plugin_store_metric, + * sdb_plugin_store_attribute, sdb_plugin_store_service_attribute, + * sdb_plugin_store_metric_attribute: + * Store an object in the database by sending it to all registered store + * writer plugins. + * + * Returns: + * - 0 on success + * - a negative value else + */ +int +sdb_plugin_store_host(const char *name, sdb_time_t last_update); +int +sdb_plugin_store_service(const char *hostname, const char *name, + sdb_time_t last_update); +int +sdb_plugin_store_metric(const char *hostname, const char *name, + sdb_metric_store_t *store, sdb_time_t last_update); +int +sdb_plugin_store_attribute(const char *hostname, const char *key, + const sdb_data_t *value, sdb_time_t last_update); +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); +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); + #ifdef __cplusplus } /* extern "C" */ #endif -- 2.30.2