1 /*
2 * SysDB - src/core/store.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/store-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>
46 #include <pthread.h>
48 /*
49 * private variables
50 */
52 static sdb_avltree_t *hosts = NULL;
53 static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER;
55 /*
56 * private types
57 */
59 static sdb_type_t sdb_host_type;
60 static sdb_type_t sdb_service_type;
61 static sdb_type_t sdb_attribute_type;
63 static int
64 store_obj_init(sdb_object_t *obj, va_list ap)
65 {
66 sdb_store_obj_t *sobj = STORE_OBJ(obj);
68 sobj->type = va_arg(ap, int);
70 sobj->last_update = va_arg(ap, sdb_time_t);
71 sobj->interval = 0;
72 sobj->backends = NULL;
73 sobj->backends_num = 0;
74 sobj->parent = NULL;
75 return 0;
76 } /* store_obj_init */
78 static void
79 store_obj_destroy(sdb_object_t *obj)
80 {
81 sdb_store_obj_t *sobj = STORE_OBJ(obj);
82 size_t i;
84 for (i = 0; i < sobj->backends_num; ++i)
85 free(sobj->backends[i]);
86 free(sobj->backends);
87 sobj->backends = NULL;
88 sobj->backends_num = 0;
90 if (sobj->parent)
91 sdb_object_deref(SDB_OBJ(sobj->parent));
92 } /* store_obj_destroy */
94 static int
95 sdb_host_init(sdb_object_t *obj, va_list ap)
96 {
97 sdb_host_t *sobj = HOST(obj);
98 int ret;
100 /* this will consume the first argument (type) of ap */
101 ret = store_obj_init(obj, ap);
102 if (ret)
103 return ret;
105 sobj->services = sdb_avltree_create();
106 if (! sobj->services)
107 return -1;
108 sobj->attributes = sdb_avltree_create();
109 if (! sobj->attributes)
110 return -1;
111 return 0;
112 } /* sdb_host_init */
114 static void
115 sdb_host_destroy(sdb_object_t *obj)
116 {
117 sdb_host_t *sobj = HOST(obj);
118 assert(obj);
120 store_obj_destroy(obj);
122 if (sobj->services)
123 sdb_avltree_destroy(sobj->services);
124 if (sobj->attributes)
125 sdb_avltree_destroy(sobj->attributes);
126 } /* sdb_host_destroy */
128 static int
129 sdb_service_init(sdb_object_t *obj, va_list ap)
130 {
131 sdb_service_t *sobj = SVC(obj);
132 int ret;
134 /* this will consume the first argument (type) of ap */
135 ret = store_obj_init(obj, ap);
136 if (ret)
137 return ret;
139 sobj->attributes = sdb_avltree_create();
140 if (! sobj->attributes)
141 return -1;
142 return 0;
143 } /* sdb_service_init */
145 static void
146 sdb_service_destroy(sdb_object_t *obj)
147 {
148 sdb_service_t *sobj = SVC(obj);
149 assert(obj);
151 store_obj_destroy(obj);
153 if (sobj->attributes)
154 sdb_avltree_destroy(sobj->attributes);
155 } /* sdb_service_destroy */
157 static int
158 sdb_attr_init(sdb_object_t *obj, va_list ap)
159 {
160 const sdb_data_t *value;
161 int ret;
163 /* this will consume the first two arguments
164 * (type and last_update) of ap */
165 ret = store_obj_init(obj, ap);
166 if (ret)
167 return ret;
168 value = va_arg(ap, const sdb_data_t *);
170 if (value)
171 if (sdb_data_copy(&ATTR(obj)->value, value))
172 return -1;
173 return 0;
174 } /* sdb_attr_init */
176 static void
177 sdb_attr_destroy(sdb_object_t *obj)
178 {
179 assert(obj);
181 store_obj_destroy(obj);
182 sdb_data_free_datum(&ATTR(obj)->value);
183 } /* sdb_attr_destroy */
185 static sdb_type_t sdb_host_type = {
186 sizeof(sdb_host_t),
187 sdb_host_init,
188 sdb_host_destroy
189 };
191 static sdb_type_t sdb_service_type = {
192 sizeof(sdb_service_t),
193 sdb_service_init,
194 sdb_service_destroy
195 };
197 static sdb_type_t sdb_attribute_type = {
198 sizeof(sdb_attribute_t),
199 sdb_attr_init,
200 sdb_attr_destroy
201 };
203 /*
204 * private helper functions
205 */
207 static sdb_host_t *
208 lookup_host(const char *name)
209 {
210 return HOST(sdb_avltree_lookup(hosts, name));
211 } /* lookup_host */
213 static int
214 record_backend(sdb_store_obj_t *obj)
215 {
216 const sdb_plugin_info_t *info;
217 char **tmp;
218 size_t i;
220 info = sdb_plugin_current();
221 if (! info)
222 return 0;
224 for (i = 0; i < obj->backends_num; ++i)
225 if (!strcasecmp(obj->backends[i], info->plugin_name))
226 return 0;
228 tmp = realloc(obj->backends,
229 (obj->backends_num + 1) * sizeof(*obj->backends));
230 if (! tmp)
231 return -1;
233 obj->backends = tmp;
234 obj->backends[obj->backends_num] = strdup(info->plugin_name);
235 if (! obj->backends[obj->backends_num])
236 return -1;
238 ++obj->backends_num;
239 return 0;
240 } /* record_backend */
242 static int
243 store_obj(sdb_avltree_t *parent_tree, int type, const char *name,
244 sdb_time_t last_update, sdb_store_obj_t **updated_obj)
245 {
246 sdb_store_obj_t *old, *new;
247 int status = 0;
249 assert(parent_tree);
251 if (last_update <= 0)
252 last_update = sdb_gettime();
254 old = STORE_OBJ(sdb_avltree_lookup(parent_tree, name));
255 if (old) {
256 if (old->last_update > last_update) {
257 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
258 "value too old (%"PRIsdbTIME" < %"PRIsdbTIME")",
259 SDB_STORE_TYPE_TO_NAME(type), name,
260 last_update, old->last_update);
261 /* don't report an error; the object may be updated by multiple
262 * backends */
263 status = 1;
264 }
265 else if (old->last_update == last_update) {
266 /* don't report an error and also don't even log this to avoid
267 * excessive noise on high sampling frequencies */
268 status = 1;
269 }
270 else {
271 sdb_time_t interval = last_update - old->last_update;
272 old->last_update = last_update;
273 if (interval) {
274 if (old->interval)
275 old->interval = (sdb_time_t)((0.9 * (double)old->interval)
276 + (0.1 * (double)interval));
277 else
278 old->interval = interval;
279 }
280 }
282 new = old;
283 sdb_object_deref(SDB_OBJ(old));
284 }
285 else {
286 if (type == SDB_ATTRIBUTE) {
287 /* the value will be updated by the caller */
288 new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
289 type, last_update, NULL));
290 }
291 else {
292 sdb_type_t t;
293 t = type == SDB_HOST ? sdb_host_type : sdb_service_type;
294 new = STORE_OBJ(sdb_object_create(name, t, type, last_update));
295 }
297 if (new) {
298 status = sdb_avltree_insert(parent_tree, SDB_OBJ(new));
300 /* pass control to the tree or destroy in case of an error */
301 sdb_object_deref(SDB_OBJ(new));
302 }
303 else {
304 char errbuf[1024];
305 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
306 SDB_STORE_TYPE_TO_NAME(type), name,
307 sdb_strerror(errno, errbuf, sizeof(errbuf)));
308 status = -1;
309 }
310 }
312 if (status < 0)
313 return status;
314 assert(new);
316 if (updated_obj)
317 *updated_obj = new;
319 if (record_backend(new))
320 return -1;
321 return status;
322 } /* store_obj */
324 static int
325 store_attr(sdb_avltree_t *attributes, const char *key, const sdb_data_t *value,
326 sdb_time_t last_update)
327 {
328 sdb_store_obj_t *attr = NULL;
329 int status;
331 status = store_obj(attributes, SDB_ATTRIBUTE, key, last_update, &attr);
332 if (status)
333 return status;
335 /* don't update unchanged values */
336 if (! sdb_data_cmp(&ATTR(attr)->value, value))
337 return status;
339 assert(attr);
340 if (sdb_data_copy(&ATTR(attr)->value, value))
341 return -1;
342 return status;
343 } /* store_attr */
345 /* The host_lock has to be acquired before calling this function. */
346 static sdb_avltree_t *
347 get_host_children(const char *hostname, int type)
348 {
349 char *cname = NULL;
350 sdb_host_t *host;
352 assert(hostname);
353 assert((type == SDB_SERVICE) || (type == SDB_ATTRIBUTE));
355 if (! hosts)
356 return NULL;
358 cname = sdb_plugin_cname(strdup(hostname));
359 if (! cname) {
360 sdb_log(SDB_LOG_ERR, "store: strdup failed");
361 return NULL;
362 }
364 host = lookup_host(cname);
365 free(cname);
366 if (! host)
367 return NULL;
369 sdb_object_deref(SDB_OBJ(host));
370 if (type == SDB_ATTRIBUTE)
371 return host->attributes;
372 else
373 return host->services;
374 } /* get_host_children */
376 /*
377 * store_common_tojson serializes common object attributes to JSON.
378 *
379 * The function never returns an error. Rather, an error message will be part
380 * of the serialized data.
381 */
382 static void
383 store_common_tojson(sdb_store_obj_t *obj, sdb_strbuf_t *buf)
384 {
385 char time_str[64];
386 char interval_str[64];
387 size_t i;
389 if (! sdb_strftime(time_str, sizeof(time_str),
390 "%F %T %z", obj->last_update))
391 snprintf(time_str, sizeof(time_str), "<error>");
392 time_str[sizeof(time_str) - 1] = '\0';
394 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
395 obj->interval))
396 snprintf(interval_str, sizeof(interval_str), "<error>");
397 interval_str[sizeof(interval_str) - 1] = '\0';
399 sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
400 "\"update_interval\": \"%s\", \"backends\": [",
401 time_str, interval_str);
403 for (i = 0; i < obj->backends_num; ++i) {
404 sdb_strbuf_append(buf, "\"%s\"", obj->backends[i]);
405 if (i < obj->backends_num - 1)
406 sdb_strbuf_append(buf, ",");
407 }
408 sdb_strbuf_append(buf, "]");
409 } /* store_common_tojson */
411 /*
412 * store_obj_tojson serializes attribute / service objects to JSON.
413 *
414 * The function never returns an error. Rather, an error message will be part
415 * of the serialized data.
416 */
417 static void
418 store_obj_tojson(sdb_avltree_t *tree, int type, sdb_strbuf_t *buf,
419 sdb_store_matcher_t *filter, int flags)
420 {
421 sdb_avltree_iter_t *iter;
423 assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
425 sdb_strbuf_append(buf, "[");
426 iter = sdb_avltree_get_iter(tree);
427 if (! iter) {
428 char errbuf[1024];
429 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
430 SDB_STORE_TYPE_TO_NAME(type),
431 sdb_strerror(errno, errbuf, sizeof(errbuf)));
432 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
433 SDB_STORE_TYPE_TO_NAME(type), errbuf);
434 }
436 /* has_next returns false if the iterator is NULL */
437 while (sdb_avltree_iter_has_next(iter)) {
438 sdb_store_obj_t *sobj = STORE_OBJ(sdb_avltree_iter_get_next(iter));
439 assert(sobj);
440 assert(sobj->type == type);
442 if (filter && (! sdb_store_matcher_matches(filter, sobj, NULL)))
443 continue;
445 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
446 if (sobj->type == SDB_ATTRIBUTE) {
447 char tmp[sdb_data_strlen(&ATTR(sobj)->value) + 1];
448 sdb_data_format(&ATTR(sobj)->value, tmp, sizeof(tmp),
449 SDB_DOUBLE_QUOTED);
450 sdb_strbuf_append(buf, "\"value\": %s, ", tmp);
451 }
452 store_common_tojson(sobj, buf);
454 if ((sobj->type == SDB_SERVICE)
455 && (! (flags & SDB_SKIP_ATTRIBUTES))) {
456 sdb_strbuf_append(buf, ", \"attributes\": ");
457 store_obj_tojson(SVC(sobj)->attributes, SDB_ATTRIBUTE,
458 buf, filter, flags);
459 }
460 sdb_strbuf_append(buf, "}");
462 if (sdb_avltree_iter_has_next(iter))
463 sdb_strbuf_append(buf, ",");
464 }
466 sdb_avltree_iter_destroy(iter);
467 sdb_strbuf_append(buf, "]");
468 } /* store_obj_tojson */
470 /*
471 * public API
472 */
474 void
475 sdb_store_clear(void)
476 {
477 sdb_avltree_destroy(hosts);
478 hosts = NULL;
479 } /* sdb_store_clear */
481 int
482 sdb_store_host(const char *name, sdb_time_t last_update)
483 {
484 char *cname = NULL;
485 int status = 0;
487 if (! name)
488 return -1;
490 cname = sdb_plugin_cname(strdup(name));
491 if (! cname) {
492 sdb_log(SDB_LOG_ERR, "store: strdup failed");
493 return -1;
494 }
496 pthread_rwlock_wrlock(&host_lock);
497 if (! hosts)
498 if (! (hosts = sdb_avltree_create()))
499 status = -1;
501 if (! status)
502 status = store_obj(hosts, SDB_HOST, cname, last_update, NULL);
503 pthread_rwlock_unlock(&host_lock);
505 free(cname);
506 return status;
507 } /* sdb_store_host */
509 _Bool
510 sdb_store_has_host(const char *name)
511 {
512 sdb_host_t *host;
514 if (! name)
515 return NULL;
517 host = lookup_host(name);
518 return host != NULL;
519 } /* sdb_store_has_host */
521 sdb_store_obj_t *
522 sdb_store_get_host(const char *name)
523 {
524 sdb_host_t *host;
526 if (! name)
527 return NULL;
529 host = lookup_host(name);
530 if (! host)
531 return NULL;
533 return STORE_OBJ(host);
534 } /* sdb_store_get_host */
536 int
537 sdb_store_attribute(const char *hostname,
538 const char *key, const sdb_data_t *value,
539 sdb_time_t last_update)
540 {
541 sdb_avltree_t *attrs;
542 int status = 0;
544 if ((! hostname) || (! key))
545 return -1;
547 pthread_rwlock_wrlock(&host_lock);
548 attrs = get_host_children(hostname, SDB_ATTRIBUTE);
549 if (! attrs) {
550 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
551 "host '%s' not found", key, hostname);
552 status = -1;
553 }
555 if (! status)
556 status = store_attr(attrs, key, value, last_update);
558 pthread_rwlock_unlock(&host_lock);
559 return status;
560 } /* sdb_store_attribute */
562 int
563 sdb_store_service(const char *hostname, const char *name,
564 sdb_time_t last_update)
565 {
566 sdb_avltree_t *services;
568 int status = 0;
570 if ((! hostname) || (! name))
571 return -1;
573 pthread_rwlock_wrlock(&host_lock);
574 services = get_host_children(hostname, SDB_SERVICE);
575 if (! services) {
576 sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
577 "host '%s' not found", name, hostname);
578 status = -1;
579 }
581 if (! status)
582 status = store_obj(services, SDB_SERVICE, name, last_update, NULL);
583 pthread_rwlock_unlock(&host_lock);
584 return status;
585 } /* sdb_store_service */
587 int
588 sdb_store_service_attr(const char *hostname, const char *service,
589 const char *key, const sdb_data_t *value, sdb_time_t last_update)
590 {
591 sdb_avltree_t *services;
592 sdb_service_t *svc;
593 int status = 0;
595 if ((! hostname) || (! service) || (! key))
596 return -1;
598 pthread_rwlock_wrlock(&host_lock);
599 services = get_host_children(hostname, SDB_SERVICE);
600 if (! services) {
601 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
602 "for service '%s' - host '%ss' not found",
603 key, service, hostname);
604 pthread_rwlock_unlock(&host_lock);
605 return -1;
606 }
608 svc = SVC(sdb_avltree_lookup(services, service));
609 if (! svc) {
610 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
611 "service '%s/%s' not found", key, hostname, service);
612 status = -1;
613 }
615 if (! status)
616 status = store_attr(svc->attributes, key, value, last_update);
618 sdb_object_deref(SDB_OBJ(svc));
619 pthread_rwlock_unlock(&host_lock);
620 return status;
621 } /* sdb_store_service_attr */
623 int
624 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
625 {
626 if ((! obj) || (! res))
627 return -1;
629 switch (field) {
630 case SDB_FIELD_LAST_UPDATE:
631 res->type = SDB_TYPE_DATETIME;
632 res->data.datetime = obj->last_update;
633 break;
634 case SDB_FIELD_AGE:
635 res->type = SDB_TYPE_DATETIME;
636 res->data.datetime = sdb_gettime() - obj->last_update;
637 break;
638 case SDB_FIELD_INTERVAL:
639 res->type = SDB_TYPE_DATETIME;
640 res->data.datetime = obj->interval;
641 break;
642 case SDB_FIELD_BACKEND:
643 /* TODO: add support for storing array values in a data object
644 * for now, fall thru to the error case */
645 default:
646 return -1;
647 }
648 return 0;
649 } /* sdb_store_get_field */
651 int
652 sdb_store_host_tojson(sdb_store_obj_t *h, sdb_strbuf_t *buf,
653 sdb_store_matcher_t *filter, int flags)
654 {
655 sdb_host_t *host = HOST(h);
657 if ((! h) || (h->type != SDB_HOST) || (! buf))
658 return -1;
660 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(host)->name);
661 store_common_tojson(h, buf);
663 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
664 sdb_strbuf_append(buf, ", \"attributes\": ");
665 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf, filter, flags);
666 }
668 if (! (flags & SDB_SKIP_SERVICES)) {
669 sdb_strbuf_append(buf, ", \"services\": ");
670 store_obj_tojson(host->services, SDB_SERVICE, buf, filter, flags);
671 }
673 sdb_strbuf_append(buf, "}");
674 return 0;
675 } /* sdb_store_host_tojson */
677 int
678 sdb_store_tojson(sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags)
679 {
680 sdb_avltree_iter_t *host_iter;
681 size_t len;
683 if (! buf)
684 return -1;
686 pthread_rwlock_rdlock(&host_lock);
688 host_iter = sdb_avltree_get_iter(hosts);
689 if (! host_iter) {
690 pthread_rwlock_unlock(&host_lock);
691 return -1;
692 }
694 sdb_strbuf_append(buf, "{\"hosts\":[");
696 len = sdb_strbuf_len(buf);
697 while (sdb_avltree_iter_has_next(host_iter)) {
698 sdb_store_obj_t *host;
700 host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
701 assert(host);
703 if (filter && (! sdb_store_matcher_matches(filter, host, NULL)))
704 continue;
706 if (sdb_strbuf_len(buf) > len)
707 sdb_strbuf_append(buf, ",");
708 len = sdb_strbuf_len(buf);
710 if (sdb_store_host_tojson(host, buf, filter, flags))
711 return -1;
712 }
714 sdb_strbuf_append(buf, "]}");
716 sdb_avltree_iter_destroy(host_iter);
717 pthread_rwlock_unlock(&host_lock);
718 return 0;
719 } /* sdb_store_tojson */
721 /* TODO: actually support hierarchical data */
722 int
723 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
724 {
725 sdb_avltree_iter_t *host_iter;
726 int status = 0;
728 pthread_rwlock_rdlock(&host_lock);
730 host_iter = sdb_avltree_get_iter(hosts);
731 if (! host_iter)
732 status = -1;
734 /* has_next returns false if the iterator is NULL */
735 while (sdb_avltree_iter_has_next(host_iter)) {
736 sdb_store_obj_t *host;
738 host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
739 assert(host);
741 if (cb(host, user_data)) {
742 status = -1;
743 break;
744 }
745 }
747 sdb_avltree_iter_destroy(host_iter);
748 pthread_rwlock_unlock(&host_lock);
749 return status;
750 } /* sdb_store_iterate */
752 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */