From: Sebastian Harl Date: Tue, 11 Dec 2012 09:43:42 +0000 (+0100) Subject: store: Return a positive value when to-be-stored host/service is too old. X-Git-Tag: sysdb-0.1.0~460 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=d170cd34bb8b4f135ec41c622e6a09ff83ff1011;p=sysdb.git store: Return a positive value when to-be-stored host/service is too old. This allows to let the caller decide what to do in that case. --- diff --git a/src/backend/collectd.c b/src/backend/collectd.c index ad5cad1..d7cbf66 100644 --- a/src/backend/collectd.c +++ b/src/backend/collectd.c @@ -105,14 +105,20 @@ sc_collectd_add_host(char *hostname, sc_time_t last_update) { sc_host_t host = SC_HOST_INIT; + int status; + host.host_name = hostname; host.host_last_update = last_update; - if (sc_store_host(&host)) { + status = sc_store_host(&host); + + if (status < 0) { fprintf(stderr, "collectd backend: Failed to store/update " "host '%s'.\n", hostname); return -1; } + else if (status > 0) /* value too old */ + return 0; fprintf(stderr, "collectd backend: Added/updated host '%s' " "(last update timestamp = %"PRIscTIME").\n", @@ -125,11 +131,14 @@ sc_collectd_add_svc(char *hostname, char *name, sc_time_t last_update) { sc_service_t svc = SC_SVC_INIT; + int status; + svc.hostname = hostname; svc.svc_name = name; svc.svc_last_update = last_update; - if (sc_store_service(&svc)) { + status = sc_store_service(&svc); + if (status < 0) { fprintf(stderr, "collectd backend: Failed to store/update " "service '%s/%s'.\n", hostname, name); return -1; diff --git a/src/backend/puppet-storeconfigs.c b/src/backend/puppet-storeconfigs.c index 5ebd16f..0d02ba0 100644 --- a/src/backend/puppet-storeconfigs.c +++ b/src/backend/puppet-storeconfigs.c @@ -53,6 +53,8 @@ sc_puppet_stcfg_get_data(sc_dbi_client_t __attribute__((unused)) *client, { sc_host_t host = SC_HOST_INIT; + int status; + assert(n == 2); assert((data[0].type == DBI_TYPE_STRING) && (data[1].type == DBI_TYPE_DATETIME)); @@ -60,16 +62,18 @@ sc_puppet_stcfg_get_data(sc_dbi_client_t __attribute__((unused)) *client, host.host_name = strdup(data[0].data.string); host.host_last_update = data[1].data.datetime; - if (sc_store_host(&host)) { + status = sc_store_host(&host); + + if (status < 0) { fprintf(stderr, "puppet storeconfigs backend: Failed to store/update " "host '%s'.\n", host.host_name); free(host.host_name); return -1; } - - fprintf(stderr, "puppet storeconfigs backend: Added/updated host '%s' " - "(last update timestamp = %"PRIscTIME").\n", - host.host_name, host.host_last_update); + else if (! status) + fprintf(stderr, "puppet storeconfigs backend: Added/updated host '%s' " + "(last update timestamp = %"PRIscTIME").\n", + host.host_name, host.host_last_update); free(host.host_name); return 0; } /* sc_puppet_stcfg_get_data */ diff --git a/src/core/store.c b/src/core/store.c index f80c49e..162a3fc 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -193,7 +193,7 @@ sc_store_host(const sc_host_t *host) host->host_name, last_update, old->host_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; @@ -316,7 +316,7 @@ sc_store_service(const sc_service_t *svc) "value too old (%"PRIscTIME" < %"PRIscTIME")\n", svc->hostname, svc->svc_name, last_update, old->host_last_update); - status = -1; + status = 1; } else { old->svc_last_update = last_update; diff --git a/src/include/core/store.h b/src/include/core/store.h index fe7eb75..5d11c09 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -74,6 +74,20 @@ sc_host_create(char *name); sc_host_t * sc_host_clone(const sc_host_t *host); +/* + * sc_store_host: + * Add/update a host in the store. If the host, identified by its name, + * already exists, it will be updated according to the specified 'host' + * object. Else, a new entry will be created in the store. Any memory required + * for storing the entry will be allocated an managed by the store itself. The + * specified host-object will not be referenced or further accessed. + * + * Returns: + * - 0 on success + * - a positive value if the new entry is older than the currently stored + * entry (in this case, no update will happen) + * - a negative value on error + */ int sc_store_host(const sc_host_t *host); @@ -86,6 +100,22 @@ sc_service_create(char *hostname, char *name); sc_service_t * sc_service_clone(const sc_service_t *svc); +/* + * sc_store_service: + * Add/update a store in the store. If the service, identified by its name, + * already exists for the specified host, it will be updated according to the + * specified 'service' object. If the referenced host does not exist, an error + * will be reported. Else, a new entry will be created in the store. Any + * memory required for storing the entry will be allocated an managed by the + * store itself. The specified service-object will not be referenced or + * further accessed. + * + * Returns: + * - 0 on success + * - a positive value if the new entry is older than the currently stored + * entry (in this case, no update will happen) + * - a negative value on error + */ int sc_store_service(const sc_service_t *svc);