Code

store: Added support for host attributes.
[sysdb.git] / src / core / store.c
1 /*
2  * syscollector - src/core/store.c
3  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #include "syscollector.h"
29 #include "core/store.h"
30 #include "utils/llist.h"
31 #include "utils/string.h"
33 #include <assert.h>
35 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include <pthread.h>
43 /*
44  * private data types
45  */
47 /* type used for looking up named objects */
48 typedef struct {
49         sc_object_t parent;
50         const char *obj_name;
51 } sc_store_lookup_obj_t;
52 #define SC_STORE_LOOKUP_OBJ_INIT { SC_OBJECT_INIT, NULL }
54 /*
55  * private variables
56  */
58 static sc_llist_t *host_list = NULL;
59 static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER;
61 /*
62  * private helper functions
63  */
65 static int
66 sc_store_obj_cmp_by_name(const sc_object_t *a, const sc_object_t *b)
67 {
68         const sc_store_obj_t *h1 = (const sc_store_obj_t *)a;
69         const sc_store_obj_t *h2 = (const sc_store_obj_t *)b;
71         assert(h1 && h2);
72         return strcasecmp(h1->name, h2->name);
73 } /* sc_store_obj_cmp_by_name */
75 static int
76 sc_cmp_store_obj_with_name(const sc_object_t *a, const sc_object_t *b)
77 {
78         const sc_store_obj_t *obj = (const sc_store_obj_t *)a;
79         const sc_store_lookup_obj_t *lookup = (const sc_store_lookup_obj_t *)b;
81         assert(obj && lookup);
82         return strcasecmp(obj->name, lookup->obj_name);
83 } /* sc_cmp_store_obj_with_name */
85 static int
86 sc_host_init(sc_object_t *obj, va_list ap)
87 {
88         const char *name = va_arg(ap, const char *);
90         SC_HOST(obj)->host_name = strdup(name);
91         if (! SC_HOST(obj)->host_name)
92                 return -1;
94         SC_HOST(obj)->host_last_update = sc_gettime();
95         /* ignore errors -> last_update will be updated later */
97         SC_HOST(obj)->attributes = sc_llist_create();
98         if (! SC_HOST(obj)->attributes)
99                 return -1;
100         SC_HOST(obj)->services = sc_llist_create();
101         if (! SC_HOST(obj)->services)
102                 return -1;
103         return 0;
104 } /* sc_host_init */
106 static void
107 sc_host_destroy(sc_object_t *obj)
109         assert(obj);
111         if (SC_HOST(obj)->host_name)
112                 free(SC_HOST(obj)->host_name);
114         if (SC_HOST(obj)->attributes)
115                 sc_llist_destroy(SC_HOST(obj)->attributes);
116         if (SC_HOST(obj)->services)
117                 sc_llist_destroy(SC_HOST(obj)->services);
118 } /* sc_host_destroy */
120 static int
121 sc_attr_init(sc_object_t *obj, va_list ap)
123         const char *hostname = va_arg(ap, const char *);
124         const char *name = va_arg(ap, const char *);
125         const char *value = va_arg(ap, const char *);
127         SC_ATTR(obj)->hostname = strdup(hostname);
128         SC_ATTR(obj)->attr_name = strdup(name);
129         SC_ATTR(obj)->attr_value = strdup(value);
130         if ((! SC_ATTR(obj)->hostname)
131                         || (! SC_ATTR(obj)->attr_name) || (! SC_ATTR(obj)->attr_value))
132                 return -1;
134         SC_ATTR(obj)->attr_last_update = sc_gettime();
135         return 0;
136 } /* sc_attr_init */
138 static void
139 sc_attr_destroy(sc_object_t *obj)
141         assert(obj);
143         if (SC_ATTR(obj)->hostname)
144                 free(SC_ATTR(obj)->hostname);
145         if (SC_ATTR(obj)->attr_name)
146                 free(SC_ATTR(obj)->attr_name);
147         if (SC_ATTR(obj)->attr_value)
148                 free(SC_ATTR(obj)->attr_value);
149 } /* sc_attr_destroy */
151 static int
152 sc_svc_init(sc_object_t *obj, va_list ap)
154         const char *hostname = va_arg(ap, const char *);
155         const char *name = va_arg(ap, const char *);
157         SC_SVC(obj)->hostname = strdup(hostname);
158         SC_SVC(obj)->svc_name = strdup(name);
159         if ((! SC_SVC(obj)->hostname) || (! SC_SVC(obj)->svc_name))
160                 return -1;
162         SC_SVC(obj)->svc_last_update = sc_gettime();
163         /* ignore errors -> last_update will be updated later */
164         return 0;
165 } /* sc_svc_init */
167 static void
168 sc_svc_destroy(sc_object_t *obj)
170         assert(obj);
172         if (SC_SVC(obj)->hostname)
173                 free(SC_SVC(obj)->hostname);
174         if (SC_SVC(obj)->svc_name)
175                 free(SC_SVC(obj)->svc_name);
176 } /* sc_svc_destroy */
178 /*
179  * public API
180  */
182 sc_host_t *
183 sc_host_create(const char *name)
185         sc_object_t *obj;
187         if (! name)
188                 return NULL;
190         obj = sc_object_create(sizeof(sc_host_t), sc_host_init,
191                         sc_host_destroy, name);
192         if (! obj)
193                 return NULL;
194         return SC_HOST(obj);
195 } /* sc_host_create */
197 sc_host_t *
198 sc_host_clone(const sc_host_t *host)
200         sc_host_t *clone;
202         clone = sc_host_create(host->host_name);
203         if (! clone)
204                 return NULL;
206         /* make sure these are initialized; else sc_object_deref() might access
207          * arbitrary memory in case of an error */
208         clone->services = clone->attributes = NULL;
210         if (host->attributes) {
211                 clone->attributes = sc_llist_clone(host->attributes);
212                 if (! clone->attributes) {
213                         sc_object_deref(SC_OBJ(clone));
214                         return NULL;
215                 }
216         }
218         clone->host_last_update = host->host_last_update;
219         if (host->services) {
220                 clone->services = sc_llist_clone(host->services);
221                 if (! clone->services) {
222                         sc_object_deref(SC_OBJ(clone));
223                         return NULL;
224                 }
225         }
226         return clone;
227 } /* sc_host_clone */
229 int
230 sc_store_host(const sc_host_t *host)
232         sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
233         sc_time_t last_update;
235         sc_host_t *old;
236         int status = 0;
238         if ((! host) || (! host->host_name))
239                 return -1;
241         last_update = host->host_last_update;
242         if (last_update <= 0)
243                 last_update = sc_gettime();
245         pthread_rwlock_wrlock(&host_lock);
247         if (! host_list) {
248                 if (! (host_list = sc_llist_create())) {
249                         pthread_rwlock_unlock(&host_lock);
250                         return -1;
251                 }
252         }
254         lookup.obj_name = host->host_name;
255         old = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup,
256                                 sc_cmp_store_obj_with_name));
258         if (old) {
259                 if (old->host_last_update > last_update) {
260                         fprintf(stderr, "store: Cannot update host '%s' - "
261                                         "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
262                                         host->host_name, last_update, old->host_last_update);
263                         /* don't report an error; the host may be updated by multiple
264                          * backends */
265                         status = 1;
266                 }
267                 else {
268                         old->host_last_update = last_update;
269                 }
270         }
271         else {
272                 sc_host_t *new = sc_host_clone(host);
273                 if (! new) {
274                         char errbuf[1024];
275                         fprintf(stderr, "store: Failed to clone host object: %s\n",
276                                         sc_strerror(errno, errbuf, sizeof(errbuf)));
277                         return -1;
278                 }
280                 if (! new->attributes) {
281                         if (! (new->attributes = sc_llist_create())) {
282                                 char errbuf[1024];
283                                 fprintf(stderr, "store: Failed to initialize "
284                                                 "host object '%s': %s\n", host->host_name,
285                                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
286                                 sc_object_deref(SC_OBJ(new));
287                                 return -1;
288                         }
289                 }
291                 if (! new->services) {
292                         if (! (new->services = sc_llist_create())) {
293                                 char errbuf[1024];
294                                 fprintf(stderr, "store: Failed to initialize "
295                                                 "host object '%s': %s\n", host->host_name,
296                                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
297                                 sc_object_deref(SC_OBJ(new));
298                                 return -1;
299                         }
300                 }
302                 status = sc_llist_insert_sorted(host_list, SC_OBJ(new),
303                                 sc_store_obj_cmp_by_name);
305                 /* pass control to the list or destroy in case of an error */
306                 sc_object_deref(SC_OBJ(new));
307         }
309         pthread_rwlock_unlock(&host_lock);
310         return status;
311 } /* sc_store_host */
313 const sc_host_t *
314 sc_store_get_host(const char *name)
316         sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
317         sc_host_t *host;
319         if (! name)
320                 return NULL;
322         lookup.obj_name = name;
323         host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup,
324                                 sc_cmp_store_obj_with_name));
326         if (! host)
327                 return NULL;
328         return host;
329 } /* sc_store_get_host */
331 sc_attribute_t *
332 sc_attribute_create(const char *hostname,
333                 const char *name, const char *value)
335         sc_object_t *obj;
337         if ((! hostname) || (! name) || (! value))
338                 return NULL;
340         obj = sc_object_create(sizeof(sc_attribute_t), sc_attr_init,
341                         sc_attr_destroy, hostname, name, value);
342         if (! obj)
343                 return NULL;
344         return SC_ATTR(obj);
345 } /* sc_attribute_create */
347 sc_attribute_t *
348 sc_attribute_clone(const sc_attribute_t *attr)
350         sc_attribute_t *clone;
352         clone = sc_attribute_create(attr->hostname,
353                         attr->attr_name, attr->attr_value);
354         if (! clone)
355                 return NULL;
357         clone->attr_last_update = attr->attr_last_update;
358         return clone;
359 } /* sc_attribute_clone */
361 int
362 sc_store_attribute(const sc_attribute_t *attr)
364         sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
365         sc_host_t *host;
367         sc_attribute_t *old;
369         sc_time_t last_update;
371         int status = 0;
373         if (! attr)
374                 return -1;
376         last_update = attr->attr_last_update;
377         if (last_update <= 0)
378                 last_update = sc_gettime();
380         if (! host_list)
381                 return -1;
383         pthread_rwlock_wrlock(&host_lock);
385         lookup.obj_name = attr->hostname;
386         host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup,
387                                 sc_cmp_store_obj_with_name));
389         if (! host)
390                 return -1;
392         lookup.obj_name = attr->attr_name;
393         old = SC_ATTR(sc_llist_search(host->attributes,
394                                 (const sc_object_t *)&lookup,
395                                 sc_cmp_store_obj_with_name));
397         if (old) {
398                 if (old->host_last_update > last_update) {
399                         fprintf(stderr, "store: Cannot update attribute '%s/%s' - "
400                                         "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
401                                         attr->hostname, attr->attr_name, last_update,
402                                         old->host_last_update);
403                         status = 1;
404                 }
405                 else {
406                         old->attr_last_update = last_update;
407                 }
408         }
409         else {
410                 sc_attribute_t *new = sc_attribute_clone(attr);
411                 if (! new) {
412                         char errbuf[1024];
413                         fprintf(stderr, "store: Failed to clone attribute object: %s\n",
414                                         sc_strerror(errno, errbuf, sizeof(errbuf)));
415                         return -1;
416                 }
418                 status = sc_llist_insert_sorted(host->attributes, SC_OBJ(new),
419                                 sc_store_obj_cmp_by_name);
421                 /* pass control to the list or destroy in case of an error */
422                 sc_object_deref(SC_OBJ(new));
423         }
425         pthread_rwlock_unlock(&host_lock);
426         return status;
427 } /* sc_store_attribute */
429 sc_service_t *
430 sc_service_create(const char *hostname, const char *name)
432         sc_object_t *obj;
434         if ((! hostname) || (! name))
435                 return NULL;
437         obj = sc_object_create(sizeof(sc_service_t), sc_svc_init,
438                         sc_svc_destroy, hostname, name);
439         if (! obj)
440                 return NULL;
441         return SC_SVC(obj);
442 } /* sc_service_create */
444 sc_service_t *
445 sc_service_clone(const sc_service_t *svc)
447         sc_service_t *clone;
449         clone = sc_service_create(svc->hostname, svc->svc_name);
450         if (! clone)
451                 return NULL;
453         clone->svc_last_update = svc->svc_last_update;
454         return clone;
455 } /* sc_service_clone */
457 int
458 sc_store_service(const sc_service_t *svc)
460         sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
461         sc_host_t *host;
463         sc_service_t *old;
465         sc_time_t last_update;
467         int status = 0;
469         if (! svc)
470                 return -1;
472         last_update = svc->svc_last_update;
473         if (last_update <= 0)
474                 last_update = sc_gettime();
476         if (! host_list)
477                 return -1;
479         pthread_rwlock_wrlock(&host_lock);
481         lookup.obj_name = svc->hostname;
482         host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup,
483                                 sc_cmp_store_obj_with_name));
485         if (! host)
486                 return -1;
488         lookup.obj_name = svc->svc_name;
489         old = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&lookup,
490                                 sc_cmp_store_obj_with_name));
492         if (old) {
493                 if (old->host_last_update > last_update) {
494                         fprintf(stderr, "store: Cannot update service '%s/%s' - "
495                                         "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
496                                         svc->hostname, svc->svc_name, last_update,
497                                         old->host_last_update);
498                         status = 1;
499                 }
500                 else {
501                         old->svc_last_update = last_update;
502                 }
503         }
504         else {
505                 sc_service_t *new = sc_service_clone(svc);
506                 if (! new) {
507                         char errbuf[1024];
508                         fprintf(stderr, "store: Failed to clone service object: %s\n",
509                                         sc_strerror(errno, errbuf, sizeof(errbuf)));
510                         return -1;
511                 }
513                 status = sc_llist_insert_sorted(host->services, SC_OBJ(new),
514                                 sc_store_obj_cmp_by_name);
516                 /* pass control to the list or destroy in case of an error */
517                 sc_object_deref(SC_OBJ(new));
518         }
520         pthread_rwlock_unlock(&host_lock);
521         return status;
522 } /* sc_store_service */
524 const sc_service_t *
525 sc_store_get_service(const sc_host_t *host, const char *name)
527         sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
528         sc_service_t *svc;
530         if ((! host) || (! name))
531                 return NULL;
533         lookup.obj_name = name;
534         svc = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&lookup,
535                                 sc_cmp_store_obj_with_name));
537         if (! svc)
538                 return NULL;
539         return svc;
540 } /* sc_store_get_service */
542 int
543 sc_store_dump(FILE *fh)
545         sc_llist_iter_t *host_iter;
547         if (! fh)
548                 return -1;
550         pthread_rwlock_rdlock(&host_lock);
552         host_iter = sc_llist_get_iter(host_list);
553         if (! host_iter)
554                 return -1;
556         while (sc_llist_iter_has_next(host_iter)) {
557                 sc_host_t *host = SC_HOST(sc_llist_iter_get_next(host_iter));
558                 sc_llist_iter_t *svc_iter;
559                 sc_llist_iter_t *attr_iter;
561                 char time_str[64];
563                 assert(host);
565                 if (! sc_strftime(time_str, sizeof(time_str),
566                                         "%F %T %z", host->host_last_update))
567                         snprintf(time_str, sizeof(time_str), "<error>");
568                 time_str[sizeof(time_str) - 1] = '\0';
570                 fprintf(fh, "Host '%s' (last updated: %s):\n",
571                                 host->host_name, time_str);
573                 attr_iter = sc_llist_get_iter(host->attributes);
574                 if (! attr_iter) {
575                         char errbuf[1024];
576                         fprintf(fh, "Failed to retrieve attributes: %s\n",
577                                         sc_strerror(errno, errbuf, sizeof(errbuf)));
578                         continue;
579                 }
581                 while (sc_llist_iter_has_next(attr_iter)) {
582                         sc_attribute_t *attr = SC_ATTR(sc_llist_iter_get_next(attr_iter));
583                         assert(attr);
585                         if (! sc_strftime(time_str, sizeof(time_str),
586                                                 "%F %T %z", attr->attr_last_update))
587                                 snprintf(time_str, sizeof(time_str), "<error>");
588                         time_str[sizeof(time_str) - 1] = '\0';
590                         fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
591                                         attr->attr_name, attr->attr_value, time_str);
592                 }
594                 sc_llist_iter_destroy(attr_iter);
596                 svc_iter = sc_llist_get_iter(host->services);
597                 if (! svc_iter) {
598                         char errbuf[1024];
599                         fprintf(fh, "Failed to retrieve services: %s\n",
600                                         sc_strerror(errno, errbuf, sizeof(errbuf)));
601                         continue;
602                 }
604                 while (sc_llist_iter_has_next(svc_iter)) {
605                         sc_service_t *svc = SC_SVC(sc_llist_iter_get_next(svc_iter));
606                         assert(svc);
608                         if (! sc_strftime(time_str, sizeof(time_str),
609                                                 "%F %T %z", svc->svc_last_update))
610                                 snprintf(time_str, sizeof(time_str), "<error>");
611                         time_str[sizeof(time_str) - 1] = '\0';
613                         fprintf(fh, "\tService '%s' (last updated: %s)\n",
614                                         svc->svc_name, time_str);
615                 }
617                 sc_llist_iter_destroy(svc_iter);
618         }
620         sc_llist_iter_destroy(host_iter);
621         return 0;
622 } /* sc_store_dump */
624 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */