X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fcore%2Fstore.c;h=92f3e510b9bccd6c57076bfbbf920c09c9754fbf;hb=d7a35839f6710e32a38f1cbd6cad7e756f31b1f2;hp=f80c49e15d8964c606a32dad0470755cef9efed3;hpb=22b93dff4d8c4eed238d60f7f6ddc43604206acd;p=sysdb.git diff --git a/src/core/store.c b/src/core/store.c index f80c49e..92f3e51 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -1,5 +1,5 @@ /* - * syscollector - src/core/store.c + * SysDB - src/core/store.c * Copyright (C) 2012 Sebastian 'tokkee' Harl * All rights reserved. * @@ -25,10 +25,11 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "syscollector.h" +#include "sysdb.h" #include "core/store.h" +#include "core/error.h" +#include "core/plugin.h" #include "utils/llist.h" -#include "utils/string.h" #include @@ -44,379 +45,538 @@ * private variables */ -static sc_llist_t *host_list = NULL; +static sdb_llist_t *host_list = NULL; static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER; /* - * private helper functions + * public types */ static int -sc_store_obj_cmp_by_name(const sc_object_t *a, const sc_object_t *b) +sdb_host_init(sdb_object_t *obj, va_list __attribute__((unused)) ap) { - const sc_store_obj_t *h1 = (const sc_store_obj_t *)a; - const sc_store_obj_t *h2 = (const sc_store_obj_t *)b; + SDB_HOST(obj)->_last_update = sdb_gettime(); + /* ignore errors -> last_update will be updated later */ - assert(h1 && h2); - return strcasecmp(h1->name, h2->name); -} /* sc_store_obj_cmp_by_name */ + SDB_HOST(obj)->attributes = sdb_llist_create(); + if (! SDB_HOST(obj)->attributes) + return -1; + SDB_HOST(obj)->services = sdb_llist_create(); + if (! SDB_HOST(obj)->services) + return -1; + return 0; +} /* sdb_host_init */ -static int -sc_host_init(sc_object_t *obj, va_list ap) +static void +sdb_host_destroy(sdb_object_t *obj) { - char *name = va_arg(ap, char *); + assert(obj); - SC_HOST(obj)->host_name = strdup(name); - if (! SC_HOST(obj)->host_name) - return -1; + if (SDB_HOST(obj)->attributes) + sdb_llist_destroy(SDB_HOST(obj)->attributes); + if (SDB_HOST(obj)->services) + sdb_llist_destroy(SDB_HOST(obj)->services); +} /* sdb_host_destroy */ - SC_HOST(obj)->host_last_update = sc_gettime(); - /* ignore errors -> last_update will be updated later */ +static sdb_object_t * +sdb_host_do_clone(const sdb_object_t *obj) +{ + const sdb_host_t *host = (const sdb_host_t *)obj; + sdb_host_t *new; + + new = SDB_HOST(sdb_object_create(obj->name, sdb_host_type)); + if (! new) + return NULL; - SC_HOST(obj)->services = sc_llist_create(); - if (! SC_HOST(obj)->services) + /* make sure these are initialized; else sdb_object_deref() might access + * arbitrary memory in case of an error */ + new->services = new->attributes = NULL; + + if (host->attributes) { + new->attributes = sdb_llist_clone(host->attributes); + if (! new->attributes) { + sdb_object_deref(SDB_OBJ(new)); + return NULL; + } + } + + new->_last_update = host->_last_update; + if (host->services) { + new->services = sdb_llist_clone(host->services); + if (! new->services) { + sdb_object_deref(SDB_OBJ(new)); + return NULL; + } + } + return SDB_OBJ(new); +} /* sdb_host_do_clone */ + +static int +sdb_attr_init(sdb_object_t *obj, va_list ap) +{ + const char *hostname = va_arg(ap, const char *); + const char *value = va_arg(ap, const char *); + + SDB_ATTR(obj)->hostname = strdup(hostname); + SDB_ATTR(obj)->attr_value = strdup(value); + if ((! SDB_ATTR(obj)->hostname) || (! SDB_ATTR(obj)->attr_value)) return -1; + + SDB_ATTR(obj)->_last_update = sdb_gettime(); return 0; -} /* sc_host_init */ +} /* sdb_attr_init */ static void -sc_host_destroy(sc_object_t *obj) +sdb_attr_destroy(sdb_object_t *obj) { assert(obj); - if (SC_HOST(obj)->host_name) - free(SC_HOST(obj)->host_name); + if (SDB_ATTR(obj)->hostname) + free(SDB_ATTR(obj)->hostname); + if (SDB_ATTR(obj)->attr_value) + free(SDB_ATTR(obj)->attr_value); +} /* sdb_attr_destroy */ + +static sdb_object_t * +sdb_attr_clone(const sdb_object_t *obj) +{ + const sdb_attribute_t *attr = (const sdb_attribute_t *)obj; + sdb_attribute_t *new; + + new = sdb_attribute_create(attr->hostname, + obj->name, attr->attr_value); + if (! new) + return NULL; - if (SC_HOST(obj)->services) - sc_llist_destroy(SC_HOST(obj)->services); -} /* sc_host_destroy */ + new->_last_update = attr->_last_update; + return SDB_OBJ(new); +} /* sdb_attr_clone */ static int -sc_svc_init(sc_object_t *obj, va_list ap) +sdb_svc_init(sdb_object_t *obj, va_list ap) { - char *hostname = va_arg(ap, char *); - char *name = va_arg(ap, char *); + const char *hostname = va_arg(ap, const char *); - SC_SVC(obj)->hostname = strdup(hostname); - SC_SVC(obj)->svc_name = strdup(name); - if ((! SC_SVC(obj)->hostname) || (! SC_SVC(obj)->svc_name)) + SDB_SVC(obj)->hostname = strdup(hostname); + if (! SDB_SVC(obj)->hostname) return -1; - SC_SVC(obj)->svc_last_update = sc_gettime(); + SDB_SVC(obj)->_last_update = sdb_gettime(); /* ignore errors -> last_update will be updated later */ return 0; -} /* sc_svc_init */ +} /* sdb_svc_init */ static void -sc_svc_destroy(sc_object_t *obj) +sdb_svc_destroy(sdb_object_t *obj) { assert(obj); - if (SC_SVC(obj)->hostname) - free(SC_SVC(obj)->hostname); - if (SC_SVC(obj)->svc_name) - free(SC_SVC(obj)->svc_name); -} /* sc_svc_destroy */ + if (SDB_SVC(obj)->hostname) + free(SDB_SVC(obj)->hostname); +} /* sdb_svc_destroy */ -/* - * public API - */ - -sc_host_t * -sc_host_create(char *name) +static sdb_object_t * +sdb_svc_clone(const sdb_object_t *obj) { - sc_object_t *obj; + const sdb_service_t *svc = (const sdb_service_t *)obj; + sdb_service_t *new; - if (! name) + new = sdb_service_create(svc->hostname, obj->name); + if (! new) return NULL; - obj = sc_object_create(sizeof(sc_host_t), sc_host_init, - sc_host_destroy, name); - if (! obj) - return NULL; - return SC_HOST(obj); -} /* sc_host_create */ + new->_last_update = svc->_last_update; + return SDB_OBJ(new); +} /* sdb_svc_clone */ -sc_host_t * -sc_host_clone(const sc_host_t *host) -{ - sc_host_t *clone; +const sdb_type_t sdb_host_type = { + sizeof(sdb_host_t), - clone = sc_host_create(host->host_name); - if (! clone) - return NULL; + sdb_host_init, + sdb_host_destroy, + sdb_host_do_clone +}; - clone->host_last_update = host->host_last_update; - if (host->services) { - clone->services = sc_llist_clone(host->services); - if (! clone->services) { - sc_object_deref(SC_OBJ(clone)); - return NULL; - } - } - else - clone->services = NULL; - return clone; -} /* sc_host_clone */ +const sdb_type_t sdb_attribute_type = { + sizeof(sdb_attribute_t), + + sdb_attr_init, + sdb_attr_destroy, + sdb_attr_clone +}; + +const sdb_type_t sdb_service_type = { + sizeof(sdb_service_t), + + sdb_svc_init, + sdb_svc_destroy, + sdb_svc_clone +}; + +/* + * public API + */ int -sc_store_host(const sc_host_t *host) +sdb_store_host(const sdb_host_t *host) { - sc_time_t last_update; + char *cname; + + sdb_time_t last_update; + sdb_host_t *old; - sc_host_t *old; int status = 0; - if ((! host) || (! host->host_name)) + if ((! host) || (! SDB_CONST_OBJ(host)->name)) + return -1; + + cname = sdb_plugin_cname(strdup(SDB_CONST_OBJ(host)->name)); + if (! cname) { + sdb_log(SDB_LOG_ERR, "store: strdup failed"); return -1; + } - last_update = host->host_last_update; + last_update = host->_last_update; if (last_update <= 0) - last_update = sc_gettime(); + last_update = sdb_gettime(); pthread_rwlock_wrlock(&host_lock); if (! host_list) { - if (! (host_list = sc_llist_create())) { + if (! (host_list = sdb_llist_create())) { pthread_rwlock_unlock(&host_lock); return -1; } } - old = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)host, - sc_store_obj_cmp_by_name)); + old = SDB_HOST(sdb_llist_search_by_name(host_list, cname)); if (old) { - if (old->host_last_update > last_update) { - fprintf(stderr, "store: Cannot update host '%s' - " - "value too old (%"PRIscTIME" < %"PRIscTIME")\n", - host->host_name, last_update, old->host_last_update); + if (old->_last_update > last_update) { + sdb_log(SDB_LOG_DEBUG, "store: Cannot update host '%s' - " + "value too old (%"PRIscTIME" < %"PRIscTIME")", + cname, last_update, old->_last_update); /* don't report an error; the host may be updated by multiple * backends */ - status = 0; + status = 1; } else { - old->host_last_update = last_update; + old->_last_update = last_update; } } else { - sc_host_t *new = sc_host_clone(host); + sdb_host_t *new = SDB_HOST(sdb_object_clone(SDB_CONST_OBJ(host))); if (! new) { char errbuf[1024]; - fprintf(stderr, "store: Failed to clone host object: %s\n", - sc_strerror(errno, errbuf, sizeof(errbuf))); + sdb_log(SDB_LOG_ERR, "store: Failed to clone host object: %s", + sdb_strerror(errno, errbuf, sizeof(errbuf))); + pthread_rwlock_unlock(&host_lock); return -1; } + free(SDB_OBJ(new)->name); + SDB_OBJ(new)->name = cname; + + if (! new->attributes) { + if (! (new->attributes = sdb_llist_create())) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "store: Failed to initialize " + "host object '%s': %s", SDB_OBJ(new)->name, + sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_object_deref(SDB_OBJ(new)); + pthread_rwlock_unlock(&host_lock); + return -1; + } + } + if (! new->services) { - if (! (new->services = sc_llist_create())) { + if (! (new->services = sdb_llist_create())) { char errbuf[1024]; - fprintf(stderr, "store: Failed to initialize " - "host object '%s': %s\n", host->host_name, - sc_strerror(errno, errbuf, sizeof(errbuf))); - sc_object_deref(SC_OBJ(new)); + sdb_log(SDB_LOG_ERR, "store: Failed to initialize " + "host object '%s': %s", SDB_OBJ(new)->name, + sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_object_deref(SDB_OBJ(new)); + pthread_rwlock_unlock(&host_lock); return -1; } } - status = sc_llist_insert_sorted(host_list, SC_OBJ(new), - sc_store_obj_cmp_by_name); + status = sdb_llist_insert_sorted(host_list, SDB_OBJ(new), + sdb_object_cmp_by_name); /* pass control to the list or destroy in case of an error */ - sc_object_deref(SC_OBJ(new)); + sdb_object_deref(SDB_OBJ(new)); } pthread_rwlock_unlock(&host_lock); return status; -} /* sc_store_host */ +} /* sdb_store_host */ -const sc_host_t * -sc_store_get_host(char *name) +const sdb_host_t * +sdb_store_get_host(const char *name) { - sc_host_t tmp = SC_HOST_INIT; - sc_host_t *host; + sdb_host_t *host; if (! name) return NULL; - tmp.host_name = name; - host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&tmp, - sc_store_obj_cmp_by_name)); - + host = SDB_HOST(sdb_llist_search_by_name(host_list, name)); if (! host) return NULL; return host; -} /* sc_store_get_host */ +} /* sdb_store_get_host */ -sc_service_t * -sc_service_create(char *hostname, char *name) +sdb_attribute_t * +sdb_attribute_create(const char *hostname, + const char *name, const char *value) { - sc_object_t *obj; + sdb_object_t *obj; - if ((! hostname) || (! name)) + if ((! hostname) || (! name) || (! value)) return NULL; - obj = sc_object_create(sizeof(sc_service_t), sc_svc_init, - sc_svc_destroy, hostname, name); + obj = sdb_object_create(name, sdb_attribute_type, hostname, value); if (! obj) return NULL; - return SC_SVC(obj); -} /* sc_service_create */ + return SDB_ATTR(obj); +} /* sdb_attribute_create */ -sc_service_t * -sc_service_clone(const sc_service_t *svc) +int +sdb_store_attribute(const sdb_attribute_t *attr) { - sc_service_t *clone; + sdb_host_t *host; + sdb_attribute_t *old; + sdb_time_t last_update; - clone = sc_service_create(svc->hostname, svc->svc_name); - if (! clone) - return NULL; + int status = 0; - clone->svc_last_update = svc->svc_last_update; - return clone; -} /* sc_service_clone */ + if (! attr) + return -1; -int -sc_store_service(const sc_service_t *svc) + last_update = attr->_last_update; + if (last_update <= 0) + last_update = sdb_gettime(); + + if (! host_list) + return -1; + + pthread_rwlock_wrlock(&host_lock); + + host = SDB_HOST(sdb_llist_search_by_name(host_list, attr->hostname)); + if (! host) { + pthread_rwlock_unlock(&host_lock); + return -1; + } + + old = SDB_ATTR(sdb_llist_search_by_name(host->attributes, + SDB_CONST_OBJ(attr)->name)); + if (old) { + if (old->_last_update > last_update) { + sdb_log(SDB_LOG_DEBUG, "store: Cannot update attribute " + "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")", + attr->hostname, SDB_CONST_OBJ(attr)->name, last_update, + old->_last_update); + status = 1; + } + else { + old->_last_update = last_update; + } + } + else { + sdb_attribute_t *new = SDB_ATTR(sdb_object_clone(SDB_CONST_OBJ(attr))); + if (! new) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute " + "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf))); + pthread_rwlock_unlock(&host_lock); + return -1; + } + + status = sdb_llist_insert_sorted(host->attributes, SDB_OBJ(new), + sdb_object_cmp_by_name); + + /* pass control to the list or destroy in case of an error */ + sdb_object_deref(SDB_OBJ(new)); + } + + pthread_rwlock_unlock(&host_lock); + return status; +} /* sdb_store_attribute */ + +sdb_service_t * +sdb_service_create(const char *hostname, const char *name) { - sc_host_t tmp = SC_HOST_INIT; - sc_host_t *host; + sdb_object_t *obj; + + if ((! hostname) || (! name)) + return NULL; - sc_service_t *old; + obj = sdb_object_create(name, sdb_service_type, hostname); + if (! obj) + return NULL; + return SDB_SVC(obj); +} /* sdb_service_create */ - sc_time_t last_update; +int +sdb_store_service(const sdb_service_t *svc) +{ + sdb_host_t *host; + sdb_service_t *old; + sdb_time_t last_update; int status = 0; if (! svc) return -1; - last_update = svc->svc_last_update; + last_update = svc->_last_update; if (last_update <= 0) - last_update = sc_gettime(); + last_update = sdb_gettime(); if (! host_list) return -1; pthread_rwlock_wrlock(&host_lock); - tmp.host_name = svc->hostname; - host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&tmp, - sc_store_obj_cmp_by_name)); - - if (! host) + host = SDB_HOST(sdb_llist_search_by_name(host_list, svc->hostname)); + if (! host) { + pthread_rwlock_unlock(&host_lock); return -1; + } - old = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)svc, - sc_store_obj_cmp_by_name)); - + old = SDB_SVC(sdb_llist_search_by_name(host->services, + SDB_CONST_OBJ(svc)->name)); if (old) { - if (old->host_last_update > last_update) { - fprintf(stderr, "store: Cannot update service '%s/%s' - " - "value too old (%"PRIscTIME" < %"PRIscTIME")\n", - svc->hostname, svc->svc_name, last_update, - old->host_last_update); - status = -1; + if (old->_last_update > last_update) { + sdb_log(SDB_LOG_DEBUG, "store: Cannot update service " + "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")", + svc->hostname, SDB_CONST_OBJ(svc)->name, last_update, + old->_last_update); + status = 1; } else { - old->svc_last_update = last_update; + old->_last_update = last_update; } } else { - sc_service_t *new = sc_service_clone(svc); + sdb_service_t *new = SDB_SVC(sdb_object_clone(SDB_CONST_OBJ(svc))); if (! new) { char errbuf[1024]; - fprintf(stderr, "store: Failed to clone service object: %s\n", - sc_strerror(errno, errbuf, sizeof(errbuf))); + sdb_log(SDB_LOG_ERR, "store: Failed to clone service " + "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf))); + pthread_rwlock_unlock(&host_lock); return -1; } - status = sc_llist_insert_sorted(host->services, SC_OBJ(new), - sc_store_obj_cmp_by_name); + status = sdb_llist_insert_sorted(host->services, SDB_OBJ(new), + sdb_object_cmp_by_name); /* pass control to the list or destroy in case of an error */ - sc_object_deref(SC_OBJ(new)); + sdb_object_deref(SDB_OBJ(new)); } pthread_rwlock_unlock(&host_lock); return status; -} /* sc_store_service */ +} /* sdb_store_service */ -const sc_service_t * -sc_store_get_service(const sc_host_t *host, char *name) +const sdb_service_t * +sdb_store_get_service(const sdb_host_t *host, const char *name) { - sc_service_t tmp = SC_SVC_INIT; - sc_service_t *svc; + sdb_service_t *svc; if ((! host) || (! name)) return NULL; - tmp.svc_name = name; - svc = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&tmp, - sc_store_obj_cmp_by_name)); - + svc = SDB_SVC(sdb_llist_search_by_name(host->services, name)); if (! svc) return NULL; return svc; -} /* sc_store_get_service */ +} /* sdb_store_get_service */ int -sc_store_dump(FILE *fh) +sdb_store_dump(FILE *fh) { - sc_llist_iter_t *host_iter; + sdb_llist_iter_t *host_iter; if (! fh) return -1; pthread_rwlock_rdlock(&host_lock); - host_iter = sc_llist_get_iter(host_list); - if (! host_iter) + host_iter = sdb_llist_get_iter(host_list); + if (! host_iter) { + pthread_rwlock_unlock(&host_lock); return -1; + } - while (sc_llist_iter_has_next(host_iter)) { - sc_host_t *host = SC_HOST(sc_llist_iter_get_next(host_iter)); - sc_llist_iter_t *svc_iter; + while (sdb_llist_iter_has_next(host_iter)) { + sdb_host_t *host = SDB_HOST(sdb_llist_iter_get_next(host_iter)); + sdb_llist_iter_t *svc_iter; + sdb_llist_iter_t *attr_iter; char time_str[64]; assert(host); - if (! sc_strftime(time_str, sizeof(time_str), - "%F %T %z", host->host_last_update)) + if (! sdb_strftime(time_str, sizeof(time_str), + "%F %T %z", host->_last_update)) snprintf(time_str, sizeof(time_str), ""); time_str[sizeof(time_str) - 1] = '\0'; fprintf(fh, "Host '%s' (last updated: %s):\n", - host->host_name, time_str); + SDB_OBJ(host)->name, time_str); + + attr_iter = sdb_llist_get_iter(host->attributes); + if (! attr_iter) { + char errbuf[1024]; + fprintf(fh, "Failed to retrieve attributes: %s\n", + sdb_strerror(errno, errbuf, sizeof(errbuf))); + continue; + } + + while (sdb_llist_iter_has_next(attr_iter)) { + sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter)); + assert(attr); - svc_iter = sc_llist_get_iter(host->services); + if (! sdb_strftime(time_str, sizeof(time_str), + "%F %T %z", attr->_last_update)) + snprintf(time_str, sizeof(time_str), ""); + time_str[sizeof(time_str) - 1] = '\0'; + + fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n", + SDB_OBJ(attr)->name, attr->attr_value, time_str); + } + + sdb_llist_iter_destroy(attr_iter); + + svc_iter = sdb_llist_get_iter(host->services); if (! svc_iter) { char errbuf[1024]; fprintf(fh, "Failed to retrieve services: %s\n", - sc_strerror(errno, errbuf, sizeof(errbuf))); + sdb_strerror(errno, errbuf, sizeof(errbuf))); continue; } - while (sc_llist_iter_has_next(svc_iter)) { - sc_service_t *svc = SC_SVC(sc_llist_iter_get_next(svc_iter)); + while (sdb_llist_iter_has_next(svc_iter)) { + sdb_service_t *svc = SDB_SVC(sdb_llist_iter_get_next(svc_iter)); assert(svc); - if (! sc_strftime(time_str, sizeof(time_str), - "%F %T %z", host->host_last_update)) + if (! sdb_strftime(time_str, sizeof(time_str), + "%F %T %z", svc->_last_update)) snprintf(time_str, sizeof(time_str), ""); time_str[sizeof(time_str) - 1] = '\0'; fprintf(fh, "\tService '%s' (last updated: %s)\n", - svc->svc_name, time_str); + SDB_OBJ(svc)->name, time_str); } - sc_llist_iter_destroy(svc_iter); + sdb_llist_iter_destroy(svc_iter); } - sc_llist_iter_destroy(host_iter); + sdb_llist_iter_destroy(host_iter); + pthread_rwlock_unlock(&host_lock); return 0; -} /* sc_store_dump */ +} /* sdb_store_dump */ /* vim: set tw=78 sw=4 ts=4 noexpandtab : */