Code

store: Added support for host attributes.
authorSebastian Harl <sh@tokkee.org>
Mon, 17 Dec 2012 10:39:48 +0000 (11:39 +0100)
committerSebastian Harl <sh@tokkee.org>
Mon, 17 Dec 2012 10:39:48 +0000 (11:39 +0100)
Attributes are a list of key / value pairs assigned to a host object. They may
be used to store arbitrary properties, settings, or other characteristics of a
host.

Later on, this information will also be used to filter, group, or
hierarchically order hosts.

src/core/store.c
src/include/core/store.h

index 87f94dd3b5edbccb2e1e6a60ecc57392399e97a2..3e9ec55eec519087f63bddf444fb6ed5df24660a 100644 (file)
@@ -94,6 +94,9 @@ sc_host_init(sc_object_t *obj, va_list ap)
        SC_HOST(obj)->host_last_update = sc_gettime();
        /* ignore errors -> last_update will be updated later */
 
        SC_HOST(obj)->host_last_update = sc_gettime();
        /* ignore errors -> last_update will be updated later */
 
+       SC_HOST(obj)->attributes = sc_llist_create();
+       if (! SC_HOST(obj)->attributes)
+               return -1;
        SC_HOST(obj)->services = sc_llist_create();
        if (! SC_HOST(obj)->services)
                return -1;
        SC_HOST(obj)->services = sc_llist_create();
        if (! SC_HOST(obj)->services)
                return -1;
@@ -108,10 +111,43 @@ sc_host_destroy(sc_object_t *obj)
        if (SC_HOST(obj)->host_name)
                free(SC_HOST(obj)->host_name);
 
        if (SC_HOST(obj)->host_name)
                free(SC_HOST(obj)->host_name);
 
+       if (SC_HOST(obj)->attributes)
+               sc_llist_destroy(SC_HOST(obj)->attributes);
        if (SC_HOST(obj)->services)
                sc_llist_destroy(SC_HOST(obj)->services);
 } /* sc_host_destroy */
 
        if (SC_HOST(obj)->services)
                sc_llist_destroy(SC_HOST(obj)->services);
 } /* sc_host_destroy */
 
+static int
+sc_attr_init(sc_object_t *obj, va_list ap)
+{
+       const char *hostname = va_arg(ap, const char *);
+       const char *name = va_arg(ap, const char *);
+       const char *value = va_arg(ap, const char *);
+
+       SC_ATTR(obj)->hostname = strdup(hostname);
+       SC_ATTR(obj)->attr_name = strdup(name);
+       SC_ATTR(obj)->attr_value = strdup(value);
+       if ((! SC_ATTR(obj)->hostname)
+                       || (! SC_ATTR(obj)->attr_name) || (! SC_ATTR(obj)->attr_value))
+               return -1;
+
+       SC_ATTR(obj)->attr_last_update = sc_gettime();
+       return 0;
+} /* sc_attr_init */
+
+static void
+sc_attr_destroy(sc_object_t *obj)
+{
+       assert(obj);
+
+       if (SC_ATTR(obj)->hostname)
+               free(SC_ATTR(obj)->hostname);
+       if (SC_ATTR(obj)->attr_name)
+               free(SC_ATTR(obj)->attr_name);
+       if (SC_ATTR(obj)->attr_value)
+               free(SC_ATTR(obj)->attr_value);
+} /* sc_attr_destroy */
+
 static int
 sc_svc_init(sc_object_t *obj, va_list ap)
 {
 static int
 sc_svc_init(sc_object_t *obj, va_list ap)
 {
@@ -167,6 +203,18 @@ sc_host_clone(const sc_host_t *host)
        if (! clone)
                return NULL;
 
        if (! clone)
                return NULL;
 
+       /* make sure these are initialized; else sc_object_deref() might access
+        * arbitrary memory in case of an error */
+       clone->services = clone->attributes = NULL;
+
+       if (host->attributes) {
+               clone->attributes = sc_llist_clone(host->attributes);
+               if (! clone->attributes) {
+                       sc_object_deref(SC_OBJ(clone));
+                       return NULL;
+               }
+       }
+
        clone->host_last_update = host->host_last_update;
        if (host->services) {
                clone->services = sc_llist_clone(host->services);
        clone->host_last_update = host->host_last_update;
        if (host->services) {
                clone->services = sc_llist_clone(host->services);
@@ -175,8 +223,6 @@ sc_host_clone(const sc_host_t *host)
                        return NULL;
                }
        }
                        return NULL;
                }
        }
-       else
-               clone->services = NULL;
        return clone;
 } /* sc_host_clone */
 
        return clone;
 } /* sc_host_clone */
 
@@ -231,6 +277,17 @@ sc_store_host(const sc_host_t *host)
                        return -1;
                }
 
                        return -1;
                }
 
+               if (! new->attributes) {
+                       if (! (new->attributes = sc_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));
+                               return -1;
+                       }
+               }
+
                if (! new->services) {
                        if (! (new->services = sc_llist_create())) {
                                char errbuf[1024];
                if (! new->services) {
                        if (! (new->services = sc_llist_create())) {
                                char errbuf[1024];
@@ -271,6 +328,104 @@ sc_store_get_host(const char *name)
        return host;
 } /* sc_store_get_host */
 
        return host;
 } /* sc_store_get_host */
 
+sc_attribute_t *
+sc_attribute_create(const char *hostname,
+               const char *name, const char *value)
+{
+       sc_object_t *obj;
+
+       if ((! hostname) || (! name) || (! value))
+               return NULL;
+
+       obj = sc_object_create(sizeof(sc_attribute_t), sc_attr_init,
+                       sc_attr_destroy, hostname, name, value);
+       if (! obj)
+               return NULL;
+       return SC_ATTR(obj);
+} /* sc_attribute_create */
+
+sc_attribute_t *
+sc_attribute_clone(const sc_attribute_t *attr)
+{
+       sc_attribute_t *clone;
+
+       clone = sc_attribute_create(attr->hostname,
+                       attr->attr_name, attr->attr_value);
+       if (! clone)
+               return NULL;
+
+       clone->attr_last_update = attr->attr_last_update;
+       return clone;
+} /* sc_attribute_clone */
+
+int
+sc_store_attribute(const sc_attribute_t *attr)
+{
+       sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
+       sc_host_t *host;
+
+       sc_attribute_t *old;
+
+       sc_time_t last_update;
+
+       int status = 0;
+
+       if (! attr)
+               return -1;
+
+       last_update = attr->attr_last_update;
+       if (last_update <= 0)
+               last_update = sc_gettime();
+
+       if (! host_list)
+               return -1;
+
+       pthread_rwlock_wrlock(&host_lock);
+
+       lookup.obj_name = attr->hostname;
+       host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup,
+                               sc_cmp_store_obj_with_name));
+
+       if (! host)
+               return -1;
+
+       lookup.obj_name = attr->attr_name;
+       old = SC_ATTR(sc_llist_search(host->attributes,
+                               (const sc_object_t *)&lookup,
+                               sc_cmp_store_obj_with_name));
+
+       if (old) {
+               if (old->host_last_update > last_update) {
+                       fprintf(stderr, "store: Cannot update attribute '%s/%s' - "
+                                       "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
+                                       attr->hostname, attr->attr_name, last_update,
+                                       old->host_last_update);
+                       status = 1;
+               }
+               else {
+                       old->attr_last_update = last_update;
+               }
+       }
+       else {
+               sc_attribute_t *new = sc_attribute_clone(attr);
+               if (! new) {
+                       char errbuf[1024];
+                       fprintf(stderr, "store: Failed to clone attribute object: %s\n",
+                                       sc_strerror(errno, errbuf, sizeof(errbuf)));
+                       return -1;
+               }
+
+               status = sc_llist_insert_sorted(host->attributes, SC_OBJ(new),
+                               sc_store_obj_cmp_by_name);
+
+               /* pass control to the list or destroy in case of an error */
+               sc_object_deref(SC_OBJ(new));
+       }
+
+       pthread_rwlock_unlock(&host_lock);
+       return status;
+} /* sc_store_attribute */
+
 sc_service_t *
 sc_service_create(const char *hostname, const char *name)
 {
 sc_service_t *
 sc_service_create(const char *hostname, const char *name)
 {
@@ -401,6 +556,7 @@ sc_store_dump(FILE *fh)
        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 (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;
+               sc_llist_iter_t *attr_iter;
 
                char time_str[64];
 
 
                char time_str[64];
 
@@ -414,6 +570,29 @@ sc_store_dump(FILE *fh)
                fprintf(fh, "Host '%s' (last updated: %s):\n",
                                host->host_name, time_str);
 
                fprintf(fh, "Host '%s' (last updated: %s):\n",
                                host->host_name, time_str);
 
+               attr_iter = sc_llist_get_iter(host->attributes);
+               if (! attr_iter) {
+                       char errbuf[1024];
+                       fprintf(fh, "Failed to retrieve attributes: %s\n",
+                                       sc_strerror(errno, errbuf, sizeof(errbuf)));
+                       continue;
+               }
+
+               while (sc_llist_iter_has_next(attr_iter)) {
+                       sc_attribute_t *attr = SC_ATTR(sc_llist_iter_get_next(attr_iter));
+                       assert(attr);
+
+                       if (! sc_strftime(time_str, sizeof(time_str),
+                                               "%F %T %z", attr->attr_last_update))
+                               snprintf(time_str, sizeof(time_str), "<error>");
+                       time_str[sizeof(time_str) - 1] = '\0';
+
+                       fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
+                                       attr->attr_name, attr->attr_value, time_str);
+               }
+
+               sc_llist_iter_destroy(attr_iter);
+
                svc_iter = sc_llist_get_iter(host->services);
                if (! svc_iter) {
                        char errbuf[1024];
                svc_iter = sc_llist_get_iter(host->services);
                if (! svc_iter) {
                        char errbuf[1024];
index 82507a9d424c2c33865e5f133a6fd753ca32d94c..cadbfe47dbf861939168c3558ddbb6c7bd53c650 100644 (file)
@@ -58,14 +58,26 @@ typedef struct {
 #define SC_SVC_INIT { SC_STORE_OBJ_INIT, NULL }
 #define SC_SVC(obj) ((sc_service_t *)(obj))
 
 #define SC_SVC_INIT { SC_STORE_OBJ_INIT, NULL }
 #define SC_SVC(obj) ((sc_service_t *)(obj))
 
+typedef struct {
+       sc_store_obj_t parent;
+#define attr_last_update parent.last_update
+#define attr_name parent.name
+
+       char *attr_value;
+       char *hostname;
+} sc_attribute_t;
+#define SC_ATTR_INIT { SC_STORE_OBJ_INIT, NULL, NULL }
+#define SC_ATTR(obj) ((sc_attribute_t *)(obj))
+
 typedef struct {
        sc_store_obj_t parent;
 #define host_last_update parent.last_update
 #define host_name parent.name
 
 typedef struct {
        sc_store_obj_t parent;
 #define host_last_update parent.last_update
 #define host_name parent.name
 
+       sc_llist_t *attributes;
        sc_llist_t *services;
 } sc_host_t;
        sc_llist_t *services;
 } sc_host_t;
-#define SC_HOST_INIT { SC_STORE_OBJ_INIT, NULL }
+#define SC_HOST_INIT { SC_STORE_OBJ_INIT, NULL, NULL }
 #define SC_HOST(obj) ((sc_host_t *)(obj))
 
 sc_host_t *
 #define SC_HOST(obj) ((sc_host_t *)(obj))
 
 sc_host_t *
@@ -94,6 +106,32 @@ sc_store_host(const sc_host_t *host);
 const sc_host_t *
 sc_store_get_host(const char *name);
 
 const sc_host_t *
 sc_store_get_host(const char *name);
 
+sc_attribute_t *
+sc_attribute_create(const char *hostname,
+               const char *name, const char *value);
+
+sc_attribute_t *
+sc_attribute_clone(const sc_attribute_t *attr);
+
+/*
+ * sc_store_attribute:
+ * Add/update a host's attribute in the store. If the attribute, identified by
+ * its name, already exists for the specified host, it will be updated
+ * according to the specified 'attr' 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 and
+ * managed by the store itself. The specified attribute-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_attribute(const sc_attribute_t *attr);
+
 sc_service_t *
 sc_service_create(const char *hostname, const char *name);
 
 sc_service_t *
 sc_service_create(const char *hostname, const char *name);