Code

store: Return a positive value when to-be-stored host/service is too old.
authorSebastian Harl <sh@tokkee.org>
Tue, 11 Dec 2012 09:43:42 +0000 (10:43 +0100)
committerSebastian Harl <sh@tokkee.org>
Tue, 11 Dec 2012 09:43:42 +0000 (10:43 +0100)
This allows to let the caller decide what to do in that case.

src/backend/collectd.c
src/backend/puppet-storeconfigs.c
src/core/store.c
src/include/core/store.h

index ad5cad1b439906b31248963b33b4b99b23fd4870..d7cbf661d1d44677257f2bd0c186587cff184956 100644 (file)
@@ -105,14 +105,20 @@ sc_collectd_add_host(char *hostname, sc_time_t last_update)
 {
        sc_host_t host = SC_HOST_INIT;
 
 {
        sc_host_t host = SC_HOST_INIT;
 
+       int status;
+
        host.host_name = hostname;
        host.host_last_update = last_update;
 
        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;
        }
                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",
 
        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;
 
 {
        sc_service_t svc = SC_SVC_INIT;
 
+       int status;
+
        svc.hostname = hostname;
        svc.svc_name = name;
        svc.svc_last_update = last_update;
 
        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;
                fprintf(stderr, "collectd backend: Failed to store/update "
                                "service '%s/%s'.\n", hostname, name);
                return -1;
index 5ebd16f61b8dd5d0fd77cd93076833260a2d5c7b..0d02ba0ecb70ed52de2cd55d3b9aafca0a250b60 100644 (file)
@@ -53,6 +53,8 @@ sc_puppet_stcfg_get_data(sc_dbi_client_t __attribute__((unused)) *client,
 {
        sc_host_t host = SC_HOST_INIT;
 
 {
        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));
        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;
 
        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: 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 */
        free(host.host_name);
        return 0;
 } /* sc_puppet_stcfg_get_data */
index f80c49e15d8964c606a32dad0470755cef9efed3..162a3fc2d7c736d97c5bd406e609a130f6ff2228 100644 (file)
@@ -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 */
                                        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;
                }
                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);
                                        "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;
                }
                else {
                        old->svc_last_update = last_update;
index fe7eb750b8860e45b3810ef76b938d1494459e88..5d11c09975277ded9c43d1425a02d3ac387a207a 100644 (file)
@@ -74,6 +74,20 @@ sc_host_create(char *name);
 sc_host_t *
 sc_host_clone(const sc_host_t *host);
 
 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);
 
 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_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);
 
 int
 sc_store_service(const sc_service_t *svc);