05c879bf8e14abf0456d368a59689fde3cf04316
1 /*
2 * SysDB - src/core/memstore.c
3 * Copyright (C) 2012-2013 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 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/memstore-private.h"
34 #include "core/plugin.h"
35 #include "utils/avltree.h"
36 #include "utils/error.h"
38 #include <assert.h>
40 #include <errno.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <strings.h>
47 #include <pthread.h>
49 /*
50 * private types
51 */
53 struct sdb_memstore {
54 sdb_object_t super;
56 /* hosts are the top-level entries and
57 * reference everything else */
58 sdb_avltree_t *hosts;
59 pthread_rwlock_t host_lock;
60 };
62 /* internal representation of a to-be-stored object */
63 typedef struct {
64 sdb_memstore_obj_t *parent;
65 sdb_avltree_t *parent_tree;
66 int type;
67 const char *name;
68 sdb_time_t last_update;
69 sdb_time_t interval;
70 const char * const *backends;
71 size_t backends_num;
72 } store_obj_t;
73 #define STORE_OBJ_INIT { NULL, NULL, 0, NULL, 0, 0, NULL, 0 }
75 static sdb_type_t host_type;
76 static sdb_type_t service_type;
77 static sdb_type_t metric_type;
78 static sdb_type_t attribute_type;
80 static int
81 store_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
82 {
83 int err;
84 if (! (SDB_MEMSTORE(obj)->hosts = sdb_avltree_create()))
85 return -1;
86 if ((err = pthread_rwlock_init(&SDB_MEMSTORE(obj)->host_lock,
87 /* attr = */ NULL))) {
88 char errbuf[128];
89 sdb_log(SDB_LOG_ERR, "memstore: Failed to initialize lock: %s",
90 sdb_strerror(err, errbuf, sizeof(errbuf)));
91 return -1;
92 }
93 return 0;
94 } /* store_init */
96 static void
97 store_destroy(sdb_object_t *obj)
98 {
99 int err;
100 if ((err = pthread_rwlock_destroy(&SDB_MEMSTORE(obj)->host_lock))) {
101 char errbuf[128];
102 sdb_log(SDB_LOG_ERR, "memstore: Failed to destroy lock: %s",
103 sdb_strerror(err, errbuf, sizeof(errbuf)));
104 return;
105 }
106 sdb_avltree_destroy(SDB_MEMSTORE(obj)->hosts);
107 SDB_MEMSTORE(obj)->hosts = NULL;
108 } /* store_destroy */
110 static int
111 store_obj_init(sdb_object_t *obj, va_list ap)
112 {
113 sdb_memstore_obj_t *sobj = STORE_OBJ(obj);
114 sobj->type = va_arg(ap, int);
115 return 0;
116 } /* store_obj_init */
118 static void
119 store_obj_destroy(sdb_object_t *obj)
120 {
121 sdb_memstore_obj_t *sobj = STORE_OBJ(obj);
122 size_t i;
124 for (i = 0; i < sobj->backends_num; ++i)
125 free(sobj->backends[i]);
126 free(sobj->backends);
127 sobj->backends = NULL;
128 sobj->backends_num = 0;
130 // We don't currently keep an extra reference for parent objects to
131 // avoid circular self-references which are not handled correctly by
132 // the ref-count base management layer.
133 //sdb_object_deref(SDB_OBJ(sobj->parent));
134 } /* store_obj_destroy */
136 static int
137 host_init(sdb_object_t *obj, va_list ap)
138 {
139 host_t *sobj = HOST(obj);
140 int ret;
142 /* this will consume the first argument (type) of ap */
143 ret = store_obj_init(obj, ap);
144 if (ret)
145 return ret;
147 sobj->services = sdb_avltree_create();
148 if (! sobj->services)
149 return -1;
150 sobj->metrics = sdb_avltree_create();
151 if (! sobj->metrics)
152 return -1;
153 sobj->attributes = sdb_avltree_create();
154 if (! sobj->attributes)
155 return -1;
156 return 0;
157 } /* host_init */
159 static void
160 host_destroy(sdb_object_t *obj)
161 {
162 host_t *sobj = HOST(obj);
163 assert(obj);
165 store_obj_destroy(obj);
167 if (sobj->services)
168 sdb_avltree_destroy(sobj->services);
169 if (sobj->metrics)
170 sdb_avltree_destroy(sobj->metrics);
171 if (sobj->attributes)
172 sdb_avltree_destroy(sobj->attributes);
173 } /* host_destroy */
175 static int
176 service_init(sdb_object_t *obj, va_list ap)
177 {
178 service_t *sobj = SVC(obj);
179 int ret;
181 /* this will consume the first argument (type) of ap */
182 ret = store_obj_init(obj, ap);
183 if (ret)
184 return ret;
186 sobj->attributes = sdb_avltree_create();
187 if (! sobj->attributes)
188 return -1;
189 return 0;
190 } /* service_init */
192 static void
193 service_destroy(sdb_object_t *obj)
194 {
195 service_t *sobj = SVC(obj);
196 assert(obj);
198 store_obj_destroy(obj);
200 if (sobj->attributes)
201 sdb_avltree_destroy(sobj->attributes);
202 } /* service_destroy */
204 static int
205 metric_init(sdb_object_t *obj, va_list ap)
206 {
207 metric_t *sobj = METRIC(obj);
208 int ret;
210 /* this will consume the first argument (type) of ap */
211 ret = store_obj_init(obj, ap);
212 if (ret)
213 return ret;
215 sobj->attributes = sdb_avltree_create();
216 if (! sobj->attributes)
217 return -1;
219 sobj->store.type = sobj->store.id = NULL;
220 return 0;
221 } /* metric_init */
223 static void
224 metric_destroy(sdb_object_t *obj)
225 {
226 metric_t *sobj = METRIC(obj);
227 assert(obj);
229 store_obj_destroy(obj);
231 if (sobj->attributes)
232 sdb_avltree_destroy(sobj->attributes);
234 if (sobj->store.type)
235 free(sobj->store.type);
236 if (sobj->store.id)
237 free(sobj->store.id);
238 } /* metric_destroy */
240 static int
241 attr_init(sdb_object_t *obj, va_list ap)
242 {
243 const sdb_data_t *value;
244 int ret;
246 /* this will consume the first argument (type) of ap */
247 ret = store_obj_init(obj, ap);
248 if (ret)
249 return ret;
250 value = va_arg(ap, const sdb_data_t *);
252 if (value)
253 if (sdb_data_copy(&ATTR(obj)->value, value))
254 return -1;
255 return 0;
256 } /* attr_init */
258 static void
259 attr_destroy(sdb_object_t *obj)
260 {
261 assert(obj);
263 store_obj_destroy(obj);
264 sdb_data_free_datum(&ATTR(obj)->value);
265 } /* attr_destroy */
267 static sdb_type_t store_type = {
268 /* size = */ sizeof(sdb_memstore_t),
269 /* init = */ store_init,
270 /* destroy = */ store_destroy,
271 };
273 static sdb_type_t host_type = {
274 /* size = */ sizeof(host_t),
275 /* init = */ host_init,
276 /* destroy = */ host_destroy
277 };
279 static sdb_type_t service_type = {
280 /* size = */ sizeof(service_t),
281 /* init = */ service_init,
282 /* destroy = */ service_destroy
283 };
285 static sdb_type_t metric_type = {
286 /* size = */ sizeof(metric_t),
287 /* init = */ metric_init,
288 /* destroy = */ metric_destroy
289 };
291 static sdb_type_t attribute_type = {
292 /* size = */ sizeof(attr_t),
293 /* init = */ attr_init,
294 /* destroy = */ attr_destroy
295 };
297 /*
298 * private helper functions
299 */
301 static int
302 record_backends(sdb_memstore_obj_t *obj,
303 const char * const *backends, size_t backends_num)
304 {
305 char **tmp;
306 size_t i;
308 for (i = 0; i < backends_num; i++) {
309 bool found = 0;
310 size_t j;
312 for (j = 0; j < obj->backends_num; ++j) {
313 if (!strcasecmp(obj->backends[j], backends[i])) {
314 found = 1;
315 break;
316 }
317 }
318 if (found)
319 continue;
321 tmp = realloc(obj->backends,
322 (obj->backends_num + 1) * sizeof(*obj->backends));
323 if (! tmp)
324 return -1;
326 obj->backends = tmp;
327 obj->backends[obj->backends_num] = strdup(backends[i]);
328 if (! obj->backends[obj->backends_num])
329 return -1;
331 ++obj->backends_num;
332 }
333 return 0;
334 } /* record_backends */
336 static int
337 store_obj(store_obj_t *obj, sdb_memstore_obj_t **updated_obj)
338 {
339 sdb_memstore_obj_t *old, *new;
340 int status = 0;
342 assert(obj->parent_tree);
344 old = STORE_OBJ(sdb_avltree_lookup(obj->parent_tree, obj->name));
345 if (old) {
346 new = old;
347 sdb_object_deref(SDB_OBJ(old));
348 }
349 else {
350 if (obj->type == SDB_ATTRIBUTE) {
351 /* the value will be updated by the caller */
352 new = STORE_OBJ(sdb_object_create(obj->name, attribute_type,
353 obj->type, NULL));
354 }
355 else {
356 sdb_type_t t;
357 t = obj->type == SDB_HOST
358 ? host_type
359 : obj->type == SDB_SERVICE
360 ? service_type
361 : metric_type;
362 new = STORE_OBJ(sdb_object_create(obj->name, t, obj->type));
363 }
365 if (new) {
366 status = sdb_avltree_insert(obj->parent_tree, SDB_OBJ(new));
368 /* pass control to the tree or destroy in case of an error */
369 sdb_object_deref(SDB_OBJ(new));
370 }
371 else {
372 char errbuf[1024];
373 sdb_log(SDB_LOG_ERR, "memstore: Failed to create %s '%s': %s",
374 SDB_STORE_TYPE_TO_NAME(obj->type), obj->name,
375 sdb_strerror(errno, errbuf, sizeof(errbuf)));
376 status = -1;
377 }
378 }
380 if (status < 0)
381 return status;
382 assert(new);
384 new->last_update = obj->last_update;
385 new->interval = obj->interval;
387 if (new->parent != obj->parent) {
388 // Avoid circular self-references which are not handled
389 // correctly by the ref-count based management layer.
390 //sdb_object_deref(SDB_OBJ(new->parent));
391 //sdb_object_ref(SDB_OBJ(obj->parent));
392 new->parent = obj->parent;
393 }
395 if (updated_obj)
396 *updated_obj = new;
398 if (record_backends(new, obj->backends, obj->backends_num))
399 return -1;
400 return status;
401 } /* store_obj */
403 static int
404 store_metric_store(metric_t *metric, sdb_store_metric_t *m)
405 {
406 char *type = metric->store.type;
407 char *id = metric->store.id;
409 if ((! metric->store.type) || strcasecmp(metric->store.type, m->store.type)) {
410 if (! (type = strdup(m->store.type)))
411 return -1;
412 }
413 if ((! metric->store.id) || strcasecmp(metric->store.id, m->store.id)) {
414 if (! (id = strdup(m->store.id))) {
415 if (type != metric->store.type)
416 free(type);
417 return -1;
418 }
419 }
421 if (type != metric->store.type) {
422 if (metric->store.type)
423 free(metric->store.type);
424 metric->store.type = type;
425 }
426 if (id != metric->store.id) {
427 if (metric->store.id)
428 free(metric->store.id);
429 metric->store.id = id;
430 }
431 return 0;
432 } /* store_metric_store */
434 /* The store's host_lock has to be acquired before calling this function. */
435 static sdb_avltree_t *
436 get_host_children(host_t *host, int type)
437 {
438 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
439 && (type != SDB_ATTRIBUTE))
440 return NULL;
442 if (! host)
443 return NULL;
445 if (type == SDB_ATTRIBUTE)
446 return host->attributes;
447 else if (type == SDB_METRIC)
448 return host->metrics;
449 else
450 return host->services;
451 } /* get_host_children */
453 static sdb_avltree_t *
454 get_obj_attrs(sdb_memstore_obj_t *obj)
455 {
456 if (obj->type == SDB_HOST)
457 return HOST(obj)->attributes;
458 else if (obj->type == SDB_SERVICE)
459 return SVC(obj)->attributes;
460 else if (obj->type == SDB_METRIC)
461 return METRIC(obj)->attributes;
462 return NULL;
463 } /* get_obj_attrs */
465 /*
466 * store writer API
467 */
469 static int
470 store_attribute(sdb_store_attribute_t *attr, sdb_object_t *user_data)
471 {
472 sdb_memstore_t *st = SDB_MEMSTORE(user_data);
473 store_obj_t obj = STORE_OBJ_INIT;
474 sdb_memstore_obj_t *new = NULL;
475 const char *hostname;
476 host_t *host;
478 sdb_avltree_t *children = NULL;
479 int status = 0;
481 if ((! attr) || (! attr->parent) || (! attr->key))
482 return -1;
484 hostname = attr->hostname;
485 if (attr->parent_type == SDB_HOST)
486 hostname = attr->parent;
487 if (! hostname)
488 return -1;
490 pthread_rwlock_wrlock(&st->host_lock);
491 host = HOST(sdb_avltree_lookup(st->hosts, hostname));
492 if (! host) {
493 sdb_log(SDB_LOG_ERR, "memstore: Failed to store attribute '%s' - "
494 "host '%s' not found", attr->key, hostname);
495 status = -1;
496 }
498 switch (attr->parent_type) {
499 case SDB_HOST:
500 obj.parent = STORE_OBJ(host);
501 obj.parent_tree = get_host_children(host, SDB_ATTRIBUTE);
502 break;
503 case SDB_SERVICE:
504 case SDB_METRIC:
505 children = get_host_children(host, attr->parent_type);
506 break;
507 default:
508 status = -1;
509 break;
510 }
512 if (children) {
513 obj.parent = STORE_OBJ(sdb_avltree_lookup(children, attr->parent));
514 if (! obj.parent) {
515 sdb_log(SDB_LOG_ERR, "memstore: Failed to store attribute '%s' - "
516 "%s '%s/%s' not found", attr->key,
517 SDB_STORE_TYPE_TO_NAME(attr->parent_type),
518 attr->hostname, attr->parent);
519 status = -1;
520 }
521 else
522 obj.parent_tree = attr->parent_type == SDB_SERVICE
523 ? SVC(obj.parent)->attributes
524 : METRIC(obj.parent)->attributes;
525 }
527 obj.type = SDB_ATTRIBUTE;
528 obj.name = attr->key;
529 obj.last_update = attr->last_update;
530 obj.interval = attr->interval;
531 obj.backends = attr->backends;
532 obj.backends_num = attr->backends_num;
533 if (! status)
534 status = store_obj(&obj, &new);
536 if (! status) {
537 assert(new);
538 /* update the value if it changed */
539 if (sdb_data_cmp(&ATTR(new)->value, &attr->value))
540 if (sdb_data_copy(&ATTR(new)->value, &attr->value))
541 status = -1;
542 }
544 if (obj.parent != STORE_OBJ(host))
545 sdb_object_deref(SDB_OBJ(obj.parent));
546 sdb_object_deref(SDB_OBJ(host));
547 pthread_rwlock_unlock(&st->host_lock);
549 return status;
550 } /* store_attribute */
552 static int
553 store_host(sdb_store_host_t *host, sdb_object_t *user_data)
554 {
555 sdb_memstore_t *st = SDB_MEMSTORE(user_data);
556 store_obj_t obj = { NULL, st->hosts, SDB_HOST, NULL, 0, 0, NULL, 0 };
557 int status = 0;
559 if ((! host) || (! host->name))
560 return -1;
562 obj.name = host->name;
563 obj.last_update = host->last_update;
564 obj.interval = host->interval;
565 obj.backends = host->backends;
566 obj.backends_num = host->backends_num;
567 pthread_rwlock_wrlock(&st->host_lock);
568 status = store_obj(&obj, NULL);
569 pthread_rwlock_unlock(&st->host_lock);
571 return status;
572 } /* store_host */
574 static int
575 store_service(sdb_store_service_t *service, sdb_object_t *user_data)
576 {
577 sdb_memstore_t *st = SDB_MEMSTORE(user_data);
578 store_obj_t obj = STORE_OBJ_INIT;
579 host_t *host;
581 int status = 0;
583 if ((! service) || (! service->hostname) || (! service->name))
584 return -1;
586 pthread_rwlock_wrlock(&st->host_lock);
587 host = HOST(sdb_avltree_lookup(st->hosts, service->hostname));
588 obj.parent = STORE_OBJ(host);
589 obj.parent_tree = get_host_children(host, SDB_SERVICE);
590 obj.type = SDB_SERVICE;
591 if (! obj.parent_tree) {
592 sdb_log(SDB_LOG_ERR, "memstore: Failed to store service '%s' - "
593 "host '%s' not found", service->name, service->hostname);
594 status = -1;
595 }
597 obj.name = service->name;
598 obj.last_update = service->last_update;
599 obj.interval = service->interval;
600 obj.backends = service->backends;
601 obj.backends_num = service->backends_num;
602 if (! status)
603 status = store_obj(&obj, NULL);
605 sdb_object_deref(SDB_OBJ(host));
606 pthread_rwlock_unlock(&st->host_lock);
607 return status;
608 } /* store_service */
610 static int
611 store_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
612 {
613 sdb_memstore_t *st = SDB_MEMSTORE(user_data);
614 store_obj_t obj = STORE_OBJ_INIT;
615 sdb_memstore_obj_t *new = NULL;
616 host_t *host;
618 int status = 0;
620 if ((! metric) || (! metric->hostname) || (! metric->name))
621 return -1;
623 if ((metric->store.type != NULL) != (metric->store.id != NULL))
624 return -1;
626 pthread_rwlock_wrlock(&st->host_lock);
627 host = HOST(sdb_avltree_lookup(st->hosts, metric->hostname));
628 obj.parent = STORE_OBJ(host);
629 obj.parent_tree = get_host_children(host, SDB_METRIC);
630 obj.type = SDB_METRIC;
631 if (! obj.parent_tree) {
632 sdb_log(SDB_LOG_ERR, "memstore: Failed to store metric '%s' - "
633 "host '%s' not found", metric->name, metric->hostname);
634 status = -1;
635 }
637 obj.name = metric->name;
638 obj.last_update = metric->last_update;
639 obj.interval = metric->interval;
640 obj.backends = metric->backends;
641 obj.backends_num = metric->backends_num;
642 if (! status)
643 status = store_obj(&obj, &new);
644 sdb_object_deref(SDB_OBJ(host));
646 if (status) {
647 pthread_rwlock_unlock(&st->host_lock);
648 return status;
649 }
651 assert(new);
652 if (metric->store.type && metric->store.id)
653 if (store_metric_store(METRIC(new), metric))
654 status = -1;
655 pthread_rwlock_unlock(&st->host_lock);
656 return status;
657 } /* store_metric */
659 sdb_store_writer_t sdb_memstore_writer = {
660 store_host, store_service, store_metric, store_attribute,
661 };
663 /*
664 * store query API
665 */
667 static sdb_object_t *
668 prepare_query(sdb_ast_node_t *ast,
669 sdb_strbuf_t __attribute__((unused)) *errbuf,
670 sdb_object_t __attribute__((unused)) *user_data)
671 {
672 return SDB_OBJ(sdb_memstore_query_prepare(ast));
673 } /* prepare_query */
675 static int
676 execute_query(sdb_object_t *q,
677 sdb_store_writer_t *w, sdb_object_t *wd, sdb_strbuf_t *errbuf,
678 sdb_object_t *user_data)
679 {
680 return sdb_memstore_query_execute(SDB_MEMSTORE(user_data),
681 QUERY(q), w, wd, errbuf);
682 } /* execute_query */
684 sdb_store_reader_t sdb_memstore_reader = {
685 prepare_query, execute_query,
686 };
688 /*
689 * public API
690 */
692 sdb_memstore_t *
693 sdb_memstore_create(void)
694 {
695 return SDB_MEMSTORE(sdb_object_create("memstore", store_type));
696 } /* sdb_memstore_create */
698 int
699 sdb_memstore_host(sdb_memstore_t *store, const char *name,
700 sdb_time_t last_update, sdb_time_t interval)
701 {
702 sdb_store_host_t host = {
703 name, last_update, interval, NULL, 0,
704 };
705 return store_host(&host, SDB_OBJ(store));
706 } /* sdb_memstore_host */
708 int
709 sdb_memstore_service(sdb_memstore_t *store, const char *hostname, const char *name,
710 sdb_time_t last_update, sdb_time_t interval)
711 {
712 sdb_store_service_t service = {
713 hostname, name, last_update, interval, NULL, 0,
714 };
715 return store_service(&service, SDB_OBJ(store));
716 } /* sdb_memstore_service */
718 int
719 sdb_memstore_metric(sdb_memstore_t *store, const char *hostname, const char *name,
720 sdb_metric_store_t *metric_store,
721 sdb_time_t last_update, sdb_time_t interval)
722 {
723 sdb_store_metric_t metric = {
724 hostname, name, { NULL, NULL }, last_update, interval, NULL, 0,
725 };
726 if (metric_store) {
727 metric.store.type = metric_store->type;
728 metric.store.id = metric_store->id;
729 }
730 return store_metric(&metric, SDB_OBJ(store));
731 } /* sdb_memstore_metric */
733 int
734 sdb_memstore_attribute(sdb_memstore_t *store, const char *hostname,
735 const char *key, const sdb_data_t *value,
736 sdb_time_t last_update, sdb_time_t interval)
737 {
738 sdb_store_attribute_t attr = {
739 NULL, SDB_HOST, hostname, key, SDB_DATA_INIT,
740 last_update, interval, NULL, 0,
741 };
742 if (value) {
743 attr.value = *value;
744 }
745 return store_attribute(&attr, SDB_OBJ(store));
746 } /* sdb_memstore_attribute */
748 int
749 sdb_memstore_service_attr(sdb_memstore_t *store, const char *hostname,
750 const char *service, const char *key, const sdb_data_t *value,
751 sdb_time_t last_update, sdb_time_t interval)
752 {
753 sdb_store_attribute_t attr = {
754 hostname, SDB_SERVICE, service, key, SDB_DATA_INIT,
755 last_update, interval, NULL, 0,
756 };
757 if (value) {
758 attr.value = *value;
759 }
760 return store_attribute(&attr, SDB_OBJ(store));
761 } /* sdb_memstore_service_attr */
763 int
764 sdb_memstore_metric_attr(sdb_memstore_t *store, const char *hostname,
765 const char *metric, const char *key, const sdb_data_t *value,
766 sdb_time_t last_update, sdb_time_t interval)
767 {
768 sdb_store_attribute_t attr = {
769 hostname, SDB_METRIC, metric, key, SDB_DATA_INIT,
770 last_update, interval, NULL, 0,
771 };
772 if (value) {
773 attr.value = *value;
774 }
775 return store_attribute(&attr, SDB_OBJ(store));
776 } /* sdb_memstore_metric_attr */
778 sdb_memstore_obj_t *
779 sdb_memstore_get_host(sdb_memstore_t *store, const char *name)
780 {
781 host_t *host;
783 if ((! store) || (! name))
784 return NULL;
786 host = HOST(sdb_avltree_lookup(store->hosts, name));
787 if (! host)
788 return NULL;
790 return STORE_OBJ(host);
791 } /* sdb_memstore_get_host */
793 sdb_memstore_obj_t *
794 sdb_memstore_get_child(sdb_memstore_obj_t *obj, int type, const char *name)
795 {
796 sdb_avltree_t *children = NULL;
798 if ((! obj) || (! name))
799 return NULL;
801 if (type & SDB_ATTRIBUTE)
802 children = get_obj_attrs(obj);
803 else if (obj->type == SDB_HOST)
804 children = get_host_children(HOST(obj), type);
805 if (! children)
806 return NULL;
807 return STORE_OBJ(sdb_avltree_lookup(children, name));
808 } /* sdb_memstore_get_child */
810 int
811 sdb_memstore_get_field(sdb_memstore_obj_t *obj, int field, sdb_data_t *res)
812 {
813 sdb_data_t tmp;
815 if (! obj)
816 return -1;
818 switch (field) {
819 case SDB_FIELD_NAME:
820 tmp.type = SDB_TYPE_STRING;
821 tmp.data.string = strdup(SDB_OBJ(obj)->name);
822 if (! tmp.data.string)
823 return -1;
824 break;
825 case SDB_FIELD_LAST_UPDATE:
826 tmp.type = SDB_TYPE_DATETIME;
827 tmp.data.datetime = obj->last_update;
828 break;
829 case SDB_FIELD_AGE:
830 tmp.type = SDB_TYPE_DATETIME;
831 tmp.data.datetime = sdb_gettime() - obj->last_update;
832 break;
833 case SDB_FIELD_INTERVAL:
834 tmp.type = SDB_TYPE_DATETIME;
835 tmp.data.datetime = obj->interval;
836 break;
837 case SDB_FIELD_BACKEND:
838 if (! res)
839 return 0;
840 tmp.type = SDB_TYPE_ARRAY | SDB_TYPE_STRING;
841 tmp.data.array.length = obj->backends_num;
842 tmp.data.array.values = obj->backends;
843 return sdb_data_copy(res, &tmp);
844 case SDB_FIELD_VALUE:
845 if (obj->type != SDB_ATTRIBUTE)
846 return -1;
847 if (! res)
848 return 0;
849 return sdb_data_copy(res, &ATTR(obj)->value);
850 case SDB_FIELD_TIMESERIES:
851 if (obj->type != SDB_METRIC)
852 return -1;
853 tmp.type = SDB_TYPE_BOOLEAN;
854 tmp.data.boolean = METRIC(obj)->store.type != NULL;
855 default:
856 return -1;
857 }
858 if (res)
859 *res = tmp;
860 else
861 sdb_data_free_datum(&tmp);
862 return 0;
863 } /* sdb_memstore_get_field */
865 int
866 sdb_memstore_get_attr(sdb_memstore_obj_t *obj, const char *name, sdb_data_t *res,
867 sdb_memstore_matcher_t *filter)
868 {
869 sdb_memstore_obj_t *attr;
871 if ((! obj) || (! name))
872 return -1;
874 attr = STORE_OBJ(sdb_avltree_lookup(get_obj_attrs(obj), name));
875 if (! attr)
876 return -1;
877 if (filter && (! sdb_memstore_matcher_matches(filter, attr, NULL))) {
878 sdb_object_deref(SDB_OBJ(attr));
879 return -1;
880 }
882 assert(STORE_OBJ(attr)->type == SDB_ATTRIBUTE);
883 if (res)
884 sdb_data_copy(res, &ATTR(attr)->value);
885 sdb_object_deref(SDB_OBJ(attr));
886 return 0;
887 } /* sdb_memstore_get_attr */
889 int
890 sdb_memstore_scan(sdb_memstore_t *store, int type,
891 sdb_memstore_matcher_t *m, sdb_memstore_matcher_t *filter,
892 sdb_memstore_lookup_cb cb, void *user_data)
893 {
894 sdb_avltree_iter_t *host_iter = NULL;
895 int status = 0;
897 if ((! store) || (! cb))
898 return -1;
900 if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) {
901 sdb_log(SDB_LOG_ERR, "memstore: Cannot scan objects of type %d", type);
902 return -1;
903 }
905 pthread_rwlock_rdlock(&store->host_lock);
906 host_iter = sdb_avltree_get_iter(store->hosts);
907 if (! host_iter)
908 status = -1;
910 /* has_next returns false if the iterator is NULL */
911 while (sdb_avltree_iter_has_next(host_iter)) {
912 sdb_memstore_obj_t *host;
913 sdb_avltree_iter_t *iter = NULL;
915 host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
916 assert(host);
918 if (! sdb_memstore_matcher_matches(filter, host, NULL))
919 continue;
921 if (type == SDB_SERVICE)
922 iter = sdb_avltree_get_iter(HOST(host)->services);
923 else if (type == SDB_METRIC)
924 iter = sdb_avltree_get_iter(HOST(host)->metrics);
926 if (iter) {
927 while (sdb_avltree_iter_has_next(iter)) {
928 sdb_memstore_obj_t *obj;
929 obj = STORE_OBJ(sdb_avltree_iter_get_next(iter));
930 assert(obj);
932 if (sdb_memstore_matcher_matches(m, obj, filter)) {
933 if (cb(obj, filter, user_data)) {
934 sdb_log(SDB_LOG_ERR, "memstore: Callback returned "
935 "an error while scanning");
936 status = -1;
937 break;
938 }
939 }
940 }
941 }
942 else if (sdb_memstore_matcher_matches(m, host, filter)) {
943 if (cb(host, filter, user_data)) {
944 sdb_log(SDB_LOG_ERR, "memstore: Callback returned "
945 "an error while scanning");
946 status = -1;
947 }
948 }
950 sdb_avltree_iter_destroy(iter);
951 if (status)
952 break;
953 }
955 sdb_avltree_iter_destroy(host_iter);
956 pthread_rwlock_unlock(&store->host_lock);
957 return status;
958 } /* sdb_memstore_scan */
960 int
961 sdb_memstore_emit(sdb_memstore_obj_t *obj, sdb_store_writer_t *w, sdb_object_t *wd)
962 {
963 if ((! obj) || (! w))
964 return -1;
966 switch (obj->type) {
967 case SDB_HOST:
968 {
969 sdb_store_host_t host = {
970 obj->_name,
971 obj->last_update,
972 obj->interval,
973 (const char * const *)obj->backends,
974 obj->backends_num,
975 };
976 if (! w->store_host)
977 return -1;
978 return w->store_host(&host, wd);
979 }
980 case SDB_SERVICE:
981 {
982 sdb_store_service_t service = {
983 obj->parent ? obj->parent->_name : NULL,
984 obj->_name,
985 obj->last_update,
986 obj->interval,
987 (const char * const *)obj->backends,
988 obj->backends_num,
989 };
990 if (! w->store_service)
991 return -1;
992 return w->store_service(&service, wd);
993 }
994 case SDB_METRIC:
995 {
996 sdb_store_metric_t metric = {
997 obj->parent ? obj->parent->_name : NULL,
998 obj->_name,
999 {
1000 METRIC(obj)->store.type,
1001 METRIC(obj)->store.id,
1002 },
1003 obj->last_update,
1004 obj->interval,
1005 (const char * const *)obj->backends,
1006 obj->backends_num,
1007 };
1008 if (! w->store_metric)
1009 return -1;
1010 return w->store_metric(&metric, wd);
1011 }
1012 case SDB_ATTRIBUTE:
1013 {
1014 sdb_store_attribute_t attr = {
1015 NULL,
1016 obj->parent ? obj->parent->type : 0,
1017 obj->parent ? obj->parent->_name : NULL,
1018 obj->_name,
1019 ATTR(obj)->value,
1020 obj->last_update,
1021 obj->interval,
1022 (const char * const *)obj->backends,
1023 obj->backends_num,
1024 };
1025 if (obj->parent && (obj->parent->type != SDB_HOST)
1026 && obj->parent->parent)
1027 attr.hostname = obj->parent->parent->_name;
1028 if (! w->store_attribute)
1029 return -1;
1030 return w->store_attribute(&attr, wd);
1031 }
1032 }
1034 return -1;
1035 } /* sdb_memstore_emit */
1037 int
1038 sdb_memstore_emit_full(sdb_memstore_obj_t *obj, sdb_memstore_matcher_t *filter,
1039 sdb_store_writer_t *w, sdb_object_t *wd)
1040 {
1041 sdb_avltree_t *trees[] = { NULL, NULL, NULL };
1042 size_t i;
1044 if (sdb_memstore_emit(obj, w, wd))
1045 return -1;
1047 if (obj->type == SDB_HOST) {
1048 trees[0] = HOST(obj)->attributes;
1049 trees[1] = HOST(obj)->metrics;
1050 trees[2] = HOST(obj)->services;
1051 }
1052 else if (obj->type == SDB_SERVICE)
1053 trees[0] = SVC(obj)->attributes;
1054 else if (obj->type == SDB_METRIC)
1055 trees[0] = METRIC(obj)->attributes;
1056 else if (obj->type == SDB_ATTRIBUTE)
1057 return 0;
1058 else
1059 return -1;
1061 for (i = 0; i < SDB_STATIC_ARRAY_LEN(trees); ++i) {
1062 sdb_avltree_iter_t *iter;
1064 if (! trees[i])
1065 continue;
1067 iter = sdb_avltree_get_iter(trees[i]);
1068 while (sdb_avltree_iter_has_next(iter)) {
1069 sdb_memstore_obj_t *child;
1070 child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
1072 if (filter && (! sdb_memstore_matcher_matches(filter, child, NULL)))
1073 continue;
1075 if (sdb_memstore_emit_full(child, filter, w, wd)) {
1076 sdb_avltree_iter_destroy(iter);
1077 return -1;
1078 }
1079 }
1080 sdb_avltree_iter_destroy(iter);
1081 }
1082 return 0;
1083 } /* sdb_memstore_emit_full */
1085 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */