8dc256ea5645563cc59b4e687eb498ade64e6533
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)
108 {
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)
122 {
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)
140 {
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)
153 {
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)
169 {
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)
184 {
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)
199 {
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)
231 {
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 pthread_rwlock_unlock(&host_lock);
278 return -1;
279 }
281 if (! new->attributes) {
282 if (! (new->attributes = sc_llist_create())) {
283 char errbuf[1024];
284 fprintf(stderr, "store: Failed to initialize "
285 "host object '%s': %s\n", host->host_name,
286 sc_strerror(errno, errbuf, sizeof(errbuf)));
287 sc_object_deref(SC_OBJ(new));
288 pthread_rwlock_unlock(&host_lock);
289 return -1;
290 }
291 }
293 if (! new->services) {
294 if (! (new->services = sc_llist_create())) {
295 char errbuf[1024];
296 fprintf(stderr, "store: Failed to initialize "
297 "host object '%s': %s\n", host->host_name,
298 sc_strerror(errno, errbuf, sizeof(errbuf)));
299 sc_object_deref(SC_OBJ(new));
300 pthread_rwlock_unlock(&host_lock);
301 return -1;
302 }
303 }
305 status = sc_llist_insert_sorted(host_list, SC_OBJ(new),
306 sc_store_obj_cmp_by_name);
308 /* pass control to the list or destroy in case of an error */
309 sc_object_deref(SC_OBJ(new));
310 }
312 pthread_rwlock_unlock(&host_lock);
313 return status;
314 } /* sc_store_host */
316 const sc_host_t *
317 sc_store_get_host(const char *name)
318 {
319 sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
320 sc_host_t *host;
322 if (! name)
323 return NULL;
325 lookup.obj_name = name;
326 host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup,
327 sc_cmp_store_obj_with_name));
329 if (! host)
330 return NULL;
331 return host;
332 } /* sc_store_get_host */
334 sc_attribute_t *
335 sc_attribute_create(const char *hostname,
336 const char *name, const char *value)
337 {
338 sc_object_t *obj;
340 if ((! hostname) || (! name) || (! value))
341 return NULL;
343 obj = sc_object_create(sizeof(sc_attribute_t), sc_attr_init,
344 sc_attr_destroy, hostname, name, value);
345 if (! obj)
346 return NULL;
347 return SC_ATTR(obj);
348 } /* sc_attribute_create */
350 sc_attribute_t *
351 sc_attribute_clone(const sc_attribute_t *attr)
352 {
353 sc_attribute_t *clone;
355 clone = sc_attribute_create(attr->hostname,
356 attr->attr_name, attr->attr_value);
357 if (! clone)
358 return NULL;
360 clone->attr_last_update = attr->attr_last_update;
361 return clone;
362 } /* sc_attribute_clone */
364 int
365 sc_store_attribute(const sc_attribute_t *attr)
366 {
367 sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
368 sc_host_t *host;
370 sc_attribute_t *old;
372 sc_time_t last_update;
374 int status = 0;
376 if (! attr)
377 return -1;
379 last_update = attr->attr_last_update;
380 if (last_update <= 0)
381 last_update = sc_gettime();
383 if (! host_list)
384 return -1;
386 pthread_rwlock_wrlock(&host_lock);
388 lookup.obj_name = attr->hostname;
389 host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup,
390 sc_cmp_store_obj_with_name));
392 if (! host) {
393 pthread_rwlock_unlock(&host_lock);
394 return -1;
395 }
397 lookup.obj_name = attr->attr_name;
398 old = SC_ATTR(sc_llist_search(host->attributes,
399 (const sc_object_t *)&lookup,
400 sc_cmp_store_obj_with_name));
402 if (old) {
403 if (old->host_last_update > last_update) {
404 fprintf(stderr, "store: Cannot update attribute '%s/%s' - "
405 "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
406 attr->hostname, attr->attr_name, last_update,
407 old->host_last_update);
408 status = 1;
409 }
410 else {
411 old->attr_last_update = last_update;
412 }
413 }
414 else {
415 sc_attribute_t *new = sc_attribute_clone(attr);
416 if (! new) {
417 char errbuf[1024];
418 fprintf(stderr, "store: Failed to clone attribute object: %s\n",
419 sc_strerror(errno, errbuf, sizeof(errbuf)));
420 pthread_rwlock_unlock(&host_lock);
421 return -1;
422 }
424 status = sc_llist_insert_sorted(host->attributes, SC_OBJ(new),
425 sc_store_obj_cmp_by_name);
427 /* pass control to the list or destroy in case of an error */
428 sc_object_deref(SC_OBJ(new));
429 }
431 pthread_rwlock_unlock(&host_lock);
432 return status;
433 } /* sc_store_attribute */
435 sc_service_t *
436 sc_service_create(const char *hostname, const char *name)
437 {
438 sc_object_t *obj;
440 if ((! hostname) || (! name))
441 return NULL;
443 obj = sc_object_create(sizeof(sc_service_t), sc_svc_init,
444 sc_svc_destroy, hostname, name);
445 if (! obj)
446 return NULL;
447 return SC_SVC(obj);
448 } /* sc_service_create */
450 sc_service_t *
451 sc_service_clone(const sc_service_t *svc)
452 {
453 sc_service_t *clone;
455 clone = sc_service_create(svc->hostname, svc->svc_name);
456 if (! clone)
457 return NULL;
459 clone->svc_last_update = svc->svc_last_update;
460 return clone;
461 } /* sc_service_clone */
463 int
464 sc_store_service(const sc_service_t *svc)
465 {
466 sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
467 sc_host_t *host;
469 sc_service_t *old;
471 sc_time_t last_update;
473 int status = 0;
475 if (! svc)
476 return -1;
478 last_update = svc->svc_last_update;
479 if (last_update <= 0)
480 last_update = sc_gettime();
482 if (! host_list)
483 return -1;
485 pthread_rwlock_wrlock(&host_lock);
487 lookup.obj_name = svc->hostname;
488 host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup,
489 sc_cmp_store_obj_with_name));
491 if (! host) {
492 pthread_rwlock_unlock(&host_lock);
493 return -1;
494 }
496 lookup.obj_name = svc->svc_name;
497 old = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&lookup,
498 sc_cmp_store_obj_with_name));
500 if (old) {
501 if (old->host_last_update > last_update) {
502 fprintf(stderr, "store: Cannot update service '%s/%s' - "
503 "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
504 svc->hostname, svc->svc_name, last_update,
505 old->host_last_update);
506 status = 1;
507 }
508 else {
509 old->svc_last_update = last_update;
510 }
511 }
512 else {
513 sc_service_t *new = sc_service_clone(svc);
514 if (! new) {
515 char errbuf[1024];
516 fprintf(stderr, "store: Failed to clone service object: %s\n",
517 sc_strerror(errno, errbuf, sizeof(errbuf)));
518 pthread_rwlock_unlock(&host_lock);
519 return -1;
520 }
522 status = sc_llist_insert_sorted(host->services, SC_OBJ(new),
523 sc_store_obj_cmp_by_name);
525 /* pass control to the list or destroy in case of an error */
526 sc_object_deref(SC_OBJ(new));
527 }
529 pthread_rwlock_unlock(&host_lock);
530 return status;
531 } /* sc_store_service */
533 const sc_service_t *
534 sc_store_get_service(const sc_host_t *host, const char *name)
535 {
536 sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
537 sc_service_t *svc;
539 if ((! host) || (! name))
540 return NULL;
542 lookup.obj_name = name;
543 svc = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&lookup,
544 sc_cmp_store_obj_with_name));
546 if (! svc)
547 return NULL;
548 return svc;
549 } /* sc_store_get_service */
551 int
552 sc_store_dump(FILE *fh)
553 {
554 sc_llist_iter_t *host_iter;
556 if (! fh)
557 return -1;
559 pthread_rwlock_rdlock(&host_lock);
561 host_iter = sc_llist_get_iter(host_list);
562 if (! host_iter) {
563 pthread_rwlock_unlock(&host_lock);
564 return -1;
565 }
567 while (sc_llist_iter_has_next(host_iter)) {
568 sc_host_t *host = SC_HOST(sc_llist_iter_get_next(host_iter));
569 sc_llist_iter_t *svc_iter;
570 sc_llist_iter_t *attr_iter;
572 char time_str[64];
574 assert(host);
576 if (! sc_strftime(time_str, sizeof(time_str),
577 "%F %T %z", host->host_last_update))
578 snprintf(time_str, sizeof(time_str), "<error>");
579 time_str[sizeof(time_str) - 1] = '\0';
581 fprintf(fh, "Host '%s' (last updated: %s):\n",
582 host->host_name, time_str);
584 attr_iter = sc_llist_get_iter(host->attributes);
585 if (! attr_iter) {
586 char errbuf[1024];
587 fprintf(fh, "Failed to retrieve attributes: %s\n",
588 sc_strerror(errno, errbuf, sizeof(errbuf)));
589 continue;
590 }
592 while (sc_llist_iter_has_next(attr_iter)) {
593 sc_attribute_t *attr = SC_ATTR(sc_llist_iter_get_next(attr_iter));
594 assert(attr);
596 if (! sc_strftime(time_str, sizeof(time_str),
597 "%F %T %z", attr->attr_last_update))
598 snprintf(time_str, sizeof(time_str), "<error>");
599 time_str[sizeof(time_str) - 1] = '\0';
601 fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
602 attr->attr_name, attr->attr_value, time_str);
603 }
605 sc_llist_iter_destroy(attr_iter);
607 svc_iter = sc_llist_get_iter(host->services);
608 if (! svc_iter) {
609 char errbuf[1024];
610 fprintf(fh, "Failed to retrieve services: %s\n",
611 sc_strerror(errno, errbuf, sizeof(errbuf)));
612 continue;
613 }
615 while (sc_llist_iter_has_next(svc_iter)) {
616 sc_service_t *svc = SC_SVC(sc_llist_iter_get_next(svc_iter));
617 assert(svc);
619 if (! sc_strftime(time_str, sizeof(time_str),
620 "%F %T %z", svc->svc_last_update))
621 snprintf(time_str, sizeof(time_str), "<error>");
622 time_str[sizeof(time_str) - 1] = '\0';
624 fprintf(fh, "\tService '%s' (last updated: %s)\n",
625 svc->svc_name, time_str);
626 }
628 sc_llist_iter_destroy(svc_iter);
629 }
631 sc_llist_iter_destroy(host_iter);
632 pthread_rwlock_unlock(&host_lock);
633 return 0;
634 } /* sc_store_dump */
636 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */